From f169224e932e4cac93ef49d2ff11f44126ca7ef0 Mon Sep 17 00:00:00 2001 From: Phu Ngo <12547020+NgoKimPhu@users.noreply.github.com> Date: Tue, 8 Oct 2024 10:28:32 +0700 Subject: [PATCH] fix: wrap native tokens when tracking new fluid-vault-t1 pools (#534) --- .../bancor-v3/pool_tracker.go | 3 +- .../bancor-v3/pools_list_updater.go | 5 +--- pkg/liquidity-source/fluid/vault-t1/config.go | 9 ++++-- .../fluid/vault-t1/constant.go | 3 +- .../fluid/vault-t1/pool_list_updater.go | 25 +++++++++-------- .../fluid/vault-t1/pool_simulator.go | 28 +++++++++---------- .../fluid/vault-t1/pool_tracker.go | 4 +-- pkg/liquidity-source/fluid/vault-t1/types.go | 1 + pkg/valueobject/weth.go | 12 ++++++++ 9 files changed, 53 insertions(+), 37 deletions(-) diff --git a/pkg/liquidity-source/bancor-v3/pool_tracker.go b/pkg/liquidity-source/bancor-v3/pool_tracker.go index 73a6d79c3..d7a9a31ae 100644 --- a/pkg/liquidity-source/bancor-v3/pool_tracker.go +++ b/pkg/liquidity-source/bancor-v3/pool_tracker.go @@ -12,10 +12,11 @@ import ( "github.com/holiman/uint256" "github.com/pkg/errors" + "github.com/ethereum/go-ethereum/ethclient/gethclient" + "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" poolpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/valueobject" - "github.com/ethereum/go-ethereum/ethclient/gethclient" ) var ( diff --git a/pkg/liquidity-source/bancor-v3/pools_list_updater.go b/pkg/liquidity-source/bancor-v3/pools_list_updater.go index 86dfb97b0..04ef837b2 100644 --- a/pkg/liquidity-source/bancor-v3/pools_list_updater.go +++ b/pkg/liquidity-source/bancor-v3/pools_list_updater.go @@ -62,10 +62,7 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte ) for _, tokenAddress := range tokenAddresses { - addr := tokenAddress - if strings.EqualFold(tokenAddress, valueobject.EtherAddress) { - addr = strings.ToLower(valueobject.WETHByChainID[u.config.ChainID]) - } + addr := valueobject.WrapETHLower(tokenAddress, u.config.ChainID) poolTokens = append(poolTokens, &entity.PoolToken{Address: addr, Swappable: true}) reserves = append(reserves, "0") } diff --git a/pkg/liquidity-source/fluid/vault-t1/config.go b/pkg/liquidity-source/fluid/vault-t1/config.go index 23984f186..8e92d25bc 100644 --- a/pkg/liquidity-source/fluid/vault-t1/config.go +++ b/pkg/liquidity-source/fluid/vault-t1/config.go @@ -1,6 +1,11 @@ package vaultT1 +import ( + "github.com/KyberNetwork/kyberswap-dex-lib/pkg/valueobject" +) + type Config struct { - DexID string `json:"dexID"` - VaultLiquidationResolver string `json:"vaultLiquidationResolver"` + DexID string `json:"dexID"` + ChainID valueobject.ChainID `json:"chainID"` + VaultLiquidationResolver string `json:"vaultLiquidationResolver"` } diff --git a/pkg/liquidity-source/fluid/vault-t1/constant.go b/pkg/liquidity-source/fluid/vault-t1/constant.go index e5916faad..7b34c68d1 100644 --- a/pkg/liquidity-source/fluid/vault-t1/constant.go +++ b/pkg/liquidity-source/fluid/vault-t1/constant.go @@ -4,8 +4,7 @@ const ( DexType = "fluid-vault-t1" ) -const ( - // VaultLiquidationResolver methods +const ( // VaultLiquidationResolver methods VLRMethodGetAllSwapPaths = "getAllSwapPaths" VLRMethodGetSwapForProtocol = "getSwapForProtocol" ) diff --git a/pkg/liquidity-source/fluid/vault-t1/pool_list_updater.go b/pkg/liquidity-source/fluid/vault-t1/pool_list_updater.go index f50649b9a..558852e54 100644 --- a/pkg/liquidity-source/fluid/vault-t1/pool_list_updater.go +++ b/pkg/liquidity-source/fluid/vault-t1/pool_list_updater.go @@ -34,39 +34,40 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte }).Infof("Finish updating pools list.") }() - extraBytes, err := json.Marshal(&StaticExtra{ - VaultLiquidationResolver: u.config.VaultLiquidationResolver, - }) - if err != nil { - return nil, nil, err - } - paths, err := u.getSwapPaths(ctx) if err != nil { return nil, nil, err } - pools := make([]entity.Pool, 0) + pools := make([]entity.Pool, 0, len(paths)) for _, swapPath := range paths { + staticExtraBytes, err := json.Marshal(&StaticExtra{ + VaultLiquidationResolver: u.config.VaultLiquidationResolver, + HasNative: strings.EqualFold(swapPath.TokenIn.Hex(), valueobject.EtherAddress) || + strings.EqualFold(swapPath.TokenOut.Hex(), valueobject.EtherAddress), + }) + if err != nil { + return nil, nil, err + } pool := entity.Pool{ - Address: swapPath.Protocol.String(), + Address: swapPath.Protocol.Hex(), Exchange: string(valueobject.ExchangeFluidVaultT1), Type: DexType, Reserves: entity.PoolReserves{"0", "0"}, Tokens: []*entity.PoolToken{ { - Address: strings.ToLower(swapPath.TokenIn.String()), + Address: valueobject.WrapETHLower(swapPath.TokenIn.Hex(), u.config.ChainID), Weight: 1, Swappable: true, }, { - Address: strings.ToLower(swapPath.TokenOut.String()), + Address: valueobject.WrapETHLower(swapPath.TokenOut.Hex(), u.config.ChainID), Weight: 1, Swappable: true, }, }, - StaticExtra: string(extraBytes), + StaticExtra: string(staticExtraBytes), } pools = append(pools, pool) diff --git a/pkg/liquidity-source/fluid/vault-t1/pool_simulator.go b/pkg/liquidity-source/fluid/vault-t1/pool_simulator.go index 91a7fe99f..caa15c75f 100644 --- a/pkg/liquidity-source/fluid/vault-t1/pool_simulator.go +++ b/pkg/liquidity-source/fluid/vault-t1/pool_simulator.go @@ -6,10 +6,11 @@ import ( "math/big" "strings" + "github.com/samber/lo" + "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" poolpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/bignumber" - "github.com/samber/lo" ) var ( @@ -20,9 +21,8 @@ var ( type PoolSimulator struct { poolpkg.Pool - - VaultLiquidationResolver string - Ratio *big.Int + StaticExtra + Ratio *big.Int } var ( @@ -42,16 +42,18 @@ func NewPoolSimulator(entityPool entity.Pool) (*PoolSimulator, error) { return &PoolSimulator{ Pool: poolpkg.Pool{Info: poolpkg.PoolInfo{ - Address: entityPool.Address, - Exchange: entityPool.Exchange, - Type: entityPool.Type, - Tokens: lo.Map(entityPool.Tokens, func(item *entity.PoolToken, index int) string { return item.Address }), - Reserves: lo.Map(entityPool.Reserves, func(item string, index int) *big.Int { return bignumber.NewBig(item) }), + Address: entityPool.Address, + Exchange: entityPool.Exchange, + Type: entityPool.Type, + Tokens: lo.Map(entityPool.Tokens, + func(item *entity.PoolToken, index int) string { return item.Address }), + Reserves: lo.Map(entityPool.Reserves, + func(item string, index int) *big.Int { return bignumber.NewBig(item) }), BlockNumber: entityPool.BlockNumber, SwapFee: big.NewInt(0), // no swap fee on liquidations }}, - VaultLiquidationResolver: staticExtra.VaultLiquidationResolver, - Ratio: extra.Ratio, + StaticExtra: staticExtra, + Ratio: extra.Ratio, }, nil } @@ -81,9 +83,7 @@ func (s *PoolSimulator) CalcAmountOut(param poolpkg.CalcAmountOutParams) (*poolp TokenAmountOut: &poolpkg.TokenAmount{Token: param.TokenOut, Amount: tokenAmountOut}, Fee: &poolpkg.TokenAmount{Token: param.TokenOut, Amount: bignumber.ZeroBI}, Gas: defaultGas.Liquidate, - SwapInfo: StaticExtra{ - VaultLiquidationResolver: s.VaultLiquidationResolver, - }, + SwapInfo: s.StaticExtra, }, nil } diff --git a/pkg/liquidity-source/fluid/vault-t1/pool_tracker.go b/pkg/liquidity-source/fluid/vault-t1/pool_tracker.go index 87c4461f4..85bff8f3d 100644 --- a/pkg/liquidity-source/fluid/vault-t1/pool_tracker.go +++ b/pkg/liquidity-source/fluid/vault-t1/pool_tracker.go @@ -5,12 +5,12 @@ import ( "encoding/json" "time" + "github.com/KyberNetwork/ethrpc" "github.com/KyberNetwork/logger" + "github.com/ethereum/go-ethereum/common" - "github.com/KyberNetwork/ethrpc" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool" - "github.com/ethereum/go-ethereum/common" ) type PoolTracker struct { diff --git a/pkg/liquidity-source/fluid/vault-t1/types.go b/pkg/liquidity-source/fluid/vault-t1/types.go index 271883371..3aee262cf 100644 --- a/pkg/liquidity-source/fluid/vault-t1/types.go +++ b/pkg/liquidity-source/fluid/vault-t1/types.go @@ -39,4 +39,5 @@ type Gas struct { type StaticExtra struct { VaultLiquidationResolver string `json:"vaultLiquidationResolver"` + HasNative bool `json:"hasNative"` } diff --git a/pkg/valueobject/weth.go b/pkg/valueobject/weth.go index f00521396..6c5a818bb 100644 --- a/pkg/valueobject/weth.go +++ b/pkg/valueobject/weth.go @@ -1,5 +1,9 @@ package valueobject +import ( + "strings" +) + var WETHByChainID = map[ChainID]string{ ChainIDEthereum: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", ChainIDEthereumW: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2", @@ -30,3 +34,11 @@ var WETHByChainID = map[ChainID]string{ ChainIDBlast: "0x4300000000000000000000000000000000000004", ChainIDMantle: "0x78c1b0C915c4FAA5FffA6CAbf0219DA63d7f4cb8", } + +// WrapETHLower wraps, if applicable, native token to wrapped token; and then lowercase it. +func WrapETHLower(token string, chainID ChainID) string { + if strings.EqualFold(token, EtherAddress) { + token = WETHByChainID[chainID] + } + return strings.ToLower(token) +}