tx-manager/test/TxManager.test.js

189 lines
5.7 KiB
JavaScript
Raw Normal View History

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')
// const Transaction = require('../src/Transaction')
const { RPC_URL, PRIVATE_KEY } = process.env
describe('TxManager', () => {
2022-05-11 16:24:39 +03:00
let manager
2020-10-01 07:56:47 +03:00
const tx1 = {
value: 1,
2021-09-02 09:25:23 +03:00
gasPrice: parseUnits('2', 'gwei').toHexString(),
2020-10-01 07:56:47 +03:00
to: '0xA43Ce8Cc89Eff3AA5593c742fC56A30Ef2427CB0',
}
const tx2 = {
2020-10-16 21:44:09 +03:00
value: 1,
2020-10-17 05:22:55 +03:00
gasPrice: parseUnits('0.5', 'gwei').toHexString(),
2020-10-16 21:44:09 +03:00
to: '0xA43Ce8Cc89Eff3AA5593c742fC56A30Ef2427CB0',
}
const tx3 = {
2020-10-01 07:56:47 +03:00
value: 2,
to: '0x0039F22efB07A647557C7C5d17854CFD6D489eF3',
}
2020-10-20 09:39:28 +03:00
const tx4 = {
value: 1,
to: '0xA43Ce8Cc89Eff3AA5593c742fC56A30Ef2427CB0',
}
2021-09-02 09:25:23 +03:00
const tx5 = {
value: 1,
to: '0xA43Ce8Cc89Eff3AA5593c742fC56A30Ef2427CB0',
maxFeePerGas: parseUnits('7', 'gwei').toHexString(),
maxPriorityFeePerGas: parseUnits('1', 'gwei').toHexString(),
type: 2,
}
2022-05-11 16:24:39 +03:00
before(async () => {
const provider = new providers.JsonRpcProvider(RPC_URL)
const { name, chainId } = await provider.getNetwork()
console.log('\n\n', 'network', { name, chainId }, '\n\n')
manager = new TxManager({
privateKey: PRIVATE_KEY,
rpcUrl: RPC_URL,
config: {
CONFIRMATIONS: 1,
GAS_BUMP_INTERVAL: 1000 * 20,
},
gasPriceOracleConfig: {
chainId: chainId,
defaultRpc: RPC_URL,
},
})
})
2020-10-01 07:56:47 +03:00
describe('#transaction', () => {
2021-09-02 09:25:23 +03:00
it('should work legacy tx', async () => {
2020-10-01 07:56:47 +03:00
const tx = manager.createTx(tx1)
2020-10-02 12:14:40 +03:00
const receipt = await tx
.send()
2020-10-02 12:55:44 +03:00
.on('transactionHash', hash => console.log('hash', hash))
.on('mined', receipt => console.log('Mined in block', receipt.blockNumber))
.on('confirmations', confirmations => console.log('confirmations', confirmations))
2020-10-01 07:56:47 +03:00
console.log('receipt', receipt)
})
2021-09-02 09:25:23 +03:00
it('should work eip-1559 tx', async () => {
const tx = 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)
})
it('should fetch gas params', async () => {
2020-10-20 09:39:28 +03:00
const tx = 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)
})
2021-09-02 09:25:23 +03:00
it('should bump gas params', async () => {
2020-10-16 21:44:09 +03:00
const tx = 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)
})
2020-10-01 07:56:47 +03:00
it('should cancel', async () => {
2020-10-16 21:44:09 +03:00
const tx = manager.createTx(tx2)
2020-10-01 07:56:47 +03:00
setTimeout(() => tx.cancel(), 1000)
2020-10-02 12:14:40 +03:00
const receipt = await tx
.send()
2020-10-02 12:55:44 +03:00
.on('transactionHash', hash => console.log('hash', hash))
.on('mined', receipt => console.log('Mined in block', receipt.blockNumber))
.on('confirmations', confirmations => console.log('confirmations', confirmations))
2020-10-01 07:56:47 +03:00
console.log('receipt', receipt)
})
it('should replace', async () => {
2020-10-16 21:44:09 +03:00
const tx = manager.createTx(tx2)
2020-10-01 07:56:47 +03:00
2020-10-16 21:44:09 +03:00
setTimeout(() => tx.replace(tx3), 1000)
2020-10-01 07:56:47 +03:00
2020-10-02 12:14:40 +03:00
const receipt = await tx
.send()
2020-10-02 12:55:44 +03:00
.on('transactionHash', hash => console.log('hash', hash))
.on('mined', receipt => console.log('Mined in block', receipt.blockNumber))
.on('confirmations', confirmations => console.log('confirmations', confirmations))
2020-10-01 07:56:47 +03:00
console.log('receipt', receipt)
})
2021-09-02 09:25:23 +03:00
it('should increase nonce', async () => {
const currentNonce = await manager._wallet.getTransactionCount('latest')
manager._nonce = currentNonce - 1
const tx = 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)
})
2022-05-11 16:24:39 +03:00
it('should disable eip-1559 transactions', async () => {
manager.config.ENABLE_EIP1559 = false
const tx = 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)
manager.config.ENABLE_EIP1559 = true
})
2021-09-02 09:25:23 +03:00
it('should send multiple txs', async () => {
2021-02-17 09:08:12 +03:00
const genTx = value => ({
value,
to: '0x0039F22efB07A647557C7C5d17854CFD6D489eF3',
})
await Promise.all([
manager.createTx(genTx(1)).send(),
manager.createTx(genTx(2)).send(),
manager.createTx(genTx(3)).send(),
manager.createTx(genTx(4)).send(),
manager.createTx(genTx(5)).send(),
manager.createTx(genTx(6)).send(),
manager.createTx(genTx(7)).send(),
manager.createTx(genTx(8)).send(),
manager.createTx(genTx(9)).send(),
manager.createTx(genTx(10)).send(),
])
}).timeout(600000)
2020-10-01 07:56:47 +03:00
})
})