updateEvents: improve script

Signed-off-by: AlienTornadosaurusHex <>
This commit is contained in:
AlienTornadosaurusHex 2023-05-19 17:53:08 +00:00
parent 937c221e22
commit 03462c0a41

@ -9,85 +9,116 @@ import ABI from '../abis/Instance.abi.json'
import { loadCachedEvents, getPastEvents } from './helpers'
const EVENTS_PATH = './static/events/'
const EVENTS = ['Deposit', 'Withdrawal']
async function main(type, netId, chosenToken) {
const { tokens, nativeCurrency, deployedBlock } = networkConfig[`netId${netId}`]
const token = chosenToken !== undefined ? chosenToken : nativeCurrency
function parseArg(netId, tokenOrEvent) {
const { tokens } = networkConfig[`netId${netId}`]
const keys = Object.keys(tokens)
if (tokenOrEvent !== undefined) {
const lower = tokenOrEvent.toLowerCase()
return keys.includes(lower)
? { token: lower }
: { event: lower[0].toUpperCase() + lower.slice(1).toLowerCase() }
} else return undefined
}
const CONTRACTS = tokens[token].instanceAddress
for (const [instance, _contract] of Object.entries(CONTRACTS)) {
const cachedEvents = loadCachedEvents({
name: `${type.toLowerCase()}s_${netId}_${token}_${instance}.json`,
directory: EVENTS_PATH,
deployedBlock
})
console.log('Update events for', instance, token.toUpperCase(), `${type.toLowerCase()}s`)
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') {
freshEvents = uniqBy(freshEvents, 'nullifierHash').sort((a, b) => a.blockNumber - b.blockNumber)
} else {
freshEvents = freshEvents.filter((e, index) => Number(e.leafIndex) === index)
}
const eventsJson = JSON.stringify(freshEvents, null, 2) + '\n'
fs.writeFileSync(`${EVENTS_PATH}${type.toLowerCase()}s_${netId}_${token}_${instance}.json`, eventsJson)
function parseDepositEvent({ blockNumber, transactionHash, returnValues }) {
const { commitment, leafIndex, timestamp } = returnValues
return {
timestamp,
commitment,
blockNumber,
transactionHash,
leafIndex: Number(leafIndex)
}
}
async function start() {
const [, , , chain, chosenToken] = process.argv
function parseWithdrawalEvent({ blockNumber, transactionHash, returnValues }) {
const { nullifierHash, to, fee } = returnValues
return {
to,
fee,
blockNumber,
nullifierHash,
transactionHash
}
}
if (!enabledChains.includes(chain)) {
function filterWithdrawalEvents(events) {
return uniqBy(events, 'nullifierHash').sort((a, b) => a.blockNumber - b.blockNumber)
}
function filterDepositEvents(events) {
return events.filter((e, index) => Number(e.leafIndex) === index)
}
async function main(netId, chosenToken, chosenEvent) {
const { tokens, deployedBlock } = networkConfig[`netId${netId}`]
const tokenSymbols = chosenToken !== undefined ? [chosenToken] : Object.keys(tokens)
const eventNames = chosenEvent !== undefined ? [chosenEvent] : ['Deposit', 'Withdrawal']
for (const eventName of eventNames) {
// Get the parser that we need
const parser = eventName === 'Deposit' ? parseDepositEvent : parseWithdrawalEvent
// Get the parser that we need
const filter = eventName === 'Deposit' ? filterDepositEvents : filterWithdrawalEvents
for (const tokenSymbol of tokenSymbols) {
// Now load the denominations and address
const instanceData = Object.entries(tokens[tokenSymbol].instanceAddress)
// And now sync
for (const data of instanceData) {
const denom = data[0]
const address = data[1]
// Now load cached events
const cachedEvents = loadCachedEvents({
name: `${eventName.toLowerCase()}s_${netId}_${tokenSymbol}_${denom}.json`,
directory: EVENTS_PATH,
deployedBlock
})
console.log('Update events for', denom, tokenSymbol.toUpperCase(), `${eventName.toLowerCase()}s`)
console.log('cachedEvents count - ', cachedEvents.events.length)
console.log('lastBlock - ', cachedEvents.lastBlock)
let events = await getPastEvents({
type: eventName,
fromBlock: cachedEvents.lastBlock + 1,
netId: netId,
events: [],
contractAttrs: [ABI, address]
})
events = filter(cachedEvents.events.concat(events.map(parser)))
fs.writeFileSync(
`${EVENTS_PATH}${eventName.toLowerCase()}s_${netId}_${tokenSymbol}_${denom}.json`,
JSON.stringify(events, null, 2) + '\n'
)
}
}
}
}
/**
* @param netId ID of the network for which event(s) should be synced.
* @param tokenOrEvent Optional token or event.
* @param eventOrToken Optional token or event. Overwrites the former option.
*/
async function start() {
const [, , , netId, tokenOrEvent, eventOrToken] = process.argv
const args = { ...parseArg(netId, tokenOrEvent), ...parseArg(netId, eventOrToken) }
console.log('ARGS => ', args, netId)
if (!enabledChains.includes(netId)) {
throw new Error(`Supported chain ids ${enabledChains.join(', ')}`)
}
for (const event of EVENTS) {
await main(event, chain, chosenToken)
}
await main(netId, args.token, args.event)
}
start()