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

View File

@ -66,6 +66,7 @@ class Transaction {
if (!tx.gasLimit) { if (!tx.gasLimit) {
tx.gasLimit = await this._wallet.estimateGas(tx) tx.gasLimit = await this._wallet.estimateGas(tx)
tx.gasLimit = Math.floor(tx.gasLimit * this.config.GAS_LIMIT_MULTIPLIER) 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.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 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) { 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) {
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) { if (!this.tx.gasPrice) {

View File

@ -14,6 +14,7 @@ const defaultConfig = {
CONFIRMATIONS: 8, CONFIRMATIONS: 8,
ESTIMATE_GAS: true, ESTIMATE_GAS: true,
THROW_ON_REVERT: true, THROW_ON_REVERT: true,
BLOCK_GAS_LIMIT: null,
} }
class TxManager { class TxManager {
@ -34,7 +35,11 @@ class TxManager {
* *
* @param tx Transaction to send * @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) return new Transaction(tx, this)
} }
} }

View File

@ -39,7 +39,7 @@ describe('TxManager', () => {
describe('#transaction', () => { describe('#transaction', () => {
it('should work', async () => { it('should work', async () => {
const tx = manager.createTx(tx1) const tx = await 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 = manager.createTx(tx4) const tx = await 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 = manager.createTx(tx2) const tx = await 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 = manager.createTx(tx2) const tx = await 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 = manager.createTx(tx2) const tx = await manager.createTx(tx2)
setTimeout(() => tx.replace(tx3), 1000) setTimeout(() => tx.replace(tx3), 1000)