"use strict";
// This script fetches deposits on the sponsored CCTP bridge.
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    var desc = Object.getOwnPropertyDescriptor(m, k);
    if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
      desc = { enumerable: true, get: function() { return m[k]; } };
    }
    Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
    if (k2 === undefined) k2 = k;
    o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
    Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
    o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
    if (mod && mod.__esModule) return mod;
    var result = {};
    if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
    __setModuleDefault(result, mod);
    return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
    return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const anchor = __importStar(require("@coral-xyz/anchor"));
const anchor_1 = require("@coral-xyz/anchor");
const yargs_1 = __importDefault(require("yargs"));
const helpers_1 = require("yargs/helpers");
const web3_v1_1 = require("../../../src/svm/web3-v1");
// Set up the provider
const provider = anchor_1.AnchorProvider.env();
anchor.setProvider(provider);
const program = (0, web3_v1_1.getSponsoredCctpSrcPeripheryProgram)(provider);
// Parse arguments
const argvPromise = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv))
    .option("from", {
    type: "number",
    demandOption: false,
    default: 0,
    describe: "Filter deposits from this timestamp (in seconds since epoch)",
})
    .option("to", {
    type: "number",
    demandOption: false,
    describe: "Filter deposits to this timestamp (in seconds since epoch)",
}).argv;
async function queryDeposits() {
    const argv = await argvPromise;
    const fromTs = argv.from;
    const latestSlot = await provider.connection.getSlot("finalized");
    const latestBlockTime = await provider.connection.getBlockTime(latestSlot);
    if (!latestBlockTime) {
        throw new Error("Could not fetch latest block time");
    }
    const toTs = argv.to || latestBlockTime;
    const events = await (0, web3_v1_1.readProgramEvents)(provider.connection, program);
    const depositEvents = events.filter((event) => {
        return event.blockTime >= fromTs && event.blockTime <= toTs && event.name === "sponsoredDepositForBurn";
    });
    if (depositEvents.length === 0) {
        console.log("No deposit events found for the given time range.");
        return;
    }
    console.log("Sponsored deposit events fetched successfully:");
    depositEvents.forEach((event, index) => {
        console.log(`Sponsored deposit event ${index + 1}:`);
        console.table([
            { Property: "slot", Value: event.slot },
            { Property: "blockTime", Value: event.blockTime },
            { Property: "txSignature", Value: event.signature },
            { Property: "quoteNonce", Value: "0x" + event.data.quoteNonce.toString("hex") },
            { Property: "originSender", Value: event.data.originSender.toString() },
            { Property: "finalRecipient", Value: (0, web3_v1_1.publicKeyToEvmAddress)(event.data.finalRecipient) },
            { Property: "quoteDeadline", Value: event.data.quoteDeadline.toNumber() },
            { Property: "maxBpsToSponsor", Value: event.data.maxBpsToSponsor.toNumber() },
            { Property: "maxUserSlippageBps", Value: event.data.maxUserSlippageBps.toNumber() },
            { Property: "finalToken", Value: (0, web3_v1_1.publicKeyToEvmAddress)(event.data.finalToken) },
            { Property: "quoteSignature", Value: "0x" + event.data.signature.toString("hex") },
        ]);
    });
}
queryDeposits();
