Skip to content
This repository has been archived by the owner on Jun 24, 2022. It is now read-only.

Commit

Permalink
add gp price strategy fetching to gp api
Browse files Browse the repository at this point in the history
  • Loading branch information
W3stside committed Dec 20, 2021
1 parent 91ff4e9 commit 8e2035e
Showing 1 changed file with 47 additions and 1 deletion.
48 changes: 47 additions & 1 deletion src/custom/api/gnosisProtocol/api.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand All @@ -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<Record<ChainId, string>> {
if (isLocal || isDev || isPr || isBarn) {
Expand Down Expand Up @@ -59,8 +60,17 @@ function getProfileUrl(): Partial<Record<ChainId, string>> {
}
}

function getPriceStrategyUrl(): Record<SupportedChainId, string> {
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',
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -174,6 +198,14 @@ function _fetchProfile(
})
}

function _fetchPriceStrategy(chainId: ChainId): Promise<Response> {
const baseUrl = _getPriceStrategyApiBaseUrl(chainId)
return fetch(baseUrl, {
headers: DEFAULT_HEADERS,
method: 'GET',
})
}

function _post(chainId: ChainId, url: string, data: any): Promise<Response> {
return _fetch(chainId, url, 'POST', data)
}
Expand Down Expand Up @@ -427,6 +459,20 @@ export async function getProfileData(chainId: ChainId, address: string): Promise
}
}

export async function getPriceStrategy(chainId: ChainId): Promise<GpPriceStrategy> {
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
Expand Down

0 comments on commit 8e2035e

Please sign in to comment.