Mostly got shapeshift tx management into its own controller

Rendering the list is still having issues, so this isn't done yet.
feature/default_network_editable
Dan Finlay 8 years ago
parent 24ff38e973
commit 13ee92909c
  1. 34
      app/scripts/lib/config-manager.js
  2. 100
      app/scripts/lib/controllers/shapeshift.js
  3. 15
      app/scripts/metamask-controller.js
  4. 3
      ui/app/actions.js

@ -250,40 +250,6 @@ ConfigManager.prototype.getTOSHash = function () {
return data.TOSHash 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 () { ConfigManager.prototype.getGasMultiplier = function () {
var data = this.getData() var data = this.getData()
return data.gasMultiplier return data.gasMultiplier

@ -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

@ -14,6 +14,7 @@ const KeyringController = require('./keyring-controller')
const PreferencesController = require('./lib/controllers/preferences') const PreferencesController = require('./lib/controllers/preferences')
const CurrencyController = require('./lib/controllers/currency') const CurrencyController = require('./lib/controllers/currency')
const NoticeController = require('./notice-controller') const NoticeController = require('./notice-controller')
const ShapeShiftController = require('./lib/controllers/shapeshift')
const MessageManager = require('./lib/message-manager') const MessageManager = require('./lib/message-manager')
const TxManager = require('./transaction-manager') const TxManager = require('./transaction-manager')
const ConfigManager = require('./lib/config-manager') const ConfigManager = require('./lib/config-manager')
@ -98,6 +99,10 @@ module.exports = class MetamaskController extends EventEmitter {
// to be uncommented when retrieving notices from a remote server. // to be uncommented when retrieving notices from a remote server.
// this.noticeController.startPolling() // this.noticeController.startPolling()
this.shapeshiftController = new ShapeShiftController({
initState: initState.ShapeShiftController,
})
this.lookupNetwork() this.lookupNetwork()
this.messageManager = new MessageManager() this.messageManager = new MessageManager()
this.publicConfigStore = this.initPublicConfigStore() this.publicConfigStore = this.initPublicConfigStore()
@ -125,6 +130,9 @@ module.exports = class MetamaskController extends EventEmitter {
this.noticeController.store.subscribe((state) => { this.noticeController.store.subscribe((state) => {
this.store.updateState({ NoticeController: state }) this.store.updateState({ NoticeController: state })
}) })
this.shapeshiftController.store.subscribe((state) => {
this.store.updateState({ ShapeShiftController: state })
})
// manual mem state subscriptions // manual mem state subscriptions
this.networkStore.subscribe(this.sendUpdate.bind(this)) this.networkStore.subscribe(this.sendUpdate.bind(this))
@ -135,6 +143,7 @@ module.exports = class MetamaskController extends EventEmitter {
this.preferencesController.store.subscribe(this.sendUpdate.bind(this)) this.preferencesController.store.subscribe(this.sendUpdate.bind(this))
this.currencyController.store.subscribe(this.sendUpdate.bind(this)) this.currencyController.store.subscribe(this.sendUpdate.bind(this))
this.noticeController.memStore.subscribe(this.sendUpdate.bind(this)) this.noticeController.memStore.subscribe(this.sendUpdate.bind(this))
this.shapeshiftController.store.subscribe(this.sendUpdate.bind(this))
} }
// //
@ -207,8 +216,8 @@ module.exports = class MetamaskController extends EventEmitter {
this.noticeController.memStore.getState(), this.noticeController.memStore.getState(),
// config manager // config manager
this.configManager.getConfig(), this.configManager.getConfig(),
this.shapeshiftController.store.getState(),
{ {
shapeShiftTxList: this.configManager.getShapeShiftTxList(),
lostAccounts: this.configManager.getLostAccounts(), lostAccounts: this.configManager.getLostAccounts(),
isDisclaimerConfirmed: this.configManager.getConfirmedDisclaimer(), isDisclaimerConfirmed: this.configManager.getConfirmedDisclaimer(),
seedWords: this.configManager.getSeedWords(), seedWords: this.configManager.getSeedWords(),
@ -327,7 +336,7 @@ module.exports = class MetamaskController extends EventEmitter {
) )
} }
sendUpdate () { sendUpdate () {
this.emit('update', this.getState()) this.emit('update', this.getState())
} }
@ -597,7 +606,7 @@ module.exports = class MetamaskController extends EventEmitter {
} }
createShapeShiftTx (depositAddress, depositType) { createShapeShiftTx (depositAddress, depositType) {
this.configManager.createShapeShiftTx(depositAddress, depositType) this.shapeshiftController.createShapeShiftTx(depositAddress, depositType)
} }
setGasMultiplier (gasMultiplier, cb) { setGasMultiplier (gasMultiplier, cb) {

@ -843,6 +843,7 @@ function coinShiftRquest (data, marketData) {
return (dispatch) => { return (dispatch) => {
dispatch(actions.showLoadingIndication()) dispatch(actions.showLoadingIndication())
shapeShiftRequest('shift', { method: 'POST', data}, (response) => { shapeShiftRequest('shift', { method: 'POST', data}, (response) => {
dispatch(actions.hideLoadingIndication())
if (response.error) return dispatch(actions.displayWarning(response.error)) if (response.error) return dispatch(actions.displayWarning(response.error))
var message = ` var message = `
Deposit your ${response.depositType} to the address bellow:` Deposit your ${response.depositType} to the address bellow:`
@ -933,4 +934,4 @@ function forceUpdateMetamaskState(dispatch){
} }
dispatch(actions.updateMetamaskState(newState)) dispatch(actions.updateMetamaskState(newState))
}) })
} }

Loading…
Cancel
Save