From 0cca88c7bd0258ec5b0c91b730c8e0cf09d86080 Mon Sep 17 00:00:00 2001 From: Minh Nhat Hoang Date: Wed, 8 Jan 2025 07:21:47 +0700 Subject: [PATCH] feat: multiple thegraph api keys (#669) --- .../algebra/integral/constant.go | 17 +- .../algebra/integral/pool_list_updater.go | 12 +- .../algebra/integral/pool_tracker.go | 14 +- pkg/liquidity-source/algebra/v1/constant.go | 16 +- .../algebra/v1/pool_list_updater.go | 18 +- .../algebra/v1/pool_tracker.go | 12 +- pkg/liquidity-source/ambient/config.go | 10 +- .../ambient/pool_list_updater.go | 14 +- .../ambient/pool_list_updater_test.go | 15 +- pkg/liquidity-source/balancer-v1/config.go | 10 +- .../balancer-v1/pools_list_updater.go | 16 +- .../composable-stable/pools_list_updater.go | 8 +- .../balancer-v2/shared/pools_list_updater.go | 19 +- .../balancer-v2/stable/pools_list_updater.go | 9 +- .../weighted/pools_list_updater.go | 9 +- .../dodo/classical/pools_list_updater.go | 8 +- .../dodo/dpp/pools_list_updater.go | 8 +- .../dodo/dsp/pools_list_updater.go | 8 +- .../dodo/dvm/pools_list_updater.go | 8 +- pkg/liquidity-source/dodo/shared/constant.go | 7 +- .../dodo/shared/pools_list_updater.go | 12 +- .../gyroscope/2clp/pools_list_updater.go | 11 +- .../gyroscope/3clp/pools_list_updater.go | 11 +- .../gyroscope/eclp/pools_list_updater.go | 11 +- .../gyroscope/shared/pools_list_updater.go | 19 +- pkg/source/algebrav1/constant.go | 16 +- pkg/source/algebrav1/pool_list_updater.go | 12 +- pkg/source/algebrav1/pool_tracker.go | 12 +- .../balancer-composable-stable/constant.go | 2 - .../pools_list_updater.go | 12 +- pkg/source/balancer/constant.go | 3 - pkg/source/balancer/pools_list_updater.go | 12 +- pkg/source/curve/constant.go | 4 +- pkg/source/dodo/constant.go | 6 +- pkg/source/dodo/pools_list_updater.go | 12 +- pkg/source/elastic/constant.go | 14 +- pkg/source/elastic/pool_tracker.go | 12 +- pkg/source/elastic/pools_list_updater.go | 11 +- pkg/source/liquiditybookv20/constant.go | 4 +- pkg/source/liquiditybookv20/pool_tracker.go | 17 +- pkg/source/liquiditybookv21/constant.go | 4 +- pkg/source/liquiditybookv21/pool_tracker.go | 17 +- pkg/source/maverickv1/constant.go | 7 +- pkg/source/maverickv1/pool_list_updater.go | 12 +- pkg/source/nuriv2/constant.go | 16 +- pkg/source/nuriv2/pool_tracker.go | 12 +- pkg/source/nuriv2/pools_list_updater.go | 14 +- pkg/source/pancakev3/constant.go | 16 +- pkg/source/pancakev3/pool_tracker.go | 12 +- pkg/source/pancakev3/pools_list_updater.go | 14 +- pkg/source/platypus/constants.go | 3 - pkg/source/platypus/pools_list_updater.go | 19 +- pkg/source/ramsesv2/constant.go | 2 - pkg/source/ramsesv2/pool_tracker.go | 11 +- pkg/source/ramsesv2/pools_list_updater.go | 14 +- pkg/source/slipstream/constant.go | 16 +- pkg/source/slipstream/pool_tracker.go | 12 +- pkg/source/slipstream/pools_list_updater.go | 14 +- pkg/source/solidly-v3/constant.go | 16 +- pkg/source/solidly-v3/pool_tracker.go | 12 +- pkg/source/solidly-v3/pools_list_updater.go | 14 +- pkg/source/uniswapv3/constant.go | 18 +- pkg/source/uniswapv3/pool_tracker.go | 12 +- pkg/source/uniswapv3/pools_list_updater.go | 14 +- pkg/source/wombat/constant.go | 4 - pkg/source/wombat/pool_tracker.go | 15 +- pkg/source/wombat/pools_list_updater.go | 12 +- pkg/util/graphql/graphql.go | 221 ++++++++++++++++++ pkg/util/graphql/graphql_test.go | 104 +++++++++ pkg/valueobject/subgraph.go | 6 + 70 files changed, 635 insertions(+), 479 deletions(-) create mode 100644 pkg/util/graphql/graphql.go create mode 100644 pkg/util/graphql/graphql_test.go diff --git a/pkg/liquidity-source/algebra/integral/constant.go b/pkg/liquidity-source/algebra/integral/constant.go index bb51aa707..bef280eed 100644 --- a/pkg/liquidity-source/algebra/integral/constant.go +++ b/pkg/liquidity-source/algebra/integral/constant.go @@ -1,20 +1,17 @@ package integral import ( - "time" - "github.com/holiman/uint256" ) const ( - DexType = "algebra-integral" - graphSkipLimit = 5000 - graphFirstLimit = 1000 - defaultTokenDecimals = 18 - defaultTokenWeight = 50 - zeroString = "0" - emptyString = "" - graphQLRequestTimeout = 20 * time.Second + DexType = "algebra-integral" + graphSkipLimit = 5000 + graphFirstLimit = 1000 + defaultTokenDecimals = 18 + defaultTokenWeight = 50 + zeroString = "0" + emptyString = "" timepointPageSize = uint16(300) maxSwapLoop = 1000000 diff --git a/pkg/liquidity-source/algebra/integral/pool_list_updater.go b/pkg/liquidity-source/algebra/integral/pool_list_updater.go index 379cd7253..bbaec312b 100644 --- a/pkg/liquidity-source/algebra/integral/pool_list_updater.go +++ b/pkg/liquidity-source/algebra/integral/pool_list_updater.go @@ -10,7 +10,6 @@ import ( "github.com/KyberNetwork/kutils" "github.com/KyberNetwork/logger" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" @@ -18,18 +17,13 @@ import ( type PoolsListUpdater struct { config *Config - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolsListUpdater( cfg *Config, + graphqlClient *graphqlpkg.Client, ) *PoolsListUpdater { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolsListUpdater{ config: cfg, graphqlClient: graphqlClient, @@ -39,7 +33,7 @@ func NewPoolsListUpdater( func (d *PoolsListUpdater) getPoolsList(ctx context.Context, lastCreatedAtTimestamp *big.Int, lastPoolIds []string, first, skip int) ([]SubgraphPool, error) { allowSubgraphError := d.config.AllowSubgraphError - req := graphql.NewRequest(getPoolsListQuery(allowSubgraphError, lastCreatedAtTimestamp, lastPoolIds, first, skip)) + req := graphqlpkg.NewRequest(getPoolsListQuery(allowSubgraphError, lastCreatedAtTimestamp, lastPoolIds, first, skip)) var response struct { Pools []SubgraphPool `json:"pools"` diff --git a/pkg/liquidity-source/algebra/integral/pool_tracker.go b/pkg/liquidity-source/algebra/integral/pool_tracker.go index 16de7088e..00f686e7f 100644 --- a/pkg/liquidity-source/algebra/integral/pool_tracker.go +++ b/pkg/liquidity-source/algebra/integral/pool_tracker.go @@ -11,7 +11,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/goccy/go-json" "github.com/holiman/uint256" - "github.com/machinebox/graphql" "github.com/sourcegraph/conc/pool" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" @@ -24,19 +23,14 @@ import ( type PoolTracker struct { config *Config ethrpcClient *ethrpc.Client - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolTracker( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) (*PoolTracker, error) { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolTracker{ config: cfg, ethrpcClient: ethrpcClient, @@ -537,7 +531,7 @@ func (d *PoolTracker) getPoolTimepoints(ctx context.Context, blockNumber *big.In end = begin begin = end - timepointPageSize if begin <= currentIndex && currentIndex < end { - //we've wrapped around full circle, so break here + // we've wrapped around full circle, so break here break } } @@ -571,7 +565,7 @@ func (d *PoolTracker) getPoolTicks(ctx context.Context, poolAddress string) ([]T var ticks []TickResp for { - req := graphql.NewRequest(getPoolTicksQuery(allowSubgraphError, poolAddress, skip)) + req := graphqlpkg.NewRequest(getPoolTicksQuery(allowSubgraphError, poolAddress, skip)) var resp struct { Pool *SubgraphPoolTicks `json:"pool"` diff --git a/pkg/liquidity-source/algebra/v1/constant.go b/pkg/liquidity-source/algebra/v1/constant.go index 0d63a9933..007bd8da7 100644 --- a/pkg/liquidity-source/algebra/v1/constant.go +++ b/pkg/liquidity-source/algebra/v1/constant.go @@ -2,21 +2,19 @@ package algebrav1 import ( "math/big" - "time" "github.com/ethereum/go-ethereum/common" "github.com/holiman/uint256" ) const ( - DexTypeAlgebraV1 = "algebra-v1" - graphSkipLimit = 5000 - graphFirstLimit = 1000 - defaultTokenDecimals = 18 - defaultTokenWeight = 50 - zeroString = "0" - emptyString = "" - graphQLRequestTimeout = 20 * time.Second + DexTypeAlgebraV1 = "algebra-v1" + graphSkipLimit = 5000 + graphFirstLimit = 1000 + defaultTokenDecimals = 18 + defaultTokenWeight = 50 + zeroString = "0" + emptyString = "" methodGetLiquidity = "liquidity" methodGetGlobalState = "globalState" diff --git a/pkg/liquidity-source/algebra/v1/pool_list_updater.go b/pkg/liquidity-source/algebra/v1/pool_list_updater.go index 947888d2c..0c9288726 100644 --- a/pkg/liquidity-source/algebra/v1/pool_list_updater.go +++ b/pkg/liquidity-source/algebra/v1/pool_list_updater.go @@ -3,32 +3,26 @@ package algebrav1 import ( "context" "fmt" + graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" + "math/big" "strconv" "github.com/KyberNetwork/kutils" + "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/logger" "github.com/goccy/go-json" - "github.com/machinebox/graphql" - - "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" - graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" ) type PoolsListUpdater struct { config *Config - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolsListUpdater( cfg *Config, + graphqlClient *graphqlpkg.Client, ) *PoolsListUpdater { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolsListUpdater{ config: cfg, graphqlClient: graphqlClient, @@ -38,7 +32,7 @@ func NewPoolsListUpdater( func (d *PoolsListUpdater) getPoolsList(ctx context.Context, lastCreatedAtTimestamp *big.Int, lastPoolIds []string, first, skip int) ([]SubgraphPool, error) { allowSubgraphError := d.config.AllowSubgraphError - req := graphql.NewRequest(getPoolsListQuery(allowSubgraphError, lastCreatedAtTimestamp, lastPoolIds, first, skip)) + req := graphqlpkg.NewRequest(getPoolsListQuery(allowSubgraphError, lastCreatedAtTimestamp, lastPoolIds, first, skip)) var response struct { Pools []SubgraphPool `json:"pools"` diff --git a/pkg/liquidity-source/algebra/v1/pool_tracker.go b/pkg/liquidity-source/algebra/v1/pool_tracker.go index bef31a47b..037472802 100644 --- a/pkg/liquidity-source/algebra/v1/pool_tracker.go +++ b/pkg/liquidity-source/algebra/v1/pool_tracker.go @@ -11,7 +11,6 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/sourcegraph/conc/pool" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" @@ -24,19 +23,14 @@ import ( type PoolTracker struct { config *Config ethrpcClient *ethrpc.Client - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolTracker( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) (*PoolTracker, error) { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolTracker{ config: cfg, ethrpcClient: ethrpcClient, @@ -622,7 +616,7 @@ func (d *PoolTracker) getPoolTicks(ctx context.Context, poolAddress string) ([]T var ticks []TickResp for { - req := graphql.NewRequest(getPoolTicksQuery(allowSubgraphError, poolAddress, skip)) + req := graphqlpkg.NewRequest(getPoolTicksQuery(allowSubgraphError, poolAddress, skip)) var resp struct { Pool *SubgraphPoolTicks `json:"pool"` diff --git a/pkg/liquidity-source/ambient/config.go b/pkg/liquidity-source/ambient/config.go index a1d8986b7..0a395cefa 100644 --- a/pkg/liquidity-source/ambient/config.go +++ b/pkg/liquidity-source/ambient/config.go @@ -10,11 +10,11 @@ import ( ) type Config struct { - DexID string `json:"dexID"` - SubgraphURL string `json:"subgraphUrl"` - SubgraphHeaders http.Header `json:"subgraphHeaders"` - SubgraphRequestTimeout durationjson.Duration `json:"subgraphRequestTimeout"` - SubgraphLimit uint64 `json:"subgraphLimit"` + DexID string `json:"dexID"` + SubgraphAPI string `json:"subgraphAPI"` + SubgraphHeaders http.Header `json:"subgraphHeaders"` + SubgraphTimeout durationjson.Duration `json:"subgraphTimeout"` + SubgraphLimit uint64 `json:"subgraphLimit"` // Ambient doesn't use ERC20 wrapped native token when swapping with native token, it uses 0x0 address instead. // kyberswap-dex-lib uses ERC20 wrapped native token to store pool's tokens that are native. diff --git a/pkg/liquidity-source/ambient/pool_list_updater.go b/pkg/liquidity-source/ambient/pool_list_updater.go index b484c6c0e..840de7c76 100644 --- a/pkg/liquidity-source/ambient/pool_list_updater.go +++ b/pkg/liquidity-source/ambient/pool_list_updater.go @@ -9,7 +9,6 @@ import ( "github.com/KyberNetwork/logger" "github.com/ethereum/go-ethereum/common" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util" @@ -20,12 +19,13 @@ import ( type PoolListUpdater struct { cfg Config poolDatastore IPoolDatastore - subgraph *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolsListUpdater( cfg Config, poolDatastore IPoolDatastore, + graphqlClient *graphqlpkg.Client, ) (*PoolListUpdater, error) { if err := cfg.Validate(); err != nil { return nil, fmt.Errorf("invalid config: %w", err) @@ -33,11 +33,7 @@ func NewPoolsListUpdater( return &PoolListUpdater{ cfg: cfg, poolDatastore: poolDatastore, - subgraph: graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphURL, - Header: cfg.SubgraphHeaders, - Timeout: cfg.SubgraphRequestTimeout.Duration, - }), + graphqlClient: graphqlClient, }, nil } @@ -118,7 +114,7 @@ func (u *PoolListUpdater) fetchSubgraph(ctx context.Context, lastCreateTime uint limit = u.cfg.SubgraphLimit } var ( - req = graphql.NewRequest(fmt.Sprintf(`{ + req = graphqlpkg.NewRequest(fmt.Sprintf(`{ pools( where: { timeCreate_gt: %d, @@ -138,7 +134,7 @@ func (u *PoolListUpdater) fetchSubgraph(ctx context.Context, lastCreateTime uint resp SubgraphPoolsResponse ) - if err := u.subgraph.Run(ctx, req, &resp); err != nil { + if err := u.graphqlClient.Run(ctx, req, &resp); err != nil { return nil, err } diff --git a/pkg/liquidity-source/ambient/pool_list_updater_test.go b/pkg/liquidity-source/ambient/pool_list_updater_test.go index ee413e9b9..2da7651ef 100644 --- a/pkg/liquidity-source/ambient/pool_list_updater_test.go +++ b/pkg/liquidity-source/ambient/pool_list_updater_test.go @@ -9,12 +9,14 @@ import ( "time" "github.com/KyberNetwork/blockchain-toolkit/time/durationjson" + "github.com/KyberNetwork/kutils" "github.com/ethereum/go-ethereum/common" "github.com/goccy/go-json" "github.com/stretchr/testify/require" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/ambient" + graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" ) type mockPoolDataStore struct { @@ -41,8 +43,8 @@ func TestPoolListUpdater(t *testing.T) { config = ambient.Config{ DexID: "ambient", - SubgraphURL: "https://api.studio.thegraph.com/query/47610/croc-mainnet/version/latest", - SubgraphRequestTimeout: durationjson.Duration{Duration: time.Second * 10}, + SubgraphAPI: "https://api.studio.thegraph.com/query/47610/croc-mainnet/version/latest", + SubgraphTimeout: durationjson.Duration{Duration: time.Second * 10}, SubgraphLimit: 10, PoolIdx: big.NewInt(420), NativeTokenAddress: "0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2", @@ -50,12 +52,17 @@ func TestPoolListUpdater(t *testing.T) { SwapDexContractAddress: "0xAaAaAAAaA24eEeb8d57D431224f73832bC34f688", MulticallContractAddress: multicallAddress, } + + httpCfg = &kutils.HttpCfg{ + Timeout: config.SubgraphTimeout.Duration, + } + graphqlClient = graphqlpkg.NewClient(config.SubgraphAPI, graphqlpkg.WithRestyClient(httpCfg.NewRestyClient())) ) { t.Logf("first run with limit = 10") - pu, err := ambient.NewPoolsListUpdater(config, mockPoolDataStore{}) + pu, err := ambient.NewPoolsListUpdater(config, mockPoolDataStore{}, graphqlClient) require.NoError(t, err) pools, metadataBytes, err = pu.GetNewPools(context.Background(), metadataBytes) require.NoError(t, err) @@ -71,7 +78,7 @@ func TestPoolListUpdater(t *testing.T) { t.Logf("second run with metadata from first run and limit = 1000") config.SubgraphLimit = 1000 - pu, err := ambient.NewPoolsListUpdater(config, mockPoolDataStore{pool: &firstRunPool}) + pu, err := ambient.NewPoolsListUpdater(config, mockPoolDataStore{pool: &firstRunPool}, graphqlClient) require.NoError(t, err) pools, metadataBytes, err = pu.GetNewPools(context.Background(), metadataBytes) require.NoError(t, err) diff --git a/pkg/liquidity-source/balancer-v1/config.go b/pkg/liquidity-source/balancer-v1/config.go index 40cc30a44..d8c8006f8 100644 --- a/pkg/liquidity-source/balancer-v1/config.go +++ b/pkg/liquidity-source/balancer-v1/config.go @@ -7,9 +7,9 @@ import ( ) type Config struct { - DexID string `json:"dexID"` - NewPoolLimit int `json:"newPoolLimit"` - SubgraphURL string `json:"subgraphUrl"` - SubgraphHeaders http.Header `json:"subgraphHeaders"` - SubgraphRequestTimeout durationjson.Duration `json:"subgraphRequestTimeout"` + DexID string `json:"dexID"` + NewPoolLimit int `json:"newPoolLimit"` + SubgraphAPI string `json:"subgraphAPI"` + SubgraphHeaders http.Header `json:"subgraphHeaders"` + SubgraphTimeout durationjson.Duration `json:"subgraphTimeout"` } diff --git a/pkg/liquidity-source/balancer-v1/pools_list_updater.go b/pkg/liquidity-source/balancer-v1/pools_list_updater.go index 8db3031b9..da92d07af 100644 --- a/pkg/liquidity-source/balancer-v1/pools_list_updater.go +++ b/pkg/liquidity-source/balancer-v1/pools_list_updater.go @@ -8,7 +8,6 @@ import ( "github.com/KyberNetwork/ethrpc" "github.com/KyberNetwork/logger" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util" @@ -19,7 +18,7 @@ type ( PoolsListUpdater struct { config *Config ethrpcClient *ethrpc.Client - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } PoolsListUpdaterMetadata struct { @@ -40,15 +39,12 @@ type ( func NewPoolsListUpdater( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) *PoolsListUpdater { return &PoolsListUpdater{ - config: cfg, - ethrpcClient: ethrpcClient, - graphqlClient: graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphURL, - Header: cfg.SubgraphHeaders, - Timeout: cfg.SubgraphRequestTimeout.Duration, - }), + config: cfg, + ethrpcClient: ethrpcClient, + graphqlClient: graphqlClient, } } @@ -147,7 +143,7 @@ func (u *PoolsListUpdater) initPools(_ context.Context, subgraphPools []FetchPoo func (u *PoolsListUpdater) fetchPoolsFromSubgraph(ctx context.Context, lastCreateTime int) ([]FetchPoolsResponsePool, error) { var ( - req = graphql.NewRequest(newFetchPoolIDsQuery(lastCreateTime, u.config.NewPoolLimit)) + req = graphqlpkg.NewRequest(newFetchPoolIDsQuery(lastCreateTime, u.config.NewPoolLimit)) resp FetchPoolsResponse ) diff --git a/pkg/liquidity-source/balancer-v2/composable-stable/pools_list_updater.go b/pkg/liquidity-source/balancer-v2/composable-stable/pools_list_updater.go index 95d5fad21..11a60315e 100644 --- a/pkg/liquidity-source/balancer-v2/composable-stable/pools_list_updater.go +++ b/pkg/liquidity-source/balancer-v2/composable-stable/pools_list_updater.go @@ -15,6 +15,7 @@ import ( "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/balancer-v2/shared" + graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" ) type PoolsListUpdater struct { @@ -23,14 +24,17 @@ type PoolsListUpdater struct { sharedUpdater *shared.PoolsListUpdater } -func NewPoolsListUpdater(config *Config, ethrpcClient *ethrpc.Client) *PoolsListUpdater { +func NewPoolsListUpdater(config *Config, + ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, +) *PoolsListUpdater { sharedUpdater := shared.NewPoolsListUpdater(&shared.Config{ DexID: config.DexID, SubgraphAPI: config.SubgraphAPI, SubgraphHeaders: config.SubgraphHeaders, NewPoolLimit: config.NewPoolLimit, PoolTypes: []string{poolTypeComposableStable}, - }) + }, graphqlClient) return &PoolsListUpdater{ config: config, diff --git a/pkg/liquidity-source/balancer-v2/shared/pools_list_updater.go b/pkg/liquidity-source/balancer-v2/shared/pools_list_updater.go index c75de9126..255705530 100644 --- a/pkg/liquidity-source/balancer-v2/shared/pools_list_updater.go +++ b/pkg/liquidity-source/balancer-v2/shared/pools_list_updater.go @@ -4,10 +4,8 @@ import ( "context" "math/big" "net/http" - "time" "github.com/goccy/go-json" - "github.com/machinebox/graphql" graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" ) @@ -15,7 +13,7 @@ import ( type ( PoolsListUpdater struct { config *Config - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } Config struct { @@ -31,15 +29,10 @@ type ( } ) -const graphQLRequestTimeout = 20 * time.Second - -func NewPoolsListUpdater(config *Config) *PoolsListUpdater { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: config.SubgraphAPI, - Header: config.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - +func NewPoolsListUpdater( + config *Config, + graphqlClient *graphqlpkg.Client, +) *PoolsListUpdater { return &PoolsListUpdater{ config: config, graphqlClient: graphqlClient, @@ -87,7 +80,7 @@ func (u *PoolsListUpdater) querySubgraph(ctx context.Context, lastCreateTime *bi u.config.NewPoolLimit, 0, ) - req := graphql.NewRequest(query) + req := graphqlpkg.NewRequest(query) if err := u.graphqlClient.Run(ctx, req, &response); err != nil { return nil, nil, err diff --git a/pkg/liquidity-source/balancer-v2/stable/pools_list_updater.go b/pkg/liquidity-source/balancer-v2/stable/pools_list_updater.go index 87d66d24e..5d9a00b6c 100644 --- a/pkg/liquidity-source/balancer-v2/stable/pools_list_updater.go +++ b/pkg/liquidity-source/balancer-v2/stable/pools_list_updater.go @@ -15,6 +15,7 @@ import ( "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/balancer-v2/shared" + graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" ) type PoolsListUpdater struct { @@ -23,14 +24,18 @@ type PoolsListUpdater struct { sharedUpdater *shared.PoolsListUpdater } -func NewPoolsListUpdater(config *Config, ethrpcClient *ethrpc.Client) *PoolsListUpdater { +func NewPoolsListUpdater( + config *Config, + ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, +) *PoolsListUpdater { sharedUpdater := shared.NewPoolsListUpdater(&shared.Config{ DexID: config.DexID, SubgraphAPI: config.SubgraphAPI, SubgraphHeaders: config.SubgraphHeaders, NewPoolLimit: config.NewPoolLimit, PoolTypes: []string{poolTypeStable, poolTypeMetaStable}, - }) + }, graphqlClient) return &PoolsListUpdater{ config: config, diff --git a/pkg/liquidity-source/balancer-v2/weighted/pools_list_updater.go b/pkg/liquidity-source/balancer-v2/weighted/pools_list_updater.go index 368d28188..0a9386340 100644 --- a/pkg/liquidity-source/balancer-v2/weighted/pools_list_updater.go +++ b/pkg/liquidity-source/balancer-v2/weighted/pools_list_updater.go @@ -17,6 +17,7 @@ import ( "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/balancer-v2/shared" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/bignumber" + graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" ) var ErrInvalidWeight = errors.New("invalid weight") @@ -27,14 +28,18 @@ type PoolsListUpdater struct { sharedUpdater *shared.PoolsListUpdater } -func NewPoolsListUpdater(config *Config, ethrpcClient *ethrpc.Client) *PoolsListUpdater { +func NewPoolsListUpdater( + config *Config, + ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, +) *PoolsListUpdater { sharedUpdater := shared.NewPoolsListUpdater(&shared.Config{ DexID: config.DexID, SubgraphAPI: config.SubgraphAPI, SubgraphHeaders: config.SubgraphHeaders, NewPoolLimit: config.NewPoolLimit, PoolTypes: []string{poolTypeWeighted}, - }) + }, graphqlClient) return &PoolsListUpdater{ config: *config, diff --git a/pkg/liquidity-source/dodo/classical/pools_list_updater.go b/pkg/liquidity-source/dodo/classical/pools_list_updater.go index ef9883e87..ec4502379 100644 --- a/pkg/liquidity-source/dodo/classical/pools_list_updater.go +++ b/pkg/liquidity-source/dodo/classical/pools_list_updater.go @@ -9,6 +9,7 @@ import ( "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/dodo/shared" + graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" ) type PoolsListUpdater struct { @@ -16,8 +17,11 @@ type PoolsListUpdater struct { sharedUpdater *shared.PoolsListUpdater } -func NewPoolsListUpdater(config *shared.Config) *PoolsListUpdater { - sharedUpdater := shared.NewPoolsListUpdater(config) +func NewPoolsListUpdater( + config *shared.Config, + graphqlClient *graphqlpkg.Client, +) *PoolsListUpdater { + sharedUpdater := shared.NewPoolsListUpdater(config, graphqlClient) return &PoolsListUpdater{ config: *config, diff --git a/pkg/liquidity-source/dodo/dpp/pools_list_updater.go b/pkg/liquidity-source/dodo/dpp/pools_list_updater.go index 4606d3d4d..6cc68e04b 100644 --- a/pkg/liquidity-source/dodo/dpp/pools_list_updater.go +++ b/pkg/liquidity-source/dodo/dpp/pools_list_updater.go @@ -9,6 +9,7 @@ import ( "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/dodo/shared" + graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" ) type PoolsListUpdater struct { @@ -16,8 +17,11 @@ type PoolsListUpdater struct { sharedUpdater *shared.PoolsListUpdater } -func NewPoolsListUpdater(config *shared.Config) *PoolsListUpdater { - sharedUpdater := shared.NewPoolsListUpdater(config) +func NewPoolsListUpdater( + config *shared.Config, + graphqlClient *graphqlpkg.Client, +) *PoolsListUpdater { + sharedUpdater := shared.NewPoolsListUpdater(config, graphqlClient) return &PoolsListUpdater{ config: *config, diff --git a/pkg/liquidity-source/dodo/dsp/pools_list_updater.go b/pkg/liquidity-source/dodo/dsp/pools_list_updater.go index d65df3d96..9b0ac86ee 100644 --- a/pkg/liquidity-source/dodo/dsp/pools_list_updater.go +++ b/pkg/liquidity-source/dodo/dsp/pools_list_updater.go @@ -9,6 +9,7 @@ import ( "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/dodo/shared" + graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" ) type PoolsListUpdater struct { @@ -16,8 +17,11 @@ type PoolsListUpdater struct { sharedUpdater *shared.PoolsListUpdater } -func NewPoolsListUpdater(config *shared.Config) *PoolsListUpdater { - sharedUpdater := shared.NewPoolsListUpdater(config) +func NewPoolsListUpdater( + config *shared.Config, + graphqlClient *graphqlpkg.Client, +) *PoolsListUpdater { + sharedUpdater := shared.NewPoolsListUpdater(config, graphqlClient) return &PoolsListUpdater{ config: *config, diff --git a/pkg/liquidity-source/dodo/dvm/pools_list_updater.go b/pkg/liquidity-source/dodo/dvm/pools_list_updater.go index 823d335d7..a12c9f63c 100644 --- a/pkg/liquidity-source/dodo/dvm/pools_list_updater.go +++ b/pkg/liquidity-source/dodo/dvm/pools_list_updater.go @@ -9,6 +9,7 @@ import ( "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/dodo/shared" + graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" ) type PoolsListUpdater struct { @@ -16,8 +17,11 @@ type PoolsListUpdater struct { sharedUpdater *shared.PoolsListUpdater } -func NewPoolsListUpdater(config *shared.Config) *PoolsListUpdater { - sharedUpdater := shared.NewPoolsListUpdater(config) +func NewPoolsListUpdater( + config *shared.Config, + graphqlClient *graphqlpkg.Client, +) *PoolsListUpdater { + sharedUpdater := shared.NewPoolsListUpdater(config, graphqlClient) return &PoolsListUpdater{ config: *config, diff --git a/pkg/liquidity-source/dodo/shared/constant.go b/pkg/liquidity-source/dodo/shared/constant.go index cfe1cc7ab..9f712b96b 100644 --- a/pkg/liquidity-source/dodo/shared/constant.go +++ b/pkg/liquidity-source/dodo/shared/constant.go @@ -1,7 +1,5 @@ package shared -import "time" - const ( // SubgraphPoolType DodoV1 SubgraphPoolTypeDodoClassical = "CLASSICAL" @@ -10,9 +8,8 @@ const ( SubgraphPoolTypeDodoStable = "DSP" SubgraphPoolTypeDodoPrivate = "DPP" - defaultTokenWeight = 50 - defaultGraphQLRequestTimeout = 20 * time.Second - defaultTokenDecimals = 18 + defaultTokenWeight = 50 + defaultTokenDecimals = 18 zeroString = "0" diff --git a/pkg/liquidity-source/dodo/shared/pools_list_updater.go b/pkg/liquidity-source/dodo/shared/pools_list_updater.go index a3cbce405..2446a98e1 100644 --- a/pkg/liquidity-source/dodo/shared/pools_list_updater.go +++ b/pkg/liquidity-source/dodo/shared/pools_list_updater.go @@ -8,7 +8,6 @@ import ( "github.com/KyberNetwork/logger" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" @@ -16,18 +15,13 @@ import ( type PoolsListUpdater struct { config *Config - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolsListUpdater( cfg *Config, + graphqlClient *graphqlpkg.Client, ) *PoolsListUpdater { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: defaultGraphQLRequestTimeout, - }) - return &PoolsListUpdater{ config: cfg, graphqlClient: graphqlClient, @@ -169,7 +163,7 @@ func (d *PoolsListUpdater) getPoolsList( lastCreateTime *big.Int, ) ([]SubgraphPool, error) { // 'CLASSICAL', 'DVM', 'DSP', 'DPP' pools - req := graphql.NewRequest(fmt.Sprintf(`{ + req := graphqlpkg.NewRequest(fmt.Sprintf(`{ pairs( first: %v, skip: %v, diff --git a/pkg/liquidity-source/gyroscope/2clp/pools_list_updater.go b/pkg/liquidity-source/gyroscope/2clp/pools_list_updater.go index 2e1bd2059..46ba4f828 100644 --- a/pkg/liquidity-source/gyroscope/2clp/pools_list_updater.go +++ b/pkg/liquidity-source/gyroscope/2clp/pools_list_updater.go @@ -15,27 +15,34 @@ import ( "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/gyroscope/shared" + graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" ) type PoolsListUpdater struct { config Config ethrpcClient *ethrpc.Client sharedUpdater *shared.PoolsListUpdater + graphqlClient *graphqlpkg.Client } -func NewPoolsListUpdater(config *Config, ethrpcClient *ethrpc.Client) *PoolsListUpdater { +func NewPoolsListUpdater( + config *Config, + ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, +) *PoolsListUpdater { sharedUpdater := shared.NewPoolsListUpdater(&shared.Config{ DexID: config.DexID, SubgraphAPI: config.SubgraphAPI, SubgraphHeaders: config.SubgraphHeaders, NewPoolLimit: config.NewPoolLimit, PoolTypes: []string{poolType}, - }) + }, graphqlClient) return &PoolsListUpdater{ config: *config, ethrpcClient: ethrpcClient, sharedUpdater: sharedUpdater, + graphqlClient: graphqlClient, } } diff --git a/pkg/liquidity-source/gyroscope/3clp/pools_list_updater.go b/pkg/liquidity-source/gyroscope/3clp/pools_list_updater.go index 5eb1a6d30..91e4f7961 100644 --- a/pkg/liquidity-source/gyroscope/3clp/pools_list_updater.go +++ b/pkg/liquidity-source/gyroscope/3clp/pools_list_updater.go @@ -15,27 +15,34 @@ import ( "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/gyroscope/shared" + graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" ) type PoolsListUpdater struct { config Config ethrpcClient *ethrpc.Client sharedUpdater *shared.PoolsListUpdater + graphqlClient *graphqlpkg.Client } -func NewPoolsListUpdater(config *Config, ethrpcClient *ethrpc.Client) *PoolsListUpdater { +func NewPoolsListUpdater( + config *Config, + ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, +) *PoolsListUpdater { sharedUpdater := shared.NewPoolsListUpdater(&shared.Config{ DexID: config.DexID, SubgraphAPI: config.SubgraphAPI, SubgraphHeaders: config.SubgraphHeaders, NewPoolLimit: config.NewPoolLimit, PoolTypes: []string{poolType}, - }) + }, graphqlClient) return &PoolsListUpdater{ config: *config, ethrpcClient: ethrpcClient, sharedUpdater: sharedUpdater, + graphqlClient: graphqlClient, } } diff --git a/pkg/liquidity-source/gyroscope/eclp/pools_list_updater.go b/pkg/liquidity-source/gyroscope/eclp/pools_list_updater.go index 825cc335f..6bca51bab 100644 --- a/pkg/liquidity-source/gyroscope/eclp/pools_list_updater.go +++ b/pkg/liquidity-source/gyroscope/eclp/pools_list_updater.go @@ -12,27 +12,34 @@ import ( "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/liquidity-source/gyroscope/shared" + graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" ) type PoolsListUpdater struct { config Config ethrpcClient *ethrpc.Client sharedUpdater *shared.PoolsListUpdater + graphqlClient *graphqlpkg.Client } -func NewPoolsListUpdater(config *Config, ethrpcClient *ethrpc.Client) *PoolsListUpdater { +func NewPoolsListUpdater( + config *Config, + ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, +) *PoolsListUpdater { sharedUpdater := shared.NewPoolsListUpdater(&shared.Config{ DexID: config.DexID, SubgraphAPI: config.SubgraphAPI, SubgraphHeaders: config.SubgraphHeaders, NewPoolLimit: config.NewPoolLimit, PoolTypes: []string{poolType}, - }) + }, graphqlClient) return &PoolsListUpdater{ config: *config, ethrpcClient: ethrpcClient, sharedUpdater: sharedUpdater, + graphqlClient: graphqlClient, } } diff --git a/pkg/liquidity-source/gyroscope/shared/pools_list_updater.go b/pkg/liquidity-source/gyroscope/shared/pools_list_updater.go index c75de9126..255705530 100644 --- a/pkg/liquidity-source/gyroscope/shared/pools_list_updater.go +++ b/pkg/liquidity-source/gyroscope/shared/pools_list_updater.go @@ -4,10 +4,8 @@ import ( "context" "math/big" "net/http" - "time" "github.com/goccy/go-json" - "github.com/machinebox/graphql" graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" ) @@ -15,7 +13,7 @@ import ( type ( PoolsListUpdater struct { config *Config - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } Config struct { @@ -31,15 +29,10 @@ type ( } ) -const graphQLRequestTimeout = 20 * time.Second - -func NewPoolsListUpdater(config *Config) *PoolsListUpdater { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: config.SubgraphAPI, - Header: config.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - +func NewPoolsListUpdater( + config *Config, + graphqlClient *graphqlpkg.Client, +) *PoolsListUpdater { return &PoolsListUpdater{ config: config, graphqlClient: graphqlClient, @@ -87,7 +80,7 @@ func (u *PoolsListUpdater) querySubgraph(ctx context.Context, lastCreateTime *bi u.config.NewPoolLimit, 0, ) - req := graphql.NewRequest(query) + req := graphqlpkg.NewRequest(query) if err := u.graphqlClient.Run(ctx, req, &response); err != nil { return nil, nil, err diff --git a/pkg/source/algebrav1/constant.go b/pkg/source/algebrav1/constant.go index 77c526ed8..dc1429686 100644 --- a/pkg/source/algebrav1/constant.go +++ b/pkg/source/algebrav1/constant.go @@ -2,21 +2,19 @@ package algebrav1 import ( "math/big" - "time" "github.com/ethereum/go-ethereum/common" "github.com/holiman/uint256" ) const ( - DexTypeAlgebraV1 = "algebra-v1" - graphSkipLimit = 5000 - graphFirstLimit = 1000 - defaultTokenDecimals = 18 - defaultTokenWeight = 50 - zeroString = "0" - emptyString = "" - graphQLRequestTimeout = 20 * time.Second + DexTypeAlgebraV1 = "algebra-v1" + graphSkipLimit = 5000 + graphFirstLimit = 1000 + defaultTokenDecimals = 18 + defaultTokenWeight = 50 + zeroString = "0" + emptyString = "" methodGetLiquidity = "liquidity" methodGetGlobalState = "globalState" diff --git a/pkg/source/algebrav1/pool_list_updater.go b/pkg/source/algebrav1/pool_list_updater.go index 70cb89c62..58ccea9f1 100644 --- a/pkg/source/algebrav1/pool_list_updater.go +++ b/pkg/source/algebrav1/pool_list_updater.go @@ -9,7 +9,6 @@ import ( "github.com/KyberNetwork/logger" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" @@ -17,18 +16,13 @@ import ( type PoolsListUpdater struct { config *Config - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolsListUpdater( cfg *Config, + graphqlClient *graphqlpkg.Client, ) *PoolsListUpdater { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolsListUpdater{ config: cfg, graphqlClient: graphqlClient, @@ -38,7 +32,7 @@ func NewPoolsListUpdater( func (d *PoolsListUpdater) getPoolsList(ctx context.Context, lastCreatedAtTimestamp *big.Int, lastPoolIds []string, first, skip int) ([]SubgraphPool, error) { allowSubgraphError := d.config.AllowSubgraphError - req := graphql.NewRequest(getPoolsListQuery(allowSubgraphError, lastCreatedAtTimestamp, lastPoolIds, first, skip)) + req := graphqlpkg.NewRequest(getPoolsListQuery(allowSubgraphError, lastCreatedAtTimestamp, lastPoolIds, first, skip)) var response struct { Pools []SubgraphPool `json:"pools"` diff --git a/pkg/source/algebrav1/pool_tracker.go b/pkg/source/algebrav1/pool_tracker.go index bef31a47b..037472802 100644 --- a/pkg/source/algebrav1/pool_tracker.go +++ b/pkg/source/algebrav1/pool_tracker.go @@ -11,7 +11,6 @@ import ( "github.com/ethereum/go-ethereum/accounts/abi" "github.com/ethereum/go-ethereum/common" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/sourcegraph/conc/pool" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" @@ -24,19 +23,14 @@ import ( type PoolTracker struct { config *Config ethrpcClient *ethrpc.Client - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolTracker( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) (*PoolTracker, error) { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolTracker{ config: cfg, ethrpcClient: ethrpcClient, @@ -622,7 +616,7 @@ func (d *PoolTracker) getPoolTicks(ctx context.Context, poolAddress string) ([]T var ticks []TickResp for { - req := graphql.NewRequest(getPoolTicksQuery(allowSubgraphError, poolAddress, skip)) + req := graphqlpkg.NewRequest(getPoolTicksQuery(allowSubgraphError, poolAddress, skip)) var resp struct { Pool *SubgraphPoolTicks `json:"pool"` diff --git a/pkg/source/balancer-composable-stable/constant.go b/pkg/source/balancer-composable-stable/constant.go index 289934321..cee20782e 100644 --- a/pkg/source/balancer-composable-stable/constant.go +++ b/pkg/source/balancer-composable-stable/constant.go @@ -2,7 +2,6 @@ package balancercomposablestable import ( "math/big" - "time" ) type PoolType string @@ -14,7 +13,6 @@ const ( subgraphPoolTypeComposableStable PoolType = "ComposableStable" DexTypeBalancerComposableStable DexType = "balancer-composable-stable" - graphQLRequestTimeout = 20 * time.Second emptyString = "" zeroString = "0" diff --git a/pkg/source/balancer-composable-stable/pools_list_updater.go b/pkg/source/balancer-composable-stable/pools_list_updater.go index 6f08779d3..d6a4fa9b0 100644 --- a/pkg/source/balancer-composable-stable/pools_list_updater.go +++ b/pkg/source/balancer-composable-stable/pools_list_updater.go @@ -12,7 +12,6 @@ import ( "github.com/KyberNetwork/logger" "github.com/ethereum/go-ethereum/common" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util" @@ -22,19 +21,14 @@ import ( type PoolsListUpdater struct { config *Config ethrpcClient *ethrpc.Client - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolsListUpdater( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) *PoolsListUpdater { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolsListUpdater{ config: cfg, ethrpcClient: ethrpcClient, @@ -202,7 +196,7 @@ func (d *PoolsListUpdater) getPoolsListByType( lastCreateTime = zeroBI } - req := graphql.NewRequest(fmt.Sprintf(`{ + req := graphqlpkg.NewRequest(fmt.Sprintf(`{ pools( where : { poolType: "%v", diff --git a/pkg/source/balancer/constant.go b/pkg/source/balancer/constant.go index 8272b767d..6f244be45 100644 --- a/pkg/source/balancer/constant.go +++ b/pkg/source/balancer/constant.go @@ -2,7 +2,6 @@ package balancer import ( "math/big" - "time" ) type PoolType string @@ -19,8 +18,6 @@ const ( DexTypeBalancerStable DexType = "balancer-stable" DexTypeBalancerMetaStable DexType = "balancer-meta-stable" - graphQLRequestTimeout = 20 * time.Second - emptyString = "" zeroString = "0" zeroFloat64 float64 = 0 diff --git a/pkg/source/balancer/pools_list_updater.go b/pkg/source/balancer/pools_list_updater.go index a671e53c5..207f00aba 100644 --- a/pkg/source/balancer/pools_list_updater.go +++ b/pkg/source/balancer/pools_list_updater.go @@ -12,7 +12,6 @@ import ( "github.com/KyberNetwork/logger" "github.com/ethereum/go-ethereum/common" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util" @@ -22,19 +21,14 @@ import ( type PoolsListUpdater struct { config *Config ethrpcClient *ethrpc.Client - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolsListUpdater( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) *PoolsListUpdater { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolsListUpdater{ config: cfg, ethrpcClient: ethrpcClient, @@ -204,7 +198,7 @@ func (d *PoolsListUpdater) getPoolsListByType( lastCreateTime = zeroBI } - req := graphql.NewRequest(fmt.Sprintf(`{ + req := graphqlpkg.NewRequest(fmt.Sprintf(`{ pools( where : { poolType: "%v", diff --git a/pkg/source/curve/constant.go b/pkg/source/curve/constant.go index dd5cea17b..4bbe05900 100644 --- a/pkg/source/curve/constant.go +++ b/pkg/source/curve/constant.go @@ -107,6 +107,6 @@ var weth9 = map[int]string{ } var ( - zeroBI = big.NewInt(0) - zero int64 = 0 + zeroBI = big.NewInt(0) + zero int64 = 0 ) diff --git a/pkg/source/dodo/constant.go b/pkg/source/dodo/constant.go index 82eec06bd..02de9ac48 100644 --- a/pkg/source/dodo/constant.go +++ b/pkg/source/dodo/constant.go @@ -2,7 +2,6 @@ package dodo import ( "math/big" - "time" ) const ( @@ -34,9 +33,8 @@ const ( poolMethodGetPMMStateForCall = "getPMMStateForCall" poolMethodGetUserFeeRate = "getUserFeeRate" - defaultTokenWeight = 50 - defaultGraphQLRequestTimeout = 20 * time.Second - defaultTokenDecimals = 18 + defaultTokenWeight = 50 + defaultTokenDecimals = 18 zeroString = "0" diff --git a/pkg/source/dodo/pools_list_updater.go b/pkg/source/dodo/pools_list_updater.go index 353b89b76..0740a103d 100644 --- a/pkg/source/dodo/pools_list_updater.go +++ b/pkg/source/dodo/pools_list_updater.go @@ -9,7 +9,6 @@ import ( "github.com/KyberNetwork/logger" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" @@ -17,18 +16,13 @@ import ( type PoolsListUpdater struct { config *Config - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolsListUpdater( cfg *Config, + graphqlClient *graphqlpkg.Client, ) *PoolsListUpdater { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: defaultGraphQLRequestTimeout, - }) - return &PoolsListUpdater{ config: cfg, graphqlClient: graphqlClient, @@ -216,7 +210,7 @@ func (d *PoolsListUpdater) getPoolsList( lastCreateTime *big.Int, ) ([]SubgraphPool, error) { // 'CLASSICAL', 'DVM', 'DSP', 'DPP' pools - req := graphql.NewRequest(fmt.Sprintf(`{ + req := graphqlpkg.NewRequest(fmt.Sprintf(`{ pairs( first: %v, skip: %v, diff --git a/pkg/source/elastic/constant.go b/pkg/source/elastic/constant.go index 09eb5798f..688f29e91 100644 --- a/pkg/source/elastic/constant.go +++ b/pkg/source/elastic/constant.go @@ -2,17 +2,15 @@ package elastic import ( "math/big" - "time" ) const ( - DexTypeElastic = "elastic" - graphSkipLimit = 5000 - graphFirstLimit = 1000 - defaultTokenDecimals = 18 - defaultTokenWeight = 50 - reserveZero = "0" - graphQLRequestTimeout = 20 * time.Second + DexTypeElastic = "elastic" + graphSkipLimit = 5000 + graphFirstLimit = 1000 + defaultTokenDecimals = 18 + defaultTokenWeight = 50 + reserveZero = "0" ) const ( diff --git a/pkg/source/elastic/pool_tracker.go b/pkg/source/elastic/pool_tracker.go index 2dad3724b..c5f703be8 100644 --- a/pkg/source/elastic/pool_tracker.go +++ b/pkg/source/elastic/pool_tracker.go @@ -9,7 +9,6 @@ import ( "github.com/KyberNetwork/logger" "github.com/ethereum/go-ethereum/common" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/sourcegraph/conc/pool" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" @@ -21,19 +20,14 @@ import ( type PoolTracker struct { config *Config ethrpcClient *ethrpc.Client - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolTracker( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) (*PoolTracker, error) { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolTracker{ config: cfg, ethrpcClient: ethrpcClient, @@ -174,7 +168,7 @@ func (d *PoolTracker) getPoolTicks(ctx context.Context, poolAddress string) ([]T var ticks []TickResp for { - req := graphql.NewRequest( + req := graphqlpkg.NewRequest( fmt.Sprintf(`{ pool(id: "%v") { id diff --git a/pkg/source/elastic/pools_list_updater.go b/pkg/source/elastic/pools_list_updater.go index da1c19ded..9e0970862 100644 --- a/pkg/source/elastic/pools_list_updater.go +++ b/pkg/source/elastic/pools_list_updater.go @@ -10,7 +10,6 @@ import ( "github.com/KyberNetwork/blockchain-toolkit/integer" "github.com/KyberNetwork/logger" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" graphqlpkg "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/graphql" @@ -18,17 +17,13 @@ import ( type PoolsListUpdater struct { config *Config - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolsListUpdater( cfg *Config, + graphqlClient *graphqlpkg.Client, ) *PoolsListUpdater { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) return &PoolsListUpdater{ config: cfg, graphqlClient: graphqlClient, @@ -36,7 +31,7 @@ func NewPoolsListUpdater( } func (d *PoolsListUpdater) getPoolsList(ctx context.Context, lastCreatedAtTimestamp *big.Int, first, skip int) ([]SubgraphPool, error) { - req := graphql.NewRequest(fmt.Sprintf(`{ + req := graphqlpkg.NewRequest(fmt.Sprintf(`{ pools(where : {createdAtTimestamp_gte: %v}, first: %v, skip: %v, orderBy: createdAtTimestamp, orderDirection: asc) { id liquidity diff --git a/pkg/source/liquiditybookv20/constant.go b/pkg/source/liquiditybookv20/constant.go index dfc818d31..1e158e211 100644 --- a/pkg/source/liquiditybookv20/constant.go +++ b/pkg/source/liquiditybookv20/constant.go @@ -2,7 +2,6 @@ package liquiditybookv20 import ( "math/big" - "time" ) const ( @@ -23,8 +22,7 @@ const ( const ( defaultTokenWeight = 50 - graphQLRequestTimeout = 20 * time.Second - graphFirstLimit = 1000 + graphFirstLimit = 1000 basisPointMax = 10000 diff --git a/pkg/source/liquiditybookv20/pool_tracker.go b/pkg/source/liquiditybookv20/pool_tracker.go index 4b09b0a6b..9f2c832bf 100644 --- a/pkg/source/liquiditybookv20/pool_tracker.go +++ b/pkg/source/liquiditybookv20/pool_tracker.go @@ -11,7 +11,6 @@ import ( "github.com/KyberNetwork/logger" "github.com/ethereum/go-ethereum/common" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "golang.org/x/sync/errgroup" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" @@ -25,16 +24,14 @@ import ( type PoolTracker struct { cfg *Config ethrpcClient *ethrpc.Client - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } -func NewPoolTracker(cfg *Config, ethrpcClient *ethrpc.Client) (*PoolTracker, error) { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - +func NewPoolTracker( + cfg *Config, + ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, +) (*PoolTracker, error) { return &PoolTracker{ cfg: cfg, ethrpcClient: ethrpcClient, @@ -205,7 +202,7 @@ func (d *PoolTracker) querySubgraph(ctx context.Context, p entity.Pool) (*queryS // query var ( query = buildQueryGetBins(p.Address, binIDGT) - req = graphql.NewRequest(query) + req = graphqlpkg.NewRequest(query) resp struct { Pair *lbpairSubgraphResp `json:"lbpair"` diff --git a/pkg/source/liquiditybookv21/constant.go b/pkg/source/liquiditybookv21/constant.go index 901295dd1..5beeb8e7f 100644 --- a/pkg/source/liquiditybookv21/constant.go +++ b/pkg/source/liquiditybookv21/constant.go @@ -2,7 +2,6 @@ package liquiditybookv21 import ( "math/big" - "time" ) const ( @@ -26,8 +25,7 @@ const ( const ( defaultTokenWeight = 50 - graphQLRequestTimeout = 20 * time.Second - graphFirstLimit = 1000 + graphFirstLimit = 1000 // https://github.com/traderjoe-xyz/joe-v2/blob/v2.1.1/src/libraries/Constants.sol#L20 basisPointMax = 10000 diff --git a/pkg/source/liquiditybookv21/pool_tracker.go b/pkg/source/liquiditybookv21/pool_tracker.go index 71b3b09e7..709ca7db0 100644 --- a/pkg/source/liquiditybookv21/pool_tracker.go +++ b/pkg/source/liquiditybookv21/pool_tracker.go @@ -10,7 +10,6 @@ import ( "github.com/KyberNetwork/ethrpc" "github.com/KyberNetwork/logger" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "golang.org/x/sync/errgroup" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" @@ -24,16 +23,14 @@ import ( type PoolTracker struct { cfg *Config ethrpcClient *ethrpc.Client - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } -func NewPoolTracker(cfg *Config, ethrpcClient *ethrpc.Client) (*PoolTracker, error) { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - +func NewPoolTracker( + cfg *Config, + ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, +) (*PoolTracker, error) { return &PoolTracker{ cfg: cfg, ethrpcClient: ethrpcClient, @@ -234,7 +231,7 @@ func (d *PoolTracker) querySubgraph(ctx context.Context, p entity.Pool) (*queryS // query var ( query = buildQueryGetBins(p.Address, binIDGT) - req = graphql.NewRequest(query) + req = graphqlpkg.NewRequest(query) resp struct { Pair *lbpairSubgraphResp `json:"lbpair"` diff --git a/pkg/source/maverickv1/constant.go b/pkg/source/maverickv1/constant.go index fc7da130a..b84113dc1 100644 --- a/pkg/source/maverickv1/constant.go +++ b/pkg/source/maverickv1/constant.go @@ -1,17 +1,14 @@ package maverickv1 import ( - "time" - "github.com/holiman/uint256" bignumber "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/big256" ) const ( - DexTypeMaverickV1 = "maverick-v1" - graphQLRequestTimeout = 20 * time.Second - defaultChunk = 200 + DexTypeMaverickV1 = "maverick-v1" + defaultChunk = 200 poolMethodFee = "fee" poolMethodGetState = "getState" diff --git a/pkg/source/maverickv1/pool_list_updater.go b/pkg/source/maverickv1/pool_list_updater.go index 59ce76184..8690d5ac0 100644 --- a/pkg/source/maverickv1/pool_list_updater.go +++ b/pkg/source/maverickv1/pool_list_updater.go @@ -10,7 +10,6 @@ import ( "github.com/KyberNetwork/kutils" "github.com/KyberNetwork/logger" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util" @@ -20,19 +19,14 @@ import ( type PoolListUpdater struct { config *Config ethrpcClient *ethrpc.Client - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolListUpdater( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) *PoolListUpdater { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolListUpdater{ config: cfg, ethrpcClient: ethrpcClient, @@ -158,7 +152,7 @@ func (d *PoolListUpdater) querySubgraph( first int, skip int, ) ([]*SubgraphPool, error) { - req := graphql.NewRequest(fmt.Sprintf(`{ + req := graphqlpkg.NewRequest(fmt.Sprintf(`{ pools( where : { timestamp_gte: %v, diff --git a/pkg/source/nuriv2/constant.go b/pkg/source/nuriv2/constant.go index c374f92c1..8113bc73d 100644 --- a/pkg/source/nuriv2/constant.go +++ b/pkg/source/nuriv2/constant.go @@ -2,18 +2,16 @@ package nuriv2 import ( "math/big" - "time" ) const ( - DexType = "nuri-v2" - graphSkipLimit = 5000 - graphFirstLimit = 1000 - defaultTokenDecimals = 18 - defaultTokenWeight = 50 - zeroString = "0" - emptyString = "" - graphQLRequestTimeout = 20 * time.Second + DexType = "nuri-v2" + graphSkipLimit = 5000 + graphFirstLimit = 1000 + defaultTokenDecimals = 18 + defaultTokenWeight = 50 + zeroString = "0" + emptyString = "" ) const ( diff --git a/pkg/source/nuriv2/pool_tracker.go b/pkg/source/nuriv2/pool_tracker.go index b988b2e07..fabb0d59e 100644 --- a/pkg/source/nuriv2/pool_tracker.go +++ b/pkg/source/nuriv2/pool_tracker.go @@ -9,7 +9,6 @@ import ( "github.com/KyberNetwork/logger" "github.com/ethereum/go-ethereum/common" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/sourcegraph/conc/pool" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" @@ -21,19 +20,14 @@ import ( type PoolTracker struct { config *Config ethrpcClient *ethrpc.Client - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolTracker( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) (*PoolTracker, error) { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolTracker{ config: cfg, ethrpcClient: ethrpcClient, @@ -231,7 +225,7 @@ func (d *PoolTracker) getPoolTicks(ctx context.Context, poolAddress string) ([]T var ticks []TickResp for { - req := graphql.NewRequest(getPoolTicksQuery(allowSubgraphError, poolAddress, lastTickIdx)) + req := graphqlpkg.NewRequest(getPoolTicksQuery(allowSubgraphError, poolAddress, lastTickIdx)) var resp struct { Ticks []TickResp `json:"ticks"` diff --git a/pkg/source/nuriv2/pools_list_updater.go b/pkg/source/nuriv2/pools_list_updater.go index 5f80f26e6..950f3a38b 100644 --- a/pkg/source/nuriv2/pools_list_updater.go +++ b/pkg/source/nuriv2/pools_list_updater.go @@ -11,7 +11,6 @@ import ( "github.com/KyberNetwork/kutils" "github.com/KyberNetwork/logger" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/samber/lo" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" @@ -21,31 +20,26 @@ import ( type PoolsListUpdater struct { config *Config - graphqlClient *graphql.Client ethrpcClient *ethrpc.Client + graphqlClient *graphqlpkg.Client } func NewPoolsListUpdater( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) *PoolsListUpdater { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolsListUpdater{ config: cfg, - graphqlClient: graphqlClient, ethrpcClient: ethrpcClient, + graphqlClient: graphqlClient, } } func (d *PoolsListUpdater) getPoolsList(ctx context.Context, lastCreatedAtTimestamp *big.Int, first, skip int) ([]SubgraphPool, error) { allowSubgraphError := d.config.IsAllowSubgraphError() - req := graphql.NewRequest(getPoolsListQuery(allowSubgraphError, lastCreatedAtTimestamp, first, skip)) + req := graphqlpkg.NewRequest(getPoolsListQuery(allowSubgraphError, lastCreatedAtTimestamp, first, skip)) var response struct { Pools []SubgraphPool `json:"pools"` diff --git a/pkg/source/pancakev3/constant.go b/pkg/source/pancakev3/constant.go index b2c32c15d..71c16c550 100644 --- a/pkg/source/pancakev3/constant.go +++ b/pkg/source/pancakev3/constant.go @@ -2,21 +2,19 @@ package pancakev3 import ( "math/big" - "time" "github.com/KyberNetwork/uniswapv3-sdk-uint256/constants" "github.com/samber/lo" ) const ( - DexTypePancakeV3 = "pancake-v3" - graphSkipLimit = 5000 - graphFirstLimit = 1000 - defaultTokenDecimals = 18 - defaultTokenWeight = 50 - zeroString = "0" - emptyString = "" - graphQLRequestTimeout = 20 * time.Second + DexTypePancakeV3 = "pancake-v3" + graphSkipLimit = 5000 + graphFirstLimit = 1000 + defaultTokenDecimals = 18 + defaultTokenWeight = 50 + zeroString = "0" + emptyString = "" ) const ( diff --git a/pkg/source/pancakev3/pool_tracker.go b/pkg/source/pancakev3/pool_tracker.go index 45d0c01b2..4c977e6f5 100644 --- a/pkg/source/pancakev3/pool_tracker.go +++ b/pkg/source/pancakev3/pool_tracker.go @@ -9,7 +9,6 @@ import ( "github.com/KyberNetwork/logger" "github.com/ethereum/go-ethereum/common" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/sourcegraph/conc/pool" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" @@ -22,19 +21,14 @@ import ( type PoolTracker struct { config *Config ethrpcClient *ethrpc.Client - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolTracker( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) (*PoolTracker, error) { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolTracker{ config: cfg, ethrpcClient: ethrpcClient, @@ -250,7 +244,7 @@ func (d *PoolTracker) getPoolTicks(ctx context.Context, poolAddress string) ([]T var ticks []TickResp for { - req := graphql.NewRequest(getPoolTicksQuery(allowSubgraphError, poolAddress, lastTickIdx)) + req := graphqlpkg.NewRequest(getPoolTicksQuery(allowSubgraphError, poolAddress, lastTickIdx)) var resp struct { Ticks []TickResp `json:"ticks"` diff --git a/pkg/source/pancakev3/pools_list_updater.go b/pkg/source/pancakev3/pools_list_updater.go index 990716213..1c30f5dfa 100644 --- a/pkg/source/pancakev3/pools_list_updater.go +++ b/pkg/source/pancakev3/pools_list_updater.go @@ -11,7 +11,6 @@ import ( "github.com/KyberNetwork/kutils" "github.com/KyberNetwork/logger" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/samber/lo" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" @@ -21,31 +20,26 @@ import ( type PoolsListUpdater struct { config *Config - graphqlClient *graphql.Client ethrpcClient *ethrpc.Client + graphqlClient *graphqlpkg.Client } func NewPoolsListUpdater( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) *PoolsListUpdater { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolsListUpdater{ config: cfg, - graphqlClient: graphqlClient, ethrpcClient: ethrpcClient, + graphqlClient: graphqlClient, } } func (d *PoolsListUpdater) getPoolsList(ctx context.Context, lastCreatedAtTimestamp *big.Int, first, skip int) ([]SubgraphPool, error) { allowSubgraphError := d.config.IsAllowSubgraphError() - req := graphql.NewRequest(getPoolsListQuery(allowSubgraphError, lastCreatedAtTimestamp, first, skip)) + req := graphqlpkg.NewRequest(getPoolsListQuery(allowSubgraphError, lastCreatedAtTimestamp, first, skip)) var response struct { Pools []SubgraphPool `json:"pools"` diff --git a/pkg/source/platypus/constants.go b/pkg/source/platypus/constants.go index ecbc84ee6..4059ecf54 100644 --- a/pkg/source/platypus/constants.go +++ b/pkg/source/platypus/constants.go @@ -2,7 +2,6 @@ package platypus import ( "math/big" - "time" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util/bignumber" ) @@ -24,8 +23,6 @@ const ( addressZero = "0x0000000000000000000000000000000000000000" addressStakedAvax = "0x2b2c81e08f1af8835a78bb2a90ae924ace0ea4be" - - graphQLRequestTimeout = 20 * time.Second ) const ( diff --git a/pkg/source/platypus/pools_list_updater.go b/pkg/source/platypus/pools_list_updater.go index ddc590436..e6806b5c2 100644 --- a/pkg/source/platypus/pools_list_updater.go +++ b/pkg/source/platypus/pools_list_updater.go @@ -11,7 +11,6 @@ import ( "github.com/KyberNetwork/logger" "github.com/ethereum/go-ethereum/common" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/samber/lo" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" @@ -20,21 +19,19 @@ import ( type PoolsListUpdater struct { config *Config - graphqlClient *graphql.Client ethClient *ethrpc.Client + graphqlClient *graphqlpkg.Client } -func NewPoolsListUpdater(cfg *Config, ethClient *ethrpc.Client) *PoolsListUpdater { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - +func NewPoolsListUpdater( + cfg *Config, + ethClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, +) *PoolsListUpdater { return &PoolsListUpdater{ config: cfg, - graphqlClient: graphqlClient, ethClient: ethClient, + graphqlClient: graphqlClient, } } @@ -99,7 +96,7 @@ func (p *PoolsListUpdater) getPoolAddresses( ctx context.Context, lastUpdate string, ) ([]SubgraphPool, error) { - req := graphql.NewRequest(fmt.Sprintf(`{ + req := graphqlpkg.NewRequest(fmt.Sprintf(`{ pools ( where: { lastUpdate_gte: "%s" diff --git a/pkg/source/ramsesv2/constant.go b/pkg/source/ramsesv2/constant.go index 4d01caef5..2909cdb0f 100644 --- a/pkg/source/ramsesv2/constant.go +++ b/pkg/source/ramsesv2/constant.go @@ -2,7 +2,6 @@ package ramsesv2 import ( "math/big" - "time" ) const ( @@ -11,7 +10,6 @@ const ( graphFirstLimit = 1000 defaultTokenDecimals = 18 defaultTokenWeight = 50 - graphQLRequestTimeout = 20 * time.Second ) const ( diff --git a/pkg/source/ramsesv2/pool_tracker.go b/pkg/source/ramsesv2/pool_tracker.go index 685ad13a7..da48f8136 100644 --- a/pkg/source/ramsesv2/pool_tracker.go +++ b/pkg/source/ramsesv2/pool_tracker.go @@ -9,7 +9,6 @@ import ( "github.com/KyberNetwork/logger" "github.com/ethereum/go-ethereum/common" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/sourcegraph/conc/pool" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" @@ -22,18 +21,14 @@ import ( type PoolTracker struct { config *Config ethrpcClient *ethrpc.Client - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolTracker( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) (*PoolTracker, error) { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) return &PoolTracker{ config: cfg, @@ -251,7 +246,7 @@ func (d *PoolTracker) getPoolTicks(ctx context.Context, poolAddress string) ([]t var ticks []ticklens.TickResp for { - req := graphql.NewRequest(getPoolTicksQuery(allowSubgraphError, poolAddress, lastTickIdx)) + req := graphqlpkg.NewRequest(getPoolTicksQuery(allowSubgraphError, poolAddress, lastTickIdx)) var resp struct { Ticks []ticklens.TickResp `json:"ticks"` diff --git a/pkg/source/ramsesv2/pools_list_updater.go b/pkg/source/ramsesv2/pools_list_updater.go index da51b5d67..fa12e2748 100644 --- a/pkg/source/ramsesv2/pools_list_updater.go +++ b/pkg/source/ramsesv2/pools_list_updater.go @@ -11,7 +11,6 @@ import ( "github.com/KyberNetwork/kutils" "github.com/KyberNetwork/logger" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/samber/lo" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" @@ -21,31 +20,26 @@ import ( type PoolsListUpdater struct { config *Config - graphqlClient *graphql.Client ethrpcClient *ethrpc.Client + graphqlClient *graphqlpkg.Client } func NewPoolsListUpdater( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) *PoolsListUpdater { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolsListUpdater{ config: cfg, - graphqlClient: graphqlClient, ethrpcClient: ethrpcClient, + graphqlClient: graphqlClient, } } func (d *PoolsListUpdater) getPoolsList(ctx context.Context, lastCreatedAtTimestamp *big.Int, first, skip int) ([]SubgraphPool, error) { allowSubgraphError := d.config.IsAllowSubgraphError() - req := graphql.NewRequest(getPoolsListQuery(allowSubgraphError, lastCreatedAtTimestamp, first, skip)) + req := graphqlpkg.NewRequest(getPoolsListQuery(allowSubgraphError, lastCreatedAtTimestamp, first, skip)) var response struct { Pools []SubgraphPool `json:"pools"` diff --git a/pkg/source/slipstream/constant.go b/pkg/source/slipstream/constant.go index c1d9254c5..d98732ca6 100644 --- a/pkg/source/slipstream/constant.go +++ b/pkg/source/slipstream/constant.go @@ -2,18 +2,16 @@ package slipstream import ( "math/big" - "time" ) const ( - DexType = "slipstream" - graphSkipLimit = 5000 - graphFirstLimit = 1000 - defaultTokenDecimals = 18 - defaultTokenWeight = 50 - zeroString = "0" - emptyString = "" - graphQLRequestTimeout = 60 * time.Second + DexType = "slipstream" + graphSkipLimit = 5000 + graphFirstLimit = 1000 + defaultTokenDecimals = 18 + defaultTokenWeight = 50 + zeroString = "0" + emptyString = "" ) const ( diff --git a/pkg/source/slipstream/pool_tracker.go b/pkg/source/slipstream/pool_tracker.go index 47e77a72c..71d13c176 100644 --- a/pkg/source/slipstream/pool_tracker.go +++ b/pkg/source/slipstream/pool_tracker.go @@ -9,7 +9,6 @@ import ( "github.com/KyberNetwork/logger" "github.com/ethereum/go-ethereum/common" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/sourcegraph/conc/pool" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" @@ -20,19 +19,14 @@ import ( type PoolTracker struct { config *Config ethrpcClient *ethrpc.Client - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolTracker( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) (*PoolTracker, error) { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolTracker{ config: cfg, ethrpcClient: ethrpcClient, @@ -248,7 +242,7 @@ func (d *PoolTracker) getPoolTicks(ctx context.Context, poolAddress string) ([]T var ticks []TickResp for { - req := graphql.NewRequest(getPoolTicksQuery(allowSubgraphError, poolAddress, lastTickIdx)) + req := graphqlpkg.NewRequest(getPoolTicksQuery(allowSubgraphError, poolAddress, lastTickIdx)) var resp struct { Ticks []TickResp `json:"ticks"` diff --git a/pkg/source/slipstream/pools_list_updater.go b/pkg/source/slipstream/pools_list_updater.go index c8a8a77d5..83cdd4d18 100644 --- a/pkg/source/slipstream/pools_list_updater.go +++ b/pkg/source/slipstream/pools_list_updater.go @@ -11,7 +11,6 @@ import ( "github.com/KyberNetwork/kutils" "github.com/KyberNetwork/logger" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/samber/lo" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" @@ -21,31 +20,26 @@ import ( type PoolsListUpdater struct { config *Config - graphqlClient *graphql.Client ethrpcClient *ethrpc.Client + graphqlClient *graphqlpkg.Client } func NewPoolsListUpdater( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) *PoolsListUpdater { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolsListUpdater{ config: cfg, - graphqlClient: graphqlClient, ethrpcClient: ethrpcClient, + graphqlClient: graphqlClient, } } func (d *PoolsListUpdater) getPoolsList(ctx context.Context, lastCreatedAtTimestamp *big.Int, first, skip int) ([]SubgraphPool, error) { allowSubgraphError := d.config.IsAllowSubgraphError() - req := graphql.NewRequest(getPoolsListQuery(allowSubgraphError, lastCreatedAtTimestamp, first, skip)) + req := graphqlpkg.NewRequest(getPoolsListQuery(allowSubgraphError, lastCreatedAtTimestamp, first, skip)) var response struct { Pools []SubgraphPool `json:"pools"` diff --git a/pkg/source/solidly-v3/constant.go b/pkg/source/solidly-v3/constant.go index 8d5ae1814..2740c40ce 100644 --- a/pkg/source/solidly-v3/constant.go +++ b/pkg/source/solidly-v3/constant.go @@ -2,18 +2,16 @@ package solidlyv3 import ( "math/big" - "time" ) const ( - DexTypeSolidlyV3 = "solidly-v3" - graphSkipLimit = 5000 - graphFirstLimit = 1000 - defaultTokenDecimals = 18 - defaultTokenWeight = 50 - zeroString = "0" - emptyString = "" - graphQLRequestTimeout = 20 * time.Second + DexTypeSolidlyV3 = "solidly-v3" + graphSkipLimit = 5000 + graphFirstLimit = 1000 + defaultTokenDecimals = 18 + defaultTokenWeight = 50 + zeroString = "0" + emptyString = "" ) const ( diff --git a/pkg/source/solidly-v3/pool_tracker.go b/pkg/source/solidly-v3/pool_tracker.go index 8c1e45c2c..6e53c094d 100644 --- a/pkg/source/solidly-v3/pool_tracker.go +++ b/pkg/source/solidly-v3/pool_tracker.go @@ -9,7 +9,6 @@ import ( "github.com/KyberNetwork/logger" "github.com/ethereum/go-ethereum/common" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/sourcegraph/conc/pool" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" @@ -21,19 +20,14 @@ import ( type PoolTracker struct { config *Config ethrpcClient *ethrpc.Client - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolTracker( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) (*PoolTracker, error) { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolTracker{ config: cfg, ethrpcClient: ethrpcClient, @@ -222,7 +216,7 @@ func (d *PoolTracker) getPoolTicks(ctx context.Context, poolAddress string) ([]T var ticks []TickResp for { - req := graphql.NewRequest(getPoolTicksQuery(allowSubgraphError, poolAddress, lastTickIdx)) + req := graphqlpkg.NewRequest(getPoolTicksQuery(allowSubgraphError, poolAddress, lastTickIdx)) var resp struct { Pool *SubgraphPoolTicks `json:"pool"` diff --git a/pkg/source/solidly-v3/pools_list_updater.go b/pkg/source/solidly-v3/pools_list_updater.go index b9b762a0c..0a605fcc8 100644 --- a/pkg/source/solidly-v3/pools_list_updater.go +++ b/pkg/source/solidly-v3/pools_list_updater.go @@ -11,7 +11,6 @@ import ( "github.com/KyberNetwork/kutils" "github.com/KyberNetwork/logger" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/samber/lo" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" @@ -21,31 +20,26 @@ import ( type PoolsListUpdater struct { config *Config - graphqlClient *graphql.Client ethrpcClient *ethrpc.Client + graphqlClient *graphqlpkg.Client } func NewPoolsListUpdater( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) *PoolsListUpdater { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolsListUpdater{ config: cfg, - graphqlClient: graphqlClient, ethrpcClient: ethrpcClient, + graphqlClient: graphqlClient, } } func (d *PoolsListUpdater) getPoolsList(ctx context.Context, lastCreatedAtTimestamp *big.Int, first, skip int) ([]SubgraphPool, error) { allowSubgraphError := d.config.IsAllowSubgraphError() - req := graphql.NewRequest(getPoolsListQuery(allowSubgraphError, lastCreatedAtTimestamp, first, skip)) + req := graphqlpkg.NewRequest(getPoolsListQuery(allowSubgraphError, lastCreatedAtTimestamp, first, skip)) var response struct { Pools []SubgraphPool `json:"pools"` diff --git a/pkg/source/uniswapv3/constant.go b/pkg/source/uniswapv3/constant.go index e435837a0..88da7c962 100644 --- a/pkg/source/uniswapv3/constant.go +++ b/pkg/source/uniswapv3/constant.go @@ -2,19 +2,17 @@ package uniswapv3 import ( "math/big" - "time" ) const ( - DexTypeUniswapV3 = "uniswapv3" - graphSkipLimit = 5000 - graphFirstLimit = 1000 - defaultTokenDecimals = 18 - defaultTokenWeight = 50 - zeroString = "0" - emptyString = "" - graphQLRequestTimeout = 60 * time.Second - rpcChunkSize = 100 + DexTypeUniswapV3 = "uniswapv3" + graphSkipLimit = 5000 + graphFirstLimit = 1000 + defaultTokenDecimals = 18 + defaultTokenWeight = 50 + zeroString = "0" + emptyString = "" + rpcChunkSize = 100 ) const ( diff --git a/pkg/source/uniswapv3/pool_tracker.go b/pkg/source/uniswapv3/pool_tracker.go index c0740b26d..efbc6e487 100644 --- a/pkg/source/uniswapv3/pool_tracker.go +++ b/pkg/source/uniswapv3/pool_tracker.go @@ -10,7 +10,6 @@ import ( "github.com/KyberNetwork/logger" "github.com/ethereum/go-ethereum/common" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/samber/lo" "github.com/sourcegraph/conc/pool" @@ -23,24 +22,19 @@ import ( type PoolTracker struct { config *Config ethrpcClient *ethrpc.Client - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolTracker( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) (*PoolTracker, error) { initializedCfg, err := initializeConfig(cfg) if err != nil { return nil, err } - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolTracker{ config: initializedCfg, ethrpcClient: ethrpcClient, @@ -288,7 +282,7 @@ func (d *PoolTracker) getPoolTicks(ctx context.Context, poolAddress string) ([]T var ticks []TickResp for { - req := graphql.NewRequest(getPoolTicksQuery(allowSubgraphError, poolAddress, lastTickIdx)) + req := graphqlpkg.NewRequest(getPoolTicksQuery(allowSubgraphError, poolAddress, lastTickIdx)) var resp struct { Ticks []TickResp `json:"ticks"` diff --git a/pkg/source/uniswapv3/pools_list_updater.go b/pkg/source/uniswapv3/pools_list_updater.go index bdfde0f08..66125cb35 100644 --- a/pkg/source/uniswapv3/pools_list_updater.go +++ b/pkg/source/uniswapv3/pools_list_updater.go @@ -11,7 +11,6 @@ import ( "github.com/KyberNetwork/kutils" "github.com/KyberNetwork/logger" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/samber/lo" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" @@ -20,31 +19,26 @@ import ( type PoolsListUpdater struct { config *Config - graphqlClient *graphql.Client ethrpcClient *ethrpc.Client + graphqlClient *graphqlpkg.Client } func NewPoolsListUpdater( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) *PoolsListUpdater { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolsListUpdater{ config: cfg, - graphqlClient: graphqlClient, ethrpcClient: ethrpcClient, + graphqlClient: graphqlClient, } } func (d *PoolsListUpdater) getPoolsList(ctx context.Context, lastCreatedAtTimestamp *big.Int, first, skip int) ([]SubgraphPool, error) { allowSubgraphError := d.config.IsAllowSubgraphError() - req := graphql.NewRequest(getPoolsListQuery(allowSubgraphError, lastCreatedAtTimestamp, first, skip)) + req := graphqlpkg.NewRequest(getPoolsListQuery(allowSubgraphError, lastCreatedAtTimestamp, first, skip)) var response struct { Pools []SubgraphPool `json:"pools"` diff --git a/pkg/source/wombat/constant.go b/pkg/source/wombat/constant.go index abbacf3ca..74d755947 100644 --- a/pkg/source/wombat/constant.go +++ b/pkg/source/wombat/constant.go @@ -1,7 +1,5 @@ package wombat -import "time" - const ( DexTypeWombat = "wombat" @@ -21,8 +19,6 @@ const ( poolMethodStartCovRatio = "startCovRatio" poolMethodPaused = "paused" poolMethodCreditForTokensHaircut = "creditForTokensHaircut" - - graphQLRequestTimeout = 20 * time.Second ) var ( diff --git a/pkg/source/wombat/pool_tracker.go b/pkg/source/wombat/pool_tracker.go index cb27475ff..8023bad18 100644 --- a/pkg/source/wombat/pool_tracker.go +++ b/pkg/source/wombat/pool_tracker.go @@ -11,7 +11,6 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/ethclient/gethclient" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/source/pool" @@ -22,16 +21,14 @@ import ( type PoolTracker struct { config *Config ethrpcClient *ethrpc.Client - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } -func NewPoolTracker(cfg *Config, ethrpcClient *ethrpc.Client) *PoolTracker { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - +func NewPoolTracker( + cfg *Config, + ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, +) *PoolTracker { return &PoolTracker{ config: cfg, ethrpcClient: ethrpcClient, diff --git a/pkg/source/wombat/pools_list_updater.go b/pkg/source/wombat/pools_list_updater.go index 5d0533908..82975a57f 100644 --- a/pkg/source/wombat/pools_list_updater.go +++ b/pkg/source/wombat/pools_list_updater.go @@ -10,7 +10,6 @@ import ( "github.com/KyberNetwork/ethrpc" "github.com/KyberNetwork/logger" "github.com/goccy/go-json" - "github.com/machinebox/graphql" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/entity" "github.com/KyberNetwork/kyberswap-dex-lib/pkg/util" @@ -20,19 +19,14 @@ import ( type PoolsListUpdater struct { config *Config ethrpcClient *ethrpc.Client - graphqlClient *graphql.Client + graphqlClient *graphqlpkg.Client } func NewPoolsListUpdater( cfg *Config, ethrpcClient *ethrpc.Client, + graphqlClient *graphqlpkg.Client, ) *PoolsListUpdater { - graphqlClient := graphqlpkg.New(graphqlpkg.Config{ - Url: cfg.SubgraphAPI, - Header: cfg.SubgraphHeaders, - Timeout: graphQLRequestTimeout, - }) - return &PoolsListUpdater{ config: cfg, ethrpcClient: ethrpcClient, @@ -145,7 +139,7 @@ func (d *PoolsListUpdater) querySubgraph( ctx context.Context, lastCreateTime uint64, ) ([]*SubgraphPool, error) { - req := graphql.NewRequest(fmt.Sprintf(`{ + req := graphqlpkg.NewRequest(fmt.Sprintf(`{ pools( orderBy: createdTimestamp orderDirection: asc diff --git a/pkg/util/graphql/graphql.go b/pkg/util/graphql/graphql.go new file mode 100644 index 000000000..67648f240 --- /dev/null +++ b/pkg/util/graphql/graphql.go @@ -0,0 +1,221 @@ +package graphql + +import ( + "bytes" + "context" + "fmt" + "net/http" + + "github.com/go-resty/resty/v2" + "github.com/goccy/go-json" + "github.com/pkg/errors" +) + +type RunFunc func(ctx context.Context, req *Request, resp interface{}) error + +type ClientInterceptor func(ctx context.Context, req *Request, resp interface{}, fn RunFunc) error + +// Client is a client for interacting with a GraphQL API. +type Client struct { + endpoint string + restyClient *resty.Client + + chainedInt ClientInterceptor + chainInterceptor []ClientInterceptor + + Log func(format string, args ...interface{}) +} + +// NewClient makes a new Client capable of making GraphQL requests. +func NewClient(endpoint string, opts ...ClientOption) *Client { + c := &Client{ + endpoint: endpoint, + Log: func(format string, args ...interface{}) {}, + } + for _, optionFunc := range opts { + optionFunc(c) + } + if c.restyClient == nil { + c.restyClient = resty.New() + } + chainClientInterceptors(c) + return c +} + +func (c *Client) logf(format string, args ...interface{}) { + c.Log(format, args...) +} + +// Run executes the query and unmarshals the response from the data field +// into the response object. +// Pass in a nil response object to skip response parsing. +// If the request fails or the server returns an error, the first error +// will be returned. +func (c *Client) Run(ctx context.Context, req *Request, resp interface{}) error { + select { + case <-ctx.Done(): + return ctx.Err() + default: + } + if c.chainedInt != nil { + return c.chainedInt(ctx, req, resp, c.run) + } + return c.run(ctx, req, resp) +} + +func (c *Client) run(ctx context.Context, req *Request, resp interface{}) error { + return c.runWithJSON(ctx, req, resp) +} + +func (c *Client) runWithJSON(ctx context.Context, req *Request, resp interface{}) error { + var requestBody bytes.Buffer + requestBodyObj := struct { + Query string `json:"query"` + Variables map[string]interface{} `json:"variables"` + }{ + Query: req.q, + Variables: req.vars, + } + if err := json.NewEncoder(&requestBody).Encode(requestBodyObj); err != nil { + return errors.Wrap(err, "encode body") + } + c.logf(">> variables: %v", req.vars) + c.logf(">> query: %s", req.q) + gr := &graphResponse{ + Data: resp, + } + endpoint := c.endpoint + if req.URL != "" { + endpoint = req.URL + } + r := c.restyClient.R().SetContext(ctx).SetBody(requestBodyObj). + SetHeader("Content-Type", "application/json; charset=utf-8"). + SetHeader("Accept", "application/json; charset=utf-8") + for key, values := range req.Header { + for _, value := range values { + r.Header.Add(key, value) + } + } + c.logf(">> headers: %v", r.Header) + res, err := r.Post(endpoint) + if err != nil { + return err + } + if err = c.restyClient.JSONUnmarshal(res.Body(), gr); err != nil { + if res.StatusCode() != http.StatusOK { + return fmt.Errorf("graphql: server returned a non-200 status code: %v", res.StatusCode()) + } + return errors.Wrap(err, "decoding response") + } + + if len(gr.Errors) > 0 { + // return first error + return gr.Errors[0] + } + return nil +} + +// chainClientInterceptors chains all client interceptors into one +func chainClientInterceptors(client *Client) { + interceptors := client.chainInterceptor + + if client.chainedInt != nil { + interceptors = append([]ClientInterceptor{client.chainedInt}, interceptors...) + } + + var chainedInt ClientInterceptor + if len(interceptors) == 0 { + chainedInt = nil + } else if len(interceptors) == 1 { + chainedInt = interceptors[0] + } else { + chainedInt = func(ctx context.Context, req *Request, resp interface{}, runFn RunFunc) error { + return interceptors[0](ctx, req, resp, getChainRunFunc(interceptors, 0, runFn)) + } + } + client.chainedInt = chainedInt +} + +// getChainRunFunc recursively generate chained RunFunc +func getChainRunFunc(interceptors []ClientInterceptor, curr int, finalFn RunFunc) RunFunc { + if curr == len(interceptors)-1 { + return finalFn + } + return func(ctx context.Context, req *Request, resp interface{}) error { + return interceptors[curr+1](ctx, req, resp, getChainRunFunc(interceptors, curr+1, finalFn)) + } +} + +func WithChainClientInterceptor(interceptors ...ClientInterceptor) ClientOption { + return func(client *Client) { + client.chainInterceptor = append(client.chainInterceptor, interceptors...) + } +} + +// WithRestyClient specifies the underlying resty.Client to use when +// making requests. +// +// NewClient(endpoint, WithRestyClient(specificRestyClient)) +func WithRestyClient(restyClient *resty.Client) ClientOption { + return func(client *Client) { + client.restyClient = restyClient + } +} + +// ClientOption are functions that are passed into NewClient to +// modify the behaviour of the Client. +type ClientOption func(*Client) + +type graphErr struct { + Message string +} + +func (e graphErr) Error() string { + return "graphql: " + e.Message +} + +type graphResponse struct { + Data interface{} + Errors []graphErr +} + +// Request is a GraphQL request. +type Request struct { + q string + vars map[string]interface{} + + // Header represent any request headers that will be set + // when the request is made. + Header http.Header + + // If the URL is not empty when the request is made, + // it will be used instead of the client's endpoint. + URL string +} + +// NewRequest makes a new Request with the specified string. +func NewRequest(q string) *Request { + req := &Request{ + q: q, + Header: make(map[string][]string), + } + return req +} + +// Var sets a variable. +func (req *Request) Var(key string, value interface{}) { + if req.vars == nil { + req.vars = make(map[string]interface{}) + } + req.vars[key] = value +} + +// Vars gets the variables for this Request. +func (req *Request) Vars() map[string]interface{} { + return req.vars +} + +// Query gets the query string of this request. +func (req *Request) Query() string { + return req.q +} diff --git a/pkg/util/graphql/graphql_test.go b/pkg/util/graphql/graphql_test.go new file mode 100644 index 000000000..f31ed5b1e --- /dev/null +++ b/pkg/util/graphql/graphql_test.go @@ -0,0 +1,104 @@ +package graphql + +import ( + "context" + "net/http" + "testing" + + "github.com/stretchr/testify/require" +) + +func TestChainClientInterceptor(t *testing.T) { + expectedLogs := []string{ + "Before interceptor 1", + + "Before interceptor 2", + "Before interceptor 3", + "Before interceptor 4", + "Before interceptor 5", + "After interceptor 5", + "After interceptor 4", + "After interceptor 3", + "After interceptor 2", + + "Before interceptor 2", + "Before interceptor 3", + "Before interceptor 4", + "Before interceptor 5", + "After interceptor 5", + "After interceptor 4", + "After interceptor 3", + "After interceptor 2", + + "Before interceptor 2", + "Before interceptor 3", + "Before interceptor 4", + "Before interceptor 5", + "After interceptor 5", + "After interceptor 4", + "After interceptor 3", + "After interceptor 2", + + "After interceptor 1", + } + + var logs []string + chainClientInterceptorOpt := WithChainClientInterceptor([]ClientInterceptor{ + func(ctx context.Context, req *Request, resp interface{}, fn RunFunc) error { + logs = append(logs, "Before interceptor 1") + if req.Header == nil { + req.Header = make(http.Header) + } + req.Header.Set("x-custom-header-1", "a") + req.Header.Set("x-custom-header-2", "b") + err := fn(ctx, req, resp) + logs = append(logs, "After interceptor 1") + return err + }, + func(ctx context.Context, req *Request, resp interface{}, fn RunFunc) error { + var err error + for i := 0; i < 3; i++ { + logs = append(logs, "Before interceptor 2") + err = fn(ctx, req, resp) + logs = append(logs, "After interceptor 2") + } + return err + }, + func(ctx context.Context, req *Request, resp interface{}, fn RunFunc) error { + logs = append(logs, "Before interceptor 3") + err := fn(ctx, req, resp) + logs = append(logs, "After interceptor 3") + return err + }, + func(ctx context.Context, req *Request, resp interface{}, fn RunFunc) error { + logs = append(logs, "Before interceptor 4") + req.URL = "http://example.com" + err := fn(ctx, req, resp) + logs = append(logs, "After interceptor 4") + return err + }, + func(ctx context.Context, req *Request, resp interface{}, fn RunFunc) error { + logs = append(logs, "Before interceptor 5") + err := fn(ctx, req, resp) + req.Header.Set("x-custom-header-3", "c") + logs = append(logs, "After interceptor 5") + return err + }, + }...) + + req := &Request{} + client := NewClient("", chainClientInterceptorOpt) + _ = client.Run(context.Background(), req, nil) + + require.Equal(t, "a", req.Header.Get("x-custom-header-1")) + require.Equal(t, "b", req.Header.Get("x-custom-header-2")) + require.Equal(t, "c", req.Header.Get("x-custom-header-3")) + require.Equal(t, "http://example.com", req.URL) + require.Equal(t, len(expectedLogs), len(logs)) + for i, log := range logs { + if log != expectedLogs[i] { + t.Errorf("expected %s, got %s", expectedLogs[i], log) + } + } + +} diff --git a/pkg/valueobject/subgraph.go b/pkg/valueobject/subgraph.go index 08eafb119..5de8b1da4 100644 --- a/pkg/valueobject/subgraph.go +++ b/pkg/valueobject/subgraph.go @@ -6,6 +6,12 @@ import ( "github.com/KyberNetwork/logger" ) +type SubgraphSource string + +var ( + SubgraphSourceTheGraph SubgraphSource = "thegraph" +) + type SubgraphMeta struct { Block struct { Timestamp int64 `json:"timestamp"`