mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Derive gas price estimate from previous transactions
Return the 50th percentile lowest gas price of the previous 20 blocks.
This commit is contained in:
parent
571f6723a6
commit
4bca98d588
@ -57,3 +57,4 @@ class BlacklistController {
|
|||||||
}
|
}
|
||||||
|
|
||||||
module.exports = BlacklistController
|
module.exports = BlacklistController
|
||||||
|
|
||||||
|
@ -32,6 +32,7 @@ module.exports = class TransactionController extends EventEmitter {
|
|||||||
this.provider = opts.provider
|
this.provider = opts.provider
|
||||||
this.blockTracker = opts.blockTracker
|
this.blockTracker = opts.blockTracker
|
||||||
this.signEthTx = opts.signTransaction
|
this.signEthTx = opts.signTransaction
|
||||||
|
this.getGasPrice = opts.getGasPrice
|
||||||
|
|
||||||
this.memStore = new ObservableStore({})
|
this.memStore = new ObservableStore({})
|
||||||
this.query = new EthQuery(this.provider)
|
this.query = new EthQuery(this.provider)
|
||||||
@ -179,7 +180,7 @@ module.exports = class TransactionController extends EventEmitter {
|
|||||||
// ensure value
|
// ensure value
|
||||||
txMeta.gasPriceSpecified = Boolean(txParams.gasPrice)
|
txMeta.gasPriceSpecified = Boolean(txParams.gasPrice)
|
||||||
txMeta.nonceSpecified = Boolean(txParams.nonce)
|
txMeta.nonceSpecified = Boolean(txParams.nonce)
|
||||||
const gasPrice = txParams.gasPrice || await this.query.gasPrice()
|
const gasPrice = txParams.gasPrice || this.getGasPrice()
|
||||||
txParams.gasPrice = ethUtil.addHexPrefix(gasPrice.toString(16))
|
txParams.gasPrice = ethUtil.addHexPrefix(gasPrice.toString(16))
|
||||||
txParams.value = txParams.value || '0x0'
|
txParams.value = txParams.value || '0x0'
|
||||||
// set gasLimit
|
// set gasLimit
|
||||||
|
@ -35,13 +35,15 @@ const accountImporter = require('./account-import-strategies')
|
|||||||
const getBuyEthUrl = require('./lib/buy-eth-url')
|
const getBuyEthUrl = require('./lib/buy-eth-url')
|
||||||
const Mutex = require('await-semaphore').Mutex
|
const Mutex = require('await-semaphore').Mutex
|
||||||
const version = require('../manifest.json').version
|
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 {
|
module.exports = class MetamaskController extends EventEmitter {
|
||||||
|
|
||||||
constructor (opts) {
|
constructor (opts) {
|
||||||
super()
|
super()
|
||||||
|
|
||||||
|
|
||||||
this.sendUpdate = debounce(this.privateSendUpdate.bind(this), 200)
|
this.sendUpdate = debounce(this.privateSendUpdate.bind(this), 200)
|
||||||
|
|
||||||
this.opts = opts
|
this.opts = opts
|
||||||
@ -139,6 +141,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
provider: this.provider,
|
provider: this.provider,
|
||||||
blockTracker: this.blockTracker,
|
blockTracker: this.blockTracker,
|
||||||
ethQuery: this.ethQuery,
|
ethQuery: this.ethQuery,
|
||||||
|
getGasPrice: this.getGasPrice.bind(this),
|
||||||
})
|
})
|
||||||
this.txController.on('newUnapprovedTx', opts.showUnapprovedTx.bind(opts))
|
this.txController.on('newUnapprovedTx', opts.showUnapprovedTx.bind(opts))
|
||||||
|
|
||||||
@ -484,6 +487,22 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
this.emit('update', this.getState())
|
this.emit('update', this.getState())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getGasPrice () {
|
||||||
|
const { recentBlocksController } = this
|
||||||
|
console.dir(recentBlocksController)
|
||||||
|
const { recentBlocks } = recentBlocksController.store.getState()
|
||||||
|
console.dir(recentBlocks)
|
||||||
|
const lowestPrices = recentBlocks.map((block) => {
|
||||||
|
return block.transactions
|
||||||
|
.sort((a, b) => {
|
||||||
|
return a.gt(b) ? 1 : -1
|
||||||
|
})[0]
|
||||||
|
})
|
||||||
|
.map(number => number.div(GWEI_BN).toNumber())
|
||||||
|
console.dir({ lowestPrices })
|
||||||
|
return percentile(50, lowestPrices)
|
||||||
|
}
|
||||||
|
|
||||||
//
|
//
|
||||||
// Vault Management
|
// Vault Management
|
||||||
//
|
//
|
||||||
|
@ -119,6 +119,7 @@
|
|||||||
"obj-multiplex": "^1.0.0",
|
"obj-multiplex": "^1.0.0",
|
||||||
"obs-store": "^3.0.0",
|
"obs-store": "^3.0.0",
|
||||||
"once": "^1.3.3",
|
"once": "^1.3.3",
|
||||||
|
"percentile": "^1.2.0",
|
||||||
"ping-pong-stream": "^1.0.0",
|
"ping-pong-stream": "^1.0.0",
|
||||||
"pojo-migrator": "^2.1.0",
|
"pojo-migrator": "^2.1.0",
|
||||||
"polyfill-crypto.getrandomvalues": "^1.0.0",
|
"polyfill-crypto.getrandomvalues": "^1.0.0",
|
||||||
|
Loading…
Reference in New Issue
Block a user