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..83138782 --- /dev/null +++ b/src/scripts/dumpContracts.ts @@ -0,0 +1,66 @@ +/** + * 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' +) +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 = WasmCodeService.extractWasmCodeKeys(codeIdsKeys) + const codeIds = WasmCodeService.getInstance().findWasmCodeIdsByKeys( + ...extractedCodeIdsKeys + ) + + if (codeIds.length === 0) { + throw new Error( + 'No code IDs found matching keys: ' + extractedCodeIdsKeys.join(', ') + ) + } + + const contracts = await Contract.findAll({ + where: { + codeId: { + [Op.in]: codeIds, + }, + }, + }) + + console.log(contracts.join(',')) + + await sequelize.close() + + process.exit(0) +} + +main()