forked from MetaMask/web3-provider-engine
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgasprice.js
73 lines (58 loc) · 2.21 KB
/
gasprice.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*
* Calculate gasPrice based on last blocks.
* @author github.com/axic
*
* FIXME: support minimum suggested gas and perhaps other options from geth:
* https://github.com/ethereum/go-ethereum/blob/master/eth/gasprice.go
* https://github.com/ethereum/go-ethereum/wiki/Gas-Price-Oracle
*/
const map = require('async/map')
const inherits = require('util').inherits
const Subprovider = require('./subprovider.js')
module.exports = GaspriceProvider
inherits(GaspriceProvider, Subprovider)
function GaspriceProvider(opts) {
opts = opts || {}
this.numberOfBlocks = opts.numberOfBlocks || 10
this.delayInBlocks = opts.delayInBlocks || 5
}
GaspriceProvider.prototype.handleRequest = function(payload, next, end){
if (payload.method !== 'eth_gasPrice')
return next()
const self = this
self.emitPayload({ method: 'eth_blockNumber' }, function(err, res) {
// FIXME: convert number using a bignum library
var lastBlock = parseInt(res.result, 16) - self.delayInBlocks
var blockNumbers = [ ]
for (var i = 0; i < self.numberOfBlocks; i++) {
blockNumbers.push('0x' + lastBlock.toString(16))
lastBlock--
}
function getBlock(item, end) {
self.emitPayload({ method: 'eth_getBlockByNumber', params: [ item, true ] }, function(err, res) {
if (err) return end(err)
if (!res.result) return end(new Error(`GaspriceProvider - No block for "${item}"`))
end(null, res.result.transactions)
})
}
// FIXME: this could be made much faster
function calcPrice(err, transactions) {
// flatten array
transactions = transactions.reduce(function(a, b) { return a.concat(b) }, [])
// leave only the gasprice
// FIXME: convert number using a bignum library
transactions = transactions.map(function(a) { return parseInt(a.gasPrice, 16) }, [])
// order ascending
transactions.sort(function(a, b) { return a - b })
// ze median
var half = Math.floor(transactions.length / 2)
var median
if (transactions.length % 2)
median = transactions[half]
else
median = Math.floor((transactions[half - 1] + transactions[half]) / 2.0)
end(null, median)
}
map(blockNumbers, getBlock, calcPrice)
})
}