Merge pull request #13 from poanetwork/enhance-conversion-rate-#4
Add POA/currency conversion rate for poa networks
This commit is contained in:
commit
5b8a8a1573
|
@ -25,6 +25,7 @@ class CurrencyController {
|
||||||
*/
|
*/
|
||||||
constructor (opts = {}) {
|
constructor (opts = {}) {
|
||||||
const initState = extend({
|
const initState = extend({
|
||||||
|
currentCoin: 'poa',
|
||||||
currentCurrency: 'usd',
|
currentCurrency: 'usd',
|
||||||
conversionRate: 0,
|
conversionRate: 0,
|
||||||
conversionDate: 'N/A',
|
conversionDate: 'N/A',
|
||||||
|
@ -36,6 +37,26 @@ class CurrencyController {
|
||||||
// PUBLIC METHODS
|
// PUBLIC METHODS
|
||||||
//
|
//
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A getter for the currentCoin property
|
||||||
|
*
|
||||||
|
* @returns {string} A 2-4 character shorthand that describes a specific coin, related to the network
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
getCurrentCoin () {
|
||||||
|
return this.store.getState().currentCoin
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A setter for the currentCoin property
|
||||||
|
*
|
||||||
|
* @param {string} currentCoin The new coin to set as the currentCoin in the store
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
setCurrentCoin (currentCoin) {
|
||||||
|
this.store.updateState({ currentCoin })
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A getter for the currentCurrency property
|
* A getter for the currentCurrency property
|
||||||
*
|
*
|
||||||
|
@ -59,7 +80,7 @@ class CurrencyController {
|
||||||
/**
|
/**
|
||||||
* A getter for the conversionRate property
|
* A getter for the conversionRate property
|
||||||
*
|
*
|
||||||
* @returns {string} The conversion rate from ETH to the selected currency.
|
* @returns {string} The conversion rate from current coin to the selected currency.
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
getConversionRate () {
|
getConversionRate () {
|
||||||
|
@ -104,15 +125,27 @@ class CurrencyController {
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
async updateConversionRate () {
|
async updateConversionRate () {
|
||||||
let currentCurrency
|
let currentCurrency, currentCoin
|
||||||
try {
|
try {
|
||||||
currentCurrency = this.getCurrentCurrency()
|
currentCurrency = this.getCurrentCurrency()
|
||||||
|
currentCoin = this.getCurrentCoin()
|
||||||
|
let conversionRate, conversionDate
|
||||||
|
if (currentCoin === 'poa') {
|
||||||
|
const coinId = await this.getCoinMarketCapId(currentCoin)
|
||||||
|
const response = await fetch(`https://api.coinmarketcap.com/v2/ticker/${coinId}/?convert=${currentCurrency.toLowerCase()}`)
|
||||||
|
const parsedResponse = await response.json()
|
||||||
|
conversionRate = Number(parsedResponse.data.quotes[currentCurrency.toUpperCase()].price)
|
||||||
|
conversionDate = Number(parsedResponse.metadata.timestamp)
|
||||||
|
} else {
|
||||||
const response = await fetch(`https://api.infura.io/v1/ticker/eth${currentCurrency.toLowerCase()}`)
|
const response = await fetch(`https://api.infura.io/v1/ticker/eth${currentCurrency.toLowerCase()}`)
|
||||||
const parsedResponse = await response.json()
|
const parsedResponse = await response.json()
|
||||||
this.setConversionRate(Number(parsedResponse.bid))
|
conversionRate = Number(parsedResponse.bid)
|
||||||
this.setConversionDate(Number(parsedResponse.timestamp))
|
conversionDate = Number(parsedResponse.timestamp)
|
||||||
|
}
|
||||||
|
this.setConversionRate(conversionRate)
|
||||||
|
this.setConversionDate(conversionDate)
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
log.warn(`Nifty Wallet - Failed to query currency conversion:`, currentCurrency, err)
|
log.warn(`Nifty Wallet - Failed to query currency conversion:`, currentCoin, currentCurrency, err)
|
||||||
this.setConversionRate(0)
|
this.setConversionRate(0)
|
||||||
this.setConversionDate('N/A')
|
this.setConversionDate('N/A')
|
||||||
}
|
}
|
||||||
|
@ -132,6 +165,17 @@ class CurrencyController {
|
||||||
this.updateConversionRate()
|
this.updateConversionRate()
|
||||||
}, POLLING_INTERVAL)
|
}, POLLING_INTERVAL)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async getCoinMarketCapId (symbol) {
|
||||||
|
const response = await fetch(`https://api.coinmarketcap.com/v2/listings/`)
|
||||||
|
const parsedResponse = await response.json()
|
||||||
|
const results = parsedResponse.data.filter(coin => coin.symbol === symbol.toUpperCase())
|
||||||
|
if (!results.length) {
|
||||||
|
throw new Error(`Nifty Wallet - Failed to fetch ${symbol} from coinmarketcap listings`)
|
||||||
|
}
|
||||||
|
return results[0].id
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = CurrencyController
|
module.exports = CurrencyController
|
||||||
|
|
|
@ -333,6 +333,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
||||||
// etc
|
// etc
|
||||||
getState: (cb) => cb(null, this.getState()),
|
getState: (cb) => cb(null, this.getState()),
|
||||||
setCurrentCurrency: this.setCurrentCurrency.bind(this),
|
setCurrentCurrency: this.setCurrentCurrency.bind(this),
|
||||||
|
setCurrentCoin: this.setCurrentCoin.bind(this),
|
||||||
setUseBlockie: this.setUseBlockie.bind(this),
|
setUseBlockie: this.setUseBlockie.bind(this),
|
||||||
setCurrentLocale: this.setCurrentLocale.bind(this),
|
setCurrentLocale: this.setCurrentLocale.bind(this),
|
||||||
markAccountsFound: this.markAccountsFound.bind(this),
|
markAccountsFound: this.markAccountsFound.bind(this),
|
||||||
|
@ -1184,6 +1185,27 @@ module.exports = class MetamaskController extends EventEmitter {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A method for setting the network coin.
|
||||||
|
* @param {string} coinCode - The code of the coin.
|
||||||
|
* @param {Function} cb - A callback function returning currency info.
|
||||||
|
*/
|
||||||
|
async setCurrentCoin (coinCode, cb) {
|
||||||
|
try {
|
||||||
|
this.currencyController.setCurrentCoin(coinCode)
|
||||||
|
await this.currencyController.updateConversionRate()
|
||||||
|
const data = {
|
||||||
|
conversionRate: this.currencyController.getConversionRate(),
|
||||||
|
currentCoin: this.currencyController.getCurrentCoin(),
|
||||||
|
currentCurrency: this.currencyController.getCurrentCurrency(),
|
||||||
|
conversionDate: this.currencyController.getConversionDate(),
|
||||||
|
}
|
||||||
|
cb(null, data)
|
||||||
|
} catch (err) {
|
||||||
|
cb(err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* A method for forwarding the user to the easiest way to obtain ether,
|
* A method for forwarding the user to the easiest way to obtain ether,
|
||||||
* or the network "gas" currency, for the current selected network.
|
* or the network "gas" currency, for the current selected network.
|
||||||
|
|
|
@ -1543,6 +1543,22 @@ function setProviderType (type) {
|
||||||
dispatch(actions.setSelectedToken())
|
dispatch(actions.setSelectedToken())
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const newCoin = type === 'poa' || type === 'sokol' ? 'poa' : 'eth'
|
||||||
|
background.setCurrentCoin(newCoin, (err, data) => {
|
||||||
|
if (err) {
|
||||||
|
log.error(err.stack)
|
||||||
|
return dispatch(actions.displayWarning(err.message))
|
||||||
|
}
|
||||||
|
dispatch({
|
||||||
|
type: actions.SET_CURRENT_FIAT,
|
||||||
|
value: {
|
||||||
|
currentCurrency: data.currentCurrency,
|
||||||
|
conversionRate: data.conversionRate,
|
||||||
|
conversionDate: data.conversionDate,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
})
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue