Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
sunspirit99 committed Dec 12, 2024
1 parent 3433eb2 commit a6388db
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 37 deletions.
8 changes: 6 additions & 2 deletions pkg/liquidity-source/virtual-fun/pool_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
57 changes: 22 additions & 35 deletions pkg/liquidity-source/virtual-fun/pool_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import (
"context"
"encoding/json"
"math/big"
"strings"
"time"

"github.com/KyberNetwork/ethrpc"
Expand Down Expand Up @@ -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
Expand All @@ -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 {
Expand Down Expand Up @@ -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)
Expand All @@ -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(&ethrpc.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) {
Expand Down Expand Up @@ -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(&ethrpc.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
}
1 change: 1 addition & 0 deletions pkg/liquidity-source/virtual-fun/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
Expand Down

0 comments on commit a6388db

Please sign in to comment.