maxPriorityFee param, tests fix

This commit is contained in:
smart_ex 2022-07-12 01:49:47 +10:00
parent 21d5103021
commit d36a8fec96
3 changed files with 49 additions and 27 deletions

10
index.d.ts vendored
View File

@ -32,11 +32,9 @@ export interface TxManagerConfig {
ESTIMATE_GAS?: boolean ESTIMATE_GAS?: boolean
THROW_ON_REVERT?: boolean THROW_ON_REVERT?: boolean
BLOCK_GAS_LIMIT?: number BLOCK_GAS_LIMIT?: number
PRIORITY_FEE_GWEI?: number
BASE_FEE_RESERVE_PERCENTAGE?: number BASE_FEE_RESERVE_PERCENTAGE?: number
ENABLE_EIP1559?: boolean ENABLE_EIP1559?: boolean
DEFAULT_PRIORITY_FEE?: number DEFAULT_PRIORITY_FEE?: number
PRIORITY_FEE_RESERVE_PERCENTAGE?: number
} }
export interface TxManagerParams { export interface TxManagerParams {
@ -80,7 +78,7 @@ export type MessageEvents = {
} }
type TEventEmitter = typeof EventEmitter type TEventEmitter = typeof EventEmitter
declare interface TxManagerEventEmmiter extends TEventEmitter { declare interface TxManagerEventEmitter extends TEventEmitter {
on<U extends TxManagerEvents>(event: U, listener: MessageEvents[U]): this on<U extends TxManagerEvents>(event: U, listener: MessageEvents[U]): this
on(event: 'confirmations', listener: MessageEvents['confirmations']): Promise<TransactionReceipt> on(event: 'confirmations', listener: MessageEvents['confirmations']): Promise<TransactionReceipt>
@ -92,7 +90,7 @@ export class Transaction {
manager: TxManager manager: TxManager
tx: TransactionData tx: TransactionData
private _promise: typeof PromiEvent private _promise: typeof PromiEvent
private _emitter: TxManagerEventEmmiter private _emitter: TxManagerEventEmitter
executed: boolean executed: boolean
retries: number retries: number
currentTxHash: string currentTxHash: string
@ -100,7 +98,7 @@ export class Transaction {
constructor(tx: TransactionData, manager: TxManager) constructor(tx: TransactionData, manager: TxManager)
send(): TxManagerEventEmmiter send(): TxManagerEventEmitter
replace(tx: TransactionData): Promise<void> replace(tx: TransactionData): Promise<void>
@ -120,8 +118,6 @@ export class Transaction {
private _hasError(message: string, errors: (string | RegExp)[]): boolean private _hasError(message: string, errors: (string | RegExp)[]): boolean
private _getGasPrice(type: 'instant' | 'fast' | 'normal' | 'slow'): Promise<string>
private _getLastNonce(): Promise<number> private _getLastNonce(): Promise<number>
private _getGasParams(): Promise<GasParams> private _getGasParams(): Promise<GasParams>

View File

@ -72,21 +72,27 @@ class Transaction {
if (!tx.gasLimit) { if (!tx.gasLimit) {
const estimatedGasLimit = await this._estimateGas(tx) const estimatedGasLimit = await this._estimateGas(tx)
tx.gasLimit = min( const gasLimit = estimatedGasLimit
estimatedGasLimit.mul(Math.floor(this.manager.config.GAS_LIMIT_MULTIPLIER * 100)).div(100), .mul(Math.floor(this.manager.config.GAS_LIMIT_MULTIPLIER * 100))
this.manager.config.BLOCK_GAS_LIMIT, .div(100)
)
}
tx.gasLimit = this.manager.config.BLOCK_GAS_LIMIT
? min(gasLimit, this.manager.config.BLOCK_GAS_LIMIT)
: gasLimit
}
// TODO: check if the new tx params is valid
tx.chainId = this.tx.chainId tx.chainId = this.tx.chainId
tx.nonce = this.tx.nonce // can be different from `this.manager._nonce` tx.nonce = this.tx.nonce // can be different from `this.manager._nonce`
// start no less than current tx gas params // start no less than current tx gas params
if (this.tx.gasPrice) { if (this.tx.gasPrice) {
tx.gasPrice = max(this.tx.gasPrice, tx.gasPrice || 0) tx.gasPrice = max(this.tx.gasPrice, tx.gasPrice || 0)
} else { } else if (this.tx.maxFeePerGas) {
tx.maxFeePerGas = max(this.tx.maxFeePerGas, tx.maxFeePerGas || 0) tx.maxFeePerGas = max(this.tx.maxFeePerGas, tx.maxFeePerGas || 0)
tx.maxPriorityFeePerGas = max(this.tx.maxPriorityFeePerGas, tx.maxPriorityFeePerGas || 0) tx.maxPriorityFeePerGas = max(this.tx.maxPriorityFeePerGas, tx.maxPriorityFeePerGas || 0)
} else {
const gasParams = await this._getGasParams()
tx = { ...tx, ...gasParams }
} }
this.tx = { ...tx } this.tx = { ...tx }
@ -418,9 +424,15 @@ class Transaction {
*/ */
async _getGasParams() { async _getGasParams() {
const maxGasPrice = parseUnits(this.manager.config.MAX_GAS_PRICE.toString(), 'gwei') const maxGasPrice = parseUnits(this.manager.config.MAX_GAS_PRICE.toString(), 'gwei')
const gasParams = await this.manager._gasPriceOracle.getTxGasParams() const gasParams = await this.manager._gasPriceOracle.getTxGasParams({
if (gasParams.gasPrice) gasParams.gasPrice = min(gasParams.gasPrice, maxGasPrice) isLegacy: !this.manager.config.ENABLE_EIP1559,
else gasParams.maxFeePerGas = min(gasParams?.maxFeePerGas, maxGasPrice) })
if (gasParams.gasPrice) {
gasParams.gasPrice = min(gasParams.gasPrice, maxGasPrice)
} else {
gasParams.maxFeePerGas = min(gasParams?.maxFeePerGas, maxGasPrice)
gasParams.maxPriorityFeePerGas = min(gasParams?.maxPriorityFeePerGas, maxGasPrice)
}
gasParams.type = gasParams?.maxFeePerGas ? 2 : 0 gasParams.type = gasParams?.maxFeePerGas ? 2 : 0
return gasParams return gasParams
} }

View File

@ -57,54 +57,68 @@ const getOptions = async () => {
return { network, provider, options } return { network, provider, options }
} }
const createTx = async transaction => { const sendTx = async tx => {
const tx = this.manager.createTx(transaction)
const receipt = await tx const receipt = await tx
.send() .send()
.on('transactionHash', hash => console.log('hash', hash)) .on('transactionHash', hash => console.log('hash', hash))
.on('mined', receipt => console.log('Mined in block', receipt.blockNumber)) .on('mined', receipt => console.log('Mined in block', receipt.blockNumber))
.on('confirmations', confirmations => console.log('confirmations', confirmations)) .on('confirmations', confirmations => console.log('confirmations', confirmations))
console.log('receipt', receipt) console.log('receipt', receipt)
return receipt
} }
const transactionTests = () => { const transactionTests = () => {
it('should work legacy tx', async () => { it('should work legacy tx', async () => {
await createTx(tx1) const tx = this.manager.createTx(tx1)
const receipt = await sendTx(tx)
receipt.type.should.equal(0)
}) })
it('should work eip-1559 tx', async () => { it('should work eip-1559 tx', async () => {
await createTx(tx5) const tx = this.manager.createTx(tx5)
const receipt = await sendTx(tx)
receipt.type.should.equal(2)
}) })
it('should fetch gas params', async () => { it('should fetch gas params', async () => {
await createTx(tx4) const tx = this.manager.createTx(tx4)
await sendTx(tx)
}) })
it('should bump gas params', async () => { it('should bump gas params', async () => {
await createTx(tx2) const tx = this.manager.createTx(tx2)
await sendTx(tx)
}) })
it('should cancel', async () => { it('should cancel', async () => {
await createTx(tx2) const tx = this.manager.createTx(tx3)
setTimeout(() => tx.cancel(), 1000)
await sendTx(tx)
}) })
it('should replace', async () => { it('should replace', async () => {
await createTx(tx2) const tx = this.manager.createTx(tx3)
setTimeout(() => tx.replace(tx4), 1000)
const receipt = await sendTx(tx)
receipt.to.should.equal(tx4.to)
}) })
it('should increase nonce', async () => { it('should increase nonce', async () => {
const currentNonce = await this.manager._wallet.getTransactionCount('latest') const currentNonce = await this.manager._wallet.getTransactionCount('latest')
this.manager._nonce = currentNonce - 1 this.manager._nonce = currentNonce - 1
await createTx(tx4) const tx = this.manager.createTx(tx4)
await sendTx(tx)
}) })
it('should disable eip-1559 transactions', async () => { it('should disable eip-1559 transactions', async () => {
this.manager.config.ENABLE_EIP1559 = false this.manager.config.ENABLE_EIP1559 = false
await createTx(tx3) const tx = this.manager.createTx(tx3)
const receipt = await sendTx(tx)
receipt.type.should.equal(0)
this.manager.config.ENABLE_EIP1559 = true this.manager.config.ENABLE_EIP1559 = true
}) })
it('should send multiple txs', async () => { it.skip('should send multiple txs', async () => {
const genTx = value => ({ const genTx = value => ({
value, value,
to: '0x0039F22efB07A647557C7C5d17854CFD6D489eF3', to: '0x0039F22efB07A647557C7C5d17854CFD6D489eF3',