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 = { module.exports = {
TxManager, 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./, /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 = [ const sameTxErrors = ['Returned error: Transaction with the same hash was already imported.']
'Returned error: Transaction with the same hash was already imported.',
]
class Transaction { class Transaction {
constructor(tx, manager) { constructor(tx, manager) {
@ -41,9 +39,7 @@ class Transaction {
throw new Error('The transaction was already executed') throw new Error('The transaction was already executed')
} }
this.executed = true this.executed = true
this._execute() this._execute().then(this._promise.resolve).catch(this._promise.reject)
.then(this._promise.resolve)
.catch(this._promise.reject)
return this._emitter return this._emitter
} }
@ -184,7 +180,7 @@ class Transaction {
} }
// Tx is still pending // 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 // todo optionally run estimateGas on each iteration and cancel the transaction if it fails
// We were waiting too long, increase gas price and resubmit // 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 // There is a mined tx with current nonce, but it's not one of ours
// Probably other tx submitted by other process/client // Probably other tx submitted by other process/client
if (!receipt) { 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 // Give node a few more attempts to respond with our receipt
let retries = 5 let retries = 5
while (!receipt && retries--) { while (!receipt && retries--) {
@ -217,7 +213,9 @@ class Transaction {
// Receipt was not found after a few retries // Receipt was not found after a few retries
// Resubmit our tx // Resubmit our tx
if (!receipt) { 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++ this.tx.nonce++
// todo drop gas price to original value? // todo drop gas price to original value?
await this._send() await this._send()
@ -293,7 +291,7 @@ class Transaction {
* @private * @private
*/ */
_hasError(message, errors) { _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() { _increaseGasPrice() {

@ -6,7 +6,8 @@ const sleep = (ms) => new Promise((res) => setTimeout(res, ms))
/** /**
* A promise that resolves when the source emits specified event * 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 = { module.exports = {
sleep, sleep,

@ -30,7 +30,8 @@ describe('TxManager', () => {
it('should work', async () => { it('should work', async () => {
const tx = manager.createTx(tx1) const tx = manager.createTx(tx1)
const receipt = await tx.send() const receipt = await tx
.send()
.on('transactionHash', (hash) => console.log('hash', hash)) .on('transactionHash', (hash) => console.log('hash', hash))
.on('mined', (receipt) => console.log('Mined in block', receipt.blockNumber)) .on('mined', (receipt) => console.log('Mined in block', receipt.blockNumber))
.on('confirmations', (confirmations) => console.log('confirmations', confirmations)) .on('confirmations', (confirmations) => console.log('confirmations', confirmations))
@ -43,7 +44,8 @@ describe('TxManager', () => {
setTimeout(() => tx.cancel(), 1000) setTimeout(() => tx.cancel(), 1000)
const receipt = await tx.send() const receipt = await tx
.send()
.on('transactionHash', (hash) => console.log('hash', hash)) .on('transactionHash', (hash) => console.log('hash', hash))
.on('mined', (receipt) => console.log('Mined in block', receipt.blockNumber)) .on('mined', (receipt) => console.log('Mined in block', receipt.blockNumber))
.on('confirmations', (confirmations) => console.log('confirmations', confirmations)) .on('confirmations', (confirmations) => console.log('confirmations', confirmations))
@ -56,7 +58,8 @@ describe('TxManager', () => {
setTimeout(() => tx.replace(tx2), 1000) setTimeout(() => tx.replace(tx2), 1000)
const receipt = await tx.send() const receipt = await tx
.send()
.on('transactionHash', (hash) => console.log('hash', hash)) .on('transactionHash', (hash) => console.log('hash', hash))
.on('mined', (receipt) => console.log('Mined in block', receipt.blockNumber)) .on('mined', (receipt) => console.log('Mined in block', receipt.blockNumber))
.on('confirmations', (confirmations) => console.log('confirmations', confirmations)) .on('confirmations', (confirmations) => console.log('confirmations', confirmations))