2020-10-16 21:44:09 +03:00
|
|
|
const ethers = require('ethers')
|
2020-10-01 07:56:47 +03:00
|
|
|
const { Mutex } = require('async-mutex')
|
|
|
|
const { GasPriceOracle } = require('gas-price-oracle')
|
|
|
|
const Transaction = require('./Transaction')
|
|
|
|
|
|
|
|
const defaultConfig = {
|
|
|
|
MAX_RETRIES: 10,
|
|
|
|
GAS_BUMP_PERCENTAGE: 5,
|
|
|
|
MIN_GWEI_BUMP: 1,
|
|
|
|
GAS_BUMP_INTERVAL: 1000 * 60 * 5,
|
|
|
|
MAX_GAS_PRICE: 1000,
|
2020-10-16 21:44:09 +03:00
|
|
|
GAS_LIMIT_MULTIPLIER: 1.1,
|
2020-10-01 07:56:47 +03:00
|
|
|
POLL_INTERVAL: 5000,
|
|
|
|
CONFIRMATIONS: 8,
|
2020-10-15 02:29:59 +03:00
|
|
|
ESTIMATE_GAS: true,
|
2020-11-19 20:33:58 +03:00
|
|
|
THROW_ON_REVERT: true,
|
2020-12-24 08:39:07 +03:00
|
|
|
BLOCK_GAS_LIMIT: null,
|
2021-09-03 13:44:17 +03:00
|
|
|
PRIORITY_FEE_GWEI: 3,
|
|
|
|
BASE_FEE_RESERVE_PERCENTAGE: 50,
|
2020-10-01 07:56:47 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
class TxManager {
|
2021-06-03 16:16:31 +03:00
|
|
|
constructor({ privateKey, rpcUrl, broadcastNodes = [], config = {}, gasPriceOracleConfig = {} }) {
|
2020-10-01 07:56:47 +03:00
|
|
|
this.config = Object.assign({ ...defaultConfig }, config)
|
2020-10-16 21:44:09 +03:00
|
|
|
this._privateKey = privateKey.startsWith('0x') ? privateKey : '0x' + privateKey
|
|
|
|
this._provider = new ethers.providers.JsonRpcProvider(rpcUrl)
|
|
|
|
this._wallet = new ethers.Wallet(this._privateKey, this._provider)
|
|
|
|
this.address = this._wallet.address
|
2020-10-01 07:56:47 +03:00
|
|
|
this._broadcastNodes = broadcastNodes
|
2021-06-03 16:16:31 +03:00
|
|
|
this._gasPriceOracle = new GasPriceOracle({ defaultRpc: rpcUrl, ...gasPriceOracleConfig })
|
2020-10-01 07:56:47 +03:00
|
|
|
this._mutex = new Mutex()
|
|
|
|
this._nonce = null
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates Transaction class instance.
|
|
|
|
*
|
|
|
|
* @param tx Transaction to send
|
|
|
|
*/
|
|
|
|
createTx(tx) {
|
|
|
|
return new Transaction(tx, this)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = TxManager
|