You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
105 lines
2.4 KiB
105 lines
2.4 KiB
8 years ago
|
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: [],
|
||
8 years ago
|
}, opts.initState)
|
||
8 years ago
|
this.store = new ObservableStore(initState)
|
||
8 years ago
|
this.pollForUpdates()
|
||
8 years ago
|
}
|
||
|
|
||
|
//
|
||
|
// PUBLIC METHODS
|
||
|
//
|
||
|
|
||
|
getShapeShiftTxList () {
|
||
|
const shapeShiftTxList = this.store.getState().shapeShiftTxList
|
||
|
return shapeShiftTxList
|
||
|
}
|
||
|
|
||
|
getPendingTxs () {
|
||
|
const txs = this.getShapeShiftTxList()
|
||
8 years ago
|
const pending = txs.filter(tx => tx.response && tx.response.status !== 'complete')
|
||
8 years ago
|
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))
|
||
8 years ago
|
this.timeout = setTimeout(this.pollForUpdates.bind(this), POLLING_INTERVAL)
|
||
8 years ago
|
})
|
||
|
}
|
||
|
|
||
|
updateTx (tx) {
|
||
|
const url = `https://shapeshift.io/txStat/${tx.depositAddress}`
|
||
|
return fetch(url)
|
||
|
.then((response) => {
|
||
8 years ago
|
return response.json()
|
||
|
}).then((json) => {
|
||
|
tx.response = json
|
||
8 years ago
|
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 })
|
||
|
}
|
||
|
}
|
||
|
|
||
8 years ago
|
removeShapeShiftTx (tx) {
|
||
|
const { shapeShiftTxList } = this.store.getState()
|
||
|
const index = shapeShiftTxList.indexOf(index)
|
||
|
if (index !== -1) {
|
||
|
shapeShiftTxList.splice(index, 1)
|
||
|
}
|
||
|
this.updateState({ shapeShiftTxList })
|
||
|
}
|
||
|
|
||
8 years ago
|
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
|