updateEvents: improve script
Signed-off-by: AlienTornadosaurusHex <>
This commit is contained in:
parent
937c221e22
commit
03462c0a41
@ -9,37 +9,19 @@ 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 }) => {
|
||||
function parseDepositEvent({ blockNumber, transactionHash, returnValues }) {
|
||||
const { commitment, leafIndex, timestamp } = returnValues
|
||||
return {
|
||||
timestamp,
|
||||
@ -48,11 +30,9 @@ async function main(type, netId, chosenToken) {
|
||||
transactionHash,
|
||||
leafIndex: Number(leafIndex)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
if (type === 'Withdrawal') {
|
||||
events = events.map(({ blockNumber, transactionHash, returnValues }) => {
|
||||
function parseWithdrawalEvent({ blockNumber, transactionHash, returnValues }) {
|
||||
const { nullifierHash, to, fee } = returnValues
|
||||
return {
|
||||
to,
|
||||
@ -61,33 +41,84 @@ async function main(type, netId, chosenToken) {
|
||||
nullifierHash,
|
||||
transactionHash
|
||||
}
|
||||
}
|
||||
|
||||
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'
|
||||
)
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @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 [, , , chain, chosenToken] = process.argv
|
||||
const [, , , netId, tokenOrEvent, eventOrToken] = process.argv
|
||||
|
||||
if (!enabledChains.includes(chain)) {
|
||||
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()
|
||||
|
Loading…
Reference in New Issue
Block a user