2020-10-16 20:44:09 +02:00
|
|
|
const ethers = require('ethers')
|
2020-10-01 06:56:47 +02: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 20:44:09 +02:00
|
|
|
GAS_LIMIT_MULTIPLIER: 1.1,
|
2020-10-01 06:56:47 +02:00
|
|
|
POLL_INTERVAL: 5000,
|
|
|
|
CONFIRMATIONS: 8,
|
2020-10-15 01:29:59 +02:00
|
|
|
ESTIMATE_GAS: true,
|
2020-11-19 18:33:58 +01:00
|
|
|
THROW_ON_REVERT: true,
|
2020-12-24 06:39:07 +01:00
|
|
|
BLOCK_GAS_LIMIT: null,
|
2020-10-01 06:56:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class TxManager {
|
|
|
|
constructor({ privateKey, rpcUrl, broadcastNodes = [], config = {} }) {
|
|
|
|
this.config = Object.assign({ ...defaultConfig }, config)
|
2020-10-16 20:44:09 +02: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 06:56:47 +02:00
|
|
|
this._broadcastNodes = broadcastNodes
|
|
|
|
this._gasPriceOracle = new GasPriceOracle({ defaultRpc: rpcUrl })
|
|
|
|
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
|