2019-12-12 13:58:58 +03:00
|
|
|
const Queue = require('bull')
|
2019-09-20 00:46:20 +03:00
|
|
|
const { numberToHex, toWei, toHex, toBN, toChecksumAddress } = require('web3-utils')
|
2019-12-03 16:26:16 +03:00
|
|
|
const mixerABI = require('../abis/mixerABI.json')
|
2020-08-04 10:39:56 +03:00
|
|
|
const { isValidProof, isValidArgs, isKnownContract, isEnoughFee } = require('./utils')
|
2019-12-03 16:26:16 +03:00
|
|
|
const config = require('../config')
|
2019-12-12 13:58:58 +03:00
|
|
|
const { redisClient, redisOpts } = require('./redis')
|
2019-07-18 17:45:33 +03:00
|
|
|
|
2020-07-16 16:33:35 +03:00
|
|
|
const { web3, fetcher, sender, gasPriceOracle } = require('./instances')
|
2019-12-12 13:58:58 +03:00
|
|
|
const withdrawQueue = new Queue('withdraw', redisOpts)
|
2019-07-24 20:19:00 +03:00
|
|
|
|
2019-12-12 21:56:58 +03:00
|
|
|
const reponseCbs = {}
|
|
|
|
let respLambda = (job, { msg, status }) => {
|
|
|
|
const resp = reponseCbs[job.id]
|
|
|
|
resp.status(status).json(msg)
|
|
|
|
delete reponseCbs[job.id]
|
|
|
|
}
|
|
|
|
withdrawQueue.on('completed', respLambda)
|
|
|
|
|
2019-12-12 13:58:58 +03:00
|
|
|
async function relayController(req, resp) {
|
|
|
|
let requestJob
|
2020-05-08 20:29:31 +03:00
|
|
|
|
2019-11-14 16:24:01 +03:00
|
|
|
const { proof, args, contract } = req.body
|
2020-07-16 16:33:35 +03:00
|
|
|
let { valid, reason } = isValidProof(proof)
|
2019-07-18 15:43:26 +03:00
|
|
|
if (!valid) {
|
|
|
|
console.log('Proof is invalid:', reason)
|
2019-11-08 04:25:43 +03:00
|
|
|
return resp.status(400).json({ error: 'Proof format is invalid' })
|
2019-07-18 15:43:26 +03:00
|
|
|
}
|
2019-09-20 00:46:20 +03:00
|
|
|
|
2020-08-04 10:39:56 +03:00
|
|
|
// eslint-disable-next-line no-extra-semi
|
|
|
|
;({ valid, reason } = isValidArgs(args))
|
2019-09-20 00:46:20 +03:00
|
|
|
if (!valid) {
|
2019-11-14 16:24:01 +03:00
|
|
|
console.log('Args are invalid:', reason)
|
|
|
|
return resp.status(400).json({ error: 'Withdraw arguments are invalid' })
|
2019-09-20 00:46:20 +03:00
|
|
|
}
|
2019-07-18 15:43:26 +03:00
|
|
|
|
2019-11-26 18:01:37 +03:00
|
|
|
let currency, amount
|
2020-08-04 10:39:56 +03:00
|
|
|
;({ valid, currency, amount } = isKnownContract(contract))
|
2019-11-14 16:24:01 +03:00
|
|
|
if (!valid) {
|
|
|
|
console.log('Contract does not exist:', contract)
|
|
|
|
return resp.status(400).json({ error: 'This relayer does not support the token' })
|
2019-11-08 04:25:43 +03:00
|
|
|
}
|
|
|
|
|
2020-07-16 16:33:35 +03:00
|
|
|
const [root, nullifierHash, recipient, relayer, fee, refund] = [
|
2019-11-14 16:24:01 +03:00
|
|
|
args[0],
|
|
|
|
args[1],
|
|
|
|
toChecksumAddress(args[2]),
|
|
|
|
toChecksumAddress(args[3]),
|
|
|
|
toBN(args[4]),
|
|
|
|
toBN(args[5])
|
|
|
|
]
|
2019-12-12 22:05:49 +03:00
|
|
|
console.log('fee, refund', fee.toString(), refund.toString(), recipient)
|
2019-11-15 12:01:59 +03:00
|
|
|
if (currency === 'eth' && !refund.isZero()) {
|
|
|
|
return resp.status(400).json({ error: 'Cannot send refund for eth currency.' })
|
|
|
|
}
|
|
|
|
|
2019-11-14 16:24:01 +03:00
|
|
|
if (relayer !== web3.eth.defaultAccount) {
|
|
|
|
console.log('This proof is for different relayer:', relayer)
|
2019-09-20 00:46:20 +03:00
|
|
|
return resp.status(400).json({ error: 'Relayer address is invalid' })
|
|
|
|
}
|
2019-07-18 15:43:26 +03:00
|
|
|
|
2020-08-04 10:39:56 +03:00
|
|
|
requestJob = await withdrawQueue.add(
|
|
|
|
{
|
|
|
|
contract,
|
|
|
|
nullifierHash,
|
|
|
|
root,
|
|
|
|
proof,
|
|
|
|
args,
|
|
|
|
currency,
|
|
|
|
amount,
|
|
|
|
fee: fee.toString(),
|
|
|
|
refund: refund.toString()
|
|
|
|
},
|
|
|
|
{ removeOnComplete: true }
|
|
|
|
)
|
2019-12-12 21:56:58 +03:00
|
|
|
reponseCbs[requestJob.id] = resp
|
2019-12-12 13:58:58 +03:00
|
|
|
}
|
|
|
|
|
2020-07-16 16:33:35 +03:00
|
|
|
withdrawQueue.process(async function (job, done) {
|
2019-12-12 13:58:58 +03:00
|
|
|
console.log(Date.now(), ' withdraw started', job.id)
|
2020-07-16 16:33:35 +03:00
|
|
|
const gasPrices = await gasPriceOracle.gasPrices()
|
2019-12-12 21:56:58 +03:00
|
|
|
const { contract, nullifierHash, root, proof, args, refund, currency, amount, fee } = job.data
|
2019-12-17 02:48:51 +03:00
|
|
|
console.log(JSON.stringify(job.data))
|
2019-12-12 13:58:58 +03:00
|
|
|
// job.data contains the custom data passed when the job was created
|
|
|
|
// job.id contains id of this job.
|
2019-07-18 15:43:26 +03:00
|
|
|
try {
|
2019-12-12 13:58:58 +03:00
|
|
|
const mixer = new web3.eth.Contract(mixerABI, contract)
|
2019-11-14 16:24:01 +03:00
|
|
|
const isSpent = await mixer.methods.isSpent(nullifierHash).call()
|
2019-07-18 15:43:26 +03:00
|
|
|
if (isSpent) {
|
2019-12-12 13:58:58 +03:00
|
|
|
done(null, {
|
|
|
|
status: 400,
|
|
|
|
msg: {
|
|
|
|
error: 'The note has been spent.'
|
|
|
|
}
|
|
|
|
})
|
2019-12-19 06:36:41 +03:00
|
|
|
return
|
2019-07-18 15:43:26 +03:00
|
|
|
}
|
2019-11-14 16:24:01 +03:00
|
|
|
const isKnownRoot = await mixer.methods.isKnownRoot(root).call()
|
2019-07-18 16:30:24 +03:00
|
|
|
if (!isKnownRoot) {
|
2019-12-12 13:58:58 +03:00
|
|
|
done(null, {
|
|
|
|
status: 400,
|
|
|
|
msg: {
|
|
|
|
error: 'The merkle root is too old or invalid.'
|
|
|
|
}
|
|
|
|
})
|
2019-12-19 06:36:41 +03:00
|
|
|
return
|
2019-07-18 16:30:24 +03:00
|
|
|
}
|
2019-11-15 12:01:59 +03:00
|
|
|
|
2019-12-03 16:26:16 +03:00
|
|
|
let gas = await mixer.methods.withdraw(proof, ...args).estimateGas({
|
2019-11-14 16:24:01 +03:00
|
|
|
from: web3.eth.defaultAccount,
|
2020-05-08 20:29:31 +03:00
|
|
|
value: refund
|
2019-11-14 16:24:01 +03:00
|
|
|
})
|
2019-11-15 12:01:59 +03:00
|
|
|
|
|
|
|
gas += 50000
|
2019-11-23 11:18:54 +03:00
|
|
|
const ethPrices = fetcher.ethPrices
|
2020-08-04 10:39:56 +03:00
|
|
|
const { isEnough, reason } = isEnoughFee({
|
|
|
|
gas,
|
|
|
|
gasPrices,
|
|
|
|
currency,
|
|
|
|
amount,
|
|
|
|
refund: toBN(refund),
|
|
|
|
ethPrices,
|
|
|
|
fee: toBN(fee)
|
|
|
|
})
|
2019-11-15 12:01:59 +03:00
|
|
|
if (!isEnough) {
|
|
|
|
console.log(`Wrong fee: ${reason}`)
|
2019-12-12 13:58:58 +03:00
|
|
|
done(null, {
|
|
|
|
status: 400,
|
|
|
|
msg: { error: reason }
|
|
|
|
})
|
2019-12-18 22:06:42 +03:00
|
|
|
return
|
2019-11-15 12:01:59 +03:00
|
|
|
}
|
|
|
|
|
2019-12-03 16:26:16 +03:00
|
|
|
const data = mixer.methods.withdraw(proof, ...args).encodeABI()
|
2019-12-12 13:58:58 +03:00
|
|
|
let nonce = Number(await redisClient.get('nonce'))
|
|
|
|
console.log('nonce', nonce)
|
2019-12-03 16:26:16 +03:00
|
|
|
const tx = {
|
2019-11-10 04:03:46 +03:00
|
|
|
from: web3.eth.defaultAccount,
|
2019-12-03 16:26:16 +03:00
|
|
|
value: numberToHex(refund),
|
2019-11-15 12:01:59 +03:00
|
|
|
gas: numberToHex(gas),
|
2019-07-18 15:43:26 +03:00
|
|
|
gasPrice: toHex(toWei(gasPrices.fast.toString(), 'gwei')),
|
2020-05-08 20:29:31 +03:00
|
|
|
// you can use this gasPrice to test watcher
|
|
|
|
// gasPrice: numberToHex(100000000),
|
2019-12-03 16:26:16 +03:00
|
|
|
to: mixer._address,
|
|
|
|
netId: config.netId,
|
|
|
|
data,
|
2019-12-12 13:58:58 +03:00
|
|
|
nonce
|
2019-12-03 16:26:16 +03:00
|
|
|
}
|
2020-05-08 20:29:31 +03:00
|
|
|
tx.date = Date.now()
|
2020-07-16 16:33:35 +03:00
|
|
|
await redisClient.set('tx:' + nonce, JSON.stringify(tx))
|
2019-12-12 13:58:58 +03:00
|
|
|
nonce += 1
|
|
|
|
await redisClient.set('nonce', nonce)
|
2020-05-08 20:29:31 +03:00
|
|
|
sender.sendTx(tx, done)
|
2019-07-18 15:43:26 +03:00
|
|
|
} catch (e) {
|
2019-12-03 16:26:16 +03:00
|
|
|
console.error(e, 'estimate gas failed')
|
2019-12-12 13:58:58 +03:00
|
|
|
done(null, {
|
|
|
|
status: 400,
|
|
|
|
msg: { error: 'Internal Relayer Error. Please use a different relayer service' }
|
|
|
|
})
|
2019-07-18 15:43:26 +03:00
|
|
|
}
|
2019-12-12 13:58:58 +03:00
|
|
|
})
|
|
|
|
|
2020-05-08 20:29:31 +03:00
|
|
|
module.exports = relayController
|