diff options
-rw-r--r-- | CHANGELOG.md | 2 | ||||
-rw-r--r-- | app/scripts/controllers/blacklist.js | 1 | ||||
-rw-r--r-- | app/scripts/controllers/transactions.js | 4 | ||||
-rw-r--r-- | app/scripts/lib/tx-gas-utils.js | 11 | ||||
-rw-r--r-- | app/scripts/metamask-controller.js | 25 | ||||
-rw-r--r-- | package.json | 3 | ||||
-rw-r--r-- | test/unit/metamask-controller-test.js | 27 | ||||
-rw-r--r-- | ui/app/app.js | 11 |
8 files changed, 74 insertions, 10 deletions
diff --git a/CHANGELOG.md b/CHANGELOG.md index 49d92cd1f..31f0406c7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,8 @@ ## Current Master +- Improve gas price suggestion to be closer to the lowest that will be accepted. +- 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. - No longer mark long-pending transactions as failed, since we now have button to retry with higher gas. - Fix rounding error when specifying an ether amount that has too much precision. diff --git a/app/scripts/controllers/blacklist.js b/app/scripts/controllers/blacklist.js index dd671943f..33c31dab9 100644 --- a/app/scripts/controllers/blacklist.js +++ b/app/scripts/controllers/blacklist.js @@ -57,3 +57,4 @@ class BlacklistController { } module.exports = BlacklistController + diff --git a/app/scripts/controllers/transactions.js b/app/scripts/controllers/transactions.js index 7c7efb84d..469deb670 100644 --- a/app/scripts/controllers/transactions.js +++ b/app/scripts/controllers/transactions.js @@ -32,6 +32,7 @@ module.exports = class TransactionController extends EventEmitter { this.provider = opts.provider this.blockTracker = opts.blockTracker this.signEthTx = opts.signTransaction + this.getGasPrice = opts.getGasPrice this.memStore = new ObservableStore({}) this.query = new EthQuery(this.provider) @@ -179,7 +180,8 @@ module.exports = class TransactionController extends EventEmitter { // ensure value txMeta.gasPriceSpecified = Boolean(txParams.gasPrice) txMeta.nonceSpecified = Boolean(txParams.nonce) - const gasPrice = txParams.gasPrice || await this.query.gasPrice() + const gasPrice = txParams.gasPrice || this.getGasPrice ? this.getGasPrice() + : await this.query.gasPrice() txParams.gasPrice = ethUtil.addHexPrefix(gasPrice.toString(16)) txParams.value = txParams.value || '0x0' // set gasLimit diff --git a/app/scripts/lib/tx-gas-utils.js b/app/scripts/lib/tx-gas-utils.js index 247b34e47..ccf8bb1b1 100644 --- a/app/scripts/lib/tx-gas-utils.js +++ b/app/scripts/lib/tx-gas-utils.js @@ -81,8 +81,15 @@ module.exports = class txProvideUtil { } async validateTxParams (txParams) { - if (('value' in txParams) && txParams.value.indexOf('-') === 0) { - throw new Error(`Invalid transaction value of ${txParams.value} not a positive number.`) + if ('value' in txParams) { + const value = txParams.value.toString() + if (value.includes('-')) { + throw new Error(`Invalid transaction value of ${txParams.value} not a positive number.`) + } + + if (value.includes('.')) { + throw new Error(`Invalid transaction value of ${txParams.value} number must be in wei`) + } } } } diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 8d8c1d094..66738db51 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -35,13 +35,15 @@ const accountImporter = require('./account-import-strategies') const getBuyEthUrl = require('./lib/buy-eth-url') const Mutex = require('await-semaphore').Mutex const version = require('../manifest.json').version +const BN = require('ethereumjs-util').BN +const GWEI_BN = new BN('1000000000') +const percentile = require('percentile') module.exports = class MetamaskController extends EventEmitter { constructor (opts) { super() - this.sendUpdate = debounce(this.privateSendUpdate.bind(this), 200) this.opts = opts @@ -139,6 +141,7 @@ module.exports = class MetamaskController extends EventEmitter { provider: this.provider, blockTracker: this.blockTracker, ethQuery: this.ethQuery, + getGasPrice: this.getGasPrice.bind(this), }) this.txController.on('newUnapprovedTx', opts.showUnapprovedTx.bind(opts)) @@ -484,6 +487,26 @@ module.exports = class MetamaskController extends EventEmitter { this.emit('update', this.getState()) } + getGasPrice () { + const { recentBlocksController } = this + const { recentBlocks } = recentBlocksController.store.getState() + const lowestPrices = recentBlocks.map((block) => { + if (!block.gasPrices) { + return new BN(0) + } + return block.gasPrices + .map(hexPrefix => hexPrefix.substr(2)) + .map(hex => new BN(hex, 16)) + .sort((a, b) => { + return a.gt(b) ? 1 : -1 + })[0] + }) + .map(number => number.div(GWEI_BN).toNumber()) + const percentileNum = percentile(50, lowestPrices) + const percentileNumBn = new BN(percentileNum) + return '0x' + percentileNumBn.mul(GWEI_BN).toString(16) + } + // // Vault Management // diff --git a/package.json b/package.json index 7184e3ea8..07ef76b0a 100644 --- a/package.json +++ b/package.json @@ -119,6 +119,7 @@ "obj-multiplex": "^1.0.0", "obs-store": "^3.0.0", "once": "^1.3.3", + "percentile": "^1.2.0", "ping-pong-stream": "^1.0.0", "pojo-migrator": "^2.1.0", "polyfill-crypto.getrandomvalues": "^1.0.0", @@ -217,7 +218,7 @@ "testem": "^1.10.3", "uglifyify": "^4.0.2", "vinyl-buffer": "^1.0.0", - "vinyl-source-stream": "^1.1.0", + "vinyl-source-stream": "^2.0.0", "watchify": "^3.9.0" }, "engines": { diff --git a/test/unit/metamask-controller-test.js b/test/unit/metamask-controller-test.js index 873b8d17d..9ec7cd0af 100644 --- a/test/unit/metamask-controller-test.js +++ b/test/unit/metamask-controller-test.js @@ -3,6 +3,8 @@ const sinon = require('sinon') const clone = require('clone') const MetaMaskController = require('../../app/scripts/metamask-controller') const firstTimeState = require('../../app/scripts/first-time-state') +const BN = require('ethereumjs-util').BN +const GWEI_BN = new BN('1000000000') describe('MetaMaskController', function () { const noop = () => {} @@ -47,6 +49,31 @@ describe('MetaMaskController', function () { metamaskController.keyringController.createNewVaultAndRestore.restore() }) + describe('#getGasPrice', function () { + it('gives the 50th percentile lowest accepted gas price from recentBlocksController', async function () { + const realRecentBlocksController = metamaskController.recentBlocksController + metamaskController.recentBlocksController = { + store: { + getState: () => { + return { + recentBlocks: [ + { gasPrices: [ '0x3b9aca00', '0x174876e800'] }, + { gasPrices: [ '0x3b9aca00', '0x174876e800'] }, + { gasPrices: [ '0x174876e800', '0x174876e800' ]}, + { gasPrices: [ '0x174876e800', '0x174876e800' ]}, + ] + } + } + } + } + + const gasPrice = metamaskController.getGasPrice() + assert.equal(gasPrice, '0x3b9aca00', 'accurately estimates 50th percentile accepted gas price') + + metamaskController.recentBlocksController = realRecentBlocksController + }) + }) + describe('#createNewVaultAndKeychain', function () { it('can only create new vault on keyringController once', async function () { const selectStub = sinon.stub(metamaskController, 'selectFirstIdentity') diff --git a/ui/app/app.js b/ui/app/app.js index bd0ccb0a2..bc198b482 100644 --- a/ui/app/app.js +++ b/ui/app/app.js @@ -460,11 +460,6 @@ App.prototype.renderPrimary = function () { }) } - if (props.seedWords) { - log.debug('rendering seed words') - return h(HDCreateVaultComplete, {key: 'HDCreateVaultComplete'}) - } - // show initialize screen if (!props.isInitialized || props.forgottenPassword) { // show current view @@ -499,6 +494,12 @@ App.prototype.renderPrimary = function () { } } + // show seed words screen + if (props.seedWords) { + log.debug('rendering seed words') + return h(HDCreateVaultComplete, {key: 'HDCreateVaultComplete'}) + } + // show current view switch (props.currentView.name) { |