From f50379a391c0121b986f52492627ef2205cfc063 Mon Sep 17 00:00:00 2001 From: Alexey Date: Fri, 8 May 2020 00:19:09 +0300 Subject: [PATCH] WIP --- src/instances.js | 5 +++- src/relayController.js | 51 ++++++++++++--------------------------- src/sender.js | 54 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 73 insertions(+), 37 deletions(-) create mode 100644 src/sender.js diff --git a/src/instances.js b/src/instances.js index caf6942..f4b35eb 100644 --- a/src/instances.js +++ b/src/instances.js @@ -1,8 +1,11 @@ const Fetcher = require('./Fetcher') +const Sender = require('./Sender') const web3 = require('./setupWeb3') const fetcher = new Fetcher(web3) +const sender = new Sender(1, web3) module.exports = { fetcher, - web3 + web3, + sender } \ No newline at end of file diff --git a/src/relayController.js b/src/relayController.js index c4387f5..50b1063 100644 --- a/src/relayController.js +++ b/src/relayController.js @@ -7,7 +7,7 @@ const { const config = require('../config') const { redisClient, redisOpts } = require('./redis') -const { web3, fetcher } = require('./instances') +const { web3, fetcher, sender } = require('./instances') const withdrawQueue = new Queue('withdraw', redisOpts) const reponseCbs = {} @@ -125,9 +125,22 @@ withdrawQueue.process(async function(job, done){ data, nonce } + await redisClient.set('tx:' + nonce, JSON.stringify(tx)) nonce += 1 await redisClient.set('nonce', nonce) - sendTx(tx, done) + try { + const txHash = await sender.sendTx(tx) + done(null, { + status: 200, + msg: { txHash } + }) + } catch (e) { + console.error('on transactionHash error', e.message) + done(null, { + status: 400, + msg: { error: 'Internal Relayer Error. Please use a different relayer service' } + }) + } } catch (e) { console.error(e, 'estimate gas failed') done(null, { @@ -137,38 +150,4 @@ withdrawQueue.process(async function(job, done){ } }) -async function sendTx(tx, done, retryAttempt = 1) { - - let signedTx = await web3.eth.accounts.signTransaction(tx, config.privateKey) - let result = web3.eth.sendSignedTransaction(signedTx.rawTransaction) - - result.once('transactionHash', function(txHash){ - done(null, { - status: 200, - msg: { txHash } - }) - console.log(`A new successfully sent tx ${txHash}`) - }).on('error', async function(e){ - console.log('error', e.message) - if(e.message === 'Returned error: Transaction gas price supplied is too low. There is another transaction with same nonce in the queue. Try increasing the gas price or incrementing the nonce.' - || e.message === 'Returned error: Transaction nonce is too low. Try incrementing the nonce.' - || e.message === 'Returned error: nonce too low' - || e.message === 'Returned error: replacement transaction underpriced') { - console.log('nonce too low, retrying') - if(retryAttempt <= 10) { - retryAttempt++ - const newNonce = tx.nonce + 1 - tx.nonce = newNonce - await redisClient.set('nonce', newNonce) - sendTx(tx, done, retryAttempt) - return - } - } - console.error('on transactionHash error', e.message) - done(null, { - status: 400, - msg: { error: 'Internal Relayer Error. Please use a different relayer service' } - }) - }) -} module.exports = relayController \ No newline at end of file diff --git a/src/sender.js b/src/sender.js new file mode 100644 index 0000000..1c6ceea --- /dev/null +++ b/src/sender.js @@ -0,0 +1,54 @@ +const { redisClient } = require('./redis') +const config = require('../config') + +class Sender { + constructor(minedNonce, web3) { + this.minedNonce = Number(minedNonce) + this.web3 = web3 + } + + async main() { + const lastNonce = await redisClient.get('nonce') + for(let nonce = this.minedNonce; nonce < lastNonce + 1; nonce++) { + let tx = await redisClient.get('tx' + nonce) + tx = JSON.parse(tx) + const isMined = await this.checkTx(tx) + + } + } + + async checkTx(tx) { + const networkNonce = await this.web3.eth.getTransactionCount(this.web3.eth.defaultAccount) + if () + } + + async sendTx(tx, retryAttempt = 1) { + let signedTx = await this.web3.eth.accounts.signTransaction(tx, config.privateKey) + let result = this.web3.eth.sendSignedTransaction(signedTx.rawTransaction) + let txHash + result.once('transactionHash', function(_txHash){ + console.log(`A new successfully sent tx ${_txHash}`) + txHash = _txHash + }).on('error', async function(e){ + console.log('error', e.message) + if(e.message === 'Returned error: Transaction gas price supplied is too low. There is another transaction with same nonce in the queue. Try increasing the gas price or incrementing the nonce.' + || e.message === 'Returned error: Transaction nonce is too low. Try incrementing the nonce.' + || e.message === 'Returned error: nonce too low' + || e.message === 'Returned error: replacement transaction underpriced') { + console.log('nonce too low, retrying') + if(retryAttempt <= 10) { + retryAttempt++ + const newNonce = tx.nonce + 1 + tx.nonce = newNonce + await redisClient.set('nonce', newNonce) + txHash = this.sendTx(tx, retryAttempt) + return + } + } + throw new Error(e.message) + }) + return txHash + } +} + +module.exports = Sender \ No newline at end of file