From 8e2035e2110cbe85f849cff61df4fd1f645bbbbd Mon Sep 17 00:00:00 2001 From: David W3stside Date: Mon, 20 Dec 2021 11:09:27 +0000 Subject: [PATCH] add gp price strategy fetching to gp api --- src/custom/api/gnosisProtocol/api.ts | 48 +++++++++++++++++++++++++++- 1 file changed, 47 insertions(+), 1 deletion(-) diff --git a/src/custom/api/gnosisProtocol/api.ts b/src/custom/api/gnosisProtocol/api.ts index 444dd400c..945e1c9fc 100644 --- a/src/custom/api/gnosisProtocol/api.ts +++ b/src/custom/api/gnosisProtocol/api.ts @@ -1,4 +1,4 @@ -import { SupportedChainId as ChainId } from 'constants/chains' +import { SupportedChainId as ChainId, SupportedChainId } from 'constants/chains' import { OrderKind, QuoteQuery } from '@gnosis.pm/gp-v2-contracts' import { stringify } from 'qs' import { getSigningSchemeApiValue, OrderCreation, OrderCancellation, SigningSchemeValue } from 'utils/signatures' @@ -24,6 +24,7 @@ import { GAS_FEE_ENDPOINTS } from 'constants/index' import * as Sentry from '@sentry/browser' import { ZERO_ADDRESS } from 'constants/misc' import { getAppDataHash } from 'constants/appDataHash' +import { GpPriceStrategy } from '@src/custom/hooks/useGetGpPriceStrategy' function getGnosisProtocolUrl(): Partial> { if (isLocal || isDev || isPr || isBarn) { @@ -59,8 +60,17 @@ function getProfileUrl(): Partial> { } } +function getPriceStrategyUrl(): Record { + return { + [SupportedChainId.MAINNET]: 'https://raw.githubusercontent.com/gnosis/cowswap/configuration/price/strategy-1.json', + [SupportedChainId.RINKEBY]: 'https://raw.githubusercontent.com/gnosis/cowswap/configuration/price/strategy-4.json', + [SupportedChainId.XDAI]: 'https://raw.githubusercontent.com/gnosis/cowswap/configuration/price/strategy-100.json', + } +} + const API_BASE_URL = getGnosisProtocolUrl() const PROFILE_API_BASE_URL = getProfileUrl() +const STRATEGY_API_BASE_URL = getPriceStrategyUrl() const DEFAULT_HEADERS = { 'Content-Type': 'application/json', @@ -145,6 +155,20 @@ function _getProfileApiBaseUrl(chainId: ChainId): string { } } +function _getPriceStrategyApiBaseUrl(chainId: ChainId): string { + const baseUrl = STRATEGY_API_BASE_URL[chainId] + + if (!baseUrl) { + new Error( + `Unsupported Network. The ${API_NAME} strategy API is not deployed in the Network ` + + chainId + + '. Defaulting to using Mainnet strategy.' + ) + } + + return baseUrl +} + export function getOrderLink(chainId: ChainId, orderId: OrderID): string { const baseUrl = _getApiBaseUrl(chainId) @@ -174,6 +198,14 @@ function _fetchProfile( }) } +function _fetchPriceStrategy(chainId: ChainId): Promise { + const baseUrl = _getPriceStrategyApiBaseUrl(chainId) + return fetch(baseUrl, { + headers: DEFAULT_HEADERS, + method: 'GET', + }) +} + function _post(chainId: ChainId, url: string, data: any): Promise { return _fetch(chainId, url, 'POST', data) } @@ -427,6 +459,20 @@ export async function getProfileData(chainId: ChainId, address: string): Promise } } +export async function getPriceStrategy(chainId: ChainId): Promise { + console.log(`[api:${API_NAME}] Get GP price strategy for`, chainId) + + const response = await _fetchPriceStrategy(chainId) + + if (!response.ok) { + const errorResponse = await response.json() + console.log(errorResponse) + throw new Error(errorResponse?.description) + } else { + return response.json() + } +} + export interface GasFeeEndpointResponse { lastUpdate: string lowest: string