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

View File

@ -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) {

View File

@ -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)
})
})
}

View File

@ -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()