|
| 1 | +#!/usr/bin/env node |
| 2 | + |
| 3 | +import { connectPostgres, PgSqlClient } from '@hirosystems/api-toolkit'; |
| 4 | +import { StacksCoreRpcClient } from '../core-rpc/client'; |
| 5 | +import { getConnectionArgs, getConnectionConfig } from '../datastore/connection'; |
| 6 | +import { ClarityAbi } from '../event-stream/contract-abi'; |
| 7 | +import { loadDotEnv } from '../helpers'; |
| 8 | +import { logger } from '../logger'; |
| 9 | + |
| 10 | +async function main() { |
| 11 | + // 1) Environment + DB Setup |
| 12 | + loadDotEnv(); |
| 13 | + const sql: PgSqlClient = await connectPostgres({ |
| 14 | + usageName: 'patch-missing-contract-abis', |
| 15 | + connectionArgs: getConnectionArgs(), |
| 16 | + connectionConfig: getConnectionConfig(), |
| 17 | + }); |
| 18 | + |
| 19 | + const BATCH_SIZE = 64; |
| 20 | + const LAST_BLOCK_HEIGHT = parseInt(process.env.LAST_BLOCK_HEIGHT ?? '-1'); |
| 21 | + |
| 22 | + try { |
| 23 | + logger.info('Starting script to patch missing contract ABIs...'); |
| 24 | + |
| 25 | + // 2) Initialize script variables and RPC client |
| 26 | + let lastBlockHeight = LAST_BLOCK_HEIGHT; // Initial value for the first query |
| 27 | + |
| 28 | + let totalConsideredCount = 0; |
| 29 | + let totalPatchedCount = 0; |
| 30 | + |
| 31 | + const rpc = new StacksCoreRpcClient(); // Default to RPC host from ENV |
| 32 | + |
| 33 | + // 3) Main processing loop: Fetch and patch contracts in batches |
| 34 | + while (true) { |
| 35 | + // 3.1) Find contracts whose ABI is still missing (paginated) |
| 36 | + const missing = await sql<{ contract_id: string; block_height: number }[]>` |
| 37 | + SELECT sc.contract_id, sc.block_height |
| 38 | + FROM smart_contracts sc |
| 39 | + JOIN txs ON sc.tx_id = txs.tx_id |
| 40 | + WHERE (sc.abi::text = '"null"') |
| 41 | + AND sc.canonical = TRUE |
| 42 | + AND txs.canonical = TRUE |
| 43 | + AND txs.microblock_canonical = TRUE |
| 44 | + AND txs.status = 1 |
| 45 | + AND sc.block_height > ${lastBlockHeight} |
| 46 | + ORDER BY sc.block_height ASC |
| 47 | + LIMIT ${BATCH_SIZE} |
| 48 | + `; |
| 49 | + |
| 50 | + if (missing.length === 0) { |
| 51 | + if (totalConsideredCount === 0) { |
| 52 | + logger.info(' - No contracts with missing ABI found.'); |
| 53 | + } else { |
| 54 | + logger.info(` - Patched ${totalPatchedCount}/${totalConsideredCount} contracts.`); |
| 55 | + } |
| 56 | + break; // Exit the while loop |
| 57 | + } |
| 58 | + |
| 59 | + logger.info(`- Found batch of ${missing.length} contracts with missing ABIs.`); |
| 60 | + |
| 61 | + // 3.2) Process each contract in the current batch |
| 62 | + for (const contract of missing) { |
| 63 | + totalConsideredCount++; |
| 64 | + const { contract_id, block_height } = contract; |
| 65 | + const [address, name] = contract_id.split('.'); |
| 66 | + if (!address || !name) { |
| 67 | + logger.warn(` - Skipping invalid contract id: ${contract_id}`); |
| 68 | + continue; |
| 69 | + } |
| 70 | + |
| 71 | + try { |
| 72 | + // 3.3) Fetch ABI from the connected Stacks node |
| 73 | + const abi = await rpc.fetchJson<ClarityAbi>(`v2/contracts/interface/${address}/${name}`); |
| 74 | + |
| 75 | + if (!abi || typeof abi !== 'object' || Object.keys(abi).length === 0) { |
| 76 | + logger.warn(` - Skipping ${contract_id}. Fetched empty or invalid ABI.`); |
| 77 | + continue; |
| 78 | + } |
| 79 | + |
| 80 | + if (typeof abi === 'string' && abi === 'null') { |
| 81 | + logger.warn(` - Skipping ${contract_id}. Fetched "null" string ABI.`); |
| 82 | + continue; |
| 83 | + } |
| 84 | + |
| 85 | + // 3.4) Update row for this contract still missing an ABI |
| 86 | + const rows = await sql` |
| 87 | + UPDATE smart_contracts |
| 88 | + SET abi = ${abi} |
| 89 | + WHERE contract_id = ${contract_id} |
| 90 | + AND (abi::text = '"null"') |
| 91 | + AND canonical = TRUE |
| 92 | + `; |
| 93 | + if (rows.count === 0) { |
| 94 | + logger.warn(` - Failed to patch ${contract_id}. No rows updated.`); |
| 95 | + continue; |
| 96 | + } |
| 97 | + |
| 98 | + logger.info(` - Patched ABI for ${contract_id}`); |
| 99 | + totalPatchedCount++; |
| 100 | + } catch (err: any) { |
| 101 | + logger.error(err, ` - Failed to patch ${contract_id}`); |
| 102 | + } |
| 103 | + |
| 104 | + // Keep track of the latest block_height we've processed |
| 105 | + if (block_height > lastBlockHeight) { |
| 106 | + lastBlockHeight = block_height; |
| 107 | + logger.info(` - Processed up to block ${lastBlockHeight}`); |
| 108 | + } |
| 109 | + } |
| 110 | + |
| 111 | + // 3.5) Check if it was the last batch |
| 112 | + if (missing.length < BATCH_SIZE) { |
| 113 | + logger.info(` - Patched ${totalPatchedCount}/${totalConsideredCount} contracts.`); |
| 114 | + break; // Last batch was smaller than batch size, so no more items. |
| 115 | + } |
| 116 | + } |
| 117 | + } catch (err: any) { |
| 118 | + logger.error(err, 'An unexpected error occurred'); |
| 119 | + throw err; |
| 120 | + } finally { |
| 121 | + // 4) Close DB connection |
| 122 | + logger.info('Closing database connection...'); |
| 123 | + await sql.end({ timeout: 5 }); |
| 124 | + logger.info('Done.'); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +main().catch(err => { |
| 129 | + logger.error(err, 'An unexpected error occurred'); |
| 130 | + process.exit(1); |
| 131 | +}); |
0 commit comments