From 0dd025bd30d925304cbc09ce178c2c1a7b576838 Mon Sep 17 00:00:00 2001 From: Roman Storm Date: Mon, 1 Mar 2021 20:38:16 -0800 Subject: [PATCH] remove multicall --- abis/MultiCall.abi.json | 22 ----------------- src/config.js | 3 +-- src/priceWatcher.js | 52 ++++++++++++++++++----------------------- src/treeWatcher.js | 42 +++++++++++++++++++-------------- src/utils.js | 2 +- src/worker.js | 42 ++++++++++++++++++--------------- 6 files changed, 73 insertions(+), 90 deletions(-) delete mode 100644 abis/MultiCall.abi.json diff --git a/abis/MultiCall.abi.json b/abis/MultiCall.abi.json deleted file mode 100644 index fa6dc92..0000000 --- a/abis/MultiCall.abi.json +++ /dev/null @@ -1,22 +0,0 @@ -[ - { - "inputs": [ - { - "components": [ - { "internalType": "address", "name": "to", "type": "address" }, - { "internalType": "bytes", "name": "data", "type": "bytes" } - ], - "internalType": "struct MultiCall.Call[]", - "name": "calls", - "type": "tuple[]" - } - ], - "name": "multicall", - "outputs": [ - { "internalType": "bytes[]", "name": "results", "type": "bytes[]" }, - { "internalType": "bool[]", "name": "success", "type": "bool[]" } - ], - "stateMutability": "view", - "type": "function" - } -] diff --git a/src/config.js b/src/config.js index be0c5a4..d502d41 100644 --- a/src/config.js +++ b/src/config.js @@ -8,8 +8,7 @@ module.exports = { httpRpcUrl: process.env.HTTP_RPC_URL, wsRpcUrl: process.env.WS_RPC_URL, oracleRpcUrl: process.env.ORACLE_RPC_URL || 'https://mainnet.infura.io/', - offchainOracleAddress: '0x080ab73787a8b13ec7f40bd7d00d6cc07f9b24d0', - multiCallAddress: '0xda3c19c6fe954576707fa24695efb830d9cca1ca', + offchainOracleAddress: '0x080AB73787A8B13EC7F40bd7d00d6CC07F9b24d0', aggregatorAddress: process.env.AGGREGATOR, minerMerkleTreeHeight: 20, privateKey: process.env.PRIVATE_KEY, diff --git a/src/priceWatcher.js b/src/priceWatcher.js index 4f187f9..b8f0139 100644 --- a/src/priceWatcher.js +++ b/src/priceWatcher.js @@ -1,48 +1,42 @@ const Redis = require('ioredis') -const { redisUrl, offchainOracleAddress, multiCallAddress, oracleRpcUrl } = require('./config') +const { redisUrl, offchainOracleAddress, oracleRpcUrl } = require('./config') const { getArgsForOracle, setSafeInterval } = require('./utils') const redis = new Redis(redisUrl) const Web3 = require('web3') -const web3 = new Web3(oracleRpcUrl) +const web3 = new Web3(oracleRpcUrl, { + timeout: 200000, // ms +}) -const multiCallABI = require('../abis/MultiCall.abi.json') const offchainOracleABI = require('../abis/OffchainOracle.abi.json') -const offchainOracle = new web3.eth.Contract(offchainOracleABI) -const multiCall = new web3.eth.Contract(multiCallABI, multiCallAddress) +const offchainOracle = new web3.eth.Contract(offchainOracleABI, offchainOracleAddress) const { tokenAddresses, oneUintAmount, currencyLookup } = getArgsForOracle() const { toBN } = require('web3-utils') async function main() { - const callData = tokenAddresses.map(address => ({ - to: offchainOracleAddress, - data: offchainOracle.methods - .getRate( - address, - '0x0000000000000000000000000000000000000000', // rate to ETH - ) - .encodeABI(), - })) + try { + const ethPrices = {} + for (let i = 0; i < tokenAddresses.length; i++) { + try { + const price = await offchainOracle.methods + .getRate(tokenAddresses[i], '0x0000000000000000000000000000000000000000') + .call() + const numerator = toBN(oneUintAmount[i]) + const denominator = toBN(10).pow(toBN(18)) // eth decimals + const priceFormatted = toBN(price).mul(numerator).div(denominator) - const { results, success } = await multiCall.methods.multicall(callData).call() - - const ethPrices = {} - for (let i = 0; i < results.length; i++) { - if (!success[i]) { - continue + ethPrices[currencyLookup[tokenAddresses[i]]] = priceFormatted.toString() + } catch (e) { + console.error('cant get price of ', tokenAddresses[i]) + } } - const decodedRate = web3.eth.abi.decodeParameter('uint256', results[i]).toString() - const numerator = toBN(oneUintAmount[i]) - const denominator = toBN(10).pow(toBN(18)) // eth decimals - const price = toBN(decodedRate).mul(numerator).div(denominator) - - ethPrices[currencyLookup[tokenAddresses[i]]] = price.toString() + await redis.hmset('prices', ethPrices) + console.log('Wrote following prices to redis', ethPrices) + } catch (e) { + console.error('priceWatcher error', e) } - - await redis.hmset('prices', ethPrices) - console.log('Wrote following prices to redis', ethPrices) } setSafeInterval(main, 30 * 1000) diff --git a/src/treeWatcher.js b/src/treeWatcher.js index 6fff7ab..d503cb4 100644 --- a/src/treeWatcher.js +++ b/src/treeWatcher.js @@ -22,13 +22,17 @@ let tree, eventSubscription, blockSubscription // todo handle the situation when we have two rewards in one block async function fetchEvents(from = 0, to = 'latest') { - const events = await contract.getPastEvents('NewAccount', { - fromBlock: from, - toBlock: to, - }) - return events - .sort((a, b) => a.returnValues.index - b.returnValues.index) - .map(e => toBN(e.returnValues.commitment)) + try { + const events = await contract.getPastEvents('NewAccount', { + fromBlock: from, + toBlock: to, + }) + return events + .sort((a, b) => a.returnValues.index - b.returnValues.index) + .map(e => toBN(e.returnValues.commitment)) + } catch (e) { + console.error('error fetching events', e) + } } async function processNewEvent(err, event) { @@ -95,16 +99,20 @@ async function rebuild() { } async function init() { - console.log('Initializing') - const miner = await resolver.resolve(torn.miningV2.address) - contract = new web3.eth.Contract(MinerABI, miner) - const block = await web3.eth.getBlockNumber() - const events = await fetchEvents(0, block) - tree = new MerkleTree(minerMerkleTreeHeight, events, { hashFunction: poseidonHash2 }) - console.log(`Rebuilt tree with ${events.length} elements, root: ${tree.root()}`) - eventSubscription = contract.events.NewAccount({ fromBlock: block + 1 }, processNewEvent) - blockSubscription = web3.eth.subscribe('newBlockHeaders', processNewBlock) - await updateRedis() + try { + console.log('Initializing') + const miner = await resolver.resolve(torn.miningV2.address) + contract = new web3.eth.Contract(MinerABI, miner) + const block = await web3.eth.getBlockNumber() + const events = await fetchEvents(0, block) + tree = new MerkleTree(minerMerkleTreeHeight, events, { hashFunction: poseidonHash2 }) + console.log(`Rebuilt tree with ${events.length} elements, root: ${tree.root()}`) + eventSubscription = contract.events.NewAccount({ fromBlock: block + 1 }, processNewEvent) + blockSubscription = web3.eth.subscribe('newBlockHeaders', processNewBlock) + await updateRedis() + } catch (e) { + console.error('error on init treeWatcher', e.message) + } } init() diff --git a/src/utils.js b/src/utils.js index 925a35f..4eb3a9c 100644 --- a/src/utils.js +++ b/src/utils.js @@ -3,7 +3,7 @@ const { poseidon } = require('circomlib') const { toBN, toChecksumAddress, BN } = require('web3-utils') const TORN_TOKEN = { - tokenAddress: '0x77777feddddffc19ff86db637967013e6c6a116c', + tokenAddress: '0x77777FeDdddFfC19Ff86DB637967013e6C6A116C', symbol: 'TORN', decimals: 18, } diff --git a/src/worker.js b/src/worker.js index 112af54..eaac65a 100644 --- a/src/worker.js +++ b/src/worker.js @@ -74,26 +74,30 @@ async function fetchTree() { } async function start() { - web3 = new Web3(httpRpcUrl) - const { CONFIRMATIONS, MAX_GAS_PRICE } = process.env - txManager = new TxManager({ - privateKey, - rpcUrl: httpRpcUrl, - config: { CONFIRMATIONS, MAX_GAS_PRICE, THROW_ON_REVERT: false }, - }) - swap = new web3.eth.Contract(swapABI, await resolver.resolve(torn.rewardSwap.address)) - minerContract = new web3.eth.Contract(miningABI, await resolver.resolve(torn.miningV2.address)) - proxyContract = new web3.eth.Contract(tornadoProxyABI, await resolver.resolve(torn.tornadoProxy.address)) - redisSubscribe.subscribe('treeUpdate', fetchTree) - await fetchTree() - const provingKeys = { - treeUpdateCircuit: require('../keys/TreeUpdate.json'), - treeUpdateProvingKey: fs.readFileSync('./keys/TreeUpdate_proving_key.bin').buffer, + try { + web3 = new Web3(httpRpcUrl) + const { CONFIRMATIONS, MAX_GAS_PRICE } = process.env + txManager = new TxManager({ + privateKey, + rpcUrl: httpRpcUrl, + config: { CONFIRMATIONS, MAX_GAS_PRICE, THROW_ON_REVERT: false }, + }) + swap = new web3.eth.Contract(swapABI, await resolver.resolve(torn.rewardSwap.address)) + minerContract = new web3.eth.Contract(miningABI, await resolver.resolve(torn.miningV2.address)) + proxyContract = new web3.eth.Contract(tornadoProxyABI, await resolver.resolve(torn.tornadoProxy.address)) + redisSubscribe.subscribe('treeUpdate', fetchTree) + await fetchTree() + const provingKeys = { + treeUpdateCircuit: require('../keys/TreeUpdate.json'), + treeUpdateProvingKey: fs.readFileSync('./keys/TreeUpdate_proving_key.bin').buffer, + } + controller = new Controller({ provingKeys }) + await controller.init() + queue.process(processJob) + console.log('Worker started') + } catch (e) { + console.error('error on start worker', e.message) } - controller = new Controller({ provingKeys }) - await controller.init() - queue.process(processJob) - console.log('Worker started') } function checkFee({ data }) {