Skip to content

Commit

Permalink
fix: ether-vista not erring for amount smaller than fee; support othe…
Browse files Browse the repository at this point in the history
…r chains (#617)
  • Loading branch information
NgoKimPhu authored Nov 27, 2024
1 parent 2bed5f5 commit fd55b08
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 29 deletions.
1 change: 0 additions & 1 deletion pkg/liquidity-source/ether-vista/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package ethervista

const (
DexType = "ether-vista"
WETH = "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2"
)

var (
Expand Down
44 changes: 25 additions & 19 deletions pkg/liquidity-source/ether-vista/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand All @@ -29,33 +29,37 @@ var (
type (
PoolSimulator struct {
poolpkg.Pool
gas Gas
extra Extra
chainID valueobject.ChainID
gas Gas
extra Extra
}

Gas struct {
Swap int64
}
)

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
}

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
}

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

Expand All @@ -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
}
Expand Down
5 changes: 2 additions & 3 deletions pkg/liquidity-source/uniswap-v1/pool_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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(&ethrpc.Call{
ABI: multicallABI,
Target: d.config.MulticallContractAddress,
Expand Down
8 changes: 2 additions & 6 deletions pkg/source/lido-steth/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
}
4 changes: 4 additions & 0 deletions pkg/valueobject/weth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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])
}

0 comments on commit fd55b08

Please sign in to comment.