Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
780df01b43 | ||
|
|
c5e4d76dc5 | ||
|
|
8cb2bb0fbe | ||
|
|
6bb265d3b9 | ||
|
|
b940fad5e0 | ||
|
|
1634e5fb16 | ||
|
|
62bcd2aa95 | ||
|
|
1eec6aa329 |
1
.gitignore
vendored
1
.gitignore
vendored
@@ -1 +1,2 @@
|
||||
node_modules
|
||||
.env
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "tx-manager",
|
||||
"version": "0.2.1",
|
||||
"version": "0.2.8",
|
||||
"description": "",
|
||||
"main": "index.js",
|
||||
"scripts": {
|
||||
@@ -22,9 +22,8 @@
|
||||
],
|
||||
"dependencies": {
|
||||
"async-mutex": "^0.2.4",
|
||||
"bn.js": "^5.1.3",
|
||||
"ethers": "^5.0.17",
|
||||
"gas-price-oracle": "^0.1.5",
|
||||
"gas-price-oracle": "^0.2.0",
|
||||
"web3-core-promievent": "^1.3.0"
|
||||
},
|
||||
"devDependencies": {
|
||||
|
||||
@@ -4,10 +4,10 @@ const BigNumber = ethers.BigNumber
|
||||
const PromiEvent = require('web3-core-promievent')
|
||||
const { sleep, min, max } = require('./utils')
|
||||
|
||||
// prettier-ignore
|
||||
const nonceErrors = [
|
||||
'Transaction nonce is too low. Try incrementing the nonce.',
|
||||
'nonce too low'
|
||||
'nonce too low',
|
||||
'nonce has already been used',
|
||||
]
|
||||
|
||||
const gasPriceErrors = [
|
||||
@@ -154,7 +154,6 @@ class Transaction {
|
||||
|
||||
this._emitter.emit('transactionHash', txHash)
|
||||
console.log(`Broadcasted transaction ${txHash}`)
|
||||
console.log(this.tx)
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -183,6 +182,9 @@ class Transaction {
|
||||
this._emitter.emit('confirmations', confirmations)
|
||||
if (confirmations >= this.config.CONFIRMATIONS) {
|
||||
// Tx is mined and has enough confirmations
|
||||
if (this.config.THROW_ON_REVERT && Number(receipt.status) === 0) {
|
||||
throw new Error('EVM execution failed, so the transaction was reverted.')
|
||||
}
|
||||
return receipt
|
||||
}
|
||||
|
||||
@@ -235,7 +237,6 @@ class Transaction {
|
||||
}
|
||||
}
|
||||
|
||||
console.log('Mined. Start waiting for confirmations...')
|
||||
this._emitter.emit('mined', receipt)
|
||||
this.currentTxHash = receipt.transactionHash
|
||||
}
|
||||
@@ -267,11 +268,8 @@ class Transaction {
|
||||
}
|
||||
|
||||
_handleSendError(e) {
|
||||
console.log('Got error', e)
|
||||
|
||||
if (e.code === 'SERVER_ERROR' && e.error) {
|
||||
const message = e.error.message
|
||||
console.log('Error', e.error.code, e.error.message)
|
||||
if (e.error && e.error.code === 'SERVER_ERROR') {
|
||||
const message = e.error.error.message
|
||||
|
||||
// nonce is too low, trying to increase and resubmit
|
||||
if (this._hasError(message, nonceErrors)) {
|
||||
@@ -341,7 +339,7 @@ class Transaction {
|
||||
const gasPrices = await this._gasPriceOracle.gasPrices()
|
||||
const result = gasPrices[type].toString()
|
||||
console.log(`${type} gas price is now ${result} gwei`)
|
||||
return parseUnits(gasPrices[type], 'gwei').toHexString()
|
||||
return parseUnits(result, 'gwei').toHexString()
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -13,6 +13,7 @@ const defaultConfig = {
|
||||
POLL_INTERVAL: 5000,
|
||||
CONFIRMATIONS: 8,
|
||||
ESTIMATE_GAS: true,
|
||||
THROW_ON_REVERT: true,
|
||||
}
|
||||
|
||||
class TxManager {
|
||||
|
||||
@@ -1,14 +1,11 @@
|
||||
const BN = require('bn.js')
|
||||
const { BigNumber } = require('ethers')
|
||||
|
||||
/**
|
||||
* A promise that resolves after `ms` milliseconds
|
||||
*/
|
||||
const sleep = ms => new Promise(res => setTimeout(res, ms))
|
||||
|
||||
const max = (a, b) => BigNumber.from(BN.max(new BN(a.toString()), new BN(b.toString())).toString())
|
||||
const max = (a, b) => (a.gt(b) ? a : b)
|
||||
|
||||
const min = (a, b) => BigNumber.from(BN.min(new BN(a.toString()), new BN(b.toString())).toString())
|
||||
const min = (a, b) => (a.lt(b) ? a : b)
|
||||
|
||||
module.exports = {
|
||||
sleep,
|
||||
|
||||
@@ -32,6 +32,11 @@ describe('TxManager', () => {
|
||||
to: '0x0039F22efB07A647557C7C5d17854CFD6D489eF3',
|
||||
}
|
||||
|
||||
const tx4 = {
|
||||
value: 1,
|
||||
to: '0xA43Ce8Cc89Eff3AA5593c742fC56A30Ef2427CB0',
|
||||
}
|
||||
|
||||
describe('#transaction', () => {
|
||||
it('should work', async () => {
|
||||
const tx = manager.createTx(tx1)
|
||||
@@ -45,6 +50,18 @@ describe('TxManager', () => {
|
||||
console.log('receipt', receipt)
|
||||
})
|
||||
|
||||
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)
|
||||
})
|
||||
|
||||
it('should bump gas price', async () => {
|
||||
const tx = manager.createTx(tx2)
|
||||
|
||||
|
||||
13
yarn.lock
13
yarn.lock
@@ -593,11 +593,6 @@ bn.js@^4.4.0:
|
||||
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-4.11.9.tgz#26d556829458f9d1e81fc48952493d0ba3507828"
|
||||
integrity sha512-E6QoYqCKZfgatHTdHzs1RRKP7ip4vvm+EyRUeE2RF0NblwVvb0p6jSVeNTOFxPn26QXN2o6SMfNxKp6kU8zQaw==
|
||||
|
||||
bn.js@^5.1.3:
|
||||
version "5.1.3"
|
||||
resolved "https://registry.yarnpkg.com/bn.js/-/bn.js-5.1.3.tgz#beca005408f642ebebea80b042b4d18d2ac0ee6b"
|
||||
integrity sha512-GkTiFpjFtUzU9CbMeJ5iazkCzGL3jrhzerzZIuqLABjbwRaFt33I9tUdSNryIptM+RxDet6OKm2WnLXzW51KsQ==
|
||||
|
||||
brace-expansion@^1.1.7:
|
||||
version "1.1.11"
|
||||
resolved "https://registry.yarnpkg.com/brace-expansion/-/brace-expansion-1.1.11.tgz#3c7fcbf529d87226f3d2f52b966ff5271eb441dd"
|
||||
@@ -1146,10 +1141,10 @@ functional-red-black-tree@^1.0.1:
|
||||
resolved "https://registry.yarnpkg.com/functional-red-black-tree/-/functional-red-black-tree-1.0.1.tgz#1b0ab3bd553b2a0d6399d29c0e3ea0b252078327"
|
||||
integrity sha1-GwqzvVU7Kg1jmdKcDj6gslIHgyc=
|
||||
|
||||
gas-price-oracle@^0.1.5:
|
||||
version "0.1.5"
|
||||
resolved "https://registry.yarnpkg.com/gas-price-oracle/-/gas-price-oracle-0.1.5.tgz#09dd0d9806465c2f5e63b682e6742f96f6eb525c"
|
||||
integrity sha512-fkaTXnxJcSVco/tMPEcN5gieoUNs8O6JYMXflGLN2+3YeGZAucUI0fgCliazM3nRVAk//bBEm9819/Zb83xhrw==
|
||||
gas-price-oracle@^0.2.0:
|
||||
version "0.2.0"
|
||||
resolved "https://registry.yarnpkg.com/gas-price-oracle/-/gas-price-oracle-0.2.0.tgz#981926c96089497115113162b03151aacfe44a5a"
|
||||
integrity sha512-2+mMyunV/pMJrmKl/IeEtX860NaE/bQ7H4D8PO2dc0OQd8ZAj/e4WJ+C9F/uOeG3dwm8SEFjofOvcYRHeGxo/Q==
|
||||
dependencies:
|
||||
axios "^0.19.2"
|
||||
bignumber.js "^9.0.0"
|
||||
|
||||
Reference in New Issue
Block a user