From fb7fb21d96c06c261bdc96f338e5ec44e74884a8 Mon Sep 17 00:00:00 2001 From: Noah Saso Date: Mon, 15 Jul 2024 14:39:45 -0500 Subject: [PATCH] added dump contracts script --- package.json | 3 +- src/scripts/dumpContracts.ts | 77 ++++++++++++++++++++++++++++++++++++ 2 files changed, 79 insertions(+), 1 deletion(-) create mode 100644 src/scripts/dumpContracts.ts diff --git a/package.json b/package.json index 0b6345a1..a7bb59d8 100644 --- a/package.json +++ b/package.json @@ -37,7 +37,8 @@ "serve:prod": "npm install && npm run build && pm2 reload ecosystem.config.js --only server && pm2 save", "serve:prod:accounts": "npm run build && pm2 reload ecosystem.config.js --only server_accounts", "search:update": "node dist/scripts/searchUpdate.js", - "console": "node dist/scripts/console.js" + "console": "node dist/scripts/console.js", + "dumpContracts": "node dist/scripts/dumpContracts.js" }, "author": "Noah Saso ", "devDependencies": { diff --git a/src/scripts/dumpContracts.ts b/src/scripts/dumpContracts.ts new file mode 100644 index 00000000..8a34a2d5 --- /dev/null +++ b/src/scripts/dumpContracts.ts @@ -0,0 +1,77 @@ +/** + * Dump all contract addresses from DB for matching code IDs keys. + */ + +import { Command } from 'commander' +import { Op } from 'sequelize' + +import { loadConfig } from '@/config' +import { Contract, loadDb } from '@/db' +import { WasmCodeService } from '@/services' +import { DbType } from '@/types' + +// Parse arguments. +const program = new Command() +program.option( + '-c, --config ', + 'path to config file, falling back to config.json' +) +program.requiredOption( + '-k, --code-ids-keys ', + 'comma separated list of code IDs keys from the config to revalidate (pass ALL to use all code IDs set)' +) +program.parse() +const { config: _config, codeIdsKeys } = program.opts() + +const main = async () => { + console.log(`\n[${new Date().toISOString()}] Dumping contracts...`) + + // Load config with config option. + loadConfig(_config) + + // Load DB on start. + const sequelize = await loadDb({ + type: DbType.Data, + }) + + // Set up wasm code service. + await WasmCodeService.setUpInstance() + + const extractedCodeIdsKeys = + codeIdsKeys === 'ALL' + ? 'ALL' + : WasmCodeService.extractWasmCodeKeys(codeIdsKeys) + const codeIds = + extractedCodeIdsKeys === 'ALL' + ? WasmCodeService.getInstance() + .getWasmCodes() + .flatMap((c) => c.codeIds) + : WasmCodeService.getInstance().findWasmCodeIdsByKeys( + ...extractedCodeIdsKeys + ) + + if (codeIds.length === 0) { + throw new Error( + 'No code IDs found matching keys: ' + + (Array.isArray(extractedCodeIdsKeys) + ? extractedCodeIdsKeys.join(', ') + : extractedCodeIdsKeys) + ) + } + + const contracts = await Contract.findAll({ + where: { + codeId: { + [Op.in]: codeIds, + }, + }, + }) + + console.log(contracts.map((c) => c.address).join(',')) + + await sequelize.close() + + process.exit(0) +} + +main()