Skip to content

Commit

Permalink
avoid using subgraph inside pool tracker (#666)
Browse files Browse the repository at this point in the history
  • Loading branch information
tien7668 authored Dec 24, 2024
1 parent c47c24b commit 9c7e995
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 58 deletions.
1 change: 1 addition & 0 deletions pkg/source/wombat/constant.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ const (
assetMethodLiability = "liability"

poolMethodAddressOfAsset = "addressOfAsset"
poolMethodIsPaused = "isPaused"
poolMethodAmpFactor = "ampFactor"
poolMethodEndCovRatio = "endCovRatio"
poolMethodHaircutRate = "haircutRate"
Expand Down
68 changes: 10 additions & 58 deletions pkg/source/wombat/pool_tracker.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,7 @@ package wombat

import (
"context"
"fmt"
"math/big"
"strings"
"time"

"github.com/KyberNetwork/blockchain-toolkit/dsmath"
Expand All @@ -19,7 +17,6 @@ import (
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/eth"
graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql"
"github.com/KyberNetwork/kyberswap-dex-lib/pkg/valueobject"
)

type PoolTracker struct {
Expand Down Expand Up @@ -72,6 +69,9 @@ func (d *PoolTracker) getNewPoolState(
var paused bool
var assetAddresses = make([]common.Address, len(p.Tokens))

// We use the asset's underlying token as the key to check whether an asset is paused.
var isAssetPaused = make([]bool, len(p.Tokens))

calls := d.ethrpcClient.NewRequest().SetContext(ctx)
if overrides != nil {
calls.SetOverrides(overrides)
Expand Down Expand Up @@ -114,6 +114,12 @@ func (d *PoolTracker) getNewPoolState(
Method: poolMethodAddressOfAsset,
Params: []interface{}{common.HexToAddress(token.Address)},
}, []interface{}{&assetAddresses[i]})
calls.AddCall(&ethrpc.Call{
ABI: PoolV2ABI,
Target: p.Address,
Method: poolMethodIsPaused,
Params: []interface{}{common.HexToAddress(token.Address)},
}, []interface{}{&isAssetPaused[i]})
}
if _, err := calls.TryAggregate(); err != nil {
logger.WithFields(logger.Fields{
Expand Down Expand Up @@ -163,36 +169,16 @@ func (d *PoolTracker) getNewPoolState(
return entity.Pool{}, err
}

subgraphQuery, err := d.querySubgraph(ctx, p)
if err != nil {
logger.WithFields(logger.Fields{
"poolAddress": p.Address,
"err": err,
}).Errorf("failed to query subgraph")

return entity.Pool{}, err
}

var assetMap = make(map[string]Asset)
var reserves = make([]string, len(p.Tokens))
for i, token := range p.Tokens {
isPaused := false
reserves[i] = zeroString
if subgraphQuery != nil {
for _, assetQuery := range subgraphQuery.Assets {
if strings.EqualFold(assetQuery.ID, assetAddresses[i].Hex()) {
isPaused = assetQuery.IsPaused
}
}
}

// This pool has token in subgraph but not in contract: https://bscscan.com/address/0x2ea772346486972e7690219c190dadda40ac5da4#readProxyContract
if eth.IsZeroAddress(assetAddresses[i]) {
continue
}

assetMap[token.Address] = Asset{
IsPause: isPaused,
IsPause: isAssetPaused[i],
Address: assetAddresses[i].Hex(),
UnderlyingTokenDecimals: p.Tokens[i].Decimals,
Cash: cashes[i],
Expand Down Expand Up @@ -234,37 +220,3 @@ func (d *PoolTracker) getNewPoolState(

return p, nil
}

func (d *PoolTracker) querySubgraph(
ctx context.Context,
p entity.Pool,
) (*SubgraphAsset, error) {
req := graphql.NewRequest(fmt.Sprintf(`{
_meta { block { timestamp }}
pool(
id: "%v"
) {
assets {
id
isPaused
}
}
}`, p.Address),
)

var response struct {
Pool *SubgraphAsset `json:"pool"`
Meta *valueobject.SubgraphMeta `json:"_meta"`
}
if err := d.graphqlClient.Run(ctx, req, &response); err != nil {
logger.WithFields(logger.Fields{
"type": DexTypeWombat,
"error": err,
}).Errorf("failed to query subgraph to get pools")
return nil, err
}

response.Meta.CheckIsLagging(d.config.DexID, p.Address)

return response.Pool, nil
}

0 comments on commit 9c7e995

Please sign in to comment.