From c4c32cd57c3a70111d1d46f1f03ca6a60399af0d Mon Sep 17 00:00:00 2001 From: SunSpirit <48086732+sunspirit99@users.noreply.github.com> Date: Thu, 12 Dec 2024 19:49:28 +0700 Subject: [PATCH] update(virtual-fun): Adjust the estimated gas if pool triggers logic to deploy Uni pool (#649) --- pkg/liquidity-source/virtual-fun/constant.go | 8 +++-- .../virtual-fun/pool_list_updater.go | 2 +- .../virtual-fun/pool_simulator.go | 10 ++++++- .../virtual-fun/pool_simulator_test.go | 11 +++---- .../virtual-fun/pool_tracker.go | 29 ++++++++++++------- pkg/liquidity-source/virtual-fun/types.go | 11 +++---- 6 files changed, 46 insertions(+), 25 deletions(-) diff --git a/pkg/liquidity-source/virtual-fun/constant.go b/pkg/liquidity-source/virtual-fun/constant.go index 32c923817..9c329410d 100644 --- a/pkg/liquidity-source/virtual-fun/constant.go +++ b/pkg/liquidity-source/virtual-fun/constant.go @@ -6,7 +6,9 @@ import ( ) var ( - defaultGas = Gas{Swap: 165000} + defaultGas = Gas{Swap: 250000} + + bondingCurveApplicationGas int64 = 5_000_000 ZERO_ADDRESS = common.Address{} @@ -30,6 +32,6 @@ const ( factorySellTaxMethod = "sellTax" factoryBuyTaxMethod = "buyTax" - bondingTokenInfoMethod = "tokenInfo" - bondingUnwrapTokenMethod = "unwrapToken" + bondingUnwrapTokenMethod = "unwrapToken" + bondingGradThresholdMethod = "gradThreshold" ) diff --git a/pkg/liquidity-source/virtual-fun/pool_list_updater.go b/pkg/liquidity-source/virtual-fun/pool_list_updater.go index 327d05ec9..ffae14021 100644 --- a/pkg/liquidity-source/virtual-fun/pool_list_updater.go +++ b/pkg/liquidity-source/virtual-fun/pool_list_updater.go @@ -96,7 +96,7 @@ func (u *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte WithFields( logger.Fields{ "dex_id": dexID, - "pools_len": len(pools), + "valid_pools": len(pools), "offset": offset, "duration_ms": time.Since(startTime).Milliseconds(), }, diff --git a/pkg/liquidity-source/virtual-fun/pool_simulator.go b/pkg/liquidity-source/virtual-fun/pool_simulator.go index 7d8b06ea6..fe591bab4 100644 --- a/pkg/liquidity-source/virtual-fun/pool_simulator.go +++ b/pkg/liquidity-source/virtual-fun/pool_simulator.go @@ -37,6 +37,8 @@ type ( kLast *uint256.Int bondingAddress string + + gradThreshold *uint256.Int } Gas struct { @@ -73,6 +75,7 @@ func NewPoolSimulator(entityPool entity.Pool) (*PoolSimulator, error) { kLast: uint256.MustFromBig(extra.KLast), reserveA: uint256.MustFromBig(extra.ReserveA), reserveB: uint256.MustFromBig(extra.ReserveB), + gradThreshold: uint256.MustFromBig(extra.GradThreshold), bondingAddress: staticExtra.BondingAddress, gas: defaultGas, @@ -150,10 +153,15 @@ func (s *PoolSimulator) CalcAmountOut(param poolpkg.CalcAmountOutParams) (*poolp isBuy = true } + gas := s.gas.Swap + if newReserveA.Cmp(s.gradThreshold) <= 0 { + gas += bondingCurveApplicationGas + } + return &poolpkg.CalcAmountOutResult{ TokenAmountOut: &poolpkg.TokenAmount{Token: s.Pool.Info.Tokens[indexOut], Amount: amountOut.ToBig()}, Fee: &poolpkg.TokenAmount{Token: s.Pool.Info.Tokens[indexIn], Amount: ZERO.ToBig()}, - Gas: s.gas.Swap, + Gas: gas, SwapInfo: SwapInfo{ IsBuy: isBuy, BondingAddress: s.bondingAddress, diff --git a/pkg/liquidity-source/virtual-fun/pool_simulator_test.go b/pkg/liquidity-source/virtual-fun/pool_simulator_test.go index 0da6da502..6077e9685 100644 --- a/pkg/liquidity-source/virtual-fun/pool_simulator_test.go +++ b/pkg/liquidity-source/virtual-fun/pool_simulator_test.go @@ -16,11 +16,12 @@ import ( func createTestPoolSimulator() *PoolSimulator { extra := Extra{ - BuyTax: big.NewInt(5), // 5% buy tax - SellTax: big.NewInt(10), // 10% sell tax - KLast: big.NewInt(1500000), - ReserveA: big.NewInt(1500), - ReserveB: big.NewInt(1000), + GradThreshold: big.NewInt(0), + BuyTax: big.NewInt(5), // 5% buy tax + SellTax: big.NewInt(10), // 10% sell tax + KLast: big.NewInt(1500000), + ReserveA: big.NewInt(1500), + ReserveB: big.NewInt(1000), } extraBytes, _ := json.Marshal(extra) diff --git a/pkg/liquidity-source/virtual-fun/pool_tracker.go b/pkg/liquidity-source/virtual-fun/pool_tracker.go index 49ece46e4..9f1fb0c15 100644 --- a/pkg/liquidity-source/virtual-fun/pool_tracker.go +++ b/pkg/liquidity-source/virtual-fun/pool_tracker.go @@ -59,7 +59,7 @@ func (d *PoolTracker) getNewPoolState( logger.WithFields(logger.Fields{"pool_id": p.Address}).Info("Started getting new pool state") - tokenReserves, pairReserves, canPoolTradable, blockNumber, err := d.getReserves(ctx, p.Address, p.Tokens, overrides) + tokenReserves, pairReserves, canPoolTradable, gradThreshold, blockNumber, err := d.getBondingData(ctx, p.Address, p.Tokens, overrides) if err != nil { return p, err } @@ -93,11 +93,12 @@ func (d *PoolTracker) getNewPoolState( } var extra = Extra{ - SellTax: sellTax, - BuyTax: buyTax, - ReserveA: pairReserves[0], - ReserveB: pairReserves[1], - KLast: kLast, + GradThreshold: gradThreshold, + SellTax: sellTax, + BuyTax: buyTax, + ReserveA: pairReserves[0], + ReserveB: pairReserves[1], + KLast: kLast, } newExtra, err := json.Marshal(&extra) @@ -113,15 +114,16 @@ func (d *PoolTracker) getNewPoolState( return p, nil } -func (d *PoolTracker) getReserves( +func (d *PoolTracker) getBondingData( ctx context.Context, poolAddress string, tokens []*entity.PoolToken, overrides map[common.Address]gethclient.OverrideAccount, -) ([]*big.Int, [2]*big.Int, bool, *big.Int, error) { +) ([]*big.Int, [2]*big.Int, bool, *big.Int, *big.Int, error) { var ( tokenReserves = make([]*big.Int, len(tokens)) pairReserves [2]*big.Int + gradThreshold *big.Int tradable = true ) @@ -148,6 +150,13 @@ func (d *PoolTracker) getReserves( Params: nil, }, []interface{}{&pairReserves}) + req.AddCall(ðrpc.Call{ + ABI: bondingABI, + Target: d.config.BondingAddress, + Method: bondingGradThresholdMethod, + Params: nil, + }, []interface{}{&gradThreshold}) + // Call to detect if pool can tradable ? Tradable if there is an error req.AddCall(ðrpc.Call{ ABI: bondingABI, @@ -158,7 +167,7 @@ func (d *PoolTracker) getReserves( resp, err := req.TryBlockAndAggregate() if err != nil { - return nil, [2]*big.Int{}, tradable, nil, err + return nil, [2]*big.Int{}, tradable, nil, nil, err } // Check the last call result @@ -166,7 +175,7 @@ func (d *PoolTracker) getReserves( tradable = false } - return tokenReserves, pairReserves, tradable, resp.BlockNumber, nil + return tokenReserves, pairReserves, tradable, gradThreshold, resp.BlockNumber, nil } func (d *PoolTracker) getTax(ctx context.Context, poolAddress string, blocknumber *big.Int) (*big.Int, *big.Int, *big.Int, error) { diff --git a/pkg/liquidity-source/virtual-fun/types.go b/pkg/liquidity-source/virtual-fun/types.go index 736cf6e60..c2a6e909c 100644 --- a/pkg/liquidity-source/virtual-fun/types.go +++ b/pkg/liquidity-source/virtual-fun/types.go @@ -11,11 +11,12 @@ type StaticExtra struct { } type Extra struct { - KLast *big.Int `json:"kLast"` - BuyTax *big.Int `json:"buyTax"` - SellTax *big.Int `json:"sellTax"` - ReserveA *big.Int `json:"reserveA"` - ReserveB *big.Int `json:"reserveB"` + GradThreshold *big.Int `json:"gradThreshold"` + KLast *big.Int `json:"kLast"` + BuyTax *big.Int `json:"buyTax"` + SellTax *big.Int `json:"sellTax"` + ReserveA *big.Int `json:"reserveA"` + ReserveB *big.Int `json:"reserveB"` } type SwapInfo struct {