This commit is contained in:
Alexey 2020-10-02 12:14:40 +03:00
parent 0df2926992
commit df118be318
5 changed files with 24 additions and 15 deletions

7
.prettierrc Normal file

@ -0,0 +1,7 @@
{
"semi": false,
"arrowParens": "always",
"singleQuote": true,
"printWidth": 110,
"trailingComma": "all"
}

@ -3,5 +3,5 @@ const Transaction = require('./src/Transaction')
module.exports = {
TxManager,
Transaction
Transaction,
}

@ -14,9 +14,7 @@ const gasPriceErrors = [
/Returned error: Transaction gas price \d+wei is too low. There is another transaction with same nonce in the queue with gas price: \d+wei. Try increasing the gas price or incrementing the nonce./,
]
const sameTxErrors = [
'Returned error: Transaction with the same hash was already imported.',
]
const sameTxErrors = ['Returned error: Transaction with the same hash was already imported.']
class Transaction {
constructor(tx, manager) {
@ -41,9 +39,7 @@ class Transaction {
throw new Error('The transaction was already executed')
}
this.executed = true
this._execute()
.then(this._promise.resolve)
.catch(this._promise.reject)
this._execute().then(this._promise.resolve).catch(this._promise.reject)
return this._emitter
}
@ -184,7 +180,7 @@ class Transaction {
}
// Tx is still pending
if (await this._getLastNonce() <= this.tx.nonce) {
if ((await this._getLastNonce()) <= this.tx.nonce) {
// todo optionally run estimateGas on each iteration and cancel the transaction if it fails
// We were waiting too long, increase gas price and resubmit
@ -206,7 +202,7 @@ class Transaction {
// There is a mined tx with current nonce, but it's not one of ours
// Probably other tx submitted by other process/client
if (!receipt) {
console.log('Can\'t find our transaction receipt, retrying a few times')
console.log("Can't find our transaction receipt, retrying a few times")
// Give node a few more attempts to respond with our receipt
let retries = 5
while (!receipt && retries--) {
@ -217,7 +213,9 @@ class Transaction {
// Receipt was not found after a few retries
// Resubmit our tx
if (!receipt) {
console.log('There is a mined tx with our nonce but unknown tx hash, resubmitting with tx with increased nonce')
console.log(
'There is a mined tx with our nonce but unknown tx hash, resubmitting with tx with increased nonce',
)
this.tx.nonce++
// todo drop gas price to original value?
await this._send()
@ -293,7 +291,7 @@ class Transaction {
* @private
*/
_hasError(message, errors) {
return errors.find(e => typeof e === 'string' ? e === message : message.match(e)) !== undefined
return errors.find((e) => (typeof e === 'string' ? e === message : message.match(e))) !== undefined
}
_increaseGasPrice() {

@ -6,7 +6,8 @@ const sleep = (ms) => new Promise((res) => setTimeout(res, ms))
/**
* A promise that resolves when the source emits specified event
*/
const when = (source, event) => new Promise((resolve, reject) => source.once(event, resolve).on('error', reject))
const when = (source, event) =>
new Promise((resolve, reject) => source.once(event, resolve).on('error', reject))
module.exports = {
sleep,

@ -30,7 +30,8 @@ describe('TxManager', () => {
it('should work', async () => {
const tx = manager.createTx(tx1)
const receipt = await tx.send()
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))
@ -43,7 +44,8 @@ describe('TxManager', () => {
setTimeout(() => tx.cancel(), 1000)
const receipt = await tx.send()
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))
@ -56,7 +58,8 @@ describe('TxManager', () => {
setTimeout(() => tx.replace(tx2), 1000)
const receipt = await tx.send()
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))