From eaa384397aef77b615a37ad5a8f9ddc7be0e97a6 Mon Sep 17 00:00:00 2001 From: Theo Date: Tue, 16 May 2023 16:39:10 +0300 Subject: [PATCH] Add script to update events cache --- package.json | 3 +- scripts/updateAllCache.js | 82 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 scripts/updateAllCache.js diff --git a/package.json b/package.json index 411ce3d..ed21f68 100644 --- a/package.json +++ b/package.json @@ -4,7 +4,8 @@ "description": "", "main": "index.js", "scripts": { - "test": "echo \"Error: no test specified\" && exit 1" + "test": "echo \"Error: no test specified\" && exit 1", + "updateCache": "node scripts/updateAllCache.js" }, "author": "", "license": "ISC", diff --git a/scripts/updateAllCache.js b/scripts/updateAllCache.js new file mode 100644 index 0000000..13b1c32 --- /dev/null +++ b/scripts/updateAllCache.js @@ -0,0 +1,82 @@ +const childProcess = require('child_process'); +const fs = require('fs'); +const path = require('path'); +const config = require('../config'); + +function getCurrentNetworkName(networkID) { + switch (networkID) { + case 1: + return 'Ethereum'; + case 56: + return 'BinanceSmartChain'; + case 100: + return 'GnosisChain'; + case 137: + return 'Polygon'; + case 42161: + return 'Arbitrum'; + case 43114: + return 'Avalanche'; + case 5: + return 'Goerli'; + case 42: + return 'Kovan'; + case 10: + return 'Optimism'; + default: + return 'testRPC'; + } +} + +function syncDeposits(currency, amount, rpc) { + return childProcess.spawnSync('node', ['cli.js', 'checkCacheValidity', currency, amount, '--rpc', rpc]); +} + +function syncWithdrawals(currency, amount, rpc) { + return childProcess.spawnSync('node', ['cli.js', 'syncEvents', 'withdrawal', currency, amount, '--rpc', rpc]); +} + +function checkSyncResult(resultData, networkName, currency, amount, eventType) { + const resultOutput = resultData.output.toString(); + + if (resultData.error || resultOutput.includes('Error:')) { + console.log(resultOutput); + console.error(`Error while updating cache for ${currency.toUpperCase()} ${amount} ${eventType}s on ${networkName}`); + } else { + console.log(`Successfully updated cache for ${currency.toUpperCase()} ${amount} ${eventType}s on ${networkName}`); + } +} + +function main() { + for (const [networkIDInfo, network] of Object.entries(config.deployments)) { + const networkID = Number(networkIDInfo.match(/\d+/)[0]); + const networkName = getCurrentNetworkName(networkID); + const defaultRpc = network.defaultRpc; + + for (const [currency, _data] of Object.entries(network.tokens)) { + for (const amount of Object.keys(_data.instanceAddress)) { + console.log(`\nStart updating cache for ${currency.toUpperCase()} ${amount} deposits on ${networkName}`); + const depositsFile = path.join('cache', networkName.toLowerCase(), `deposits_${currency.toUpperCase()}_${amount}.json`); + let depositSyncResult = syncDeposits(currency, amount, defaultRpc); + + // If deposit events tree has invalid root, need to reload it all from deployment block + if (depositSyncResult.output.includes('invalid root')) { + console.log( + `Events tree for ${currency.toUpperCase()} ${amount} ${eventType}s on ${networkName} has invalid root. Start full reloading.` + ); + fs.rmSync(depositsFile); + depositSyncgResult = syncDeposits(currency, amount, defaultRpc); + } + checkSyncResult(depositSyncResult, networkName, currency, amount, 'deposit'); + + console.log(`\nStart updating cache for ${currency.toUpperCase()} ${amount} withdrawals on ${networkName}`); + const withdrawalSyncResult = syncWithdrawals(currency, amount, defaultRpc); + checkSyncResult(withdrawalSyncResult, networkName, currency, amount, 'withdrawal'); + } + } + } + + console.log('\nAll event trees cache updated!\n'); +} + +main();