add methods for mining
This commit is contained in:
parent
6b9acb8c6b
commit
b1ed87e537
@ -1,4 +1,4 @@
|
|||||||
const { getTornadoWithdrawInputError } = require('./validator')
|
const { getTornadoWithdrawInputError, getMiningRewardInputError, getMiningWithdrawInputError } = require('./validator')
|
||||||
const { postJob } = require('./queue')
|
const { postJob } = require('./queue')
|
||||||
|
|
||||||
async function tornadoWithdraw(req, res) {
|
async function tornadoWithdraw(req, res) {
|
||||||
@ -8,14 +8,43 @@ async function tornadoWithdraw(req, res) {
|
|||||||
return res.status(400).json({ error: inputError })
|
return res.status(400).json({ error: inputError })
|
||||||
}
|
}
|
||||||
|
|
||||||
const { proof, args, contract } = req.body
|
|
||||||
const id = await postJob({
|
const id = await postJob({
|
||||||
type: 'tornadoWithdraw',
|
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 })
|
return res.json({ id })
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
tornadoWithdraw,
|
tornadoWithdraw,
|
||||||
|
miningReward,
|
||||||
|
miningWithdraw,
|
||||||
}
|
}
|
||||||
|
@ -30,8 +30,8 @@ app.post('/v1/jobs/:id', status.getJob)
|
|||||||
app.post('/v1/tornadoWithdraw', controller.tornadoWithdraw)
|
app.post('/v1/tornadoWithdraw', controller.tornadoWithdraw)
|
||||||
app.get('/status', status.status)
|
app.get('/status', status.status)
|
||||||
app.post('/relay', controller.tornadoWithdraw)
|
app.post('/relay', controller.tornadoWithdraw)
|
||||||
// app.post('/v1/miningReward', controller.miningReward)
|
app.post('/v1/miningReward', controller.miningReward)
|
||||||
// app.post('/v1/miningWithdraw', controller.miningWithdraw)
|
app.post('/v1/miningWithdraw', controller.miningWithdraw)
|
||||||
|
|
||||||
worker.start()
|
worker.start()
|
||||||
app.listen(port)
|
app.listen(port)
|
||||||
|
@ -5,9 +5,10 @@ const Redis = require('ioredis')
|
|||||||
const { GasPriceOracle } = require('gas-price-oracle')
|
const { GasPriceOracle } = require('gas-price-oracle')
|
||||||
|
|
||||||
const tornadoABI = require('../abis/tornadoABI.json')
|
const tornadoABI = require('../abis/tornadoABI.json')
|
||||||
|
const miningABI = require('../abis/mining.abi.json')
|
||||||
const { queue } = require('./queue')
|
const { queue } = require('./queue')
|
||||||
const { poseidonHash2 } = require('./utils')
|
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')
|
const TxManager = require('./TxManager')
|
||||||
|
|
||||||
let web3
|
let web3
|
||||||
@ -45,9 +46,22 @@ async function checkTornadoFee(/* contract, fee, refund*/) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
async function process(job) {
|
async function process(job) {
|
||||||
if (job.data.type !== 'tornadoWithdraw') {
|
switch (job.data.type) {
|
||||||
throw new Error('not implemented')
|
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
|
currentJob = job
|
||||||
console.log(Date.now(), ' withdraw started', job.id)
|
console.log(Date.now(), ' withdraw started', job.id)
|
||||||
const { proof, args, contract } = job.data.data
|
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) {
|
async function updateTxHash(txHash) {
|
||||||
console.log(`A new successfully sent tx ${txHash}`)
|
console.log(`A new successfully sent tx ${txHash}`)
|
||||||
currentJob.data.txHash = txHash
|
currentJob.data.txHash = txHash
|
||||||
|
Loading…
Reference in New Issue
Block a user