aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib/controllers/shapeshift.js
diff options
context:
space:
mode:
authorDan Finlay <dan@danfinlay.com>2017-02-04 12:45:20 +0800
committerDan Finlay <dan@danfinlay.com>2017-02-04 12:45:20 +0800
commit13ee92909cce93b37eec2092757e4aab174a970e (patch)
tree40802425ce3731679b0c877168ca9e9ee067dd9d /app/scripts/lib/controllers/shapeshift.js
parent24ff38e97386eafe0cd9c8e4e77dbed856b2464e (diff)
downloadtangerine-wallet-browser-13ee92909cce93b37eec2092757e4aab174a970e.tar
tangerine-wallet-browser-13ee92909cce93b37eec2092757e4aab174a970e.tar.gz
tangerine-wallet-browser-13ee92909cce93b37eec2092757e4aab174a970e.tar.bz2
tangerine-wallet-browser-13ee92909cce93b37eec2092757e4aab174a970e.tar.lz
tangerine-wallet-browser-13ee92909cce93b37eec2092757e4aab174a970e.tar.xz
tangerine-wallet-browser-13ee92909cce93b37eec2092757e4aab174a970e.tar.zst
tangerine-wallet-browser-13ee92909cce93b37eec2092757e4aab174a970e.zip
Mostly got shapeshift tx management into its own controller
Rendering the list is still having issues, so this isn't done yet.
Diffstat (limited to 'app/scripts/lib/controllers/shapeshift.js')
-rw-r--r--app/scripts/lib/controllers/shapeshift.js100
1 files changed, 100 insertions, 0 deletions
diff --git a/app/scripts/lib/controllers/shapeshift.js b/app/scripts/lib/controllers/shapeshift.js
new file mode 100644
index 000000000..6c02dc3eb
--- /dev/null
+++ b/app/scripts/lib/controllers/shapeshift.js
@@ -0,0 +1,100 @@
+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)
+ this.store = new ObservableStore(initState)
+ }
+
+ //
+ // PUBLIC METHODS
+ //
+
+ getShapeShiftTxList () {
+ const shapeShiftTxList = this.store.getState().shapeShiftTxList
+
+ shapeShiftTxList.forEach((tx) => {
+ if (tx.response.status === 'no_deposits') {
+ this.updateTx(tx)
+ }
+ })
+ console.dir({shapeShiftTxList})
+ return shapeShiftTxList
+ }
+
+ getPendingTxs () {
+ const txs = this.getShapeShiftTxList()
+ const pending = txs.filter(tx => 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))
+ setTimeout(this.pollForUpdates.bind(this), POLLING_INTERVAL)
+ })
+ }
+
+ updateTx (tx) {
+ const url = `https://shapeshift.io/txStat/${tx.depositAddress}`
+ return fetch(url)
+ .then((response) => {
+ tx.response = 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 })
+ }
+ }
+
+ 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)
+ }
+ console.dir({ shapeShiftTxList })
+
+ this.store.updateState({ shapeShiftTxList })
+ this.pollForUpdates()
+ }
+
+}
+
+module.exports = ShapeshiftController