mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
feature: switch token pricing to CoinGecko API (#6424)
This commit is contained in:
parent
7e0006a7fd
commit
a973a7420a
@ -28,16 +28,16 @@ class TokenRatesController {
|
|||||||
async updateExchangeRates () {
|
async updateExchangeRates () {
|
||||||
if (!this.isActive) { return }
|
if (!this.isActive) { return }
|
||||||
const contractExchangeRates = {}
|
const contractExchangeRates = {}
|
||||||
const nativeCurrency = this.currency ? this.currency.getState().nativeCurrency.toUpperCase() : 'ETH'
|
const nativeCurrency = this.currency ? this.currency.getState().nativeCurrency.toLowerCase() : 'eth'
|
||||||
const pairs = this._tokens.map(token => `pairs[]=${token.address}/${nativeCurrency}`)
|
const pairs = this._tokens.map(token => token.address).join(',')
|
||||||
const query = pairs.join('&')
|
const query = `contract_addresses=${pairs}&vs_currencies=${nativeCurrency}`
|
||||||
if (this._tokens.length > 0) {
|
if (this._tokens.length > 0) {
|
||||||
try {
|
try {
|
||||||
const response = await fetch(`https://exchanges.balanc3.net/pie?${query}&autoConversion=false`)
|
const response = await fetch(`https://api.coingecko.com/api/v3/simple/token_price/ethereum?${query}`)
|
||||||
const { prices = [] } = await response.json()
|
const prices = await response.json()
|
||||||
prices.forEach(({ pair, price }) => {
|
this._tokens.forEach(token => {
|
||||||
const address = pair.split('/')[0]
|
const price = prices[token.address.toLowerCase()]
|
||||||
contractExchangeRates[normalizeAddress(address)] = typeof price === 'number' ? price : 0
|
contractExchangeRates[normalizeAddress(token.address)] = price ? price[nativeCurrency] : 0
|
||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
log.warn(`MetaMask - TokenRatesController exchange rate fetch failed.`, error)
|
log.warn(`MetaMask - TokenRatesController exchange rate fetch failed.`, error)
|
||||||
|
Loading…
x
Reference in New Issue
Block a user