@@ -9,10 +9,12 @@ import (
99 "github.com/KyberNetwork/ethrpc"
1010 "github.com/ethereum/go-ethereum/common"
1111 "github.com/ethereum/go-ethereum/ethclient/gethclient"
12+ "github.com/samber/lo"
1213
1314 "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity"
1415 "github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool"
1516 pooltrack "github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool/tracker"
17+ "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/bignumber"
1618)
1719
1820type PoolTracker struct {
@@ -63,7 +65,6 @@ func (t *PoolTracker) getNewPoolState(
6365
6466func TrackPools (ctx context.Context , pools []entity.Pool , rpcClient * ethrpc.Client , cfg * Config ) ([]entity.Pool , error ) {
6567 req := rpcClient .NewRequest ().SetContext (ctx )
66- rates := make ([][]* big.Int , len (pools ))
6768 reserves := make ([][]* big.Int , len (pools ))
6869 extras := make ([]Extra , len (pools ))
6970 for i , pool := range pools {
@@ -96,29 +97,40 @@ func TrackPools(ctx context.Context, pools []entity.Pool, rpcClient *ethrpc.Clie
9697 }
9798
9899 req = rpcClient .NewRequest ().SetContext (ctx )
100+ samples := make ([][][][2 ]* big.Int , len (pools ))
99101 for i , pool := range pools {
100- rates [i ] = make ([]* big.Int , 2 )
102+ samples [i ] = make ([][][ 2 ] * big.Int , len ( pool . Tokens ) )
101103 for j := range pool .Tokens {
102- if reserves [i ][(j + 1 )% 2 ].Sign () == 0 {
103- rates [i ][j ] = big .NewInt (0 )
104- continue
104+ samples [i ][j ] = make ([][2 ]* big.Int , sampleSize )
105+ start := lo .Ternary (pool .Tokens [j ].Decimals < sampleSize / 2 , 0 , pool .Tokens [j ].Decimals - sampleSize / 2 )
106+ end := pool .Tokens [j ].Decimals + sampleSize / 2
107+ index := 0
108+ for k := start ; k <= end ; k ++ {
109+ samples [i ][j ][index ] = [2 ]* big.Int {bignumber .TenPowInt (k ), big .NewInt (0 )}
110+ req .AddCall (& ethrpc.Call {
111+ ABI : pairABI ,
112+ Target : pool .Address ,
113+ Method : "getAmountOut" ,
114+ Params : []any {j == 0 , samples [i ][j ][index ][0 ]}, // true = 0->1 (getAmountIn(zero_for_one, amount_out))
115+ }, []any {& samples [i ][j ][index ][1 ]})
116+ index ++
105117 }
106- req .AddCall (& ethrpc.Call {
107- ABI : pairABI ,
108- Target : pool .Address ,
109- Method : "getAmountIn" ,
110- Params : []any {j == 0 , reserves [i ][(j + 1 )% 2 ]}, // true = 0->1 (getAmountIn(zero_for_one, amount_out))
111- }, []any {& rates [i ][j ]})
112118 }
113119 }
114- _ , err = req .Aggregate ()
120+ _ , err = req .TryAggregate ()
115121 if err != nil {
116122 return nil , err
117123 }
118-
124+ for i := range samples {
125+ for j := range samples [i ] {
126+ samples [i ][j ] = lo .Filter (samples [i ][j ], func (sample [2 ]* big.Int , _ int ) bool {
127+ return sample [0 ] != nil && sample [1 ] != nil
128+ })
129+ }
130+ }
119131 for i := range pools {
120132 pools [i ].Reserves = []string {reserves [i ][0 ].String (), reserves [i ][1 ].String ()}
121- extras [i ].Rates = rates [i ]
133+ extras [i ].Samples = samples [i ]
122134 extraBytes , err := json .Marshal (extras [i ])
123135 if err != nil {
124136 return nil , err
0 commit comments