Skip to content

Commit

Permalink
fix: wrap native tokens when tracking new fluid-vault-t1 pools (#534)
Browse files Browse the repository at this point in the history
  • Loading branch information
NgoKimPhu authored Oct 8, 2024
1 parent 9792140 commit f169224
Show file tree
Hide file tree
Showing 9 changed files with 53 additions and 37 deletions.
3 changes: 2 additions & 1 deletion pkg/liquidity-source/bancor-v3/pool_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
5 changes: 1 addition & 4 deletions pkg/liquidity-source/bancor-v3/pools_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
Expand Down
9 changes: 7 additions & 2 deletions pkg/liquidity-source/fluid/vault-t1/config.go
Original file line number Diff line number Diff line change
@@ -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"`
}
3 changes: 1 addition & 2 deletions pkg/liquidity-source/fluid/vault-t1/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ const (
DexType = "fluid-vault-t1"
)

const (
// VaultLiquidationResolver methods
const ( // VaultLiquidationResolver methods
VLRMethodGetAllSwapPaths = "getAllSwapPaths"
VLRMethodGetSwapForProtocol = "getSwapForProtocol"
)
Expand Down
25 changes: 13 additions & 12 deletions pkg/liquidity-source/fluid/vault-t1/pool_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
28 changes: 14 additions & 14 deletions pkg/liquidity-source/fluid/vault-t1/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -20,9 +21,8 @@ var (

type PoolSimulator struct {
poolpkg.Pool

VaultLiquidationResolver string
Ratio *big.Int
StaticExtra
Ratio *big.Int
}

var (
Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/liquidity-source/fluid/vault-t1/pool_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
1 change: 1 addition & 0 deletions pkg/liquidity-source/fluid/vault-t1/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,5 @@ type Gas struct {

type StaticExtra struct {
VaultLiquidationResolver string `json:"vaultLiquidationResolver"`
HasNative bool `json:"hasNative"`
}
12 changes: 12 additions & 0 deletions pkg/valueobject/weth.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package valueobject

import (
"strings"
)

var WETHByChainID = map[ChainID]string{
ChainIDEthereum: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
ChainIDEthereumW: "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2",
Expand Down Expand Up @@ -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)
}

0 comments on commit f169224

Please sign in to comment.