From 30b32a61d168054118a885bbb1051488c912069b Mon Sep 17 00:00:00 2001 From: Alexey Date: Wed, 23 Dec 2020 19:08:36 -0600 Subject: [PATCH] adds BLOCK_GAS_LIMIT const --- src/Transaction.js | 4 +++- src/TxManager.js | 7 ++++++- test/TxManager.test.js | 10 +++++----- 3 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Transaction.js b/src/Transaction.js index b632d3e..e681ce8 100644 --- a/src/Transaction.js +++ b/src/Transaction.js @@ -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) { diff --git a/src/TxManager.js b/src/TxManager.js index 7de0582..669d077 100644 --- a/src/TxManager.js +++ b/src/TxManager.js @@ -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) } } diff --git a/test/TxManager.test.js b/test/TxManager.test.js index aeb8816..56890ff 100644 --- a/test/TxManager.test.js +++ b/test/TxManager.test.js @@ -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)