diff --git a/package.json b/package.json index 99e4742d..6c8b29ac 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@cowprotocol/cow-sdk", - "version": "4.0.6", + "version": "4.0.7", "license": "(MIT OR Apache-2.0)", "files": [ "/dist" diff --git a/src/subgraph/api.ts b/src/subgraph/api.ts index 64ad9056..d1559b64 100644 --- a/src/subgraph/api.ts +++ b/src/subgraph/api.ts @@ -3,13 +3,19 @@ import { LastDaysVolumeQuery, LastHoursVolumeQuery, TotalsQuery } from './graphq import { LAST_DAYS_VOLUME_QUERY, LAST_HOURS_VOLUME_QUERY, TOTALS_QUERY } from './queries' import { DocumentNode } from 'graphql/index' import { request, Variables } from 'graphql-request' -import { ApiContext, CowEnv, DEFAULT_COW_API_CONTEXT, PartialApiContext } from '../common/configs' +import { ApiContext, CowEnv, DEFAULT_COW_API_CONTEXT } from '../common/configs' import { SupportedChainId } from '../common/chains' const SUBGRAPH_BASE_URL = 'https://api.thegraph.com/subgraphs/name/cowprotocol' type SubgraphApiBaseUrls = Record +interface SubgraphApiContext extends Omit { + baseUrls?: SubgraphApiBaseUrls +} + +type PartialSubgraphApiContext = Partial + /** * CoW Protocol Production Subgraph API configuration. * @see {@link https://api.thegraph.com/subgraphs/name/cowprotocol/cow} @@ -42,13 +48,13 @@ export const SUBGRAPH_STAGING_CONFIG: SubgraphApiBaseUrls = { export class SubgraphApi { API_NAME = 'CoW Protocol Subgraph' - public context: ApiContext + public context: SubgraphApiContext /** * Create a new CoW Protocol API instance. - * @param context Any properties of the {@link ApiContext} may be overridden by passing a {@link PartialApiContext}. + * @param context Any properties of the {@link SubgraphApiContext} may be overridden by passing a {@link PartialSubgraphApiContext}. */ - constructor(context: PartialApiContext = {}) { + constructor(context: PartialSubgraphApiContext = {}) { this.context = { ...DEFAULT_COW_API_CONTEXT, ...context, @@ -60,7 +66,7 @@ export class SubgraphApi { * @param contextOverride Override the context for this call only. * @returns The totals for the CoW Protocol. */ - async getTotals(contextOverride: PartialApiContext = {}): Promise { + async getTotals(contextOverride: PartialSubgraphApiContext = {}): Promise { const response = await this.runQuery(TOTALS_QUERY, undefined, contextOverride) return response.totals[0] } @@ -68,20 +74,23 @@ export class SubgraphApi { /** * Query the volume over the last N days from TheGraph for the CoW Protocol. * @param {number} days The number of days to query. - * @param {PartialApiContext} contextOverride Override the context for this call only. + * @param {PartialSubgraphApiContext} contextOverride Override the context for this call only. * @returns The volume for the last N days. */ - async getLastDaysVolume(days: number, contextOverride: PartialApiContext = {}): Promise { + async getLastDaysVolume(days: number, contextOverride: PartialSubgraphApiContext = {}): Promise { return this.runQuery(LAST_DAYS_VOLUME_QUERY, { days }, contextOverride) } /** * Query the volume over the last N hours from TheGraph for the CoW Protocol. * @param {number} hours The number of hours to query. - * @param {PartialApiContext} contextOverride Override the context for this call only. + * @param {PartialSubgraphApiContext} contextOverride Override the context for this call only. * @returns The volume for the last N hours. */ - async getLastHoursVolume(hours: number, contextOverride: PartialApiContext = {}): Promise { + async getLastHoursVolume( + hours: number, + contextOverride: PartialSubgraphApiContext = {} + ): Promise { return this.runQuery(LAST_HOURS_VOLUME_QUERY, { hours }, contextOverride) } @@ -89,14 +98,14 @@ export class SubgraphApi { * Run a query against the CoW Protocol Subgraph. * @param {string | DocumentNode} query GQL query string or DocumentNode. * @param {Variables | undefined} variables To be passed to the query. - * @param {PartialApiContext} contextOverride Override the context for this call only. + * @param {PartialSubgraphApiContext} contextOverride Override the context for this call only. * @returns Results of the query. * @throws {@link CowError} if the query fails. */ async runQuery( query: string | DocumentNode, variables: Variables | undefined = undefined, - contextOverride: PartialApiContext = {} + contextOverride: PartialSubgraphApiContext = {} ): Promise { const { chainId, env } = this.getContextWithOverride(contextOverride) const baseUrl = this.getEnvConfigs(env)[chainId] @@ -117,10 +126,10 @@ export class SubgraphApi { /** * Override parts of the context for a specific call. - * @param {PartialApiContext} contextOverride Override the context for this call only. - * @returns {ApiContext} The context with the override applied. + * @param {PartialSubgraphApiContext} contextOverride Override the context for this call only. + * @returns {SubgraphApiContext} The context with the override applied. */ - private getContextWithOverride(contextOverride: PartialApiContext = {}): ApiContext { + private getContextWithOverride(contextOverride: PartialSubgraphApiContext = {}): SubgraphApiContext { return { ...this.context, ...contextOverride } }