2022-06-08 06:45:37 -04:00
|
|
|
import 'dotenv/config'
|
|
|
|
|
|
|
|
import fs from 'fs'
|
|
|
|
import { uniqBy } from 'lodash'
|
|
|
|
|
2023-05-16 11:24:52 -04:00
|
|
|
import networkConfig, { enabledChains } from '../networkConfig'
|
2022-06-08 06:45:37 -04:00
|
|
|
import ABI from '../abis/Instance.abi.json'
|
2023-05-12 18:19:58 -04:00
|
|
|
|
2022-06-08 06:45:37 -04:00
|
|
|
import { loadCachedEvents, getPastEvents } from './helpers'
|
|
|
|
|
|
|
|
const EVENTS_PATH = './static/events/'
|
|
|
|
const EVENTS = ['Deposit', 'Withdrawal']
|
|
|
|
|
2023-05-18 18:26:08 -04:00
|
|
|
async function main(type, netId, chosenToken) {
|
2022-06-08 06:45:37 -04:00
|
|
|
const { tokens, nativeCurrency, deployedBlock } = networkConfig[`netId${netId}`]
|
2023-05-18 18:26:08 -04:00
|
|
|
const token = chosenToken !== undefined ? chosenToken : nativeCurrency
|
|
|
|
|
|
|
|
const CONTRACTS = tokens[token].instanceAddress
|
2022-06-08 06:45:37 -04:00
|
|
|
|
|
|
|
for (const [instance, _contract] of Object.entries(CONTRACTS)) {
|
2023-05-18 18:26:08 -04:00
|
|
|
const cachedEvents = loadCachedEvents({
|
|
|
|
name: `${type.toLowerCase()}s_${netId}_${token}_${instance}.json`,
|
2022-06-08 06:45:37 -04:00
|
|
|
directory: EVENTS_PATH,
|
|
|
|
deployedBlock
|
|
|
|
})
|
|
|
|
|
2023-05-18 18:26:08 -04:00
|
|
|
console.log('Update events for', instance, token.toUpperCase(), `${type.toLowerCase()}s`)
|
2022-06-08 06:45:37 -04:00
|
|
|
console.log('cachedEvents count - ', cachedEvents.events.length)
|
|
|
|
console.log('lastBlock - ', cachedEvents.lastBlock)
|
|
|
|
|
|
|
|
let events = []
|
|
|
|
|
|
|
|
events = await getPastEvents({
|
|
|
|
type,
|
|
|
|
netId,
|
|
|
|
events,
|
|
|
|
contractAttrs: [ABI, _contract],
|
|
|
|
fromBlock: cachedEvents.lastBlock + 1
|
|
|
|
})
|
|
|
|
|
|
|
|
if (type === 'Deposit') {
|
|
|
|
events = events.map(({ blockNumber, transactionHash, returnValues }) => {
|
|
|
|
const { commitment, leafIndex, timestamp } = returnValues
|
|
|
|
return {
|
|
|
|
timestamp,
|
|
|
|
commitment,
|
|
|
|
blockNumber,
|
|
|
|
transactionHash,
|
|
|
|
leafIndex: Number(leafIndex)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
if (type === 'Withdrawal') {
|
|
|
|
events = events.map(({ blockNumber, transactionHash, returnValues }) => {
|
|
|
|
const { nullifierHash, to, fee } = returnValues
|
|
|
|
return {
|
|
|
|
to,
|
|
|
|
fee,
|
|
|
|
blockNumber,
|
|
|
|
nullifierHash,
|
|
|
|
transactionHash
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
let freshEvents = cachedEvents.events.concat(events)
|
|
|
|
|
|
|
|
if (type === 'Withdrawal') {
|
2022-09-21 00:24:44 -04:00
|
|
|
freshEvents = uniqBy(freshEvents, 'nullifierHash').sort((a, b) => a.blockNumber - b.blockNumber)
|
2022-06-08 06:45:37 -04:00
|
|
|
} else {
|
|
|
|
freshEvents = freshEvents.filter((e, index) => Number(e.leafIndex) === index)
|
|
|
|
}
|
|
|
|
|
|
|
|
const eventsJson = JSON.stringify(freshEvents, null, 2) + '\n'
|
2023-05-12 18:19:58 -04:00
|
|
|
|
2023-05-18 18:26:08 -04:00
|
|
|
fs.writeFileSync(`${EVENTS_PATH}${type.toLowerCase()}s_${netId}_${token}_${instance}.json`, eventsJson)
|
2022-06-08 06:45:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async function start() {
|
2023-05-18 18:26:08 -04:00
|
|
|
const [, , , chain, chosenToken] = process.argv
|
2023-05-12 18:19:58 -04:00
|
|
|
|
2022-06-08 06:45:37 -04:00
|
|
|
if (!enabledChains.includes(chain)) {
|
|
|
|
throw new Error(`Supported chain ids ${enabledChains.join(', ')}`)
|
|
|
|
}
|
|
|
|
|
2023-05-12 18:19:58 -04:00
|
|
|
for (const event of EVENTS) {
|
2023-05-18 18:26:08 -04:00
|
|
|
await main(event, chain, chosenToken)
|
2022-06-08 06:45:37 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
start()
|