From 45414ae010ea016bb765543ff7a02d5b07337f98 Mon Sep 17 00:00:00 2001 From: poma Date: Sun, 6 Oct 2019 10:54:48 +0300 Subject: [PATCH] Add refund support --- index.js | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index e85f4bd..65c90e4 100644 --- a/index.js +++ b/index.js @@ -61,15 +61,19 @@ app.post('/relay', async (req, resp) => { } const fee = toBN(publicSignals[4]) + const refund = toBN(publicSignals[5]) const expense = toBN(toWei(gasPrices.fast.toString(), 'gwei')).mul(toBN('1000000')) let desiredFee switch (currency) { case 'eth': { + if (refund !== 0) { + return resp.status(400).json({ error: 'Cannot send refund for eth currency.' }) + } desiredFee = expense break } case 'dai': { - desiredFee = expense.mul(toBN(ethPriceInDai)).div(toBN(10 ** 18)) + desiredFee = expense.add(refund).mul(toBN(ethPriceInDai)).div(toBN(10 ** 18)) break } } @@ -91,8 +95,9 @@ app.post('/relay', async (req, resp) => { if (!isKnownRoot) { return resp.status(400).json({ error: 'The merkle root is too old or invalid.' }) } - const gas = await mixer.methods.withdraw(pi_a, pi_b, pi_c, publicSignals).estimateGas() + const gas = await mixer.methods.withdraw(pi_a, pi_b, pi_c, publicSignals).estimateGas({ value: refund }) const result = mixer.methods.withdraw(pi_a, pi_b, pi_c, publicSignals).send({ + value: refund, gas: numberToHex(gas + 50000), gasPrice: toHex(toWei(gasPrices.fast.toString(), 'gwei')), // TODO: nonce