1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

Merge branch 'master' into tx-param-vaalidation

This commit is contained in:
Frankie 2017-12-21 11:37:37 -08:00 committed by GitHub
commit 9940ea71df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 122 additions and 18 deletions

View File

@ -3,6 +3,7 @@
## Current Master ## Current Master
- Throw an error if a application tries to submit a tx whose value is a decimal, and inform that it should be in wei. - Throw an error if a application tries to submit a tx whose value is a decimal, and inform that it should be in wei.
- Fix bug that prevented updating custom token details.
## 3.13.3 2017-12-14 ## 3.13.3 2017-12-14

View File

@ -26,23 +26,23 @@ class PreferencesController {
return this.store.getState().selectedAddress return this.store.getState().selectedAddress
} }
addToken (rawAddress, symbol, decimals) { async addToken (rawAddress, symbol, decimals) {
const address = normalizeAddress(rawAddress) const address = normalizeAddress(rawAddress)
const newEntry = { address, symbol, decimals } const newEntry = { address, symbol, decimals }
const tokens = this.store.getState().tokens const tokens = this.store.getState().tokens
const previousIndex = tokens.find((token, index) => { const previousEntry = tokens.find((token, index) => {
return token.address === address return token.address === address
}) })
const previousIndex = tokens.indexOf(previousEntry)
if (previousIndex) { if (previousEntry) {
tokens[previousIndex] = newEntry tokens[previousIndex] = newEntry
} else { } else {
tokens.push(newEntry) tokens.push(newEntry)
} }
this.store.updateState({ tokens }) this.store.updateState({ tokens })
return Promise.resolve()
} }
getTokens () { getTokens () {

View File

@ -0,0 +1,44 @@
const ObservableStore = require('obs-store')
const extend = require('xtend')
class RecentBlocksController {
constructor (opts = {}) {
const { blockTracker } = opts
this.blockTracker = blockTracker
this.historyLength = opts.historyLength || 40
const initState = extend({
recentBlocks: [],
}, opts.initState)
this.store = new ObservableStore(initState)
this.blockTracker.on('block', this.processBlock.bind(this))
}
resetState () {
this.store.updateState({
recentBlocks: [],
})
}
processBlock (newBlock) {
const block = extend(newBlock, {
gasPrices: newBlock.transactions.map((tx) => {
return tx.gasPrice
}),
})
delete block.transactions
const state = this.store.getState()
state.recentBlocks.push(block)
while (state.recentBlocks.length > this.historyLength) {
state.recentBlocks.shift()
}
this.store.updateState(state)
}
}
module.exports = RecentBlocksController

View File

@ -23,6 +23,7 @@ const ShapeShiftController = require('./controllers/shapeshift')
const AddressBookController = require('./controllers/address-book') const AddressBookController = require('./controllers/address-book')
const InfuraController = require('./controllers/infura') const InfuraController = require('./controllers/infura')
const BlacklistController = require('./controllers/blacklist') const BlacklistController = require('./controllers/blacklist')
const RecentBlocksController = require('./controllers/recent-blocks')
const MessageManager = require('./lib/message-manager') const MessageManager = require('./lib/message-manager')
const PersonalMessageManager = require('./lib/personal-message-manager') const PersonalMessageManager = require('./lib/personal-message-manager')
const TypedMessageManager = require('./lib/typed-message-manager') const TypedMessageManager = require('./lib/typed-message-manager')
@ -91,6 +92,10 @@ module.exports = class MetamaskController extends EventEmitter {
this.provider = this.initializeProvider() this.provider = this.initializeProvider()
this.blockTracker = this.provider._blockTracker this.blockTracker = this.provider._blockTracker
this.recentBlocksController = new RecentBlocksController({
blockTracker: this.blockTracker,
})
// eth data query tools // eth data query tools
this.ethQuery = new EthQuery(this.provider) this.ethQuery = new EthQuery(this.provider)
// account tracker watches balances, nonces, and any code at their address. // account tracker watches balances, nonces, and any code at their address.
@ -196,25 +201,30 @@ module.exports = class MetamaskController extends EventEmitter {
this.blacklistController.store.subscribe((state) => { this.blacklistController.store.subscribe((state) => {
this.store.updateState({ BlacklistController: state }) this.store.updateState({ BlacklistController: state })
}) })
this.recentBlocksController.store.subscribe((state) => {
this.store.updateState({ RecentBlocks: state })
})
this.infuraController.store.subscribe((state) => { this.infuraController.store.subscribe((state) => {
this.store.updateState({ InfuraController: state }) this.store.updateState({ InfuraController: state })
}) })
// manual mem state subscriptions // manual mem state subscriptions
this.networkController.store.subscribe(this.sendUpdate.bind(this)) const sendUpdate = this.sendUpdate.bind(this)
this.accountTracker.store.subscribe(this.sendUpdate.bind(this)) this.networkController.store.subscribe(sendUpdate)
this.txController.memStore.subscribe(this.sendUpdate.bind(this)) this.accountTracker.store.subscribe(sendUpdate)
this.balancesController.store.subscribe(this.sendUpdate.bind(this)) this.txController.memStore.subscribe(sendUpdate)
this.messageManager.memStore.subscribe(this.sendUpdate.bind(this)) this.balancesController.store.subscribe(sendUpdate)
this.personalMessageManager.memStore.subscribe(this.sendUpdate.bind(this)) this.messageManager.memStore.subscribe(sendUpdate)
this.typedMessageManager.memStore.subscribe(this.sendUpdate.bind(this)) this.personalMessageManager.memStore.subscribe(sendUpdate)
this.keyringController.memStore.subscribe(this.sendUpdate.bind(this)) this.typedMessageManager.memStore.subscribe(sendUpdate)
this.preferencesController.store.subscribe(this.sendUpdate.bind(this)) this.keyringController.memStore.subscribe(sendUpdate)
this.addressBookController.store.subscribe(this.sendUpdate.bind(this)) this.preferencesController.store.subscribe(sendUpdate)
this.currencyController.store.subscribe(this.sendUpdate.bind(this)) this.recentBlocksController.store.subscribe(sendUpdate)
this.noticeController.memStore.subscribe(this.sendUpdate.bind(this)) this.addressBookController.store.subscribe(sendUpdate)
this.shapeshiftController.store.subscribe(this.sendUpdate.bind(this)) this.currencyController.store.subscribe(sendUpdate)
this.infuraController.store.subscribe(this.sendUpdate.bind(this)) this.noticeController.memStore.subscribe(sendUpdate)
this.shapeshiftController.store.subscribe(sendUpdate)
this.infuraController.store.subscribe(sendUpdate)
} }
// //
@ -298,6 +308,7 @@ module.exports = class MetamaskController extends EventEmitter {
this.currencyController.store.getState(), this.currencyController.store.getState(),
this.noticeController.memStore.getState(), this.noticeController.memStore.getState(),
this.infuraController.store.getState(), this.infuraController.store.getState(),
this.recentBlocksController.store.getState(),
// config manager // config manager
this.configManager.getConfig(), this.configManager.getConfig(),
this.shapeshiftController.store.getState(), this.shapeshiftController.store.getState(),

View File

@ -0,0 +1,48 @@
const assert = require('assert')
const PreferencesController = require('../../app/scripts/controllers/preferences')
describe('preferences controller', function () {
let preferencesController
before(() => {
preferencesController = new PreferencesController()
})
describe('addToken', function () {
it('should add that token to its state', async function () {
const address = '0xabcdef1234567'
const symbol = 'ABBR'
const decimals = 5
await preferencesController.addToken(address, symbol, decimals)
const tokens = preferencesController.getTokens()
assert.equal(tokens.length, 1, 'one token added')
const added = tokens[0]
assert.equal(added.address, address, 'set address correctly')
assert.equal(added.symbol, symbol, 'set symbol correctly')
assert.equal(added.decimals, decimals, 'set decimals correctly')
})
it('should allow updating a token value', async function () {
const address = '0xabcdef1234567'
const symbol = 'ABBR'
const decimals = 5
await preferencesController.addToken(address, symbol, decimals)
const newDecimals = 6
await preferencesController.addToken(address, symbol, newDecimals)
const tokens = preferencesController.getTokens()
assert.equal(tokens.length, 1, 'one token added')
const added = tokens[0]
assert.equal(added.address, address, 'set address correctly')
assert.equal(added.symbol, symbol, 'set symbol correctly')
assert.equal(added.decimals, newDecimals, 'updated decimals correctly')
})
})
})