add methods for mining

This commit is contained in:
poma 2020-10-01 09:30:50 +03:00
parent 6b9acb8c6b
commit b1ed87e537
No known key found for this signature in database
GPG Key ID: BA20CB01FE165657
3 changed files with 103 additions and 8 deletions

@ -1,4 +1,4 @@
const { getTornadoWithdrawInputError } = require('./validator')
const { getTornadoWithdrawInputError, getMiningRewardInputError, getMiningWithdrawInputError } = require('./validator')
const { postJob } = require('./queue')
async function tornadoWithdraw(req, res) {
@ -8,14 +8,43 @@ async function tornadoWithdraw(req, res) {
return res.status(400).json({ error: inputError })
}
const { proof, args, contract } = req.body
const id = await postJob({
type: 'tornadoWithdraw',
data: { proof, args, contract },
data: req.body,
})
return res.json({ id })
}
async function miningReward(req, res) {
const inputError = getMiningRewardInputError(req.body)
if (inputError) {
console.log('Invalid input:', inputError)
return res.status(400).json({ error: inputError })
}
const id = await postJob({
type: 'miningReward',
data: req.body,
})
return res.json({ id })
}
async function miningWithdraw(req, res) {
const inputError = getMiningWithdrawInputError(req.body)
if (inputError) {
console.log('Invalid input:', inputError)
return res.status(400).json({ error: inputError })
}
const id = await postJob({
type: 'miningWithdraw',
data: req.body,
})
return res.json({ id })
}
module.exports = {
tornadoWithdraw,
miningReward,
miningWithdraw,
}

@ -30,8 +30,8 @@ app.post('/v1/jobs/:id', status.getJob)
app.post('/v1/tornadoWithdraw', controller.tornadoWithdraw)
app.get('/status', status.status)
app.post('/relay', controller.tornadoWithdraw)
// app.post('/v1/miningReward', controller.miningReward)
// app.post('/v1/miningWithdraw', controller.miningWithdraw)
app.post('/v1/miningReward', controller.miningReward)
app.post('/v1/miningWithdraw', controller.miningWithdraw)
worker.start()
app.listen(port)

@ -5,9 +5,10 @@ const Redis = require('ioredis')
const { GasPriceOracle } = require('gas-price-oracle')
const tornadoABI = require('../abis/tornadoABI.json')
const miningABI = require('../abis/mining.abi.json')
const { queue } = require('./queue')
const { poseidonHash2 } = require('./utils')
const { rpcUrl, redisUrl, privateKey, updateConfig, rewardAccount } = require('../config')
const { rpcUrl, redisUrl, privateKey, updateConfig, rewardAccount, minerAddress } = require('../config')
const TxManager = require('./TxManager')
let web3
@ -45,9 +46,22 @@ async function checkTornadoFee(/* contract, fee, refund*/) {
}
async function process(job) {
if (job.data.type !== 'tornadoWithdraw') {
throw new Error('not implemented')
switch (job.data.type) {
case 'tornadoWithdraw':
await processTornadoWithdraw(job)
break
case 'miningReward':
await processMiningReward(job)
break
case 'miningWithdraw':
await processMiningWithdraw(job)
break
default:
throw new Error(`Unknown job type: ${job.data.type}`)
}
}
async function processTornadoWithdraw(job) {
currentJob = job
console.log(Date.now(), ' withdraw started', job.id)
const { proof, args, contract } = job.data.data
@ -77,6 +91,58 @@ async function process(job) {
}
}
async function processMiningReward(job) {
currentJob = job
console.log(Date.now(), ' reward started', job.id)
const { proof, args } = job.data.data
const contract = new web3.eth.Contract(miningABI, minerAddress)
const data = contract.methods.reward(proof, args).encodeABI()
currentTx = await txManager.createTx({
to: minerAddress,
data,
})
try {
await currentTx
.send()
.on('transactionHash', updateTxHash)
.on('mined', (receipt) => {
console.log('Mined in block', receipt.blockNumber)
})
.on('confirmations', updateConfirmations)
} catch (e) {
console.error('Revert', e)
throw new Error(`Revert by smart contract ${e.message}`)
}
}
async function processMiningWithdraw(job) {
currentJob = job
console.log(Date.now(), ' mining withdraw started', job.id)
const { proof, args } = job.data.data
const contract = new web3.eth.Contract(miningABI, minerAddress)
const data = contract.methods.withdraw(proof, args).encodeABI()
currentTx = await txManager.createTx({
to: minerAddress,
data,
})
try {
await currentTx
.send()
.on('transactionHash', updateTxHash)
.on('mined', (receipt) => {
console.log('Mined in block', receipt.blockNumber)
})
.on('confirmations', updateConfirmations)
} catch (e) {
console.error('Revert', e)
throw new Error(`Revert by smart contract ${e.message}`)
}
}
async function updateTxHash(txHash) {
console.log(`A new successfully sent tx ${txHash}`)
currentJob.data.txHash = txHash