aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/lib
diff options
context:
space:
mode:
Diffstat (limited to 'app/scripts/lib')
-rw-r--r--app/scripts/lib/config-manager.js47
-rw-r--r--app/scripts/lib/controllers/currency.js70
-rw-r--r--app/scripts/lib/eth-store.js214
-rw-r--r--app/scripts/lib/idStore.js3
-rw-r--r--app/scripts/lib/message-manager.js52
5 files changed, 188 insertions, 198 deletions
diff --git a/app/scripts/lib/config-manager.js b/app/scripts/lib/config-manager.js
index 357e081b1..a9b86ca8c 100644
--- a/app/scripts/lib/config-manager.js
+++ b/app/scripts/lib/config-manager.js
@@ -250,53 +250,6 @@ ConfigManager.prototype.getTOSHash = function () {
return data.TOSHash
}
-ConfigManager.prototype.setCurrentFiat = function (currency) {
- var data = this.getData()
- data.fiatCurrency = currency
- this.setData(data)
-}
-
-ConfigManager.prototype.getCurrentFiat = function () {
- var data = this.getData()
- return data.fiatCurrency || 'USD'
-}
-
-ConfigManager.prototype.updateConversionRate = function () {
- var data = this.getData()
- return fetch(`https://www.cryptonator.com/api/ticker/eth-${data.fiatCurrency}`)
- .then(response => response.json())
- .then((parsedResponse) => {
- this.setConversionPrice(parsedResponse.ticker.price)
- this.setConversionDate(parsedResponse.timestamp)
- }).catch((err) => {
- console.warn('MetaMask - Failed to query currency conversion.')
- this.setConversionPrice(0)
- this.setConversionDate('N/A')
- })
-}
-
-ConfigManager.prototype.setConversionPrice = function (price) {
- var data = this.getData()
- data.conversionRate = Number(price)
- this.setData(data)
-}
-
-ConfigManager.prototype.setConversionDate = function (datestring) {
- var data = this.getData()
- data.conversionDate = datestring
- this.setData(data)
-}
-
-ConfigManager.prototype.getConversionRate = function () {
- var data = this.getData()
- return (data.conversionRate) || 0
-}
-
-ConfigManager.prototype.getConversionDate = function () {
- var data = this.getData()
- return (data.conversionDate) || 'N/A'
-}
-
ConfigManager.prototype.getShapeShiftTxList = function () {
var data = this.getData()
var shapeShiftTxList = data.shapeShiftTxList ? data.shapeShiftTxList : []
diff --git a/app/scripts/lib/controllers/currency.js b/app/scripts/lib/controllers/currency.js
new file mode 100644
index 000000000..c4904f8ac
--- /dev/null
+++ b/app/scripts/lib/controllers/currency.js
@@ -0,0 +1,70 @@
+const ObservableStore = require('obs-store')
+const extend = require('xtend')
+
+// every ten minutes
+const POLLING_INTERVAL = 600000
+
+class CurrencyController {
+
+ constructor (opts = {}) {
+ const initState = extend({
+ currentCurrency: 'USD',
+ conversionRate: 0,
+ conversionDate: 'N/A',
+ }, opts.initState)
+ this.store = new ObservableStore(initState)
+ }
+
+ //
+ // PUBLIC METHODS
+ //
+
+ getCurrentCurrency () {
+ return this.store.getState().currentCurrency
+ }
+
+ setCurrentCurrency (currentCurrency) {
+ this.store.updateState({ currentCurrency })
+ }
+
+ getConversionRate () {
+ return this.store.getState().conversionRate
+ }
+
+ setConversionRate (conversionRate) {
+ this.store.updateState({ conversionRate })
+ }
+
+ getConversionDate () {
+ return this.store.getState().conversionDate
+ }
+
+ setConversionDate (conversionDate) {
+ this.store.updateState({ conversionDate })
+ }
+
+ updateConversionRate () {
+ const currentCurrency = this.getCurrentCurrency()
+ return fetch(`https://www.cryptonator.com/api/ticker/eth-${currentCurrency}`)
+ .then(response => response.json())
+ .then((parsedResponse) => {
+ this.setConversionRate(Number(parsedResponse.ticker.price))
+ this.setConversionDate(Number(parsedResponse.timestamp))
+ }).catch((err) => {
+ console.warn('MetaMask - Failed to query currency conversion.')
+ this.setConversionRate(0)
+ this.setConversionDate('N/A')
+ })
+ }
+
+ scheduleConversionInterval () {
+ if (this.conversionInterval) {
+ clearInterval(this.conversionInterval)
+ }
+ this.conversionInterval = setInterval(() => {
+ this.updateConversionRate()
+ }, POLLING_INTERVAL)
+ }
+}
+
+module.exports = CurrencyController
diff --git a/app/scripts/lib/eth-store.js b/app/scripts/lib/eth-store.js
index 7e2caf884..96b4a60f2 100644
--- a/app/scripts/lib/eth-store.js
+++ b/app/scripts/lib/eth-store.js
@@ -7,140 +7,122 @@
* on each new block.
*/
-const EventEmitter = require('events').EventEmitter
-const inherits = require('util').inherits
const async = require('async')
-const clone = require('clone')
const EthQuery = require('eth-query')
+const ObservableStore = require('obs-store')
+function noop() {}
-module.exports = EthereumStore
+class EthereumStore extends ObservableStore {
-inherits(EthereumStore, EventEmitter)
-function EthereumStore(engine) {
- const self = this
- EventEmitter.call(self)
- self._currentState = {
- accounts: {},
- transactions: {},
+ constructor (opts = {}) {
+ super({
+ accounts: {},
+ transactions: {},
+ })
+ this._provider = opts.provider
+ this._query = new EthQuery(this._provider)
+ this._blockTracker = opts.blockTracker
+ // subscribe to latest block
+ this._blockTracker.on('block', this._updateForBlock.bind(this))
}
- self._query = new EthQuery(engine)
-
- engine.on('block', self._updateForBlock.bind(self))
-}
-
-//
-// public
-//
-EthereumStore.prototype.getState = function () {
- const self = this
- return clone(self._currentState)
-}
-
-EthereumStore.prototype.addAccount = function (address) {
- const self = this
- self._currentState.accounts[address] = {}
- self._didUpdate()
- if (!self.currentBlockNumber) return
- self._updateAccount(address, () => {
- self._didUpdate()
- })
-}
+ //
+ // public
+ //
-EthereumStore.prototype.removeAccount = function (address) {
- const self = this
- delete self._currentState.accounts[address]
- self._didUpdate()
-}
+ addAccount (address) {
+ const accounts = this.getState().accounts
+ accounts[address] = {}
+ this.updateState({ accounts })
+ if (!this._currentBlockNumber) return
+ this._updateAccount(address)
+ }
-EthereumStore.prototype.addTransaction = function (txHash) {
- const self = this
- self._currentState.transactions[txHash] = {}
- self._didUpdate()
- if (!self.currentBlockNumber) return
- self._updateTransaction(self.currentBlockNumber, txHash, noop)
-}
+ removeAccount (address) {
+ const accounts = this.getState().accounts
+ delete accounts[address]
+ this.updateState({ accounts })
+ }
-EthereumStore.prototype.removeTransaction = function (address) {
- const self = this
- delete self._currentState.transactions[address]
- self._didUpdate()
-}
+ addTransaction (txHash) {
+ const transactions = this.getState().transactions
+ transactions[txHash] = {}
+ this.updateState({ transactions })
+ if (!this._currentBlockNumber) return
+ this._updateTransaction(this._currentBlockNumber, txHash, noop)
+ }
+ removeTransaction (txHash) {
+ const transactions = this.getState().transactions
+ delete transactions[txHash]
+ this.updateState({ transactions })
+ }
-//
-// private
-//
-EthereumStore.prototype._didUpdate = function () {
- const self = this
- var state = self.getState()
- self.emit('update', state)
-}
+ //
+ // private
+ //
+
+ _updateForBlock (block) {
+ const blockNumber = '0x' + block.number.toString('hex')
+ this._currentBlockNumber = blockNumber
+ async.parallel([
+ this._updateAccounts.bind(this),
+ this._updateTransactions.bind(this, blockNumber),
+ ], (err) => {
+ if (err) return console.error(err)
+ this.emit('block', this.getState())
+ })
+ }
-EthereumStore.prototype._updateForBlock = function (block) {
- const self = this
- var blockNumber = '0x' + block.number.toString('hex')
- self.currentBlockNumber = blockNumber
- async.parallel([
- self._updateAccounts.bind(self),
- self._updateTransactions.bind(self, blockNumber),
- ], function (err) {
- if (err) return console.error(err)
- self.emit('block', self.getState())
- self._didUpdate()
- })
-}
+ _updateAccounts (cb = noop) {
+ const accounts = this.getState().accounts
+ const addresses = Object.keys(accounts)
+ async.each(addresses, this._updateAccount.bind(this), cb)
+ }
-EthereumStore.prototype._updateAccounts = function (cb) {
- var accountsState = this._currentState.accounts
- var addresses = Object.keys(accountsState)
- async.each(addresses, this._updateAccount.bind(this), cb)
-}
+ _updateAccount (address, cb = noop) {
+ const accounts = this.getState().accounts
+ this._getAccount(address, (err, result) => {
+ if (err) return cb(err)
+ result.address = address
+ // only populate if the entry is still present
+ if (accounts[address]) {
+ accounts[address] = result
+ }
+ cb(null, result)
+ })
+ }
-EthereumStore.prototype._updateAccount = function (address, cb) {
- var accountsState = this._currentState.accounts
- this.getAccount(address, function (err, result) {
- if (err) return cb(err)
- result.address = address
- // only populate if the entry is still present
- if (accountsState[address]) {
- accountsState[address] = result
- }
- cb(null, result)
- })
-}
+ _updateTransactions (block, cb = noop) {
+ const transactions = this.getState().transactions
+ const txHashes = Object.keys(transactions)
+ async.each(txHashes, this._updateTransaction.bind(this, block), cb)
+ }
-EthereumStore.prototype.getAccount = function (address, cb) {
- const query = this._query
- async.parallel({
- balance: query.getBalance.bind(query, address),
- nonce: query.getTransactionCount.bind(query, address),
- code: query.getCode.bind(query, address),
- }, cb)
-}
+ _updateTransaction (block, txHash, cb = noop) {
+ // would use the block here to determine how many confirmations the tx has
+ const transactions = this.getState().transactions
+ this._query.getTransaction(txHash, (err, result) => {
+ if (err) return cb(err)
+ // only populate if the entry is still present
+ if (transactions[txHash]) {
+ transactions[txHash] = result
+ }
+ cb(null, result)
+ })
+ }
-EthereumStore.prototype._updateTransactions = function (block, cb) {
- const self = this
- var transactionsState = self._currentState.transactions
- var txHashes = Object.keys(transactionsState)
- async.each(txHashes, self._updateTransaction.bind(self, block), cb)
-}
+ _getAccount (address, cb = noop) {
+ const query = this._query
+ async.parallel({
+ balance: query.getBalance.bind(query, address),
+ nonce: query.getTransactionCount.bind(query, address),
+ code: query.getCode.bind(query, address),
+ }, cb)
+ }
-EthereumStore.prototype._updateTransaction = function (block, txHash, cb) {
- const self = this
- // would use the block here to determine how many confirmations the tx has
- var transactionsState = self._currentState.transactions
- self._query.getTransaction(txHash, function (err, result) {
- if (err) return cb(err)
- // only populate if the entry is still present
- if (transactionsState[txHash]) {
- transactionsState[txHash] = result
- self._didUpdate()
- }
- cb(null, result)
- })
}
-function noop() {}
+module.exports = EthereumStore \ No newline at end of file
diff --git a/app/scripts/lib/idStore.js b/app/scripts/lib/idStore.js
index e4cbca456..ac395440d 100644
--- a/app/scripts/lib/idStore.js
+++ b/app/scripts/lib/idStore.js
@@ -97,9 +97,6 @@ IdentityStore.prototype.getState = function () {
isDisclaimerConfirmed: configManager.getConfirmedDisclaimer(),
selectedAddress: configManager.getSelectedAccount(),
shapeShiftTxList: configManager.getShapeShiftTxList(),
- currentFiat: configManager.getCurrentFiat(),
- conversionRate: configManager.getConversionRate(),
- conversionDate: configManager.getConversionDate(),
gasMultiplier: configManager.getGasMultiplier(),
}))
}
diff --git a/app/scripts/lib/message-manager.js b/app/scripts/lib/message-manager.js
index 18bf54ae1..38fa42017 100644
--- a/app/scripts/lib/message-manager.js
+++ b/app/scripts/lib/message-manager.js
@@ -6,18 +6,11 @@ const createId = require('./random-id')
module.exports = class MessageManager extends EventEmitter{
constructor (opts) {
super()
- this.memStore = new ObservableStore({ messages: [] })
- }
-
- getState() {
- return {
- unapprovedMsgs: this.getUnapprovedMsgs(),
- messages: this.getMsgList(),
- }
- }
-
- getMsgList () {
- return this.memStore.getState().messages
+ this.memStore = new ObservableStore({
+ unapprovedMsgs: {},
+ unapprovedMsgCount: 0,
+ })
+ this.messages = []
}
get unapprovedMsgCount () {
@@ -25,8 +18,7 @@ module.exports = class MessageManager extends EventEmitter{
}
getUnapprovedMsgs () {
- let messages = this.getMsgList()
- return messages.filter(msg => msg.status === 'unapproved')
+ return this.messages.filter(msg => msg.status === 'unapproved')
.reduce((result, msg) => { result[msg.id] = msg; return result }, {})
}
@@ -41,10 +33,6 @@ module.exports = class MessageManager extends EventEmitter{
status: 'unapproved',
}
this.addMsg(msgData)
- console.log('addUnapprovedMessage:', msgData)
-
- // keep the cb around for after approval (requires user interaction)
- // This cb fires completion to the Dapp's write operation.
// signal update
this.emit('update')
@@ -52,15 +40,12 @@ module.exports = class MessageManager extends EventEmitter{
}
addMsg (msg) {
- let messages = this.getMsgList()
- messages.push(msg)
- this._saveMsgList(messages)
+ this.messages.push(msg)
+ this._saveMsgList()
}
getMsg (msgId) {
- let messages = this.getMsgList()
- let matching = messages.filter(msg => msg.id === msgId)
- return matching.length > 0 ? matching[0] : null
+ return this.messages.find(msg => msg.id === msgId)
}
approveMessage (msgParams) {
@@ -85,7 +70,10 @@ module.exports = class MessageManager extends EventEmitter{
brodcastMessage (rawSig, msgId, status) {
this.emit(`${msgId}:finished`, {status, rawSig})
}
-// PRIVATE METHODS
+
+ //
+ // PRIVATE METHODS
+ //
_setMsgStatus (msgId, status) {
let msg = this.getMsg(msgId)
@@ -94,18 +82,18 @@ module.exports = class MessageManager extends EventEmitter{
}
_updateMsg (msg) {
- let messages = this.getMsgList()
- let index = messages.findIndex((message) => message.id === msg.id)
+ let index = this.messages.findIndex((message) => message.id === msg.id)
if (index !== -1) {
- messages[index] = msg
+ this.messages[index] = msg
}
- this._saveMsgList(messages)
+ this._saveMsgList()
}
- _saveMsgList (msgList) {
+ _saveMsgList () {
+ const unapprovedMsgs = this.getUnapprovedMsgs()
+ const unapprovedMsgCount = Object.keys(unapprovedMsgs).length
+ this.memStore.updateState({ unapprovedMsgs, unapprovedMsgCount })
this.emit('updateBadge')
- this.memStore.updateState({ messages: msgList })
}
-
}