using gas-price-oracle v0.5.0

This commit is contained in:
smart_ex 2022-06-29 13:29:43 +10:00
parent 13bb669f53
commit b4235ec8f7
6 changed files with 32 additions and 134 deletions

View File

@ -1,3 +1,6 @@
# Needed for tests only
RPC_URL=https://kovan.infura.io/v3/...
PRIVATE_KEY=...
ETHERSCAN_API_KEY=...
ALCHEMY_API_KEY=...
INFURA_API_KEY=...

3
index.d.ts vendored
View File

@ -2,9 +2,8 @@ import { BigNumberish, providers, Wallet } from 'ethers'
import { EventEmitter } from 'eventemitter3'
import { TransactionReceipt } from '@ethersproject/abstract-provider'
import PromiEvent from 'web3-core-promievent'
import { GasPriceOracle } from 'gas-price-oracle'
import { GasOracleOptions, GasPriceOracle } from 'gas-price-oracle'
import { Mutex } from 'async-mutex'
import { Options as GasOracleOptions } from 'gas-price-oracle/lib/types'
import { JsonRpcProvider } from '@ethersproject/providers'
export interface TransactionData {

View File

@ -28,7 +28,7 @@
"dependencies": {
"async-mutex": "^0.2.4",
"ethers": "^5.4.6",
"gas-price-oracle": "^0.4.7",
"gas-price-oracle": "git+https://github.com/peppersec/gas-price-oracle.git#da3de92dea7c75afc2c8ba141f23c4eea597a614",
"web3-core-promievent": "^1.3.0"
},
"devDependencies": {

View File

@ -400,20 +400,6 @@ class Transaction {
return true
}
/**
* Fetches gas price from the oracle
*
* @param {'instant'|'fast'|'normal'|'slow'} type
* @returns {Promise<string>} A hex string representing gas price in wei
* @private
*/
async _getGasPrice(type) {
const gasPrices = await this.manager._gasPriceOracle.gasPrices()
const result = gasPrices[type].toString()
console.log(`${type} gas price is now ${result} gwei`)
return parseUnits(result, 'gwei').toHexString()
}
/**
* Gets current nonce for the current account, ignoring any pending transactions
*
@ -432,29 +418,10 @@ class Transaction {
*/
async _getGasParams() {
const maxGasPrice = parseUnits(this.manager.config.MAX_GAS_PRICE.toString(), 'gwei')
const block = await this.manager._provider.getBlock('latest')
// Check network support for EIP-1559
if (this.manager.config.ENABLE_EIP1559 && block && block.baseFeePerGas) {
const maxPriorityFeePerGas = parseUnits(this.manager.config.DEFAULT_PRIORITY_FEE.toString(), 'gwei')
const maxFeePerGas = block.baseFeePerGas
.mul(100 + this.manager.config.BASE_FEE_RESERVE_PERCENTAGE)
.div(100)
.add(maxPriorityFeePerGas)
return {
maxFeePerGas: min(maxFeePerGas, maxGasPrice).toHexString(),
maxPriorityFeePerGas: min(maxPriorityFeePerGas, maxGasPrice).toHexString(),
type: 2,
}
} else {
const fastGasPrice = BigNumber.from(await this._getGasPrice('fast'))
return {
gasPrice: min(fastGasPrice, maxGasPrice).toHexString(),
type: 0,
}
}
const gasParams = await this.manager._gasPriceOracle.getTxGasParams({})
if (gasParams.gasPrice) gasParams.gasPrice = min(gasParams.gasPrice, maxGasPrice)
gasParams.type = gasParams?.maxFeePerGas ? 2 : 0
return gasParams
}
async _estimateGas(tx) {

View File

@ -56,110 +56,51 @@ const getOptions = async () => {
const options = { ...defaultOptions }
return { network, provider, options }
}
const createTx = async transaction => {
const tx = this.manager.createTx(transaction)
const receipt = await tx
.send()
.on('transactionHash', hash => console.log('hash', hash))
.on('mined', receipt => console.log('Mined in block', receipt.blockNumber))
.on('confirmations', confirmations => console.log('confirmations', confirmations))
console.log('receipt', receipt)
}
const transactionTests = () => {
it('should work legacy tx', async () => {
const tx = this.manager.createTx(tx1)
const receipt = await tx
.send()
.on('transactionHash', hash => console.log('hash', hash))
.on('mined', receipt => console.log('Mined in block', receipt.blockNumber))
.on('confirmations', confirmations => console.log('confirmations', confirmations))
console.log('receipt', receipt)
await createTx(tx1)
})
it('should work eip-1559 tx', async () => {
const tx = this.manager.createTx(tx5)
const receipt = await tx
.send()
.on('transactionHash', hash => console.log('hash', hash))
.on('mined', receipt => console.log('Mined in block', receipt.blockNumber))
.on('confirmations', confirmations => console.log('confirmations', confirmations))
console.log('receipt', receipt)
await createTx(tx5)
})
it('should fetch gas params', async () => {
const tx = this.manager.createTx(tx4)
const receipt = await tx
.send()
.on('transactionHash', hash => console.log('hash', hash))
.on('mined', receipt => console.log('Mined in block', receipt.blockNumber))
.on('confirmations', confirmations => console.log('confirmations', confirmations))
console.log('receipt', receipt)
await createTx(tx4)
})
it('should bump gas params', async () => {
const tx = this.manager.createTx(tx2)
const receipt = await tx
.send()
.on('transactionHash', hash => console.log('hash', hash))
.on('mined', receipt => console.log('Mined in block', receipt.blockNumber))
.on('confirmations', confirmations => console.log('confirmations', confirmations))
console.log('receipt', receipt)
await createTx(tx2)
})
it('should cancel', async () => {
const tx = this.manager.createTx(tx2)
setTimeout(() => tx.cancel(), 1000)
const receipt = await tx
.send()
.on('transactionHash', hash => console.log('hash', hash))
.on('mined', receipt => console.log('Mined in block', receipt.blockNumber))
.on('confirmations', confirmations => console.log('confirmations', confirmations))
console.log('receipt', receipt)
await createTx(tx2)
})
it('should replace', async () => {
const tx = this.manager.createTx(tx2)
setTimeout(() => tx.replace(tx3), 1000)
const receipt = await tx
.send()
.on('transactionHash', hash => console.log('hash', hash))
.on('mined', receipt => console.log('Mined in block', receipt.blockNumber))
.on('confirmations', confirmations => console.log('confirmations', confirmations))
console.log('receipt', receipt)
await createTx(tx2)
})
it('should increase nonce', async () => {
const currentNonce = await this.manager._wallet.getTransactionCount('latest')
this.manager._nonce = currentNonce - 1
const tx = this.manager.createTx(tx4)
const receipt = await tx
.send()
.on('transactionHash', hash => console.log('hash', hash))
.on('mined', receipt => console.log('Mined in block', receipt.blockNumber))
.on('confirmations', confirmations => console.log('confirmations', confirmations))
console.log('receipt', receipt)
await createTx(tx4)
})
it('should disable eip-1559 transactions', async () => {
this.manager.config.ENABLE_EIP1559 = false
const tx = this.manager.createTx(tx3)
const receipt = await tx
.send()
.on('transactionHash', hash => console.log('hash', hash))
.on('mined', receipt => console.log('Mined in block', receipt.blockNumber))
.on('confirmations', confirmations => console.log('confirmations', confirmations))
console.log('receipt', receipt)
await createTx(tx3)
this.manager.config.ENABLE_EIP1559 = true
})
@ -168,18 +109,7 @@ const transactionTests = () => {
value,
to: '0x0039F22efB07A647557C7C5d17854CFD6D489eF3',
})
await Promise.all([
this.manager.createTx(genTx(1)).send(),
this.manager.createTx(genTx(2)).send(),
this.manager.createTx(genTx(3)).send(),
this.manager.createTx(genTx(4)).send(),
this.manager.createTx(genTx(5)).send(),
this.manager.createTx(genTx(6)).send(),
this.manager.createTx(genTx(7)).send(),
this.manager.createTx(genTx(8)).send(),
this.manager.createTx(genTx(9)).send(),
this.manager.createTx(genTx(10)).send(),
])
await Promise.all(Array.from({ length: 10 }).map(n => this.manager.createTx(genTx(n + 1)).send()))
}).timeout(600000)
}

