This commit is contained in:
Alexey 2020-09-29 23:13:45 +03:00
parent b22311e2c6
commit 6367bad194
3 changed files with 45 additions and 18 deletions

@ -35,11 +35,7 @@ class TxManager {
this._web3.eth.defaultAccount = this.address this._web3.eth.defaultAccount = this.address
this._gasPriceOracle = new GasPriceOracle({ defaultRpc: rpcUrl }) this._gasPriceOracle = new GasPriceOracle({ defaultRpc: rpcUrl })
this._mutex = new Mutex() this._mutex = new Mutex()
} this.nonce
// todo get rid of it
async init() {
this.nonce = await this._web3.eth.getTransactionCount(this.address, 'latest')
} }
/** /**
@ -57,6 +53,9 @@ class TxManager {
async _submit(tx, emitter) { async _submit(tx, emitter) {
const release = await this._mutex.acquire() const release = await this._mutex.acquire()
try { try {
if (!this.nonce) {
this.nonce = await this._web3.eth.getTransactionCount(this.address, 'latest')
}
return new Transaction(tx, emitter, this).submit() return new Transaction(tx, emitter, this).submit()
} finally { } finally {
release() release()
@ -85,7 +84,9 @@ class Transaction {
async _prepare() { async _prepare() {
this.tx.gas = await this._web3.eth.estimateGas(this.tx) this.tx.gas = await this._web3.eth.estimateGas(this.tx)
if (!this.tx.gasPrice) {
this.tx.gasPrice = await this._getGasPrice('fast') this.tx.gasPrice = await this._getGasPrice('fast')
}
this.tx.nonce = this.nonce this.tx.nonce = this.nonce
} }
@ -111,13 +112,23 @@ class Transaction {
await sleep(this.config.POLL_INTERVAL) await sleep(this.config.POLL_INTERVAL)
} }
console.log('Mined. Start waiting for confirmations...')
await sleep(5000) // todo
let receipt = await this._getReceipts() 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) { if (!receipt) {
// resubmit // resubmit
} }
console.log('Mined. Start waiting for confirmations...')
this.emitter.emit('mined', receipt)
let currentBlock = await this._web3.eth.getBlockNumber() let currentBlock = await this._web3.eth.getBlockNumber()
let confirmations = currentBlock > receipt.blockNumber ? currentBlock - receipt.blockNumber : 0 let confirmations = currentBlock > receipt.blockNumber ? currentBlock - receipt.blockNumber : 0
while (confirmations <= this.config.CONFIRMATIONS) { while (confirmations <= this.config.CONFIRMATIONS) {

@ -2,7 +2,7 @@ const { instances, netId } = require('../config')
const { poseidon } = require('circomlib') const { poseidon } = require('circomlib')
const { toBN } = require('web3-utils') 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) { function getInstance(address) {
const inst = instances[`netId${netId}`] const inst = instances[`netId${netId}`]
@ -30,7 +30,9 @@ const poseidonHash = (items) => toBN(poseidon(items).toString())
const poseidonHash2 = (a, b) => poseidonHash([a, b]) const poseidonHash2 = (a, b) => poseidonHash([a, b])
function setSafeInterval(func, interval) { function setSafeInterval(func, interval) {
func().catch(console.error).finally(() => { func()
.catch(console.error)
.finally(() => {
setTimeout(() => setSafeInterval(func, interval), interval) setTimeout(() => setSafeInterval(func, interval), interval)
}) })
} }
@ -39,10 +41,14 @@ function setSafeInterval(func, interval) {
* A promise that resolves when the source emits specified event * A promise that resolves when the source emits specified event
*/ */
function when(source, event) { function when(source, event) {
return new Promise(resolve => { return new Promise((resolve, reject) => {
source.once(event, payload => { source
.once(event, (payload) => {
resolve(payload) resolve(payload)
}) })
.on('error', (error) => {
reject(error)
})
}) })
} }

@ -1,28 +1,38 @@
const { toHex, toWei } = require('web3-utils') const { toHex, toWei } = require('web3-utils')
const TxManager = require('./src/TxManager') const TxManager = require('./src/TxManager')
const { rpcUrl, privateKey } = require('./config') const { rpcUrl, privateKey } = require('./config')
const TxM = new TxManager({ const TxM = new TxManager({
privateKey, privateKey,
rpcUrl, rpcUrl,
config: {
CONFIRMATIONS: 2,
GAS_BUMP_INTERVAL: 1000 * 15,
},
}) })
const tx = { const tx = {
from: '0x03Ebd0748Aa4D1457cF479cce56309641e0a98F5', from: '0x03Ebd0748Aa4D1457cF479cce56309641e0a98F5',
value: 0, value: 0,
gasPrice: toHex(toWei('1', 'gwei')), // gasPrice: toHex(toWei('0.1', 'gwei')),
to: '0x03Ebd0748Aa4D1457cF479cce56309641e0a98F5', to: '0x03Ebd0748Aa4D1457cF479cce56309641e0a98F5',
} }
async function main() { async function main() {
await TxM.init()
const receipt = await TxM.submit(tx) const receipt = await TxM.submit(tx)
.on('transactionHash', (hash) => { .on('transactionHash', (hash) => {
console.log('hash', hash) console.log('hash', hash)
}) })
.on('mined', (receipt) => {
console.log('Mined in block', receipt.blockNumber)
})
.on('confirmations', (confirmations) => { .on('confirmations', (confirmations) => {
console.log('confirmations', confirmations) console.log('confirmations', confirmations)
}) })
console.log('receipt', receipt) console.log('receipt', receipt)
// const hash = await when(TxM.submit(tx), 'transactionHash')
// console.log('hash', hash)
} }
main() main()