#PR1: classic-ui: Prefix static caches with network id and code quality #25

Merged
Theo merged 9 commits from :master into master 2023-06-27 14:53:50 +03:00
Showing only changes of commit 03462c0a41 - Show all commits

View File

@ -9,85 +9,116 @@ import ABI from '../abis/Instance.abi.json'
import { loadCachedEvents, getPastEvents } from './helpers' import { loadCachedEvents, getPastEvents } from './helpers'
const EVENTS_PATH = './static/events/' const EVENTS_PATH = './static/events/'
const EVENTS = ['Deposit', 'Withdrawal']
async function main(type, netId, chosenToken) { function parseArg(netId, tokenOrEvent) {
const { tokens, nativeCurrency, deployedBlock } = networkConfig[`netId${netId}`] const { tokens } = networkConfig[`netId${netId}`]
const token = chosenToken !== undefined ? chosenToken : nativeCurrency 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 function parseDepositEvent({ blockNumber, transactionHash, returnValues }) {
const { commitment, leafIndex, timestamp } = returnValues
for (const [instance, _contract] of Object.entries(CONTRACTS)) { return {
const cachedEvents = loadCachedEvents({ timestamp,
name: `${type.toLowerCase()}s_${netId}_${token}_${instance}.json`, commitment,
directory: EVENTS_PATH, blockNumber,
deployedBlock transactionHash,
}) leafIndex: Number(leafIndex)
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)
} }
} }
async function start() { function parseWithdrawalEvent({ blockNumber, transactionHash, returnValues }) {
const [, , , chain, chosenToken] = process.argv 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(', ')}`) throw new Error(`Supported chain ids ${enabledChains.join(', ')}`)
} }
for (const event of EVENTS) { await main(netId, args.token, args.event)
await main(event, chain, chosenToken)
}
} }
start() start()