@f5i23q999d/cow-sdk
Version:
<p align="center"> <img width="400" src="https://github.com/cowprotocol/cow-sdk/raw/main/docs/images/CoW.png" /> </p>
120 lines • 5.38 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SubgraphApi = exports.SUBGRAPH_STAGING_CONFIG = exports.SUBGRAPH_PROD_CONFIG = void 0;
const graphql_request_1 = require("graphql-request");
const types_1 = require("../chains/types.js");
const cow_error_1 = require("../common/types/cow-error.js");
const queries_1 = require("./queries.js");
const config_1 = require("../common/consts/config.js");
const SUBGRAPH_BASE_URL = 'https://api.thegraph.com/subgraphs/name/cowprotocol';
/**
* CoW Protocol Production Subgraph API configuration.
* @see {@link https://api.thegraph.com/subgraphs/name/cowprotocol/cow}
* @see {@link https://api.thegraph.com/subgraphs/name/cowprotocol/cow-gc}
*/
exports.SUBGRAPH_PROD_CONFIG = {
[types_1.SupportedChainId.MAINNET]: SUBGRAPH_BASE_URL + '/cow',
[types_1.SupportedChainId.GNOSIS_CHAIN]: SUBGRAPH_BASE_URL + '/cow-gc',
[types_1.SupportedChainId.ARBITRUM_ONE]: null,
[types_1.SupportedChainId.BASE]: null,
[types_1.SupportedChainId.SEPOLIA]: null,
};
/**
* CoW Protocol Staging Subgraph API configuration.
* @deprecated
* @see {@link https://api.thegraph.com/subgraphs/name/cowprotocol/cow-staging}
* @see {@link https://api.thegraph.com/subgraphs/name/cowprotocol/cow-gc-staging}
*/
exports.SUBGRAPH_STAGING_CONFIG = {
[types_1.SupportedChainId.MAINNET]: SUBGRAPH_BASE_URL + '/cow-staging',
[types_1.SupportedChainId.GNOSIS_CHAIN]: SUBGRAPH_BASE_URL + '/cow-gc-staging',
[types_1.SupportedChainId.ARBITRUM_ONE]: null,
[types_1.SupportedChainId.BASE]: null,
[types_1.SupportedChainId.SEPOLIA]: null,
};
/**
* TheGraph API client for CoW Protocol.
*/
class SubgraphApi {
API_NAME = 'CoW Protocol Subgraph';
context;
/**
* Create a new CoW Protocol API instance.
* @param context Any properties of the {@link SubgraphApiContext} may be overridden by passing a {@link PartialSubgraphApiContext}.
*/
constructor(context = {}) {
this.context = {
...config_1.DEFAULT_COW_API_CONTEXT,
...context,
};
}
/**
* Query the totals from TheGraph for the CoW Protocol.
* @param contextOverride Override the context for this call only.
* @returns The totals for the CoW Protocol.
*/
async getTotals(contextOverride = {}) {
const response = await this.runQuery(queries_1.TOTALS_QUERY, undefined, contextOverride);
return response.totals[0];
}
/**
* Query the volume over the last N days from TheGraph for the CoW Protocol.
* @param {number} days The number of days to query.
* @param {PartialSubgraphApiContext} contextOverride Override the context for this call only.
* @returns The volume for the last N days.
*/
async getLastDaysVolume(days, contextOverride = {}) {
return this.runQuery(queries_1.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 {PartialSubgraphApiContext} contextOverride Override the context for this call only.
* @returns The volume for the last N hours.
*/
async getLastHoursVolume(hours, contextOverride = {}) {
return this.runQuery(queries_1.LAST_HOURS_VOLUME_QUERY, { hours }, contextOverride);
}
/**
* 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 {PartialSubgraphApiContext} contextOverride Override the context for this call only.
* @returns Results of the query.
* @throws {@link CowError} if the query fails.
*/
async runQuery(query, variables = undefined, contextOverride = {}) {
const { chainId, env } = this.getContextWithOverride(contextOverride);
const baseUrl = this.getEnvConfigs(env)[chainId];
if (baseUrl === null) {
throw new Error('Unsupported Network. The subgraph API is not available in the Network ' + chainId);
}
try {
return await (0, graphql_request_1.request)(baseUrl, query, variables);
}
catch (error) {
console.error(`[subgraph:${this.API_NAME}]`, error);
throw new cow_error_1.CowError(`Error running query: ${query}. Variables: ${JSON.stringify(variables)}. API: ${baseUrl}. Inner Error: ${error}`);
}
}
/**
* Override parts of the context for a specific call.
* @param {PartialSubgraphApiContext} contextOverride Override the context for this call only.
* @returns {SubgraphApiContext} The context with the override applied.
*/
getContextWithOverride(contextOverride = {}) {
return { ...this.context, ...contextOverride };
}
/**
* Get the base URLs for the given environment.
* @param {CowEnv} env The environment to get the base URLs for.
* @returns {ApiBaseUrls} The base URLs for the given environment.
*/
getEnvConfigs(env) {
if (this.context.baseUrls)
return this.context.baseUrls;
return env === 'prod' ? exports.SUBGRAPH_PROD_CONFIG : exports.SUBGRAPH_STAGING_CONFIG;
}
}
exports.SubgraphApi = SubgraphApi;
//# sourceMappingURL=api.js.map