mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Update Balanc3 API (#5744)
* Update balanc3 API used in TokenRatesController * Remove demo URL and use nativeCurrency when fetching token rates
This commit is contained in:
parent
3866222285
commit
0549782595
@ -14,8 +14,9 @@ class TokenRatesController {
|
|||||||
*
|
*
|
||||||
* @param {Object} [config] - Options to configure controller
|
* @param {Object} [config] - Options to configure controller
|
||||||
*/
|
*/
|
||||||
constructor ({ interval = DEFAULT_INTERVAL, preferences } = {}) {
|
constructor ({ interval = DEFAULT_INTERVAL, currency, preferences } = {}) {
|
||||||
this.store = new ObservableStore()
|
this.store = new ObservableStore()
|
||||||
|
this.currency = currency
|
||||||
this.preferences = preferences
|
this.preferences = preferences
|
||||||
this.interval = interval
|
this.interval = interval
|
||||||
}
|
}
|
||||||
@ -26,32 +27,23 @@ class TokenRatesController {
|
|||||||
async updateExchangeRates () {
|
async updateExchangeRates () {
|
||||||
if (!this.isActive) { return }
|
if (!this.isActive) { return }
|
||||||
const contractExchangeRates = {}
|
const contractExchangeRates = {}
|
||||||
// copy array to ensure its not modified during iteration
|
const nativeCurrency = this.currency ? this.currency.getState().nativeCurrency.toUpperCase() : 'ETH'
|
||||||
const tokens = this._tokens.slice()
|
const pairs = this._tokens.map(token => `pairs[]=${token.address}/${nativeCurrency}`)
|
||||||
for (const token of tokens) {
|
const query = pairs.join('&')
|
||||||
if (!token) return log.error(`TokenRatesController - invalid tokens state:\n${JSON.stringify(tokens, null, 2)}`)
|
if (this._tokens.length > 0) {
|
||||||
const address = token.address
|
try {
|
||||||
contractExchangeRates[address] = await this.fetchExchangeRate(address)
|
const response = await fetch(`https://exchanges.balanc3.net/pie?${query}&autoConversion=true`)
|
||||||
|
const { prices = [] } = await response.json()
|
||||||
|
prices.forEach(({ pair, price }) => {
|
||||||
|
contractExchangeRates[pair.split('/')[0]] = typeof price === 'number' ? price : 0
|
||||||
|
})
|
||||||
|
} catch (error) {
|
||||||
|
log.warn(`MetaMask - TokenRatesController exchange rate fetch failed.`, error)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
this.store.putState({ contractExchangeRates })
|
this.store.putState({ contractExchangeRates })
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetches a token exchange rate by address
|
|
||||||
*
|
|
||||||
* @param {String} address - Token contract address
|
|
||||||
*/
|
|
||||||
async fetchExchangeRate (address) {
|
|
||||||
try {
|
|
||||||
const response = await fetch(`https://metamask.balanc3.net/prices?from=${address}&to=ETH&autoConversion=false&summaryOnly=true`)
|
|
||||||
const json = await response.json()
|
|
||||||
return json && json.length ? json[0].averagePrice : 0
|
|
||||||
} catch (error) {
|
|
||||||
log.warn(`MetaMask - TokenRatesController exchange rate fetch failed for ${address}.`, error)
|
|
||||||
return 0
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @type {Number}
|
* @type {Number}
|
||||||
*/
|
*/
|
||||||
|
@ -117,6 +117,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
|
|
||||||
// token exchange rate tracker
|
// token exchange rate tracker
|
||||||
this.tokenRatesController = new TokenRatesController({
|
this.tokenRatesController = new TokenRatesController({
|
||||||
|
currency: this.currencyController.store,
|
||||||
preferences: this.preferencesController.store,
|
preferences: this.preferencesController.store,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
@ -17,13 +17,4 @@ describe('TokenRatesController', () => {
|
|||||||
assert.strictEqual(stub.getCall(0).args[1], 1337)
|
assert.strictEqual(stub.getCall(0).args[1], 1337)
|
||||||
stub.restore()
|
stub.restore()
|
||||||
})
|
})
|
||||||
|
|
||||||
it('should fetch each token rate based on address', async () => {
|
|
||||||
const controller = new TokenRatesController()
|
|
||||||
controller.isActive = true
|
|
||||||
controller.fetchExchangeRate = address => address
|
|
||||||
controller.tokens = [{ address: 'foo' }, { address: 'bar' }]
|
|
||||||
await controller.updateExchangeRates()
|
|
||||||
assert.deepEqual(controller.store.getState().contractExchangeRates, { foo: 'foo', bar: 'bar' })
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
Loading…
x
Reference in New Issue
Block a user