Skip to content

Commit

Permalink
ft: update ticklens AlgebraV1
Browse files Browse the repository at this point in the history
  • Loading branch information
anhducpn67 committed Dec 31, 2024
1 parent acfa233 commit 579d07f
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 40 deletions.
1 change: 1 addition & 0 deletions pkg/liquidity-source/algebra/v1/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const (
methodGetFeeConfigOtz = "feeConfigOtz"
methodGetTimepoints = "timepoints"
methodGetTickSpacing = "tickSpacing"
methodGetTicks = "ticks"
erc20MethodBalanceOf = "balanceOf"

maxSwapLoop = 1000000
Expand Down
68 changes: 28 additions & 40 deletions pkg/liquidity-source/algebra/v1/ticklens.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,71 +2,59 @@ package algebrav1

import (
"context"
"math/big"
"sort"
"strconv"

"github.com/KyberNetwork/ethrpc"
"github.com/KyberNetwork/logger"
mapset "github.com/deckarep/golang-set/v2"
"github.com/ethereum/go-ethereum/common"
"github.com/goccy/go-json"
"github.com/samber/lo"

"github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity"
sourcePool "github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/util"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/ticklens"
)

const (
batchSize = 500
maxWordSize = 256
)

func (d *PoolTracker) getPoolTicksFromSC(ctx context.Context, pool entity.Pool, param sourcePool.GetNewPoolStateParams) ([]TickResp, error) {
var wordIndexes []int16

changedTicks := ticklens.GetChangedTicks(param.Logs)
if len(changedTicks) > 0 {
// only refetch changed tick if possible
wordIndexes = lo.Uniq(lo.Map(changedTicks, func(t int64, _ int) int16 { return int16(t >> 8) }))
} else {
if len(changedTicks) == 0 {
// Algebra doesn't compact the tick table, so it's not feasible to fetch all for now
return nil, ErrNotSupportFetchFullTick
}

logger.Infof("Fetch tick from wordPosition %v to %v (%v)", wordIndexes[0], wordIndexes[len(wordIndexes)-1], changedTicks)
logger.Infof("Fetch changed ticks (%v)", changedTicks)

chunkedWordIndexes := lo.Chunk(wordIndexes, batchSize)

var ticks []TickResp
rpcRequest := d.ethrpcClient.NewRequest()
rpcRequest.SetContext(util.NewContextWithTimestamp(ctx))
populatedTicks := make([]Tick, len(changedTicks))
for i, tick := range changedTicks {
rpcRequest.AddCall(&ethrpc.Call{
ABI: algebraV1PoolABI,
Target: pool.Address,
Method: methodGetTicks,
Params: []interface{}{new(big.Int).SetInt64(tick)},
}, []interface{}{&populatedTicks[i]})
}

for _, chunk := range chunkedWordIndexes {
rpcRequest := d.ethrpcClient.NewRequest()
rpcRequest.SetContext(util.NewContextWithTimestamp(ctx))
resp, err := rpcRequest.TryAggregate()
if err != nil {
return nil, err
}

var populatedTicks []ticklens.PopulatedTick
rpcRequest.AddCall(&ethrpc.Call{
ABI: ticklensABI,
Target: d.config.TickLensAddress,
Method: "getPopulatedTicksInWord",
Params: []interface{}{common.HexToAddress(pool.Address), chunk},
}, []interface{}{&populatedTicks})

_, err := rpcRequest.Call()
if err != nil {
return nil, err
ticks := make([]TickResp, 0, len(resp.Request.Calls))
for i, result := range resp.Result {
if !result {
logger.Errorf("failed to try multicall with param: %v", resp.Request.Calls[i].Params)
continue
}

if len(populatedTicks) > 0 {
for _, pt := range populatedTicks {
ticks = append(ticks, TickResp{
TickIdx: pt.Tick.String(),
LiquidityGross: pt.LiquidityGross.String(),
LiquidityNet: pt.LiquidityNet.String(),
})
}
}
ticks = append(ticks, TickResp{
TickIdx: strconv.FormatInt(changedTicks[i], 10),
LiquidityGross: populatedTicks[i].LiquidityTotal.String(),
LiquidityNet: populatedTicks[i].LiquidityDelta.String(),
})
}

// if we only fetched some ticks, then update them to the original ticks and return
Expand Down
11 changes: 11 additions & 0 deletions pkg/liquidity-source/algebra/v1/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ type TickResp struct {
LiquidityNet string `json:"liquidityNet"`
}

type Tick struct {
LiquidityTotal *big.Int
LiquidityDelta *big.Int
OuterFeeGrowth0Token *big.Int
OuterFeeGrowth1Token *big.Int
OuterTickCumulative *big.Int
OuterSecondsPerLiquidity *big.Int
OuterSecondsSpent uint32
Initialized bool
}

type SubgraphPoolTicks struct {
ID string `json:"id"`
Ticks []TickResp `json:"ticks"`
Expand Down

0 comments on commit 579d07f

Please sign in to comment.