tornado-relayer/src/utils.js

167 lines
4.3 KiB
JavaScript
Raw Normal View History

2019-12-18 20:25:09 +01:00
const { isHexStrict, toBN, toWei, BN } = require('web3-utils')
2019-12-02 13:49:24 +01:00
const { netId, mixers, relayerServiceFee } = require('../config')
2019-09-19 17:21:57 +02:00
2019-11-14 14:24:01 +01:00
function isValidProof(proof) {
2019-07-18 14:43:26 +02:00
// validator expects `websnarkUtils.toSolidityInput(proof)` output
2019-11-14 14:24:01 +01:00
if (!(proof)) {
return { valid: false, reason: 'The proof is empty.' }
2019-07-18 14:43:26 +02:00
}
2019-11-14 14:24:01 +01:00
if (!isHexStrict(proof) || proof.length !== 2 + 2 * 8 * 32) {
2019-10-06 09:55:24 +02:00
return { valid: false, reason: 'Corrupted proof' }
2019-07-18 14:43:26 +02:00
}
2019-11-14 14:24:01 +01:00
return { valid: true }
}
function isValidArgs(args) {
if (!(args)) {
return { valid: false, reason: 'Args are empty' }
}
if (args.length !== 6) {
return { valid: false, reason: 'Length of args is lower than 6' }
2019-07-18 14:43:26 +02:00
}
2019-11-14 14:24:01 +01:00
for(let signal of args) {
2019-11-08 02:25:43 +01:00
if (!isHexStrict(signal)) {
2019-11-14 14:24:01 +01:00
return { valid: false, reason: `Corrupted signal ${signal}` }
2019-07-18 14:43:26 +02:00
}
}
2019-11-08 02:25:43 +01:00
2019-11-14 14:24:01 +01:00
if (args[0].length !== 66 ||
args[1].length !== 66 ||
args[2].length !== 42 ||
args[3].length !== 42 ||
args[4].length !== 66 ||
args[5].length !== 66) {
return { valid: false, reason: 'The length one of the signals is incorrect' }
2019-11-08 02:25:43 +01:00
}
2019-07-18 14:43:26 +02:00
return { valid: true }
}
2019-09-19 21:56:45 +02:00
function isKnownContract(contract) {
2019-12-02 13:49:24 +01:00
const mixers = getMixers()
for (let currency of Object.keys(mixers)) {
for (let amount of Object.keys(mixers[currency].mixerAddress)) {
if (mixers[currency].mixerAddress[amount] === contract) {
return { valid: true, currency, amount }
}
2019-09-19 21:56:45 +02:00
}
}
return { valid: false }
}
2019-07-23 18:43:24 +02:00
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms))
}
2019-12-18 20:25:09 +01:00
function fromDecimals(value, decimals) {
value = value.toString()
let ether = value.toString()
const base = new BN('10').pow(new BN(decimals))
const baseLength = base.toString(10).length - 1 || 1
const negative = ether.substring(0, 1) === '-'
if (negative) {
ether = ether.substring(1)
}
if (ether === '.') {
throw new Error('[ethjs-unit] while converting number ' + value + ' to wei, invalid value')
}
// Split it into a whole and fractional part
const comps = ether.split('.')
if (comps.length > 2) {
throw new Error(
'[ethjs-unit] while converting number ' + value + ' to wei, too many decimal points'
)
}
let whole = comps[0]
let fraction = comps[1]
if (!whole) {
whole = '0'
}
if (!fraction) {
fraction = '0'
}
if (fraction.length > baseLength) {
throw new Error(
'[ethjs-unit] while converting number ' + value + ' to wei, too many decimal places'
)
}
while (fraction.length < baseLength) {
fraction += '0'
}
whole = new BN(whole)
fraction = new BN(fraction)
let wei = whole.mul(base).add(fraction)
if (negative) {
wei = wei.mul(negative)
}
return new BN(wei.toString(10), 10)
}
2019-11-26 16:01:37 +01:00
function isEnoughFee({ gas, gasPrices, currency, amount, refund, ethPrices, fee }) {
2019-12-18 20:25:09 +01:00
const { decimals } = mixers[`netId${netId}`][currency]
const feePercent = toBN(fromDecimals(amount, decimals)).mul(toBN(relayerServiceFee * 10)).div(toBN('1000'))
2019-11-15 10:01:59 +01:00
const expense = toBN(toWei(gasPrices.fast.toString(), 'gwei')).mul(toBN(gas))
let desiredFee
switch (currency) {
case 'eth': {
2019-11-26 16:01:37 +01:00
desiredFee = expense.add(feePercent)
2019-11-15 10:01:59 +01:00
break
}
2019-12-18 20:25:09 +01:00
default: {
2019-11-23 10:20:08 +01:00
desiredFee =
expense.add(refund)
2019-12-18 20:25:09 +01:00
.mul(toBN(10 ** decimals))
.div(toBN(ethPrices[currency]))
2019-11-26 16:01:37 +01:00
desiredFee = desiredFee.add(feePercent)
2019-11-15 10:01:59 +01:00
break
}
}
2019-12-18 20:25:09 +01:00
console.log('sent fee, desired fee, feePercent', fee.toString(), desiredFee.toString(), feePercent.toString())
2019-11-15 10:01:59 +01:00
if (fee.lt(desiredFee)) {
return { isEnough: false, reason: 'Not enough fee' }
}
return { isEnough: true }
}
2019-12-23 17:38:44 +01:00
function getArgsForOracle() {
2019-12-02 13:49:24 +01:00
const tokens = mixers['netId1']
const tokenAddresses = []
2019-12-23 17:38:44 +01:00
const oneUintAmount = []
const parts = [] // this is probably should be removed
2019-12-02 13:49:24 +01:00
const currencyLookup = {}
Object.entries(tokens).map(([currency, data]) => {
if (currency !== 'eth') {
tokenAddresses.push(data.tokenAddress)
2019-12-23 17:38:44 +01:00
oneUintAmount.push(
toBN('10')
.pow(toBN(data.decimals.toString()))
.toString()
)
parts.push('1')
currencyLookup[data.tokenAddress] = currency
2019-12-02 13:49:24 +01:00
}
})
2019-12-23 17:38:44 +01:00
return { tokenAddresses, oneUintAmount, parts, currencyLookup }
2019-12-02 13:49:24 +01:00
}
function getMixers() {
return mixers[`netId${netId}`]
}
2019-12-23 17:38:44 +01:00
module.exports = { isValidProof, isValidArgs, sleep, isKnownContract, isEnoughFee, getMixers, getArgsForOracle }