|
|
@ -12,48 +12,49 @@ and used to do things like calculate gas of a tx. |
|
|
|
*/ |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
module.exports = class txProviderUtils { |
|
|
|
module.exports = class txProviderUtils { |
|
|
|
|
|
|
|
|
|
|
|
constructor (provider) { |
|
|
|
constructor (provider) { |
|
|
|
this.provider = provider |
|
|
|
this.provider = provider |
|
|
|
this.query = new EthQuery(provider) |
|
|
|
this.query = new EthQuery(provider) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
analyzeGasUsage (txData, cb) { |
|
|
|
analyzeGasUsage (txMeta, cb) { |
|
|
|
var self = this |
|
|
|
var self = this |
|
|
|
this.query.getBlockByNumber('latest', true, (err, block) => { |
|
|
|
this.query.getBlockByNumber('latest', true, (err, block) => { |
|
|
|
if (err) return cb(err) |
|
|
|
if (err) return cb(err) |
|
|
|
async.waterfall([ |
|
|
|
async.waterfall([ |
|
|
|
self.estimateTxGas.bind(self, txData, block.gasLimit), |
|
|
|
self.estimateTxGas.bind(self, txMeta, block.gasLimit), |
|
|
|
self.setTxGas.bind(self, txData, block.gasLimit), |
|
|
|
self.setTxGas.bind(self, txMeta, block.gasLimit), |
|
|
|
], cb) |
|
|
|
], cb) |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
estimateTxGas (txData, blockGasLimitHex, cb) { |
|
|
|
estimateTxGas (txMeta, blockGasLimitHex, cb) { |
|
|
|
const txParams = txData.txParams |
|
|
|
const txParams = txMeta.txParams |
|
|
|
// check if gasLimit is already specified
|
|
|
|
// check if gasLimit is already specified
|
|
|
|
txData.gasLimitSpecified = Boolean(txParams.gas) |
|
|
|
txMeta.gasLimitSpecified = Boolean(txParams.gas) |
|
|
|
// if not, fallback to block gasLimit
|
|
|
|
// if not, fallback to block gasLimit
|
|
|
|
if (!txData.gasLimitSpecified) { |
|
|
|
if (!txMeta.gasLimitSpecified) { |
|
|
|
txParams.gas = blockGasLimitHex |
|
|
|
txParams.gas = blockGasLimitHex |
|
|
|
} |
|
|
|
} |
|
|
|
// run tx, see if it will OOG
|
|
|
|
// run tx, see if it will OOG
|
|
|
|
this.query.estimateGas(txParams, cb) |
|
|
|
this.query.estimateGas(txParams, cb) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
setTxGas (txData, blockGasLimitHex, estimatedGasHex, cb) { |
|
|
|
setTxGas (txMeta, blockGasLimitHex, estimatedGasHex, cb) { |
|
|
|
txData.estimatedGas = estimatedGasHex |
|
|
|
txMeta.estimatedGas = estimatedGasHex |
|
|
|
const txParams = txData.txParams |
|
|
|
const txParams = txMeta.txParams |
|
|
|
|
|
|
|
|
|
|
|
// if gasLimit was specified and doesnt OOG,
|
|
|
|
// if gasLimit was specified and doesnt OOG,
|
|
|
|
// use original specified amount
|
|
|
|
// use original specified amount
|
|
|
|
if (txData.gasLimitSpecified) { |
|
|
|
if (txMeta.gasLimitSpecified) { |
|
|
|
txData.estimatedGas = txParams.gas |
|
|
|
txMeta.estimatedGas = txParams.gas |
|
|
|
cb() |
|
|
|
cb() |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
|
// if gasLimit not originally specified,
|
|
|
|
// if gasLimit not originally specified,
|
|
|
|
// try adding an additional gas buffer to our estimation for safety
|
|
|
|
// try adding an additional gas buffer to our estimation for safety
|
|
|
|
const recommendedGasHex = this.addGasBuffer(txData.estimatedGas, blockGasLimitHex) |
|
|
|
const recommendedGasHex = this.addGasBuffer(txMeta.estimatedGas, blockGasLimitHex) |
|
|
|
txParams.gas = recommendedGasHex |
|
|
|
txParams.gas = recommendedGasHex |
|
|
|
cb() |
|
|
|
cb() |
|
|
|
return |
|
|
|
return |
|
|
@ -90,16 +91,13 @@ module.exports = class txProviderUtils { |
|
|
|
|
|
|
|
|
|
|
|
// builds ethTx from txParams object
|
|
|
|
// builds ethTx from txParams object
|
|
|
|
buildEthTxFromParams (txParams) { |
|
|
|
buildEthTxFromParams (txParams) { |
|
|
|
// apply gas multiplyer
|
|
|
|
|
|
|
|
let gasPrice = hexToBn(txParams.gasPrice) |
|
|
|
|
|
|
|
// multiply and divide by 100 so as to add percision to integer mul
|
|
|
|
|
|
|
|
txParams.gasPrice = ethUtil.intToHex(gasPrice.toNumber()) |
|
|
|
|
|
|
|
// normalize values
|
|
|
|
// normalize values
|
|
|
|
txParams.to = normalize(txParams.to) |
|
|
|
txParams.to = normalize(txParams.to) |
|
|
|
txParams.from = normalize(txParams.from) |
|
|
|
txParams.from = normalize(txParams.from) |
|
|
|
txParams.value = normalize(txParams.value) |
|
|
|
txParams.value = normalize(txParams.value) |
|
|
|
txParams.data = normalize(txParams.data) |
|
|
|
txParams.data = normalize(txParams.data) |
|
|
|
txParams.gas = normalize(txParams.gas || txParams.gasLimit) |
|
|
|
txParams.gas = normalize(txParams.gas || txParams.gasLimit) |
|
|
|
|
|
|
|
txParams.gasPrice = normalize(txParams.gasPrice) |
|
|
|
txParams.nonce = normalize(txParams.nonce) |
|
|
|
txParams.nonce = normalize(txParams.nonce) |
|
|
|
// build ethTx
|
|
|
|
// build ethTx
|
|
|
|
log.info(`Prepared tx for signing: ${JSON.stringify(txParams)}`) |
|
|
|
log.info(`Prepared tx for signing: ${JSON.stringify(txParams)}`) |
|
|
|