diff options
author | kumavis <kumavis@users.noreply.github.com> | 2017-02-06 08:27:45 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2017-02-06 08:27:45 +0800 |
commit | 15ae20033c22a3958ad7ff6ac74f74b1c0d2dbf5 (patch) | |
tree | 0008a579764bc5fad04568e2b3eea2dc90a7629d /app/scripts/lib | |
parent | 24ff38e97386eafe0cd9c8e4e77dbed856b2464e (diff) | |
parent | c0637f8d6ad969f16b7e8b582f462a7f9c480537 (diff) | |
download | tangerine-wallet-browser-15ae20033c22a3958ad7ff6ac74f74b1c0d2dbf5.tar tangerine-wallet-browser-15ae20033c22a3958ad7ff6ac74f74b1c0d2dbf5.tar.gz tangerine-wallet-browser-15ae20033c22a3958ad7ff6ac74f74b1c0d2dbf5.tar.bz2 tangerine-wallet-browser-15ae20033c22a3958ad7ff6ac74f74b1c0d2dbf5.tar.lz tangerine-wallet-browser-15ae20033c22a3958ad7ff6ac74f74b1c0d2dbf5.tar.xz tangerine-wallet-browser-15ae20033c22a3958ad7ff6ac74f74b1c0d2dbf5.tar.zst tangerine-wallet-browser-15ae20033c22a3958ad7ff6ac74f74b1c0d2dbf5.zip |
Merge pull request #1088 from MetaMask/CreateShapeshiftController
Create shapeshift controller
Diffstat (limited to 'app/scripts/lib')
-rw-r--r-- | app/scripts/lib/config-manager.js | 34 | ||||
-rw-r--r-- | app/scripts/lib/controllers/shapeshift.js | 104 | ||||
-rw-r--r-- | app/scripts/lib/idStore.js | 1 |
3 files changed, 104 insertions, 35 deletions
diff --git a/app/scripts/lib/config-manager.js b/app/scripts/lib/config-manager.js index a9b86ca8c..7ae2d4400 100644 --- a/app/scripts/lib/config-manager.js +++ b/app/scripts/lib/config-manager.js @@ -250,40 +250,6 @@ ConfigManager.prototype.getTOSHash = function () { return data.TOSHash } -ConfigManager.prototype.getShapeShiftTxList = function () { - var data = this.getData() - var shapeShiftTxList = data.shapeShiftTxList ? data.shapeShiftTxList : [] - shapeShiftTxList.forEach((tx) => { - if (tx.response.status !== 'complete') { - var requestListner = function (request) { - tx.response = JSON.parse(this.responseText) - if (tx.response.status === 'complete') { - tx.time = new Date().getTime() - } - } - - var shapShiftReq = new XMLHttpRequest() - shapShiftReq.addEventListener('load', requestListner) - shapShiftReq.open('GET', `https://shapeshift.io/txStat/${tx.depositAddress}`, true) - shapShiftReq.send() - } - }) - this.setData(data) - return shapeShiftTxList -} - -ConfigManager.prototype.createShapeShiftTx = function (depositAddress, depositType) { - var data = this.getData() - - var shapeShiftTx = {depositAddress, depositType, key: 'shapeshift', time: new Date().getTime(), response: {}} - if (!data.shapeShiftTxList) { - data.shapeShiftTxList = [shapeShiftTx] - } else { - data.shapeShiftTxList.push(shapeShiftTx) - } - this.setData(data) -} - ConfigManager.prototype.getGasMultiplier = function () { var data = this.getData() return data.gasMultiplier diff --git a/app/scripts/lib/controllers/shapeshift.js b/app/scripts/lib/controllers/shapeshift.js new file mode 100644 index 000000000..3d955c01f --- /dev/null +++ b/app/scripts/lib/controllers/shapeshift.js @@ -0,0 +1,104 @@ +const ObservableStore = require('obs-store') +const extend = require('xtend') + +// every three seconds when an incomplete tx is waiting +const POLLING_INTERVAL = 3000 + +class ShapeshiftController { + + constructor (opts = {}) { + const initState = extend({ + shapeShiftTxList: [], + }, opts.initState) + this.store = new ObservableStore(initState) + this.pollForUpdates() + } + + // + // PUBLIC METHODS + // + + getShapeShiftTxList () { + const shapeShiftTxList = this.store.getState().shapeShiftTxList + return shapeShiftTxList + } + + getPendingTxs () { + const txs = this.getShapeShiftTxList() + const pending = txs.filter(tx => tx.response && tx.response.status !== 'complete') + return pending + } + + pollForUpdates () { + const pendingTxs = this.getPendingTxs() + + if (pendingTxs.length === 0) { + return + } + + Promise.all(pendingTxs.map((tx) => { + return this.updateTx(tx) + })) + .then((results) => { + results.forEach(tx => this.saveTx(tx)) + this.timeout = setTimeout(this.pollForUpdates.bind(this), POLLING_INTERVAL) + }) + } + + updateTx (tx) { + const url = `https://shapeshift.io/txStat/${tx.depositAddress}` + return fetch(url) + .then((response) => { + return response.json() + }).then((json) => { + tx.response = json + if (tx.response.status === 'complete') { + tx.time = new Date().getTime() + } + return tx + }) + } + + saveTx (tx) { + const { shapeShiftTxList } = this.store.getState() + const index = shapeShiftTxList.indexOf(tx) + if (index !== -1) { + shapeShiftTxList[index] = tx + this.store.updateState({ shapeShiftTxList }) + } + } + + removeShapeShiftTx (tx) { + const { shapeShiftTxList } = this.store.getState() + const index = shapeShiftTxList.indexOf(index) + if (index !== -1) { + shapeShiftTxList.splice(index, 1) + } + this.updateState({ shapeShiftTxList }) + } + + createShapeShiftTx (depositAddress, depositType) { + const state = this.store.getState() + let { shapeShiftTxList } = state + + var shapeShiftTx = { + depositAddress, + depositType, + key: 'shapeshift', + time: new Date().getTime(), + response: {}, + } + + if (!shapeShiftTxList) { + shapeShiftTxList = [shapeShiftTx] + } else { + shapeShiftTxList.push(shapeShiftTx) + } + + this.store.updateState({ shapeShiftTxList }) + this.pollForUpdates() + } + +} + +module.exports = ShapeshiftController diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js index ac395440d..1afe5f651 100644 --- a/app/scripts/lib/idStore.js +++ b/app/scripts/lib/idStore.js @@ -96,7 +96,6 @@ IdentityStore.prototype.getState = function () { seedWords: seedWords, isDisclaimerConfirmed: configManager.getConfirmedDisclaimer(), selectedAddress: configManager.getSelectedAccount(), - shapeShiftTxList: configManager.getShapeShiftTxList(), gasMultiplier: configManager.getGasMultiplier(), })) } |