-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: add getBffUsdPrice * feat: replace Coingecko price source with BFF Keep the existing fallbacks in case BFF is down * feat: remove getCoingeckoUsdPrice * chore: remove redundant variable * refactor: use try/catch instead of promise chainning
- Loading branch information
1 parent
7d3242c
commit 2bd17bf
Showing
3 changed files
with
85 additions
and
135 deletions.
There are no files selected for viewing
62 changes: 62 additions & 0 deletions
62
apps/cowswap-frontend/src/modules/usdAmount/apis/getBffUsdPrice.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
import { BFF_BASE_URL } from '@cowprotocol/common-const' | ||
import { FractionUtils } from '@cowprotocol/common-utils' | ||
import { Fraction, Token } from '@uniswap/sdk-core' | ||
|
||
import ms from 'ms.macro' | ||
|
||
import { fetchWithRateLimit } from 'common/utils/fetch' | ||
|
||
import { UnknownCurrencyError } from './errors' | ||
|
||
type BffUsdPriceResponse = { | ||
price: number | ||
} | ||
type BffUsdErrorResponse = { message: string } | ||
type BffResponse = BffUsdPriceResponse | BffUsdErrorResponse | ||
|
||
const fetchRateLimited = fetchWithRateLimit({ | ||
// Allow 5 requests per second | ||
rateLimit: { | ||
tokensPerInterval: 5, | ||
interval: 'second', | ||
}, | ||
// 2 retry attempts with 100ms delay | ||
backoff: { | ||
maxDelay: ms`0.1s`, | ||
numOfAttempts: 2, | ||
}, | ||
}) | ||
|
||
export async function getBffUsdPrice(currency: Token): Promise<Fraction | null> { | ||
const url = `${BFF_BASE_URL}/${currency.chainId}/tokens/${currency.address}/usdPrice` | ||
|
||
try { | ||
const res = await fetchRateLimited(url) | ||
|
||
// Token not found | ||
if (res.status === 404) { | ||
throw new UnknownCurrencyError({ | ||
cause: `BFF did not return a price for '${currency.address}' on chain '${currency.chainId}'`, | ||
}) | ||
// Unknown error case | ||
} else if (res.status !== 200) { | ||
throw new Error(`Unexpected response from BFF: ${res.status}`) | ||
} | ||
|
||
const data: BffResponse = await res.json() | ||
|
||
// 200 response with error message | ||
if (isErrorResponse(data)) { | ||
throw new Error(`Unexpected response from BFF: ${JSON.stringify(data)}`) | ||
} | ||
|
||
// Happy path | ||
return FractionUtils.fromNumber(data.price) | ||
} catch (error) { | ||
return Promise.reject(error) | ||
} | ||
} | ||
|
||
function isErrorResponse(response: BffResponse): response is BffUsdErrorResponse { | ||
return 'message' in response && !('price' in response) | ||
} |
98 changes: 0 additions & 98 deletions
98
apps/cowswap-frontend/src/modules/usdAmount/apis/getCoingeckoUsdPrice.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters