From 13ee92909cce93b37eec2092757e4aab174a970e Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Fri, 3 Feb 2017 20:45:20 -0800 Subject: Mostly got shapeshift tx management into its own controller Rendering the list is still having issues, so this isn't done yet. --- app/scripts/lib/controllers/shapeshift.js | 100 ++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 app/scripts/lib/controllers/shapeshift.js (limited to 'app/scripts/lib/controllers/shapeshift.js') 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 -- cgit v1.2.3 From 4dc71ed57bab3e8310d37e2fb2a4c495ed3ca5d0 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Fri, 3 Feb 2017 21:12:18 -0800 Subject: Got ShapeShiftController back to working --- app/scripts/lib/controllers/shapeshift.js | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) (limited to 'app/scripts/lib/controllers/shapeshift.js') diff --git a/app/scripts/lib/controllers/shapeshift.js b/app/scripts/lib/controllers/shapeshift.js index 6c02dc3eb..bcfe3e14c 100644 --- a/app/scripts/lib/controllers/shapeshift.js +++ b/app/scripts/lib/controllers/shapeshift.js @@ -9,8 +9,9 @@ class ShapeshiftController { constructor (opts = {}) { const initState = extend({ shapeShiftTxList: [], - }, opts) + }, opts.initState) this.store = new ObservableStore(initState) + this.pollForUpdates() } // @@ -19,19 +20,12 @@ class ShapeshiftController { 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') + const pending = txs.filter(tx => tx.response && tx.response.status !== 'complete') return pending } @@ -47,7 +41,7 @@ class ShapeshiftController { })) .then((results) => { results.forEach(tx => this.saveTx(tx)) - setTimeout(this.pollForUpdates.bind(this), POLLING_INTERVAL) + this.timeout = setTimeout(this.pollForUpdates.bind(this), POLLING_INTERVAL) }) } @@ -55,7 +49,9 @@ class ShapeshiftController { const url = `https://shapeshift.io/txStat/${tx.depositAddress}` return fetch(url) .then((response) => { - tx.response = response.json() + return response.json() + }).then((json) => { + tx.response = json if (tx.response.status === 'complete') { tx.time = new Date().getTime() } @@ -89,7 +85,6 @@ class ShapeshiftController { } else { shapeShiftTxList.push(shapeShiftTx) } - console.dir({ shapeShiftTxList }) this.store.updateState({ shapeShiftTxList }) this.pollForUpdates() -- cgit v1.2.3 From 5d37f90787cdeec130537e61626f92d6f8a7b5e3 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Fri, 3 Feb 2017 21:36:04 -0800 Subject: Automatically remove shapeshift txs over 11 minutes old with no payment --- app/scripts/lib/controllers/shapeshift.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) (limited to 'app/scripts/lib/controllers/shapeshift.js') diff --git a/app/scripts/lib/controllers/shapeshift.js b/app/scripts/lib/controllers/shapeshift.js index bcfe3e14c..6d1c95323 100644 --- a/app/scripts/lib/controllers/shapeshift.js +++ b/app/scripts/lib/controllers/shapeshift.js @@ -4,6 +4,9 @@ const extend = require('xtend') // every three seconds when an incomplete tx is waiting const POLLING_INTERVAL = 3000 +// drop txs that haven't been paid to in 11 mins +const TIMEOUT_LIMIT = 660000 + class ShapeshiftController { constructor (opts = {}) { @@ -24,11 +27,21 @@ class ShapeshiftController { } getPendingTxs () { + this.removeOldTxs() const txs = this.getShapeShiftTxList() const pending = txs.filter(tx => tx.response && tx.response.status !== 'complete') return pending } + removeOldTxs() { + const { shapeShiftTxList } = this.store.getState() + const now = new Date().getTime() + const old = shapeShiftTxList.find((tx) => { + return tx.time + TIMEOUT_LIMIT < now + }) + old.forEach(tx => this.removeShapeShiftTx(tx)) + } + pollForUpdates () { const pendingTxs = this.getPendingTxs() @@ -68,6 +81,15 @@ class ShapeshiftController { } } + 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 -- cgit v1.2.3 From 901eeb5c102dbd8e42d5835e4d35c10fe0301086 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Fri, 3 Feb 2017 21:39:22 -0800 Subject: Fix bug when clearing old shapeshift txs --- app/scripts/lib/controllers/shapeshift.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) (limited to 'app/scripts/lib/controllers/shapeshift.js') diff --git a/app/scripts/lib/controllers/shapeshift.js b/app/scripts/lib/controllers/shapeshift.js index 6d1c95323..ec27b37a0 100644 --- a/app/scripts/lib/controllers/shapeshift.js +++ b/app/scripts/lib/controllers/shapeshift.js @@ -39,7 +39,9 @@ class ShapeshiftController { const old = shapeShiftTxList.find((tx) => { return tx.time + TIMEOUT_LIMIT < now }) - old.forEach(tx => this.removeShapeShiftTx(tx)) + if (old) { + old.forEach(tx => this.removeShapeShiftTx(tx)) + } } pollForUpdates () { -- cgit v1.2.3 From af439cc6cf10fa0387f7e2196d5fefaf84f2f0a2 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Fri, 3 Feb 2017 21:40:27 -0800 Subject: Do not remove completed shapeshift deposits --- app/scripts/lib/controllers/shapeshift.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'app/scripts/lib/controllers/shapeshift.js') diff --git a/app/scripts/lib/controllers/shapeshift.js b/app/scripts/lib/controllers/shapeshift.js index ec27b37a0..fbdc2c180 100644 --- a/app/scripts/lib/controllers/shapeshift.js +++ b/app/scripts/lib/controllers/shapeshift.js @@ -37,7 +37,8 @@ class ShapeshiftController { const { shapeShiftTxList } = this.store.getState() const now = new Date().getTime() const old = shapeShiftTxList.find((tx) => { - return tx.time + TIMEOUT_LIMIT < now + return tx.time + TIMEOUT_LIMIT < now && + tx.response && tx.response.status === 'no_deposits' }) if (old) { old.forEach(tx => this.removeShapeShiftTx(tx)) -- cgit v1.2.3 From 89bbccb09cbb29dd4b1f6a8c7ef3be137da4b243 Mon Sep 17 00:00:00 2001 From: Dan Finlay Date: Sat, 4 Feb 2017 15:15:50 -0800 Subject: Stop removing old shapeshift txs --- app/scripts/lib/controllers/shapeshift.js | 16 ---------------- 1 file changed, 16 deletions(-) (limited to 'app/scripts/lib/controllers/shapeshift.js') diff --git a/app/scripts/lib/controllers/shapeshift.js b/app/scripts/lib/controllers/shapeshift.js index fbdc2c180..3d955c01f 100644 --- a/app/scripts/lib/controllers/shapeshift.js +++ b/app/scripts/lib/controllers/shapeshift.js @@ -4,9 +4,6 @@ const extend = require('xtend') // every three seconds when an incomplete tx is waiting const POLLING_INTERVAL = 3000 -// drop txs that haven't been paid to in 11 mins -const TIMEOUT_LIMIT = 660000 - class ShapeshiftController { constructor (opts = {}) { @@ -27,24 +24,11 @@ class ShapeshiftController { } getPendingTxs () { - this.removeOldTxs() const txs = this.getShapeShiftTxList() const pending = txs.filter(tx => tx.response && tx.response.status !== 'complete') return pending } - removeOldTxs() { - const { shapeShiftTxList } = this.store.getState() - const now = new Date().getTime() - const old = shapeShiftTxList.find((tx) => { - return tx.time + TIMEOUT_LIMIT < now && - tx.response && tx.response.status === 'no_deposits' - }) - if (old) { - old.forEach(tx => this.removeShapeShiftTx(tx)) - } - } - pollForUpdates () { const pendingTxs = this.getPendingTxs() -- cgit v1.2.3