From a6388db3e10c0a655ddd524d61155fd2e984249c Mon Sep 17 00:00:00 2001 From: sunspirit99 Date: Thu, 12 Dec 2024 14:59:57 +0700 Subject: [PATCH] update --- .../virtual-fun/pool_simulator.go | 8 ++- .../virtual-fun/pool_tracker.go | 57 +++++++------------ pkg/liquidity-source/virtual-fun/types.go | 1 + 3 files changed, 29 insertions(+), 37 deletions(-) diff --git a/pkg/liquidity-source/virtual-fun/pool_simulator.go b/pkg/liquidity-source/virtual-fun/pool_simulator.go index 3b769d4b5..7d8b06ea6 100644 --- a/pkg/liquidity-source/virtual-fun/pool_simulator.go +++ b/pkg/liquidity-source/virtual-fun/pool_simulator.go @@ -57,7 +57,7 @@ func NewPoolSimulator(entityPool entity.Pool) (*PoolSimulator, error) { return nil, err } - return &PoolSimulator{ + p := &PoolSimulator{ Pool: poolpkg.Pool{Info: poolpkg.PoolInfo{ Address: entityPool.Address, ReserveUsd: entityPool.ReserveUsd, @@ -76,7 +76,9 @@ func NewPoolSimulator(entityPool entity.Pool) (*PoolSimulator, error) { bondingAddress: staticExtra.BondingAddress, gas: defaultGas, - }, nil + } + + return p, nil } func (s *PoolSimulator) CalcAmountOut(param poolpkg.CalcAmountOutParams) (*poolpkg.CalcAmountOutResult, error) { @@ -155,6 +157,7 @@ func (s *PoolSimulator) CalcAmountOut(param poolpkg.CalcAmountOutParams) (*poolp SwapInfo: SwapInfo{ IsBuy: isBuy, BondingAddress: s.bondingAddress, + TokenAddress: s.Pool.Info.Tokens[0], NewReserveA: newReserveA, NewReserveB: newReserveB, NewBalanceA: newBalanceA, @@ -245,6 +248,7 @@ func (s *PoolSimulator) CalcAmountIn(param poolpkg.CalcAmountInParams) (*poolpkg SwapInfo: SwapInfo{ IsBuy: isBuy, BondingAddress: s.bondingAddress, + TokenAddress: s.Pool.Info.Tokens[0], NewReserveA: newReserveA, NewReserveB: newReserveB, NewBalanceA: newBalanceA, diff --git a/pkg/liquidity-source/virtual-fun/pool_tracker.go b/pkg/liquidity-source/virtual-fun/pool_tracker.go index dde95ea04..ee98dedc5 100644 --- a/pkg/liquidity-source/virtual-fun/pool_tracker.go +++ b/pkg/liquidity-source/virtual-fun/pool_tracker.go @@ -4,7 +4,6 @@ import ( "context" "encoding/json" "math/big" - "strings" "time" "github.com/KyberNetwork/ethrpc" @@ -60,11 +59,15 @@ func (d *PoolTracker) getNewPoolState( return p, nil } - canPoolTradable, err := d.canPoolTradable(ctx, p.Tokens[0].Address) + tokenReserves, pairReserves, canPoolTradable, blockNumber, err := d.getReserves(ctx, p.Address, p.Tokens, overrides) if err != nil { return p, err } + if p.BlockNumber > blockNumber.Uint64() { + return p, nil + } + // Disable pool : need a solution to clear these pools if !canPoolTradable { p.Tokens[0].Swappable = false @@ -75,15 +78,6 @@ func (d *PoolTracker) getNewPoolState( return p, nil } - tokenReserves, pairReserves, blockNumber, err := d.getReserves(ctx, p.Address, p.Tokens, overrides) - if err != nil { - return p, err - } - - if p.BlockNumber > blockNumber.Uint64() { - return p, nil - } - newReserves := make(entity.PoolReserves, 0, len(tokenReserves)) for _, reserve := range tokenReserves { if reserve == nil { @@ -124,10 +118,11 @@ func (d *PoolTracker) getReserves( poolAddress string, tokens []*entity.PoolToken, overrides map[common.Address]gethclient.OverrideAccount, -) ([]*big.Int, [2]*big.Int, *big.Int, error) { +) ([]*big.Int, [2]*big.Int, bool, *big.Int, error) { var ( tokenReserves = make([]*big.Int, len(tokens)) pairReserves [2]*big.Int + tradable = true ) req := d.ethrpcClient.NewRequest().SetContext(ctx) @@ -153,12 +148,25 @@ func (d *PoolTracker) getReserves( Params: nil, }, []interface{}{&pairReserves}) + // Call to detect if pool can tradable ? Tradable if there is an error + req.AddCall(ðrpc.Call{ + ABI: bondingABI, + Target: d.config.BondingAddress, + Method: bondingUnwrapTokenMethod, + Params: []interface{}{common.HexToAddress(tokens[0].Address), []common.Address{}}, + }, []interface{}{&struct{}{}}) + resp, err := req.TryBlockAndAggregate() if err != nil { - return nil, [2]*big.Int{}, nil, err + return nil, [2]*big.Int{}, tradable, nil, err + } + + // Check the last call result + if resp.Result[len(resp.Result)-1] { + tradable = false } - return tokenReserves, pairReserves, resp.BlockNumber, nil + return tokenReserves, pairReserves, tradable, resp.BlockNumber, nil } func (d *PoolTracker) getTax(ctx context.Context, poolAddress string, blocknumber *big.Int) (*big.Int, *big.Int, *big.Int, error) { @@ -198,24 +206,3 @@ func (d *PoolTracker) getTax(ctx context.Context, poolAddress string, blocknumbe return buyTax, sellTax, kLast, nil } - -func (d *PoolTracker) canPoolTradable(ctx context.Context, tokenAddress string) (bool, error) { - req := d.ethrpcClient.NewRequest().SetContext(ctx) - - req.AddCall(ðrpc.Call{ - ABI: bondingABI, - Target: d.config.BondingAddress, - Method: bondingUnwrapTokenMethod, - Params: []interface{}{common.HexToAddress(tokenAddress), []common.Address{}}, - }, []interface{}{&struct{}{}}) - - if _, err := req.Call(); err != nil { - if strings.Contains(err.Error(), "execution reverted") { - return true, nil - } - - return true, err - } - - return false, nil -} diff --git a/pkg/liquidity-source/virtual-fun/types.go b/pkg/liquidity-source/virtual-fun/types.go index b627b331b..736cf6e60 100644 --- a/pkg/liquidity-source/virtual-fun/types.go +++ b/pkg/liquidity-source/virtual-fun/types.go @@ -21,6 +21,7 @@ type Extra struct { type SwapInfo struct { IsBuy bool `json:"isBuy"` BondingAddress string `json:"bondingAddress"` + TokenAddress string `json:"tokenAddress"` NewReserveA *uint256.Int `json:"-"` NewReserveB *uint256.Int `json:"-"` NewBalanceA *uint256.Int `json:"-"`