estimate gas

This commit is contained in:
smart_ex 2022-05-24 11:45:03 +10:00
parent 1b59ac8923
commit bb19222835
2 changed files with 8 additions and 5 deletions

4
index.d.ts vendored
View File

@ -7,13 +7,13 @@ import { Mutex } from 'async-mutex'
import { Options as GasOracleOptions } from 'gas-price-oracle/lib/types' import { Options as GasOracleOptions } from 'gas-price-oracle/lib/types'
export interface TransactionData { export interface TransactionData {
to?: string to: string
from?: string from?: string
nonce?: number nonce?: number
gasLimit?: BigNumberish gasLimit?: BigNumberish
gasPrice?: BigNumberish gasPrice?: BigNumberish
data?: string data?: string
value?: BigNumberish value: BigNumberish
chainId?: number chainId?: number
type?: number type?: number
maxFeePerGas?: BigNumberish maxFeePerGas?: BigNumberish

View File

@ -28,6 +28,7 @@ class Transaction {
constructor(tx, manager) { constructor(tx, manager) {
this.manager = manager this.manager = manager
this.tx = { ...tx } this.tx = { ...tx }
this.nonce = manager._nonce
this._promise = PromiEvent() this._promise = PromiEvent()
this._emitter = this._promise.eventEmitter this._emitter = this._promise.eventEmitter
this.executed = false this.executed = false
@ -64,9 +65,11 @@ class Transaction {
return return
} }
if (!tx.gasLimit) { if (!tx.gasLimit) {
tx.gasLimit = await this.manager._wallet.estimateGas(tx) const estimatedGasLimit = await this.manager._wallet.estimateGas(tx)
tx.gasLimit = Math.floor(tx.gasLimit.mul(this.manager.config.GAS_LIMIT_MULTIPLIER).toNumber()) tx.gasLimit = min(
tx.gasLimit = min(tx.gasLimit, this.manager.config.BLOCK_GAS_LIMIT) estimatedGasLimit.mul(this.manager.config.GAS_LIMIT_MULTIPLIER * 100).div(100),
this.manager.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`