diff --git a/pkg/liquidity-source/ether-vista/constant.go b/pkg/liquidity-source/ether-vista/constant.go index e22ad4e44..495461f49 100644 --- a/pkg/liquidity-source/ether-vista/constant.go +++ b/pkg/liquidity-source/ether-vista/constant.go @@ -2,7 +2,6 @@ package ethervista const ( DexType = "ether-vista" - WETH = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2" ) var ( diff --git a/pkg/liquidity-source/ether-vista/pool_simulator.go b/pkg/liquidity-source/ether-vista/pool_simulator.go index 405f5d91e..840331b88 100644 --- a/pkg/liquidity-source/ether-vista/pool_simulator.go +++ b/pkg/liquidity-source/ether-vista/pool_simulator.go @@ -4,15 +4,15 @@ import ( "errors" "math/big" - "github.com/KyberNetwork/blockchain-toolkit/integer" - "github.com/KyberNetwork/blockchain-toolkit/number" "github.com/goccy/go-json" "github.com/holiman/uint256" "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" utils "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/bignumber" + "github.com/KyberNetwork/kyberswap-dex-lib/pkg/valueobject" ) var ( @@ -29,8 +29,9 @@ var ( type ( PoolSimulator struct { poolpkg.Pool - gas Gas - extra Extra + chainID valueobject.ChainID + gas Gas + extra Extra } Gas struct { @@ -38,7 +39,7 @@ type ( } ) -func NewPoolSimulator(entityPool entity.Pool) (*PoolSimulator, error) { +func NewPoolSimulator(entityPool entity.Pool, chainID valueobject.ChainID) (*PoolSimulator, error) { var extra Extra if err := json.Unmarshal([]byte(entityPool.Extra), &extra); err != nil { return nil, err @@ -46,16 +47,19 @@ func NewPoolSimulator(entityPool entity.Pool) (*PoolSimulator, error) { return &PoolSimulator{ Pool: poolpkg.Pool{Info: poolpkg.PoolInfo{ - Address: entityPool.Address, - ReserveUsd: entityPool.ReserveUsd, - 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 utils.NewBig(item) }), + Address: entityPool.Address, + ReserveUsd: entityPool.ReserveUsd, + 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 utils.NewBig(item) }), BlockNumber: entityPool.BlockNumber, }}, - gas: defaultGas, - extra: extra, + chainID: chainID, + gas: defaultGas, + extra: extra, }, nil } @@ -75,12 +79,12 @@ func (s *PoolSimulator) CalcAmountOut(param poolpkg.CalcAmountOutParams) (*poolp } // Take Router fee if swap from ETH -> Token - if param.TokenAmountIn.Token == WETH { + if valueobject.IsWETH(param.TokenAmountIn.Token, s.chainID) { fee, _ := uint256.FromBig(s.extra.USDCToETHBuyTotalFee) amountIn.Sub(amountIn, fee) } - if amountIn.Cmp(number.Zero) <= 0 { + if amountIn.Sign() < 0 { return nil, ErrInsufficientInputAmount } @@ -94,24 +98,26 @@ func (s *PoolSimulator) CalcAmountOut(param poolpkg.CalcAmountOutParams) (*poolp return nil, ErrInvalidReserve } - if reserveIn.Cmp(number.Zero) <= 0 || reserveOut.Cmp(number.Zero) <= 0 { + if reserveIn.Sign() <= 0 || reserveOut.Sign() <= 0 { return nil, ErrInsufficientLiquidity } amountOut := s.getAmountOut(amountIn, reserveIn, reserveOut) - if param.TokenOut == WETH { + if valueobject.IsWETH(param.TokenOut, s.chainID) { fee, _ := uint256.FromBig(s.extra.USDCToETHSellTotalFee) amountOut.Sub(amountOut, fee) } - if amountOut.Cmp(reserveOut) > 0 || amountOut.Cmp(number.Zero) <= 0 { + if amountOut.Sign() <= 0 { + return nil, ErrInsufficientOutputAmount + } else if amountOut.Cmp(reserveOut) > 0 { return nil, ErrInsufficientLiquidity } return &poolpkg.CalcAmountOutResult{ TokenAmountOut: &poolpkg.TokenAmount{Token: s.Pool.Info.Tokens[indexOut], Amount: amountOut.ToBig()}, // NOTE: we don't use fee to update balance so that we don't need to calculate it. I put it number.Zero to avoid null pointer exception - Fee: &poolpkg.TokenAmount{Token: s.Pool.Info.Tokens[indexIn], Amount: integer.Zero()}, + Fee: &poolpkg.TokenAmount{Token: s.Pool.Info.Tokens[indexIn], Amount: bignumber.ZeroBI}, Gas: s.gas.Swap, }, nil } diff --git a/pkg/liquidity-source/uniswap-v1/pool_tracker.go b/pkg/liquidity-source/uniswap-v1/pool_tracker.go index 70fd6b714..28b5c0f61 100644 --- a/pkg/liquidity-source/uniswap-v1/pool_tracker.go +++ b/pkg/liquidity-source/uniswap-v1/pool_tracker.go @@ -3,17 +3,16 @@ package uniswapv1 import ( "context" "math/big" - "strings" "time" "github.com/KyberNetwork/ethrpc" "github.com/KyberNetwork/logger" "github.com/ethereum/go-ethereum/common" + "github.com/ethereum/go-ethereum/ethclient/gethclient" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/valueobject" - "github.com/ethereum/go-ethereum/ethclient/gethclient" ) type PoolTracker struct { @@ -120,7 +119,7 @@ func (d *PoolTracker) getReserves( } for i, token := range tokens { - if strings.EqualFold(token.Address, valueobject.WETHByChainID[d.config.ChainID]) { + if valueobject.IsWETH(token.Address, d.config.ChainID) { req.AddCall(ðrpc.Call{ ABI: multicallABI, Target: d.config.MulticallContractAddress, diff --git a/pkg/source/lido-steth/pool_simulator.go b/pkg/source/lido-steth/pool_simulator.go index 8e57c7252..56ccddb8e 100644 --- a/pkg/source/lido-steth/pool_simulator.go +++ b/pkg/source/lido-steth/pool_simulator.go @@ -19,7 +19,7 @@ type PoolSimulator struct { func NewPoolSimulator(entityPool entity.Pool, chainID valueobject.ChainID) (*PoolSimulator, error) { numTokens := len(entityPool.Tokens) - if numTokens != 2 || !isWrappedEther(entityPool.Tokens[0].Address, chainID) { + if numTokens != 2 || !valueobject.IsWETH(entityPool.Tokens[0].Address, chainID) { return nil, fmt.Errorf("invalid pool tokens %v, %v", entityPool, numTokens) } if numTokens != len(entityPool.Reserves) { @@ -53,7 +53,7 @@ func (p *PoolSimulator) CalcAmountOut(param pool.CalcAmountOutParams) (*pool.Cal tokenOut := param.TokenOut stEth := p.Info.Tokens[1] // can only swap from ETH to stETH - if !isWrappedEther(tokenAmountIn.Token, p.chainID) || !strings.EqualFold(tokenOut, stEth) { + if !valueobject.IsWETH(tokenAmountIn.Token, p.chainID) || !strings.EqualFold(tokenOut, stEth) { return nil, fmt.Errorf("Invalid tokenIn/Out %v %v", tokenAmountIn.Token, tokenOut) } @@ -121,7 +121,3 @@ func (p *PoolSimulator) CanSwapFrom(address string) []string { func (p *PoolSimulator) GetMetaInfo(_ string, _ string) interface{} { return nil } - -func isWrappedEther(address string, chainID valueobject.ChainID) bool { - return strings.EqualFold(valueobject.WETHByChainID[chainID], address) -} diff --git a/pkg/valueobject/weth.go b/pkg/valueobject/weth.go index 6c5a818bb..085ba0a38 100644 --- a/pkg/valueobject/weth.go +++ b/pkg/valueobject/weth.go @@ -42,3 +42,7 @@ func WrapETHLower(token string, chainID ChainID) string { } return strings.ToLower(token) } + +func IsWETH(address string, chainID ChainID) bool { + return strings.EqualFold(address, WETHByChainID[chainID]) +}