From 6367bad194123e54c3d07ef4ce889b51643d2edc Mon Sep 17 00:00:00 2001 From: Alexey Date: Tue, 29 Sep 2020 23:13:45 +0300 Subject: [PATCH] update --- src/TxManager.js | 27 +++++++++++++++++++-------- src/utils.js | 22 ++++++++++++++-------- testTxManager.js | 14 ++++++++++++-- 3 files changed, 45 insertions(+), 18 deletions(-) diff --git a/src/TxManager.js b/src/TxManager.js index 330abe7..34dd466 100644 --- a/src/TxManager.js +++ b/src/TxManager.js @@ -35,11 +35,7 @@ class TxManager { this._web3.eth.defaultAccount = this.address this._gasPriceOracle = new GasPriceOracle({ defaultRpc: rpcUrl }) this._mutex = new Mutex() - } - - // todo get rid of it - async init() { - this.nonce = await this._web3.eth.getTransactionCount(this.address, 'latest') + this.nonce } /** @@ -57,6 +53,9 @@ class TxManager { async _submit(tx, emitter) { const release = await this._mutex.acquire() try { + if (!this.nonce) { + this.nonce = await this._web3.eth.getTransactionCount(this.address, 'latest') + } return new Transaction(tx, emitter, this).submit() } finally { release() @@ -85,7 +84,9 @@ class Transaction { async _prepare() { this.tx.gas = await this._web3.eth.estimateGas(this.tx) - this.tx.gasPrice = await this._getGasPrice('fast') + if (!this.tx.gasPrice) { + this.tx.gasPrice = await this._getGasPrice('fast') + } this.tx.nonce = this.nonce } @@ -111,13 +112,23 @@ class Transaction { await sleep(this.config.POLL_INTERVAL) } - console.log('Mined. Start waiting for confirmations...') - await sleep(5000) // todo + let receipt = await this._getReceipts() + let retryAttempt = 5 + while (retryAttempt >= 0 && !receipt) { + console.log('retryAttempt', retryAttempt) + await sleep(1000) + receipt = await this._getReceipts() + retryAttempt-- + } + if (!receipt) { // resubmit } + console.log('Mined. Start waiting for confirmations...') + this.emitter.emit('mined', receipt) + let currentBlock = await this._web3.eth.getBlockNumber() let confirmations = currentBlock > receipt.blockNumber ? currentBlock - receipt.blockNumber : 0 while (confirmations <= this.config.CONFIRMATIONS) { diff --git a/src/utils.js b/src/utils.js index 9cf5645..950fa41 100644 --- a/src/utils.js +++ b/src/utils.js @@ -2,7 +2,7 @@ const { instances, netId } = require('../config') const { poseidon } = require('circomlib') const { toBN } = require('web3-utils') -const sleep = (ms) => new Promise(res => setTimeout(res, ms)) +const sleep = (ms) => new Promise((res) => setTimeout(res, ms)) function getInstance(address) { const inst = instances[`netId${netId}`] @@ -30,19 +30,25 @@ const poseidonHash = (items) => toBN(poseidon(items).toString()) const poseidonHash2 = (a, b) => poseidonHash([a, b]) function setSafeInterval(func, interval) { - func().catch(console.error).finally(() => { - setTimeout(() => setSafeInterval(func, interval), interval) - }) + func() + .catch(console.error) + .finally(() => { + setTimeout(() => setSafeInterval(func, interval), interval) + }) } /** * A promise that resolves when the source emits specified event */ function when(source, event) { - return new Promise(resolve => { - source.once(event, payload => { - resolve(payload) - }) + return new Promise((resolve, reject) => { + source + .once(event, (payload) => { + resolve(payload) + }) + .on('error', (error) => { + reject(error) + }) }) } diff --git a/testTxManager.js b/testTxManager.js index 9474534..8e9d0ef 100644 --- a/testTxManager.js +++ b/testTxManager.js @@ -1,28 +1,38 @@ const { toHex, toWei } = require('web3-utils') const TxManager = require('./src/TxManager') const { rpcUrl, privateKey } = require('./config') + const TxM = new TxManager({ privateKey, rpcUrl, + config: { + CONFIRMATIONS: 2, + GAS_BUMP_INTERVAL: 1000 * 15, + }, }) const tx = { from: '0x03Ebd0748Aa4D1457cF479cce56309641e0a98F5', value: 0, - gasPrice: toHex(toWei('1', 'gwei')), + // gasPrice: toHex(toWei('0.1', 'gwei')), to: '0x03Ebd0748Aa4D1457cF479cce56309641e0a98F5', } async function main() { - await TxM.init() const receipt = await TxM.submit(tx) .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) + + // const hash = await when(TxM.submit(tx), 'transactionHash') + // console.log('hash', hash) } main()