move BLOCK_GAS_LIMIT init to _prepare

This commit is contained in:
Alexey 2020-12-23 19:39:09 -06:00
parent 30b32a61d1
commit ab82b37e0d
3 changed files with 12 additions and 11 deletions

@ -116,6 +116,11 @@ class Transaction {
* @private * @private
*/ */
async _prepare() { 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) { if (!this.tx.gasLimit || this.config.ESTIMATE_GAS) {
const gas = await this._wallet.estimateGas(this.tx) const gas = await this._wallet.estimateGas(this.tx)
if (!this.tx.gasLimit) { if (!this.tx.gasLimit) {

@ -35,11 +35,7 @@ class TxManager {
* *
* @param tx Transaction to send * @param tx Transaction to send
*/ */
async createTx(tx) { 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) return new Transaction(tx, this)
} }
} }

@ -10,7 +10,7 @@ describe('TxManager', () => {
privateKey: PRIVATE_KEY, privateKey: PRIVATE_KEY,
rpcUrl: RPC_URL, rpcUrl: RPC_URL,
config: { config: {
CONFIRMATIONS: 3, CONFIRMATIONS: 1,
GAS_BUMP_INTERVAL: 1000 * 15, GAS_BUMP_INTERVAL: 1000 * 15,
}, },
}) })
@ -39,7 +39,7 @@ describe('TxManager', () => {
describe('#transaction', () => { describe('#transaction', () => {
it('should work', async () => { it('should work', async () => {
const tx = await manager.createTx(tx1) const tx = manager.createTx(tx1)
const receipt = await tx const receipt = await tx
.send() .send()
@ -51,7 +51,7 @@ describe('TxManager', () => {
}) })
it('should fetch gas price', async () => { it('should fetch gas price', async () => {
const tx = await manager.createTx(tx4) const tx = manager.createTx(tx4)
const receipt = await tx const receipt = await tx
.send() .send()
@ -63,7 +63,7 @@ describe('TxManager', () => {
}) })
it('should bump gas price', async () => { it('should bump gas price', async () => {
const tx = await manager.createTx(tx2) const tx = manager.createTx(tx2)
const receipt = await tx const receipt = await tx
.send() .send()
@ -75,7 +75,7 @@ describe('TxManager', () => {
}) })
it('should cancel', async () => { it('should cancel', async () => {
const tx = await manager.createTx(tx2) const tx = manager.createTx(tx2)
setTimeout(() => tx.cancel(), 1000) setTimeout(() => tx.cancel(), 1000)
@ -89,7 +89,7 @@ describe('TxManager', () => {
}) })
it('should replace', async () => { it('should replace', async () => {
const tx = await manager.createTx(tx2) const tx = manager.createTx(tx2)
setTimeout(() => tx.replace(tx3), 1000) setTimeout(() => tx.replace(tx3), 1000)