From ab82b37e0d56c5022baae364ac8df488a417d4cd Mon Sep 17 00:00:00 2001 From: Alexey Date: Wed, 23 Dec 2020 19:39:09 -0600 Subject: [PATCH] move BLOCK_GAS_LIMIT init to _prepare --- src/Transaction.js | 5 +++++ src/TxManager.js | 6 +----- test/TxManager.test.js | 12 ++++++------ 3 files changed, 12 insertions(+), 11 deletions(-) diff --git a/src/Transaction.js b/src/Transaction.js index e681ce8..def47d1 100644 --- a/src/Transaction.js +++ b/src/Transaction.js @@ -116,6 +116,11 @@ class Transaction { * @private */ async _prepare() { + if (!this.config.BLOCK_GAS_LIMIT) { + const lastBlock = await this._provider.getBlock('latest') + this.config.BLOCK_GAS_LIMIT = lastBlock.gasLimit.toNumber() + } + if (!this.tx.gasLimit || this.config.ESTIMATE_GAS) { const gas = await this._wallet.estimateGas(this.tx) if (!this.tx.gasLimit) { diff --git a/src/TxManager.js b/src/TxManager.js index 669d077..00a5198 100644 --- a/src/TxManager.js +++ b/src/TxManager.js @@ -35,11 +35,7 @@ class TxManager { * * @param tx Transaction to send */ - async createTx(tx) { - if (!this.config.BLOCK_GAS_LIMIT) { - const lastBlock = await this._provider.getBlock('latest') - this.config.BLOCK_GAS_LIMIT = lastBlock.gasLimit.toNumber() - } + createTx(tx) { return new Transaction(tx, this) } } diff --git a/test/TxManager.test.js b/test/TxManager.test.js index 56890ff..a8e461e 100644 --- a/test/TxManager.test.js +++ b/test/TxManager.test.js @@ -10,7 +10,7 @@ describe('TxManager', () => { privateKey: PRIVATE_KEY, rpcUrl: RPC_URL, config: { - CONFIRMATIONS: 3, + CONFIRMATIONS: 1, GAS_BUMP_INTERVAL: 1000 * 15, }, }) @@ -39,7 +39,7 @@ describe('TxManager', () => { describe('#transaction', () => { it('should work', async () => { - const tx = await manager.createTx(tx1) + const tx = manager.createTx(tx1) const receipt = await tx .send() @@ -51,7 +51,7 @@ describe('TxManager', () => { }) it('should fetch gas price', async () => { - const tx = await manager.createTx(tx4) + const tx = manager.createTx(tx4) const receipt = await tx .send() @@ -63,7 +63,7 @@ describe('TxManager', () => { }) it('should bump gas price', async () => { - const tx = await manager.createTx(tx2) + const tx = manager.createTx(tx2) const receipt = await tx .send() @@ -75,7 +75,7 @@ describe('TxManager', () => { }) it('should cancel', async () => { - const tx = await manager.createTx(tx2) + const tx = manager.createTx(tx2) setTimeout(() => tx.cancel(), 1000) @@ -89,7 +89,7 @@ describe('TxManager', () => { }) it('should replace', async () => { - const tx = await manager.createTx(tx2) + const tx = manager.createTx(tx2) setTimeout(() => tx.replace(tx3), 1000)