Skip to content

Commit

Permalink
added dump contracts script
Browse files Browse the repository at this point in the history
  • Loading branch information
NoahSaso committed Jul 15, 2024
1 parent 84467ff commit fb7fb21
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 1 deletion.
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -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 <[email protected]>",
"devDependencies": {
Expand Down
77 changes: 77 additions & 0 deletions src/scripts/dumpContracts.ts
Original file line number Diff line number Diff line change
@@ -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>',
'path to config file, falling back to config.json'
)
program.requiredOption(
'-k, --code-ids-keys <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()

0 comments on commit fb7fb21

Please sign in to comment.