2020-10-01 07:56:47 +03:00
|
|
|
require('dotenv').config()
|
|
|
|
require('chai').should()
|
2022-05-11 16:24:39 +03:00
|
|
|
const { providers } = require('ethers')
|
2020-10-17 05:22:55 +03:00
|
|
|
const { parseUnits } = require('ethers').utils
|
2020-10-01 07:56:47 +03:00
|
|
|
const TxManager = require('../src/TxManager')
|
2022-07-14 08:39:17 +03:00
|
|
|
const Web3 = require('web3')
|
2020-10-01 07:56:47 +03:00
|
|
|
const { RPC_URL, PRIVATE_KEY } = process.env
|
|
|
|
|
2022-07-14 08:39:17 +03:00
|
|
|
const tx1 = {
|
|
|
|
value: 1,
|
|
|
|
gasPrice: parseUnits('2', 'gwei').toHexString(),
|
|
|
|
to: '0xA43Ce8Cc89Eff3AA5593c742fC56A30Ef2427CB0',
|
|
|
|
}
|
|
|
|
|
|
|
|
const tx2 = {
|
|
|
|
value: 1,
|
|
|
|
gasPrice: parseUnits('0.5', 'gwei').toHexString(),
|
|
|
|
to: '0xA43Ce8Cc89Eff3AA5593c742fC56A30Ef2427CB0',
|
|
|
|
}
|
|
|
|
|
|
|
|
const tx3 = {
|
|
|
|
value: 2,
|
|
|
|
to: '0x0039F22efB07A647557C7C5d17854CFD6D489eF3',
|
|
|
|
}
|
|
|
|
|
|
|
|
const tx4 = {
|
|
|
|
value: 1,
|
|
|
|
to: '0xA43Ce8Cc89Eff3AA5593c742fC56A30Ef2427CB0',
|
|
|
|
}
|
|
|
|
|
|
|
|
const tx5 = {
|
|
|
|
value: 1,
|
|
|
|
to: '0xA43Ce8Cc89Eff3AA5593c742fC56A30Ef2427CB0',
|
|
|
|
maxFeePerGas: parseUnits('7', 'gwei').toHexString(),
|
|
|
|
maxPriorityFeePerGas: parseUnits('1', 'gwei').toHexString(),
|
|
|
|
type: 2,
|
|
|
|
}
|
|
|
|
|
|
|
|
const defaultOptions = {
|
|
|
|
privateKey: PRIVATE_KEY,
|
|
|
|
rpcUrl: RPC_URL,
|
|
|
|
config: {
|
|
|
|
CONFIRMATIONS: 1,
|
|
|
|
GAS_BUMP_INTERVAL: 1000 * 20,
|
|
|
|
},
|
|
|
|
gasPriceOracleConfig: {
|
|
|
|
chainId: 1,
|
|
|
|
defaultRpc: RPC_URL,
|
|
|
|
},
|
|
|
|
provider: undefined,
|
|
|
|
}
|
|
|
|
|
|
|
|
const getOptions = async () => {
|
|
|
|
const provider = new providers.JsonRpcProvider(RPC_URL)
|
|
|
|
const network = await provider.getNetwork()
|
|
|
|
const options = { ...defaultOptions }
|
|
|
|
return { network, provider, options }
|
|
|
|
}
|
|
|
|
|
|
|
|
const sendTx = async tx => {
|
|
|
|
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)
|
|
|
|
return receipt
|
|
|
|
}
|
|
|
|
|
|
|
|
const transactionTests = () => {
|
|
|
|
it('should work legacy tx', async () => {
|
|
|
|
const tx = this.manager.createTx(tx1)
|
|
|
|
const receipt = await sendTx(tx)
|
|
|
|
receipt.type.should.equal(0)
|
2022-05-11 16:24:39 +03:00
|
|
|
})
|
|
|
|
|
2022-07-14 08:39:17 +03:00
|
|
|
it('should work eip-1559 tx', async () => {
|
|
|
|
const tx = this.manager.createTx(tx5)
|
|
|
|
const receipt = await sendTx(tx)
|
|
|
|
receipt.type.should.equal(2)
|
|
|
|
})
|
2020-10-01 07:56:47 +03:00
|
|
|
|
2022-07-14 08:39:17 +03:00
|
|
|
it('should fetch gas params', async () => {
|
|
|
|
const tx = this.manager.createTx(tx4)
|
|
|
|
await sendTx(tx)
|
|
|
|
})
|
2020-10-01 07:56:47 +03:00
|
|
|
|
2022-07-14 08:39:17 +03:00
|
|
|
it('should bump gas params', async () => {
|
|
|
|
const tx = this.manager.createTx(tx2)
|
|
|
|
await sendTx(tx)
|
|
|
|
})
|
2020-10-01 07:56:47 +03:00
|
|
|
|
2022-07-14 08:39:17 +03:00
|
|
|
it('should cancel', async () => {
|
|
|
|
const currentNonce = await this.manager._wallet.getTransactionCount('latest')
|
|
|
|
const tx = this.manager.createTx(tx3)
|
|
|
|
setTimeout(() => tx.cancel(), 1000)
|
|
|
|
const receipt = await sendTx(tx)
|
|
|
|
const transaction = await this.manager._provider.getTransaction(receipt.transactionHash)
|
|
|
|
transaction.value.toNumber().should.equal(0)
|
|
|
|
transaction.nonce.should.equal(currentNonce)
|
|
|
|
})
|
2020-10-01 07:56:47 +03:00
|
|
|
|
2022-07-14 08:39:17 +03:00
|
|
|
it('should replace', async () => {
|
|
|
|
const currentNonce = await this.manager._wallet.getTransactionCount('latest')
|
|
|
|
const tx = this.manager.createTx(tx3)
|
|
|
|
setTimeout(() => tx.replace(tx4), 1000)
|
|
|
|
const receipt = await sendTx(tx)
|
|
|
|
const transaction = await this.manager._provider.getTransaction(receipt.transactionHash)
|
|
|
|
receipt.to.should.equal(tx4.to)
|
|
|
|
transaction.nonce.should.equal(currentNonce)
|
|
|
|
})
|
2020-10-01 07:56:47 +03:00
|
|
|
|
2022-07-14 08:39:17 +03:00
|
|
|
it('should increase nonce', async () => {
|
|
|
|
const currentNonce = await this.manager._wallet.getTransactionCount('latest')
|
|
|
|
this.manager._nonce = currentNonce - 1
|
|
|
|
const tx = this.manager.createTx(tx4)
|
|
|
|
await sendTx(tx)
|
|
|
|
})
|
2020-10-01 07:56:47 +03:00
|
|
|
|
2022-07-14 08:39:17 +03:00
|
|
|
it('should disable eip-1559 transactions', async () => {
|
|
|
|
this.manager.config.ENABLE_EIP1559 = false
|
|
|
|
const tx = this.manager.createTx(tx3)
|
|
|
|
const receipt = await sendTx(tx)
|
|
|
|
receipt.type.should.equal(0)
|
|
|
|
this.manager.config.ENABLE_EIP1559 = true
|
|
|
|
})
|
2020-10-01 07:56:47 +03:00
|
|
|
|
2022-07-14 08:39:17 +03:00
|
|
|
it('should send multiple txs', async () => {
|
|
|
|
const genTx = value => ({
|
|
|
|
value,
|
|
|
|
to: '0x0039F22efB07A647557C7C5d17854CFD6D489eF3',
|
2020-10-01 07:56:47 +03:00
|
|
|
})
|
2022-07-14 08:39:17 +03:00
|
|
|
await Promise.all(Array.from({ length: 10 }).map(n => this.manager.createTx(genTx(n + 1)).send()))
|
|
|
|
}).timeout(600000)
|
|
|
|
}
|
2021-09-02 09:25:23 +03:00
|
|
|
|
2022-07-14 08:39:17 +03:00
|
|
|
describe('TxManager.default', () => {
|
|
|
|
before(async () => {
|
|
|
|
const {
|
|
|
|
network: { name, chainId },
|
|
|
|
options,
|
|
|
|
} = await getOptions()
|
|
|
|
options.chainId = chainId
|
|
|
|
console.log('default\n\n', 'network', { name, chainId }, '\n\n')
|
|
|
|
this.manager = new TxManager(options)
|
|
|
|
})
|
|
|
|
describe('#transaction', transactionTests)
|
|
|
|
})
|
2022-04-26 16:49:03 +03:00
|
|
|
|
2022-07-14 08:39:17 +03:00
|
|
|
describe('TxManager.EtherscanProvider', () => {
|
|
|
|
before(async () => {
|
|
|
|
const {
|
|
|
|
network: { name, chainId },
|
|
|
|
options,
|
|
|
|
} = await getOptions()
|
|
|
|
options.chainId = chainId
|
|
|
|
options.provider = new providers.EtherscanProvider(chainId, process.env.ETHERSCAN_API_KEY)
|
|
|
|
console.log('EtherscanProvider\n\n', 'network', { name, chainId }, '\n\n')
|
|
|
|
this.manager = new TxManager(options)
|
|
|
|
})
|
|
|
|
describe('#transaction', transactionTests)
|
|
|
|
})
|
2022-05-11 16:24:39 +03:00
|
|
|
|
2022-07-14 08:39:17 +03:00
|
|
|
describe('TxManager.InfuraProvider', () => {
|
|
|
|
before(async () => {
|
|
|
|
const {
|
|
|
|
network: { name, chainId },
|
|
|
|
options,
|
|
|
|
} = await getOptions()
|
|
|
|
options.chainId = chainId
|
|
|
|
options.provider = new providers.InfuraProvider(chainId, process.env.INFURA_API_KEY)
|
|
|
|
console.log('InfuraProvider\n\n', 'network', { name, chainId }, '\n\n')
|
|
|
|
this.manager = new TxManager(options)
|
|
|
|
})
|
|
|
|
describe('#transaction', transactionTests)
|
|
|
|
})
|
2022-05-11 16:24:39 +03:00
|
|
|
|
2022-07-14 08:39:17 +03:00
|
|
|
describe('TxManager.Web3Provider', () => {
|
|
|
|
before(async () => {
|
|
|
|
const {
|
|
|
|
network: { name, chainId },
|
|
|
|
options,
|
|
|
|
} = await getOptions()
|
|
|
|
options.chainId = chainId
|
|
|
|
options.provider = new providers.Web3Provider(new Web3.providers.HttpProvider(RPC_URL))
|
|
|
|
console.log('Web3Provider\n\n', 'network', { name, chainId }, '\n\n')
|
|
|
|
this.manager = new TxManager(options)
|
2020-10-01 07:56:47 +03:00
|
|
|
})
|
2022-07-14 08:39:17 +03:00
|
|
|
describe('#transaction', transactionTests)
|
2020-10-01 07:56:47 +03:00
|
|
|
})
|