This repository was archived by the owner on Jun 24, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 54
[USD Price] Add coingecko api and hooks #1356
Merged
Merged
Changes from 8 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
5ec18b5
coingecko api
W3stside b07b95a
coingecko usd hooks
W3stside 6778b5c
use in swapmod
W3stside b9178af
replace useUSDCValue with useHigherUSDValue
W3stside 1034fef
comment
W3stside 70565af
mod computeFiatValue fn
W3stside 37c2604
made dumb comp
W3stside 0cbcffa
smarter container
W3stside 4e6b850
naming
W3stside fe1e867
remove unnecessary mod
W3stside 25176af
parse usd price from API as USDC
W3stside 59da08b
fix infiniLoop (TM)
W3stside 835420c
Merge remote-tracking branch 'origin/develop' into add-coingecko-usd-…
W3stside 8381aa0
remove trans from priceImpact
W3stside d0c2b22
fix baseAmount
W3stside 30c8219
Merge remote-tracking branch 'origin/develop' into add-coingecko-usd-…
W3stside File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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,98 @@ | ||
import { SupportedChainId as ChainId } from 'constants/chains' | ||
import { PriceInformation } from 'utils/price' | ||
|
||
function getApiUrl(): string { | ||
// it's all the same base url | ||
return 'https://api.coingecko.com/api' | ||
} | ||
|
||
// https://api.coingecko.com/api/v3/simple/token_price/ethereum?contract_addresses=0x33e18a092a93ff21ad04746c7da12e35d34dc7c4&vs_currencies=usd | ||
// Defaults | ||
const API_NAME = 'Coingecko' | ||
const API_BASE_URL = getApiUrl() | ||
const API_VERSION = 'v3' | ||
const DEFAULT_HEADERS = { | ||
'Content-Type': 'application/json', | ||
} | ||
|
||
function _getApiBaseUrl(chainId: ChainId): string { | ||
const baseUrl = API_BASE_URL | ||
|
||
if (!baseUrl) { | ||
throw new Error(`Unsupported Network. The ${API_NAME} API is not deployed in the Network ${chainId}`) | ||
} else { | ||
return baseUrl + '/' + API_VERSION | ||
} | ||
} | ||
|
||
function _getCoinGeckoAssetPlatform(chainId: ChainId) { | ||
switch (chainId) { | ||
// Use of asset platforms - supports ethereum and xdai | ||
// https://api.coingecko.com/api/v3/asset_platforms | ||
case ChainId.MAINNET: | ||
return 'ethereum' | ||
case ChainId.XDAI: | ||
return 'xdai' | ||
default: | ||
return null | ||
} | ||
} | ||
|
||
function _fetch(chainId: ChainId, url: string, method: 'GET' | 'POST' | 'DELETE', data?: any): Promise<Response> { | ||
const baseUrl = _getApiBaseUrl(chainId) | ||
return fetch(baseUrl + url, { | ||
headers: DEFAULT_HEADERS, | ||
method, | ||
body: data !== undefined ? JSON.stringify(data) : data, | ||
}) | ||
} | ||
|
||
// TODO: consider making these _get/_delete/_post etc reusable across apis | ||
function _get(chainId: ChainId, url: string): Promise<Response> { | ||
return _fetch(chainId, url, 'GET') | ||
} | ||
|
||
export interface CoinGeckoUsdPriceParams { | ||
chainId: ChainId | ||
tokenAddress: string | ||
} | ||
|
||
interface CoinGeckoUsdQuote { | ||
[address: string]: { | ||
usd: number | ||
} | ||
} | ||
|
||
export async function getUSDPriceQuote(params: CoinGeckoUsdPriceParams): Promise<CoinGeckoUsdQuote | null> { | ||
const { chainId, tokenAddress } = params | ||
// ethereum/xdai (chains) | ||
const assetPlatform = _getCoinGeckoAssetPlatform(chainId) | ||
if (assetPlatform == null) { | ||
// Unsupported | ||
return null | ||
} | ||
|
||
console.log(`[api:${API_NAME}] Get USD price from ${API_NAME}`, params) | ||
|
||
const response = await _get( | ||
chainId, | ||
`/simple/token_price/${assetPlatform}?contract_addresses=${tokenAddress}&vs_currencies=usd` | ||
).catch((error) => { | ||
console.error(`Error getting ${API_NAME} USD price quote:`, error) | ||
throw new Error(error) | ||
}) | ||
|
||
return response.json() | ||
} | ||
|
||
export function toPriceInformation(priceRaw: CoinGeckoUsdQuote | null): PriceInformation | null { | ||
// We only receive/want the first key/value pair in the return object | ||
const token = priceRaw ? Object.keys(priceRaw)[0] : null | ||
|
||
if (!token || !priceRaw?.[token].usd) { | ||
return null | ||
} | ||
|
||
const { usd } = priceRaw[token] | ||
return { amount: usd.toString(), token } | ||
} |
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
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
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
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
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
higher or highest?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
well technically "highest" would mean just that, the "highest" whereas "higher" implies of the choices given, which is "highest"
but yes it's very nitpicky here. happy to change it to whatever we like best @alfetopito @Anxo @nenadV91
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
whatever