migration #9 - break out CurrencyController substate
parent
b233e7e37c
commit
9e4ef45b6a
@ -0,0 +1,70 @@ |
||||
const ObservableStore = require('obs-store') |
||||
const extend = require('xtend') |
||||
|
||||
// every ten minutes
|
||||
const POLLING_INTERVAL = 600000 |
||||
|
||||
class CurrencyController { |
||||
|
||||
constructor (opts = {}) { |
||||
const initState = extend({ |
||||
currentCurrency: 'USD', |
||||
conversionRate: 0, |
||||
conversionDate: 'N/A', |
||||
}, opts.initState) |
||||
this.store = new ObservableStore(initState) |
||||
} |
||||
|
||||
//
|
||||
// PUBLIC METHODS
|
||||
//
|
||||
|
||||
getCurrentCurrency () { |
||||
return this.store.getState().currentCurrency |
||||
} |
||||
|
||||
setCurrentCurrency (currentCurrency) { |
||||
this.store.updateState({ currentCurrency }) |
||||
} |
||||
|
||||
getConversionRate () { |
||||
return this.store.getState().conversionRate |
||||
} |
||||
|
||||
setConversionRate (conversionRate) { |
||||
this.store.updateState({ conversionRate }) |
||||
} |
||||
|
||||
getConversionDate () { |
||||
return this.store.getState().conversionDate |
||||
} |
||||
|
||||
setConversionDate (conversionDate) { |
||||
this.store.updateState({ conversionDate }) |
||||
} |
||||
|
||||
updateConversionRate () { |
||||
const currentCurrency = this.getCurrentCurrency() |
||||
return fetch(`https://www.cryptonator.com/api/ticker/eth-${currentCurrency}`) |
||||
.then(response => response.json()) |
||||
.then((parsedResponse) => { |
||||
this.setConversionRate(Number(parsedResponse.ticker.price)) |
||||
this.setConversionDate(Number(parsedResponse.timestamp)) |
||||
}).catch((err) => { |
||||
console.warn('MetaMask - Failed to query currency conversion.') |
||||
this.setConversionRate(0) |
||||
this.setConversionDate('N/A') |
||||
}) |
||||
} |
||||
|
||||
scheduleConversionInterval () { |
||||
if (this.conversionInterval) { |
||||
clearInterval(this.conversionInterval) |
||||
} |
||||
this.conversionInterval = setInterval(() => { |
||||
this.updateConversionRate() |
||||
}, POLLING_INTERVAL) |
||||
} |
||||
} |
||||
|
||||
module.exports = CurrencyController |
@ -0,0 +1,40 @@ |
||||
const version = 9 |
||||
|
||||
/* |
||||
|
||||
This migration breaks out the CurrencyController substate |
||||
|
||||
*/ |
||||
|
||||
const merge = require('deep-merge') |
||||
|
||||
module.exports = { |
||||
version,
|
||||
|
||||
migrate: function (versionedData) { |
||||
versionedData.meta.version = version |
||||
try { |
||||
const state = versionedData.data |
||||
const newState = transformState(state) |
||||
versionedData.data = newState |
||||
} catch (err) { |
||||
console.warn(`MetaMask Migration #${version}` + err.stack) |
||||
} |
||||
return Promise.resolve(versionedData) |
||||
}, |
||||
} |
||||
|
||||
function transformState (state) { |
||||
const newState = merge(state, { |
||||
CurrencyController: { |
||||
currentCurrency: state.currentFiat || 'USD', |
||||
conversionRate: state.conversionRate, |
||||
conversionDate: state.conversionDate, |
||||
}, |
||||
}) |
||||
delete newState.currentFiat |
||||
delete newState.conversionRate |
||||
delete newState.conversionDate |
||||
|
||||
return newState |
||||
} |
@ -0,0 +1,87 @@ |
||||
// polyfill fetch
|
||||
global.fetch = global.fetch || require('isomorphic-fetch') |
||||
|
||||
const assert = require('assert') |
||||
const extend = require('xtend') |
||||
const rp = require('request-promise') |
||||
const nock = require('nock') |
||||
const CurrencyController = require('../../app/scripts/lib/controllers/currency') |
||||
|
||||
describe('config-manager', function() { |
||||
var currencyController |
||||
|
||||
beforeEach(function() { |
||||
currencyController = new CurrencyController() |
||||
}) |
||||
|
||||
describe('currency conversions', function() { |
||||
|
||||
describe('#setCurrentCurrency', function() { |
||||
it('should return USD as default', function() { |
||||
assert.equal(currencyController.getCurrentCurrency(), 'USD') |
||||
}) |
||||
|
||||
it('should be able to set to other currency', function() { |
||||
assert.equal(currencyController.getCurrentCurrency(), 'USD') |
||||
currencyController.setCurrentCurrency('JPY') |
||||
var result = currencyController.getCurrentCurrency() |
||||
assert.equal(result, 'JPY') |
||||
}) |
||||
}) |
||||
|
||||
describe('#getConversionRate', function() { |
||||
it('should return undefined if non-existent', function() { |
||||
var result = currencyController.getConversionRate() |
||||
assert.ok(!result) |
||||
}) |
||||
}) |
||||
|
||||
describe('#updateConversionRate', function() { |
||||
it('should retrieve an update for ETH to USD and set it in memory', function(done) { |
||||
this.timeout(15000) |
||||
var usdMock = nock('https://www.cryptonator.com') |
||||
.get('/api/ticker/eth-USD') |
||||
.reply(200, '{"ticker":{"base":"ETH","target":"USD","price":"11.02456145","volume":"44948.91745289","change":"-0.01472534"},"timestamp":1472072136,"success":true,"error":""}') |
||||
|
||||
assert.equal(currencyController.getConversionRate(), 0) |
||||
currencyController.setCurrentCurrency('USD') |
||||
currencyController.updateConversionRate() |
||||
.then(function() { |
||||
var result = currencyController.getConversionRate() |
||||
console.log('currencyController.getConversionRate:', result) |
||||
assert.equal(typeof result, 'number') |
||||
done() |
||||
}).catch(function(err) { |
||||
done(err) |
||||
}) |
||||
|
||||
}) |
||||
|
||||
it('should work for JPY as well.', function() { |
||||
this.timeout(15000) |
||||
assert.equal(currencyController.getConversionRate(), 0) |
||||
|
||||
var jpyMock = nock('https://www.cryptonator.com') |
||||
.get('/api/ticker/eth-JPY') |
||||
.reply(200, '{"ticker":{"base":"ETH","target":"JPY","price":"11.02456145","volume":"44948.91745289","change":"-0.01472534"},"timestamp":1472072136,"success":true,"error":""}') |
||||
|
||||
|
||||
var promise = new Promise( |
||||
function (resolve, reject) { |
||||
currencyController.setCurrentCurrency('JPY') |
||||
currencyController.updateConversionRate().then(function() { |
||||
resolve() |
||||
}) |
||||
}) |
||||
|
||||
promise.then(function() { |
||||
var result = currencyController.getConversionRate() |
||||
assert.equal(typeof result, 'number') |
||||
}).catch(function(err) { |
||||
done(err) |
||||
}) |
||||
}) |
||||
}) |
||||
}) |
||||
|
||||
}) |
Loading…
Reference in new issue