View File

@ -2086,10 +2086,9 @@ functions-have-names@^1.2.2:
resolved "https://registry.yarnpkg.com/functions-have-names/-/functions-have-names-1.2.3.tgz#0404fe4ee2ba2f607f0e0ec3c80bae994133b834"
integrity sha512-xckBUXyTIqT97tq2x2AMb+g163b5JFysYk0x4qxNFwbfQkmNZoiRHb6sPzI9/QV33WeuvVYBUIiD4NzNIyqaRQ==
gas-price-oracle@^0.4.7:
version "0.4.7"
resolved "https://registry.yarnpkg.com/gas-price-oracle/-/gas-price-oracle-0.4.7.tgz#47406048083074bcab677efb9de08663e742153d"
integrity sha512-Ti8nhpATm83YebWU/Pz5xclZoTkzOblIhT504ZViZJUcd8jOxgj9pWtCasg8RYw+d0f19m0dJUPvdj04RC4o3A==
"gas-price-oracle@git+https://github.com/peppersec/gas-price-oracle.git#da3de92dea7c75afc2c8ba141f23c4eea597a614":
version "0.5.0"
resolved "git+https://github.com/peppersec/gas-price-oracle.git#da3de92dea7c75afc2c8ba141f23c4eea597a614"
dependencies:
axios "^0.21.2"
bignumber.js "^9.0.0"