remove multicall

This commit is contained in:
Roman Storm 2021-03-01 20:38:16 -08:00
parent 77021a52a9
commit 0dd025bd30
No known key found for this signature in database
GPG Key ID: 522F2A785F34E71F
6 changed files with 73 additions and 90 deletions

@ -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"
}
]

@ -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,

@ -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)

@ -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()

@ -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,
}

@ -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 }) {