mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Change function names. Add interval polling for api. Refactor functions.
This commit is contained in:
parent
716e65424d
commit
a612fcee64
@ -282,19 +282,30 @@ ConfigManager.prototype.getCurrentFiat = function () {
|
|||||||
return ('fiatCurrency' in data) && data.fiatCurrency
|
return ('fiatCurrency' in data) && data.fiatCurrency
|
||||||
}
|
}
|
||||||
|
|
||||||
ConfigManager.prototype.setConversionRate = function () {
|
ConfigManager.prototype.updateConversionRate = function () {
|
||||||
var data = this.getData()
|
var data = this.getData()
|
||||||
return rp(`https://www.cryptonator.com/api/ticker/eth-${data.fiatCurrency}`)
|
return rp(`https://www.cryptonator.com/api/ticker/eth-${data.fiatCurrency}`)
|
||||||
.then(function (response) {
|
.then((response) => {
|
||||||
const parsedResponse = JSON.parse(response)
|
const parsedResponse = JSON.parse(response)
|
||||||
data.conversionRate = Number(parsedResponse.ticker.price)
|
this.setConversionPrice(parsedResponse.ticker.price)
|
||||||
data.conversionDate = new Date(parsedResponse.timestamp).toString()
|
this.setConversionDate(parsedResponse.timestamp)
|
||||||
this.setData(data)
|
}).catch((err) => {
|
||||||
}.bind(this)).catch(function (err) {
|
console.error('Error in conversion.', err)
|
||||||
console.log('Error in conversion.', err)
|
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
ConfigManager.prototype.setConversionPrice = function(price) {
|
||||||
|
var data = this.getData()
|
||||||
|
data.conversionRate = Number(parsedResponse.ticker.price)
|
||||||
|
this.setData(data)
|
||||||
|
}
|
||||||
|
|
||||||
|
ConfigManager.prototype.setConversionDate = function (datestring) {
|
||||||
|
var data = this.getData()
|
||||||
|
data.conversionDate = datestring
|
||||||
|
this.setData(data)
|
||||||
|
}
|
||||||
|
|
||||||
ConfigManager.prototype.getConversionRate = function () {
|
ConfigManager.prototype.getConversionRate = function () {
|
||||||
var data = this.getData()
|
var data = this.getData()
|
||||||
return ('conversionRate' in data) && data.conversionRate
|
return ('conversionRate' in data) && data.conversionRate
|
||||||
|
@ -21,6 +21,7 @@ module.exports = class MetamaskController {
|
|||||||
this.idStore.setStore(this.ethStore)
|
this.idStore.setStore(this.ethStore)
|
||||||
this.messageManager = messageManager
|
this.messageManager = messageManager
|
||||||
this.publicConfigStore = this.initPublicConfigStore()
|
this.publicConfigStore = this.initPublicConfigStore()
|
||||||
|
this.scheduleConversionInterval()
|
||||||
}
|
}
|
||||||
|
|
||||||
getState () {
|
getState () {
|
||||||
@ -241,7 +242,8 @@ module.exports = class MetamaskController {
|
|||||||
setCurrentFiat (fiat, cb) {
|
setCurrentFiat (fiat, cb) {
|
||||||
try {
|
try {
|
||||||
this.configManager.setCurrentFiat(fiat)
|
this.configManager.setCurrentFiat(fiat)
|
||||||
this.configManager.setConversionRate()
|
this.configManager.updateConversionRate()
|
||||||
|
this.scheduleConversionInterval()
|
||||||
const data = {
|
const data = {
|
||||||
conversionRate: this.configManager.getConversionRate,
|
conversionRate: this.configManager.getConversionRate,
|
||||||
currentFiat: this.configManager.getCurrentFiat,
|
currentFiat: this.configManager.getCurrentFiat,
|
||||||
@ -253,6 +255,16 @@ module.exports = class MetamaskController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
scheduleConversionInterval () {
|
||||||
|
if (this.conversionInterval) {
|
||||||
|
clearInterval(this.conversionInterval)
|
||||||
|
}
|
||||||
|
this.conversionInterval = setInterval(() => {
|
||||||
|
console.log("Updated currency!")
|
||||||
|
this.configManager.updateConversionRate()
|
||||||
|
}, 1000)
|
||||||
|
}
|
||||||
|
|
||||||
// called from popup
|
// called from popup
|
||||||
setRpcTarget (rpcTarget) {
|
setRpcTarget (rpcTarget) {
|
||||||
this.configManager.setRpcTarget(rpcTarget)
|
this.configManager.setRpcTarget(rpcTarget)
|
||||||
|
@ -44,14 +44,14 @@ describe('config-manager', function() {
|
|||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('#setConversionRate', function() {
|
describe('#updateConversionRate', function() {
|
||||||
it('should retrieve an update for ETH to USD and set it in memory', function(done) {
|
it('should retrieve an update for ETH to USD and set it in memory', function(done) {
|
||||||
this.timeout(15000)
|
this.timeout(15000)
|
||||||
assert.equal(configManager.getConversionRate(), false)
|
assert.equal(configManager.getConversionRate(), false)
|
||||||
var promise = new Promise(
|
var promise = new Promise(
|
||||||
function (resolve, reject) {
|
function (resolve, reject) {
|
||||||
configManager.setCurrentFiat('usd')
|
configManager.setCurrentFiat('usd')
|
||||||
configManager.setConversionRate().then(function() {
|
configManager.updateConversionRate().then(function() {
|
||||||
resolve()
|
resolve()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
@ -72,7 +72,7 @@ describe('config-manager', function() {
|
|||||||
var promise = new Promise(
|
var promise = new Promise(
|
||||||
function (resolve, reject) {
|
function (resolve, reject) {
|
||||||
configManager.setCurrentFiat('jpy')
|
configManager.setCurrentFiat('jpy')
|
||||||
configManager.setConversionRate().then(function() {
|
configManager.updateConversionRate().then(function() {
|
||||||
resolve()
|
resolve()
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
Loading…
Reference in New Issue
Block a user