Skip to content

Commit

Permalink
EX-2312: add tickSpacing to extra when init univ3 pools (#549)
Browse files Browse the repository at this point in the history
  • Loading branch information
lehainam-dev authored Oct 22, 2024
1 parent f5f634a commit 860b8db
Show file tree
Hide file tree
Showing 14 changed files with 188 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pkg/source/nuriv2/pool_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (d *PoolTracker) fetchRPCData(ctx context.Context, p entity.Pool, blockNumb
Liquidity: liquidity,
Slot0: slot0,
FeeTier: feeTier.Int64(),
TickSpacing: tickSpacing.Int64(),
TickSpacing: tickSpacing.Uint64(),
Reserve0: reserve0,
Reserve1: reserve1,
}, err
Expand Down
21 changes: 21 additions & 0 deletions pkg/source/nuriv2/pools_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@ import (
"time"

"github.com/KyberNetwork/blockchain-toolkit/integer"
"github.com/KyberNetwork/ethrpc"
"github.com/KyberNetwork/logger"
"github.com/machinebox/graphql"
"github.com/samber/lo"

"github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/uniswapv3"
graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql"
)

type PoolsListUpdater struct {
config *Config
graphqlClient *graphql.Client
ethrpcClient *ethrpc.Client
}

func NewPoolsListUpdater(
cfg *Config,
ethrpcClient *ethrpc.Client,
) *PoolsListUpdater {
graphqlClient := graphqlpkg.New(graphqlpkg.Config{
Url: cfg.SubgraphAPI,
Expand All @@ -33,6 +38,7 @@ func NewPoolsListUpdater(
return &PoolsListUpdater{
config: cfg,
graphqlClient: graphqlClient,
ethrpcClient: ethrpcClient,
}
}

Expand Down Expand Up @@ -78,11 +84,23 @@ func (d *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte

logger.Infof("got %v subgraph pools from %s subgraph", numSubgraphPools, d.config.DexID)

tickSpacings, _ := uniswapv3.FetchTickSpacings(
ctx,
lo.Map(subgraphPools, func(item SubgraphPool, _ int) string { return item.ID }),
d.ethrpcClient,
nuriV2PoolABI,
methodTickSpacing,
)

pools := make([]entity.Pool, 0, len(subgraphPools))
for _, p := range subgraphPools {
tokens := make([]*entity.PoolToken, 0, 2)
reserves := make([]string, 0, 2)

extraField := Extra{
TickSpacing: tickSpacings[p.ID],
}

if p.Token0.Address != emptyString {
token0Decimals, err := strconv.Atoi(p.Token0.Decimals)

Expand Down Expand Up @@ -125,6 +143,8 @@ func (d *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte

var swapFee, _ = strconv.ParseFloat(p.FeeTier, 64)

extraBytes, _ := json.Marshal(extraField)

var newPool = entity.Pool{
Address: p.ID,
ReserveUsd: 0,
Expand All @@ -135,6 +155,7 @@ func (d *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
Timestamp: time.Now().Unix(),
Reserves: reserves,
Tokens: tokens,
Extra: string(extraBytes),
}

pools = append(pools, newPool)
Expand Down
4 changes: 2 additions & 2 deletions pkg/source/nuriv2/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Extra struct {
Liquidity *big.Int `json:"liquidity"`
SqrtPriceX96 *big.Int `json:"sqrtPriceX96"`
FeeTier int64 `json:"feeTier"`
TickSpacing int64 `json:"tickSpacing"`
TickSpacing uint64 `json:"tickSpacing"`
Tick *big.Int `json:"tick"`
Ticks []Tick `json:"ticks"`
}
Expand All @@ -82,7 +82,7 @@ type FetchRPCResult struct {
Liquidity *big.Int
Slot0 Slot0
FeeTier int64
TickSpacing int64
TickSpacing uint64
Reserve0 *big.Int
Reserve1 *big.Int
}
Expand Down
19 changes: 19 additions & 0 deletions pkg/source/pancakev3/pools_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@ import (
"time"

"github.com/KyberNetwork/blockchain-toolkit/integer"
"github.com/KyberNetwork/ethrpc"
"github.com/KyberNetwork/logger"
"github.com/machinebox/graphql"
"github.com/samber/lo"

"github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/uniswapv3"
graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql"
)

type PoolsListUpdater struct {
config *Config
graphqlClient *graphql.Client
ethrpcClient *ethrpc.Client
}

func NewPoolsListUpdater(
cfg *Config,
ethrpcClient *ethrpc.Client,
) *PoolsListUpdater {
graphqlClient := graphqlpkg.New(graphqlpkg.Config{
Url: cfg.SubgraphAPI,
Expand All @@ -33,6 +38,7 @@ func NewPoolsListUpdater(
return &PoolsListUpdater{
config: cfg,
graphqlClient: graphqlClient,
ethrpcClient: ethrpcClient,
}
}

Expand Down Expand Up @@ -83,10 +89,21 @@ func (d *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte

logger.Infof("got %v subgraph pools from Pancake V3 subgraph", numSubgraphPools)

tickSpacings, _ := uniswapv3.FetchTickSpacings(
ctx,
lo.Map(subgraphPools, func(item SubgraphPool, _ int) string { return item.ID }),
d.ethrpcClient,
pancakeV3PoolABI,
methodTickSpacing,
)

pools := make([]entity.Pool, 0, len(subgraphPools))
for _, p := range subgraphPools {
tokens := make([]*entity.PoolToken, 0, 2)
reserves := make([]string, 0, 2)
extraField := Extra{
TickSpacing: tickSpacings[p.ID],
}
staticField := StaticExtra{
PoolId: p.ID,
}
Expand Down Expand Up @@ -133,6 +150,7 @@ func (d *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte

var swapFee, _ = strconv.ParseFloat(p.FeeTier, 64)

extraBytes, _ := json.Marshal(extraField)
staticBytes, _ := json.Marshal(staticField)
var newPool = entity.Pool{
Address: p.ID,
Expand All @@ -144,6 +162,7 @@ func (d *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
Timestamp: time.Now().Unix(),
Reserves: reserves,
Tokens: tokens,
Extra: string(extraBytes),
StaticExtra: string(staticBytes),
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/source/ramsesv2/pool_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ func (d *PoolTracker) fetchRPCData(ctx context.Context, p entity.Pool, blockNumb
Liquidity: liquidity,
Slot0: slot0,
FeeTier: feeTier.Int64(),
TickSpacing: tickSpacing.Int64(),
TickSpacing: tickSpacing.Uint64(),
Reserve0: reserve0,
Reserve1: reserve1,
}, err
Expand Down
21 changes: 21 additions & 0 deletions pkg/source/ramsesv2/pools_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@ import (
"time"

"github.com/KyberNetwork/blockchain-toolkit/integer"
"github.com/KyberNetwork/ethrpc"
"github.com/KyberNetwork/logger"
"github.com/machinebox/graphql"
"github.com/samber/lo"

"github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/uniswapv3"
graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql"
)

type PoolsListUpdater struct {
config *Config
graphqlClient *graphql.Client
ethrpcClient *ethrpc.Client
}

func NewPoolsListUpdater(
cfg *Config,
ethrpcClient *ethrpc.Client,
) *PoolsListUpdater {
graphqlClient := graphqlpkg.New(graphqlpkg.Config{
Url: cfg.SubgraphAPI,
Expand All @@ -33,6 +38,7 @@ func NewPoolsListUpdater(
return &PoolsListUpdater{
config: cfg,
graphqlClient: graphqlClient,
ethrpcClient: ethrpcClient,
}
}

Expand Down Expand Up @@ -78,11 +84,23 @@ func (d *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte

logger.Infof("got %v subgraph pools from %s subgraph", numSubgraphPools, d.config.DexID)

tickSpacings, _ := uniswapv3.FetchTickSpacings(
ctx,
lo.Map(subgraphPools, func(item SubgraphPool, _ int) string { return item.ID }),
d.ethrpcClient,
ramsesV2PoolABI,
methodTickSpacing,
)

pools := make([]entity.Pool, 0, len(subgraphPools))
for _, p := range subgraphPools {
tokens := make([]*entity.PoolToken, 0, 2)
reserves := make([]string, 0, 2)

extraField := Extra{
TickSpacing: tickSpacings[p.ID],
}

if p.Token0.Address != emptyString {
token0Decimals, err := strconv.Atoi(p.Token0.Decimals)

Expand Down Expand Up @@ -125,6 +143,8 @@ func (d *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte

var swapFee, _ = strconv.ParseFloat(p.FeeTier, 64)

extraBytes, _ := json.Marshal(extraField)

var newPool = entity.Pool{
Address: p.ID,
ReserveUsd: 0,
Expand All @@ -135,6 +155,7 @@ func (d *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
Timestamp: time.Now().Unix(),
Reserves: reserves,
Tokens: tokens,
Extra: string(extraBytes),
}

pools = append(pools, newPool)
Expand Down
4 changes: 2 additions & 2 deletions pkg/source/ramsesv2/type.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ type Extra struct {
Liquidity *big.Int `json:"liquidity"`
SqrtPriceX96 *big.Int `json:"sqrtPriceX96"`
FeeTier int64 `json:"feeTier"`
TickSpacing int64 `json:"tickSpacing"`
TickSpacing uint64 `json:"tickSpacing"`
Tick *big.Int `json:"tick"`
Ticks []Tick `json:"ticks"`
}
Expand All @@ -82,7 +82,7 @@ type FetchRPCResult struct {
Liquidity *big.Int
Slot0 Slot0
FeeTier int64
TickSpacing int64
TickSpacing uint64
Reserve0 *big.Int
Reserve1 *big.Int
}
Expand Down
20 changes: 20 additions & 0 deletions pkg/source/slipstream/pools_list_updater.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,20 +9,25 @@ import (
"time"

"github.com/KyberNetwork/blockchain-toolkit/integer"
"github.com/KyberNetwork/ethrpc"
"github.com/KyberNetwork/logger"
"github.com/machinebox/graphql"
"github.com/samber/lo"

"github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/uniswapv3"
graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql"
)

type PoolsListUpdater struct {
config *Config
graphqlClient *graphql.Client
ethrpcClient *ethrpc.Client
}

func NewPoolsListUpdater(
cfg *Config,
ethrpcClient *ethrpc.Client,
) *PoolsListUpdater {
graphqlClient := graphqlpkg.New(graphqlpkg.Config{
Url: cfg.SubgraphAPI,
Expand All @@ -33,6 +38,7 @@ func NewPoolsListUpdater(
return &PoolsListUpdater{
config: cfg,
graphqlClient: graphqlClient,
ethrpcClient: ethrpcClient,
}
}

Expand Down Expand Up @@ -83,10 +89,22 @@ func (d *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte

logger.Infof("got %v subgraph pools from %s subgraph", numSubgraphPools, d.config.DexID)

tickSpacings, _ := uniswapv3.FetchTickSpacings(
ctx,
lo.Map(subgraphPools, func(item SubgraphPool, _ int) string { return item.ID }),
d.ethrpcClient,
poolABI,
methodTickSpacing,
)

pools := make([]entity.Pool, 0, len(subgraphPools))
for _, p := range subgraphPools {
tokens := make([]*entity.PoolToken, 0, 2)
reserves := make([]string, 0, 2)

extraField := Extra{
TickSpacing: tickSpacings[p.ID],
}
staticField := StaticExtra{
PoolId: p.ID,
}
Expand Down Expand Up @@ -131,6 +149,7 @@ func (d *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
reserves = append(reserves, zeroString)
}

extraBytes, _ := json.Marshal(extraField)
staticBytes, _ := json.Marshal(staticField)
var newPool = entity.Pool{
Address: p.ID,
Expand All @@ -142,6 +161,7 @@ func (d *PoolsListUpdater) GetNewPools(ctx context.Context, metadataBytes []byte
Timestamp: time.Now().Unix(),
Reserves: reserves,
Tokens: tokens,
Extra: string(extraBytes),
StaticExtra: string(staticBytes),
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/source/solidly-v3/pool_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func (d *PoolTracker) GetNewPoolState(
extraBytes, err := json.Marshal(Extra{
Liquidity: rpcData.Liquidity,
SqrtPriceX96: rpcData.Slot0.SqrtPriceX96,
TickSpacing: int32(rpcData.TickSpacing.Int64()),
TickSpacing: rpcData.TickSpacing.Uint64(),
Tick: rpcData.Slot0.Tick,
Ticks: ticks,
})
Expand Down
Loading

0 comments on commit 860b8db

Please sign in to comment.