multiple contracts

This commit is contained in:
Alexey 2019-09-19 22:56:45 +03:00
parent 2d2115ab43
commit cd9f19e1f5
5 changed files with 44 additions and 13 deletions

View File

@ -1,4 +1,5 @@
NET_ID=42 NET_ID=42
RPC_URL=https://kovan.infura.io/v3/a3f4d001c1fc4a359ea70dd27fd9cb51 RPC_URL=https://kovan.infura.io/v3/a3f4d001c1fc4a359ea70dd27fd9cb51
PRIVATE_KEY= PRIVATE_KEY=
MIXER_ADDRESS=0xb2aD997a43768aB9279Cd9E72D5B75D789a09011 ETH_MIXER_ADDRESS=0x1Cea940cA15a303A0E01B7F8589F39fF34308DB2
DAI_MIXER_ADDRESS=0x7ed3fC8042e18db889A0466F49c438bB1410b3c7

View File

@ -16,7 +16,8 @@
"rules": { "rules": {
"indent": [ "indent": [
"error", "error",
2 2,
{"SwitchCase": 1}
], ],
"linebreak-style": [ "linebreak-style": [
"error", "error",

View File

@ -4,7 +4,14 @@ module.exports = {
netId: process.env.NET_ID || 42, netId: process.env.NET_ID || 42,
rpcUrl: process.env.RPC_URL || 'https://kovan.infura.io/v3/a3f4d001c1fc4a359ea70dd27fd9cb51', rpcUrl: process.env.RPC_URL || 'https://kovan.infura.io/v3/a3f4d001c1fc4a359ea70dd27fd9cb51',
privateKey: process.env.PRIVATE_KEY, privateKey: process.env.PRIVATE_KEY,
mixerAddress: process.env.MIXER_ADDRESS, mixers: [ {
address: process.env.ETH_MIXER_ADDRESS,
currency: 'eth'
},
{
address: process.env.DAI_MIXER_ADDRESS,
currency: 'dai'
} ],
defaultGasPrice: 2, defaultGasPrice: 2,
gasOracleUrls: ['https://www.etherchain.org/api/gasPriceOracle', 'https://gasprice.poa.network/'], gasOracleUrls: ['https://www.etherchain.org/api/gasPriceOracle', 'https://gasprice.poa.network/'],
ethdaiAddress: '0x7Ef645705cb7D401C5CD91a395cfcc3Db3C93689' ethdaiAddress: '0x7Ef645705cb7D401C5CD91a395cfcc3Db3C93689'

View File

@ -20,8 +20,8 @@ app.use(function(req, res, next) {
next() next()
}) })
const { netId, rpcUrl, privateKey, mixerAddress, defaultGasPrice } = require('./config') const { netId, rpcUrl, privateKey, mixers, defaultGasPrice } = require('./config')
const { fetchGasPrice, isValidProof, fetchDAIprice } = require('./utils') const { fetchGasPrice, isValidProof, fetchDAIprice, isKnownContract } = require('./utils')
const web3 = new Web3(rpcUrl, null, { transactionConfirmationBlocks: 1 }) const web3 = new Web3(rpcUrl, null, { transactionConfirmationBlocks: 1 })
const account = web3.eth.accounts.privateKeyToAccount('0x' + privateKey) const account = web3.eth.accounts.privateKeyToAccount('0x' + privateKey)
@ -29,33 +29,46 @@ web3.eth.accounts.wallet.add('0x' + privateKey)
web3.eth.defaultAccount = account.address web3.eth.defaultAccount = account.address
const mixerABI = require('./abis/mixerABI.json') const mixerABI = require('./abis/mixerABI.json')
const mixer = new web3.eth.Contract(mixerABI, mixerAddress)
const gasPrices = { fast: defaultGasPrice } const gasPrices = { fast: defaultGasPrice }
const ethPriceInDai = toWei('200') const ethPriceInDai = toWei('200')
app.get('/', function (req, res) { app.get('/', function (req, res) {
// just for testing purposes // just for testing purposes
res.send(`Tornado mixer relayer. Gas Price is ${JSON.stringify(gasPrices)}. Mixer address is ${mixerAddress}`) res.send(`Tornado mixer relayer. Gas Price is ${JSON.stringify(gasPrices)}. Mixer addresses are ${mixers}`)
}) })
app.post('/relay', async (req, resp) => { app.post('/relay', async (req, resp) => {
console.log(JSON.stringify(req.body, null, 2)) let { valid , reason } = isValidProof(req.body.proof)
const { valid , reason } = isValidProof(req.body)
if (!valid) { if (!valid) {
console.log('Proof is invalid:', reason) console.log('Proof is invalid:', reason)
return resp.status(400).json({ error: 'Proof is invalid' }) return resp.status(400).json({ error: 'Proof is invalid' })
} }
let currency
( { valid, currency } = isKnownContract(req.body.contract))
let { pi_a, pi_b, pi_c, publicSignals } = req.body let { pi_a, pi_b, pi_c, publicSignals } = req.body.proof
const fee = toBN(publicSignals[3]) const fee = toBN(publicSignals[3])
const desiredFee = toBN(toWei(gasPrices.fast.toString(), 'gwei')).mul(toBN('1000000')) const expense = toBN(toWei(gasPrices.fast.toString(), 'gwei')).mul(toBN('1000000'))
let desiredFee
switch (currency) {
case 'eth': {
desiredFee = expense
break
}
case 'dai': {
desiredFee = expense.mul(toBN(ethPriceInDai)).div(toBN(10 ** 18))
break
}
}
if (fee.lt(desiredFee)) { if (fee.lt(desiredFee)) {
console.log('Fee is too low') console.log('Fee is too low')
return resp.status(400).json({ error: 'Fee is too low. Try to resend.' }) return resp.status(400).json({ error: 'Fee is too low. Try to resend.' })
} }
try { try {
const mixer = new web3.eth.Contract(mixerABI, req.body.contract)
const nullifier = publicSignals[1] const nullifier = publicSignals[1]
const isSpent = await mixer.methods.isSpent(nullifier).call() const isSpent = await mixer.methods.isSpent(nullifier).call()
if (isSpent) { if (isSpent) {

View File

@ -1,6 +1,6 @@
const fetch = require('node-fetch') const fetch = require('node-fetch')
const { isHexStrict, hexToNumberString } = require('web3-utils') const { isHexStrict, hexToNumberString } = require('web3-utils')
const { gasOracleUrls, ethdaiAddress } = require('./config') const { gasOracleUrls, ethdaiAddress, mixers } = require('./config')
const oracleABI = require('./abis/ETHDAIOracle.json') const oracleABI = require('./abis/ETHDAIOracle.json')
async function fetchGasPrice({ gasPrices, oracleIndex = 0 }) { async function fetchGasPrice({ gasPrices, oracleIndex = 0 }) {
@ -90,8 +90,17 @@ function isValidProof(proof) {
return { valid: true } return { valid: true }
} }
function isKnownContract(contract) {
for (let i = 0; i < mixers.length; i++) {
if (mixers[i].address === contract) {
return { valid: true, currency: mixers[i].currency }
}
}
return { valid: false }
}
function sleep(ms) { function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms)) return new Promise(resolve => setTimeout(resolve, ms))
} }
module.exports = { fetchGasPrice, isValidProof, sleep, fetchDAIprice } module.exports = { fetchGasPrice, isValidProof, sleep, fetchDAIprice, isKnownContract }