adds BLOCK_GAS_LIMIT const

This commit is contained in:
Alexey 2020-12-23 19:08:36 -06:00
parent 682f2b03c8
commit 30b32a61d1
3 changed files with 14 additions and 7 deletions

@ -66,6 +66,7 @@ class Transaction {
if (!tx.gasLimit) {
tx.gasLimit = await this._wallet.estimateGas(tx)
tx.gasLimit = Math.floor(tx.gasLimit * this.config.GAS_LIMIT_MULTIPLIER)
tx.gasLimit = Math.min(tx.gasLimit, this.config.BLOCK_GAS_LIMIT)
}
tx.nonce = this.tx.nonce // can be different from `this.manager._nonce`
tx.gasPrice = Math.max(this.tx.gasPrice, tx.gasPrice || 0) // start no less than current tx gas price
@ -118,7 +119,8 @@ class Transaction {
if (!this.tx.gasLimit || this.config.ESTIMATE_GAS) {
const gas = await this._wallet.estimateGas(this.tx)
if (!this.tx.gasLimit) {
this.tx.gasLimit = Math.floor(gas * this.config.GAS_LIMIT_MULTIPLIER)
const gasLimit = Math.floor(gas * this.config.GAS_LIMIT_MULTIPLIER)
this.tx.gasLimit = Math.min(gasLimit, this.config.BLOCK_GAS_LIMIT)
}
}
if (!this.tx.gasPrice) {

@ -14,6 +14,7 @@ const defaultConfig = {
CONFIRMATIONS: 8,
ESTIMATE_GAS: true,
THROW_ON_REVERT: true,
BLOCK_GAS_LIMIT: null,
}
class TxManager {
@ -34,7 +35,11 @@ class TxManager {
*
* @param tx Transaction to send
*/
createTx(tx) {
async createTx(tx) {
if (!this.config.BLOCK_GAS_LIMIT) {
const lastBlock = await this._provider.getBlock('latest')
this.config.BLOCK_GAS_LIMIT = lastBlock.gasLimit.toNumber()
}
return new Transaction(tx, this)
}
}

@ -39,7 +39,7 @@ describe('TxManager', () => {
describe('#transaction', () => {
it('should work', async () => {
const tx = manager.createTx(tx1)
const tx = await manager.createTx(tx1)
const receipt = await tx
.send()
@ -51,7 +51,7 @@ describe('TxManager', () => {
})
it('should fetch gas price', async () => {
const tx = manager.createTx(tx4)
const tx = await manager.createTx(tx4)
const receipt = await tx
.send()
@ -63,7 +63,7 @@ describe('TxManager', () => {
})
it('should bump gas price', async () => {
const tx = manager.createTx(tx2)
const tx = await manager.createTx(tx2)
const receipt = await tx
.send()
@ -75,7 +75,7 @@ describe('TxManager', () => {
})
it('should cancel', async () => {
const tx = manager.createTx(tx2)
const tx = await manager.createTx(tx2)
setTimeout(() => tx.cancel(), 1000)
@ -89,7 +89,7 @@ describe('TxManager', () => {
})
it('should replace', async () => {
const tx = manager.createTx(tx2)
const tx = await manager.createTx(tx2)
setTimeout(() => tx.replace(tx3), 1000)