#!/usr/bin/env node
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
/**
 * One-shot: record all txs in a Chain 138 block on AddressActivityRegistry (+ optional v1 mirror).
 */
const ethers_1 = require("ethers");
const checkpoint_core_1 = require("@dbis/checkpoint-core");
const dualMirror_1 = require("./dualMirror");
const activityRegistry_1 = require("./activityRegistry");
const activityRegistryV2_1 = require("./activityRegistryV2");
const iso20022Enrich_1 = require("./iso20022Enrich");
const receiptsRoot_1 = require("./receiptsRoot");
const usdEnrich_1 = require("./usdEnrich");
function arg(name) {
    const i = process.argv.indexOf(`--${name}`);
    return i >= 0 ? process.argv[i + 1] : undefined;
}
async function main() {
    const block = parseInt(arg('block') || '0', 10);
    const batchId = BigInt(arg('batch-id') || '0');
    const registry = arg('registry') || '';
    const rpc138 = arg('rpc138') || 'http://192.168.11.211:8545';
    const rpc1 = arg('rpc1') || 'https://ethereum-rpc.publicnode.com';
    const mirror = arg('mirror') || '';
    const dualMirror = process.argv.includes('--dual-mirror');
    const dryRun = process.argv.includes('--dry-run');
    const pk = process.env.PRIVATE_KEY;
    if (!pk && !dryRun)
        throw new Error('PRIVATE_KEY required (or pass --dry-run)');
    if (!registry && !dryRun)
        throw new Error('--registry required');
    const chain138 = new ethers_1.ethers.JsonRpcProvider(rpc138);
    const blk = await chain138.getBlock(block, true);
    if (!blk)
        throw new Error(`block ${block} not found`);
    const leaves = [];
    const entries = blk.prefetchedTransactions?.length
        ? blk.prefetchedTransactions
        : blk.transactions ?? [];
    for (const entry of entries) {
        const tx = typeof entry === 'string' ? await chain138.getTransaction(entry) : entry;
        if (!tx || typeof tx === 'string')
            continue;
        const receipt = await chain138.getTransactionReceipt(tx.hash);
        const meta = await (0, receiptsRoot_1.fetchReceiptMeta)(chain138, tx.hash);
        const leaf = {
            txHash: tx.hash,
            from: tx.from,
            to: tx.to ?? ethers_1.ethers.ZeroAddress,
            value: tx.value,
            blockNumber: block,
            blockTimestamp: Number(blk.timestamp),
            gasUsed: receipt?.gasUsed ?? 0n,
            success: receipt?.status === 1,
            logCount: meta.logCount,
            receiptHash: meta.receiptHash,
        };
        const blockscoutApi = process.env.CHECKPOINT_BLOCKSCOUT_API || 'https://explorer.d-bis.org/api/v2';
        const { allErc20 } = await (0, usdEnrich_1.enrichLeafFromBlockscoutApi)(leaf, blockscoutApi, true);
        await (0, usdEnrich_1.enrichLeafUsd)(leaf, {
            apiBaseUrl: process.env.CHECKPOINT_TOKEN_AGGREGATION_URL ||
                process.env.TOKEN_AGGREGATION_API_URL ||
                'https://explorer.d-bis.org/api/v1',
            chainId: 138,
            enabled: process.env.CHECKPOINT_USD_ENRICH !== '0',
            requestDelayMs: 80,
            blockscoutApi,
            useBlockscout: true,
        }, allErc20);
        leaves.push(leaf);
        if (!dryRun) {
            console.log('leaf', tx.hash, 'wei', leaf.value.toString(), 'usdE8', (0, checkpoint_core_1.usdStringToE8)(leaf.totalTransfersUsd ?? leaf.valueUsd).toString());
        }
    }
    if (leaves.length === 0) {
        console.log('No transactions in block', block);
        return;
    }
    if (dryRun) {
        console.log(JSON.stringify({
            dryRun: true,
            block,
            batchId: batchId.toString(),
            registry: registry || null,
            txCount: leaves.length,
            leaves: leaves.map((l) => ({
                txHash: l.txHash,
                from: l.from,
                to: l.to,
                valueWei: l.value?.toString(),
                valueUsd: l.valueUsd,
                totalTransfersUsd: l.totalTransfersUsd,
                usdE8: (0, checkpoint_core_1.usdStringToE8)(l.totalTransfersUsd ?? l.valueUsd).toString(),
            })),
        }, null, 2));
        return;
    }
    const wallet = new ethers_1.ethers.Wallet(pk, new ethers_1.ethers.JsonRpcProvider(rpc1));
    (0, iso20022Enrich_1.attachIso20022ToLeaves)(leaves);
    const activityTx = await (0, activityRegistry_1.recordActivityBatch)(wallet, registry, batchId, leaves);
    console.log('AddressActivityRegistry tx', activityTx);
    const registryV2 = process.env.ADDRESS_ACTIVITY_REGISTRY_V2_MAINNET || '';
    if (registryV2) {
        const isoTx = await (0, activityRegistryV2_1.recordIsoAttestationBatch)(wallet, registryV2, batchId, leaves);
        console.log('AddressActivityRegistryV2 tx', isoTx);
    }
    if (dualMirror && mirror) {
        const mirrorTx = await (0, dualMirror_1.dualWriteV1Mirror)(wallet, mirror, leaves);
        console.log('TransactionMirror tx', mirrorTx);
    }
}
main().catch((e) => {
    console.error(e);
    process.exit(1);
});
