Skip to content

Commit

Permalink
update(virtual-fun): Adjust the estimated gas if pool triggers logic …
Browse files Browse the repository at this point in the history
…to deploy Uni pool (#649)
  • Loading branch information
sunspirit99 authored Dec 12, 2024
1 parent 207b483 commit c4c32cd
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 25 deletions.
8 changes: 5 additions & 3 deletions pkg/liquidity-source/virtual-fun/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import (
)

var (
defaultGas = Gas{Swap: 165000}
defaultGas = Gas{Swap: 250000}

bondingCurveApplicationGas int64 = 5_000_000

ZERO_ADDRESS = common.Address{}

Expand All @@ -30,6 +32,6 @@ const (
factorySellTaxMethod = "sellTax"
factoryBuyTaxMethod = "buyTax"

bondingTokenInfoMethod = "tokenInfo"
bondingUnwrapTokenMethod = "unwrapToken"
bondingUnwrapTokenMethod = "unwrapToken"
bondingGradThresholdMethod = "gradThreshold"
)
2 changes: 1 addition & 1 deletion pkg/liquidity-source/virtual-fun/pool_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
},
Expand Down
10 changes: 9 additions & 1 deletion pkg/liquidity-source/virtual-fun/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ type (

kLast *uint256.Int
bondingAddress string

gradThreshold *uint256.Int
}

Gas struct {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
11 changes: 6 additions & 5 deletions pkg/liquidity-source/virtual-fun/pool_simulator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
29 changes: 19 additions & 10 deletions pkg/liquidity-source/virtual-fun/pool_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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)
Expand All @@ -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
)

Expand All @@ -148,6 +150,13 @@ func (d *PoolTracker) getReserves(
Params: nil,
}, []interface{}{&pairReserves})

req.AddCall(&ethrpc.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(&ethrpc.Call{
ABI: bondingABI,
Expand All @@ -158,15 +167,15 @@ 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
if resp.Result[len(resp.Result)-1] {
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) {
Expand Down
11 changes: 6 additions & 5 deletions pkg/liquidity-source/virtual-fun/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit c4c32cd

Please sign in to comment.