2020-10-01 07:56:47 +03:00
|
|
|
require('dotenv').config()
|
|
|
|
require('chai').should()
|
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', () => {
|
|
|
|
const manager = new TxManager({
|
|
|
|
privateKey: PRIVATE_KEY,
|
|
|
|
rpcUrl: RPC_URL,
|
|
|
|
config: {
|
2020-12-24 08:39:07 +03:00
|
|
|
CONFIRMATIONS: 1,
|
2020-10-01 07:56:47 +03:00
|
|
|
GAS_BUMP_INTERVAL: 1000 * 15,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const tx1 = {
|
|
|
|
value: 1,
|
2020-10-17 05:22:55 +03:00
|
|
|
gasPrice: parseUnits('1', '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',
|
|
|
|
}
|
|
|
|
|
2020-10-01 07:56:47 +03:00
|
|
|
describe('#transaction', () => {
|
|
|
|
it('should work', async () => {
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2020-10-20 09:39:28 +03:00
|
|
|
it('should fetch gas price', async () => {
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
|
2020-10-16 21:44:09 +03:00
|
|
|
it('should bump gas price', async () => {
|
|
|
|
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)
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|