Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix some issue in Overnight-usdp, Ramses-v2 & Curve-xCrypto-Ng #730

Merged
merged 6 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/liquidity-source/curve/shared/pools_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type (
}

Config struct {
DexID string `mapstructure:"dex_id" json:"dex_id,omitempty"`
DexID string `mapstructure:"dexID" json:"dexID,omitempty"`
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use the same casing style for the other fields as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So we need to update config in pool service too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

i think it's better when do it on the next PR :)

ChainCode string `mapstructure:"chain_code" json:"chain_code,omitempty"`
ChainID valueobject.ChainID `mapstructure:"chain_id" json:"chain_id,omitempty"`
HTTPConfig HTTPConfig `mapstructure:"http_config" json:"http_config,omitempty"`
Expand Down
1 change: 0 additions & 1 deletion pkg/liquidity-source/curve/tricrypto-ng/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ const (
poolMethodVirtualPrice = "virtual_price"
poolMethodAllowedExtraProfit = "allowed_extra_profit"
poolMethodAdjustmentStep = "adjustment_step"
poolMethodMaTime = "ma_time"
poolMethodBalances = "balances"
poolMethodPriceScale = "price_scale"
poolMethodPriceOracle = "price_oracle"
Expand Down
9 changes: 8 additions & 1 deletion pkg/liquidity-source/curve/tricrypto-ng/pool_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,13 @@ func (t *PoolTracker) getNewPoolState(
p.BlockNumber = res.BlockNumber.Uint64()
}

// https://basescan.org/address/0x20ece59d541a752261867641796c1553287513b8#code
// Some pools like this do not have a public `get_prices_timestamp` method, so it will fall back to 0 without affecting the simulation logic
var lastPriceTimestampInt int64 = 0
if lastPriceTimestamp != nil {
lastPriceTimestampInt = lastPriceTimestamp.Int64()
}

var extra = Extra{
InitialA: number.SetFromBig(new(big.Int).Rsh(initialAGamma, 128)),
InitialGamma: new(uint256.Int).And(number.SetFromBig(initialAGamma), PriceMask),
Expand All @@ -234,7 +241,7 @@ func (t *PoolTracker) getNewPoolState(
FutureGamma: new(uint256.Int).And(number.SetFromBig(futureAGamma), PriceMask),
FutureAGammaTime: futureAGammaTime.Int64(),
D: number.SetFromBig(d),
LastPricesTimestamp: lastPriceTimestamp.Int64(),
LastPricesTimestamp: lastPriceTimestampInt,
FeeGamma: number.SetFromBig(feeGamma),
MidFee: number.SetFromBig(midFee),
OutFee: number.SetFromBig(outFee),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/curve/shared"
poollist "github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool/list"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/valueobject"
)

type PoolsListUpdater struct {
Expand Down Expand Up @@ -72,11 +71,9 @@ func (u *PoolsListUpdater) initPools(_ context.Context, curvePools []shared.Curv
continue
}

if u.config.ChainID != valueobject.ChainIDSonic {
if !SupportedImplementation.Contains(curvePool.Implementation) {
lg.Debugf("ignore pool with implementation=%s", curvePool.Implementation)
continue
}
if curvePool.Implementation != "" && !SupportedImplementation.Contains(curvePool.Implementation) {
lg.Debugf("ignore pool with implementation=%s", curvePool.Implementation)
continue
}

poolTokens := make([]*entity.PoolToken, 0, len(curvePool.Coins))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/curve/shared"
poollist "github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool/list"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/valueobject"
)

type PoolsListUpdater struct {
Expand Down Expand Up @@ -72,11 +71,9 @@ func (u *PoolsListUpdater) initPools(_ context.Context, curvePools []shared.Curv
continue
}

if u.config.ChainID != valueobject.ChainIDSonic {
if !SupportedImplementation.Contains(curvePool.Implementation) {
lg.Debugf("ignore pool with implementation=%s", curvePool.Implementation)
continue
}
if curvePool.Implementation != "" && !SupportedImplementation.Contains(curvePool.Implementation) {
lg.Debugf("ignore pool with implementation=%s", curvePool.Implementation)
continue
}

poolTokens := make([]*entity.PoolToken, 0, len(curvePool.Coins))
Expand Down
1 change: 0 additions & 1 deletion pkg/liquidity-source/overnight-usdp/pool_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, _ []byte) ([]entity.
}

staticExtraBytes, err := json.Marshal(&StaticExtra{
Exchange: u.config.Exchange,
AssetDecimals: int64(usdcDecimals),
UsdPlusDecimals: int64(usdPlusDecimals),
})
Expand Down
5 changes: 2 additions & 3 deletions pkg/liquidity-source/overnight-usdp/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type PoolSimulator struct {

usdPlusDecimals int64
assetDecimals int64 // USDC
exchange string

gas int64
}
Expand Down Expand Up @@ -53,7 +52,6 @@ func NewPoolSimulator(entityPool entity.Pool) (*PoolSimulator, error) {
redeemFee: extra.RedeemFee,
usdPlusDecimals: staticExtra.UsdPlusDecimals,
assetDecimals: staticExtra.AssetDecimals,
exchange: staticExtra.Exchange,
gas: defaultGas,
}, nil
}
Expand Down Expand Up @@ -91,7 +89,8 @@ func (s *PoolSimulator) UpdateBalance(_ pool.UpdateBalanceParams) {}

func (s *PoolSimulator) GetMetaInfo(_ string, _ string) interface{} {
return PoolMeta{
Exchange: s.exchange,
Asset: s.Info.Tokens[0],
UsdPlus: s.Info.Tokens[1],
BlockNumber: s.Pool.Info.BlockNumber,
}
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/liquidity-source/overnight-usdp/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ package overnightusdp
import "math/big"

type PoolMeta struct {
Exchange string `json:"exchange"`
Asset string `json:"asset"`
UsdPlus string `json:"usdPlus"`
BlockNumber uint64 `json:"blockNumber"`
}

Expand All @@ -14,7 +15,6 @@ type Extra struct {
}

type StaticExtra struct {
Exchange string `json:"exchange"`
AssetDecimals int64 `json:"assetDecimals"`
UsdPlusDecimals int64 `json:"usdPlusDecimals"`
AssetDecimals int64 `json:"assetDecimals"`
UsdPlusDecimals int64 `json:"usdPlusDecimals"`
}
20 changes: 0 additions & 20 deletions pkg/source/curve/pools/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -205,26 +205,6 @@
}
]
},
{
"id": "0x6e53131f68a034873b6bfa15502af094ef0c5854",
"name": "tricrypto",
"type": "curve-tricrypto",
"tokens": [
{
"address": "0x417ac0e078398c154edfadd9ef675d30be60af93",
"precision": "1"
},
{
"address": "0x236aa50979d5f3de3bd1eeb40e81137f22ab794b",
"precision": "1"
},
{
"address": "0x4200000000000000000000000000000000000006",
"precision": "1"
}
],
"lpToken": "0x6e53131f68a034873b6bfa15502af094ef0c5854"
},
{
"id": "0x11c1fbd4b3de66bc0565779b35171a6cf3e71f59",
"name": "0x11c1fbd4b3de66bc0565779b35171a6cf3e71f59",
Expand Down
10 changes: 5 additions & 5 deletions pkg/source/ramsesv2/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ import (
)

const (
DexTypeRamsesV2 = "ramses-v2"
graphSkipLimit = 5000
graphFirstLimit = 1000
defaultTokenDecimals = 18
defaultTokenWeight = 50
DexTypeRamsesV2 = "ramses-v2"
graphSkipLimit = 5000
graphFirstLimit = 1000
defaultTokenDecimals = 18
defaultTokenWeight = 50
)

const (
Expand Down
46 changes: 33 additions & 13 deletions pkg/source/ramsesv2/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,16 +22,19 @@ import (
)

var (
ErrTickNil = errors.New("tick is nil")
ErrV3TicksEmpty = errors.New("v3Ticks empty")
ErrTickNil = errors.New("tick is nil")
ErrV3TicksEmpty = errors.New("v3Ticks empty")
ErrInvalidSqrtPrice = errors.New("SPL")
ErrPoolIsLocked = errors.New("LOK")
)

type PoolSimulator struct {
V3Pool *v3Entities.Pool
pool.Pool
gas Gas
tickMin int
tickMax int
tickMin int
tickMax int
unlocked bool
gas Gas
}

var _ = pool.RegisterFactory1(DexTypeRamsesV2, NewPoolSimulator)
Expand Down Expand Up @@ -114,11 +117,12 @@ func NewPoolSimulator(entityPool entity.Pool, chainID valueobject.ChainID) (*Poo
}

return &PoolSimulator{
Pool: pool.Pool{Info: info},
V3Pool: v3Pool,
gas: defaultGas,
tickMin: tickMin,
tickMax: tickMax,
Pool: pool.Pool{Info: info},
V3Pool: v3Pool,
tickMin: tickMin,
tickMax: tickMax,
unlocked: extra.Unlocked,
gas: defaultGas,
}, nil
}

Expand All @@ -134,7 +138,6 @@ func (p *PoolSimulator) getSqrtPriceLimit(zeroForOne bool) *big.Int {
}

sqrtPriceX96Limit, err := v3Utils.GetSqrtRatioAtTick(tickLimit)

if err != nil {
return nil
}
Expand All @@ -143,6 +146,10 @@ func (p *PoolSimulator) getSqrtPriceLimit(zeroForOne bool) *big.Int {
}

func (p *PoolSimulator) CalcAmountOut(param pool.CalcAmountOutParams) (*pool.CalcAmountOutResult, error) {
if !p.unlocked {
return nil, ErrPoolIsLocked
}

tokenAmountIn := param.TokenAmountIn
tokenOut := param.TokenOut
var tokenInIndex = p.GetTokenIndex(tokenAmountIn.Token)
Expand All @@ -159,7 +166,19 @@ func (p *PoolSimulator) CalcAmountOut(param pool.CalcAmountOutParams) (*pool.Cal
zeroForOne = true
}
amountIn := coreEntities.FromRawAmount(tokenIn, tokenAmountIn.Amount)
amountOutResult, err := p.V3Pool.GetOutputAmount(amountIn, p.getSqrtPriceLimit(zeroForOne))

sqrtPriceLimit := p.getSqrtPriceLimit(zeroForOne)
if zeroForOne {
if p.V3Pool.SqrtRatioX96.Cmp(sqrtPriceLimit) <= 0 || p.V3Pool.SqrtRatioX96.Cmp(bignumber.MIN_SQRT_RATIO) <= 0 {
return nil, ErrInvalidSqrtPrice
}
} else {
if p.V3Pool.SqrtRatioX96.Cmp(sqrtPriceLimit) >= 0 || p.V3Pool.SqrtRatioX96.Cmp(bignumber.MAX_SQRT_RATIO) >= 0 {
return nil, ErrInvalidSqrtPrice
}
}

amountOutResult, err := p.V3Pool.GetOutputAmount(amountIn, sqrtPriceLimit)

if err != nil {
return &pool.CalcAmountOutResult{}, fmt.Errorf("can not GetOutputAmount, err: %+v", err)
Expand Down Expand Up @@ -217,6 +236,7 @@ func (p *PoolSimulator) UpdateBalance(params pool.UpdateBalanceParams) {
func (p *PoolSimulator) GetMetaInfo(tokenIn string, tokenOut string) interface{} {
zeroForOne := strings.EqualFold(tokenIn, hexutil.Encode(p.V3Pool.Token0.Address[:]))
return PoolMeta{
PriceLimit: bignumber.CapPriceLimit(p.getSqrtPriceLimit(zeroForOne)),
PriceLimit: bignumber.CapPriceLimit(p.getSqrtPriceLimit(zeroForOne)),
BlockNumber: p.Pool.GetInfo().BlockNumber,
}
}
6 changes: 3 additions & 3 deletions pkg/source/ramsesv2/pool_simulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,16 @@ func TestPoolSimulator_CalcAmountOut(t *testing.T) {
out string
expectedOutAmount int64
}{
{token0, 1000000000000000000, token1, 1172208},
{token0, 1000000000000000000, token1, 486457},
}
p, err := NewPoolSimulator(
entity.Pool{
Exchange: "ramses-v2",
Type: "ramses-v2",
SwapFee: 500,
Reserves: entity.PoolReserves{"269329183753846211200", "526169379"},
Reserves: entity.PoolReserves{"69893656923366160706", "2169623"},
Tokens: []*entity.PoolToken{{Address: token0, Decimals: 18}, {Address: token1, Decimals: 6}},
Extra: "{\"liquidity\":4360306776077439,\"sqrtPriceX96\":85811322860530180084948,\"feeTier\":500,\"tickSpacing\":10,\"tick\":-274728,\"ticks\":[{\"index\":-283380,\"liquidityGross\":17166285019404,\"liquidityNet\":17166285019404},{\"index\":-279780,\"liquidityGross\":977381896105089,\"liquidityNet\":977381896105089},{\"index\":-278630,\"liquidityGross\":157248791282830,\"liquidityNet\":157248791282830},{\"index\":-278550,\"liquidityGross\":7351763429974,\"liquidityNet\":7351763429974},{\"index\":-276800,\"liquidityGross\":380989062434636,\"liquidityNet\":380989062434636},{\"index\":-276680,\"liquidityGross\":1196219220219038,\"liquidityNet\":1196219220219038},{\"index\":-276330,\"liquidityGross\":7351763429974,\"liquidityNet\":-7351763429974},{\"index\":-276170,\"liquidityGross\":294632869974088,\"liquidityNet\":294632869974088},{\"index\":-276070,\"liquidityGross\":826497613613152,\"liquidityNet\":826497613613152},{\"index\":-275100,\"liquidityGross\":510171037429202,\"liquidityNet\":510171037429202},{\"index\":-274550,\"liquidityGross\":157248791282830,\"liquidityNet\":-157248791282830},{\"index\":-274500,\"liquidityGross\":510171037429202,\"liquidityNet\":-510171037429202},{\"index\":-274170,\"liquidityGross\":1196219220219038,\"liquidityNet\":-1196219220219038},{\"index\":-274030,\"liquidityGross\":294632869974088,\"liquidityNet\":-294632869974088},{\"index\":-273320,\"liquidityGross\":826497613613152,\"liquidityNet\":-826497613613152},{\"index\":-272280,\"liquidityGross\":380989062434636,\"liquidityNet\":-380989062434636},{\"index\":-271750,\"liquidityGross\":977381896105089,\"liquidityNet\":-977381896105089},{\"index\":-269510,\"liquidityGross\":17166285019404,\"liquidityNet\":-17166285019404}]}",
Extra: "{\"liquidity\":481329773989005,\"sqrtPriceX96\":55312754561266099398800,\"feeTier\":500,\"tickSpacing\":10,\"tick\":-283511,\"ticks\":[{\"index\":-887270,\"liquidityGross\":106514621957,\"liquidityNet\":106514621957},{\"index\":-283610,\"liquidityGross\":312504599701008,\"liquidityNet\":312504599701008},{\"index\":-283580,\"liquidityGross\":168718659666040,\"liquidityNet\":168718659666040},{\"index\":-283380,\"liquidityGross\":17166285019404,\"liquidityNet\":17166285019404},{\"index\":-282780,\"liquidityGross\":481223259367048,\"liquidityNet\":-481223259367048},{\"index\":-278550,\"liquidityGross\":7351763429974,\"liquidityNet\":7351763429974},{\"index\":-276330,\"liquidityGross\":7351763429974,\"liquidityNet\":-7351763429974},{\"index\":-276170,\"liquidityGross\":294632869974088,\"liquidityNet\":294632869974088},{\"index\":-275820,\"liquidityGross\":22619085245,\"liquidityNet\":22619085245},{\"index\":-274030,\"liquidityGross\":294632869974088,\"liquidityNet\":-294632869974088},{\"index\":-269510,\"liquidityGross\":17166285019404,\"liquidityNet\":-17166285019404},{\"index\":887270,\"liquidityGross\":129133707202,\"liquidityNet\":-129133707202}],\"unlocked\":true}",
}, 1)
require.Nil(t, err)

Expand Down
8 changes: 5 additions & 3 deletions pkg/source/ramsesv2/pool_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,6 @@ func (d *PoolTracker) GetNewPoolState(
"poolAddress": p.Address,
"error": err,
}).Errorf("failed to fetch data from RPC")

}

return err
Expand Down Expand Up @@ -118,6 +117,7 @@ func (d *PoolTracker) GetNewPoolState(
TickSpacing: rpcData.TickSpacing,
Tick: rpcData.Slot0.Tick,
Ticks: ticks,
Unlocked: rpcData.Slot0.Unlocked,
})
if err != nil {
logger.WithFields(logger.Fields{
Expand All @@ -129,6 +129,7 @@ func (d *PoolTracker) GetNewPoolState(

p.Extra = string(extraBytes)
p.Timestamp = time.Now().Unix()
p.BlockNumber = rpcData.BlockNumber
p.Reserves = entity.PoolReserves{
rpcData.Reserve0.String(),
rpcData.Reserve1.String(),
Expand Down Expand Up @@ -224,12 +225,12 @@ func (d *PoolTracker) fetchRPCData(ctx context.Context, p entity.Pool, blockNumb
}, []interface{}{&reserve1})
}

_, err := rpcRequest.TryAggregate()
resp, err := rpcRequest.Aggregate()
if err != nil {
logger.WithFields(logger.Fields{
"poolAddress": p.Address,
"error": err,
}).Errorf("failed to process tryAggregate")
}).Errorf("failed to process Aggregate")
return FetchRPCResult{}, err
}

Expand All @@ -240,6 +241,7 @@ func (d *PoolTracker) fetchRPCData(ctx context.Context, p entity.Pool, blockNumb
TickSpacing: tickSpacing.Uint64(),
Reserve0: reserve0,
Reserve1: reserve1,
BlockNumber: resp.BlockNumber.Uint64(),
}, err
}

Expand Down
5 changes: 4 additions & 1 deletion pkg/source/ramsesv2/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,10 +61,12 @@ type Extra struct {
TickSpacing uint64 `json:"tickSpacing"`
Tick *big.Int `json:"tick"`
Ticks []Tick `json:"ticks"`
Unlocked bool `json:"unlocked"`
}

type PoolMeta struct {
PriceLimit *big.Int `json:"priceLimit"`
PriceLimit *big.Int `json:"priceLimit"`
BlockNumber uint64 `json:"blockNumber"`
}

type Slot0 struct {
Expand All @@ -84,6 +86,7 @@ type FetchRPCResult struct {
TickSpacing uint64
Reserve0 *big.Int
Reserve1 *big.Int
BlockNumber uint64
}

func transformTickRespToTick(tickResp ticklens.TickResp) (Tick, error) {
Expand Down