"use strict";
// This script fetches all fills for a given spoke pool.
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 web3_js_1 = require("@solana/web3.js");
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.getSpokePoolProgram)(provider);
const programId = program.programId;
// Parse arguments
const argv = (0, yargs_1.default)((0, helpers_1.hideBin)(process.argv)).option("seed", {
    type: "string",
    demandOption: true,
    describe: "Seed for the state account PDA",
}).argv;
async function queryFills() {
    const resolvedArgv = await argv;
    const seed = new anchor_1.BN(resolvedArgv.seed);
    // Define the state account PDA
    const [statePda, _] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("state"), seed.toArrayLike(Buffer, "le", 8)], programId);
    console.table([
        { Property: "seed", Value: seed.toString() },
        { Property: "programId", Value: programId.toString() },
        { Property: "statePda", Value: statePda.toString() },
    ]);
    try {
        const events = await (0, web3_v1_1.readProgramEvents)(provider.connection, program);
        const fillEvents = events.filter((event) => event.name === "filledRelay");
        if (fillEvents.length === 0) {
            console.log("No fill events found for the given seed.");
            return;
        }
        console.log("Fill events fetched successfully:");
        fillEvents.forEach((event, index) => {
            console.log(`Fill Event ${index + 1}:`);
            console.table([
                { Property: "inputToken", Value: (0, web3_v1_1.strPublicKey)(event.data.inputToken) },
                { Property: "outputToken", Value: (0, web3_v1_1.strPublicKey)(event.data.outputToken) },
                { Property: "inputAmount", Value: event.data.inputAmount.toString() },
                { Property: "outputAmount", Value: event.data.outputAmount.toString() },
                { Property: "repaymentChainId", Value: event.data.repaymentChainId.toString() },
                { Property: "originChainId", Value: event.data.originChainId.toString() },
                { Property: "depositId", Value: event.data.depositId.toString() },
                { Property: "depositIdNum", Value: (0, web3_v1_1.u8Array32ToInt)(event.data.depositId).toString() },
                { Property: "fillDeadline", Value: event.data.fillDeadline.toString() },
                { Property: "exclusivityDeadline", Value: event.data.exclusivityDeadline.toString() },
                { Property: "exclusiveRelayer", Value: (0, web3_v1_1.strPublicKey)(event.data.exclusiveRelayer) },
                { Property: "relayer", Value: (0, web3_v1_1.strPublicKey)(event.data.relayer) },
                { Property: "depositor", Value: (0, web3_v1_1.strPublicKey)(event.data.depositor) },
                { Property: "recipient", Value: (0, web3_v1_1.strPublicKey)(event.data.recipient) },
                { Property: "messageHash", Value: event.data.messageHash.toString() },
                { Property: "updatedRecipient", Value: (0, web3_v1_1.strPublicKey)(event.data.relayExecutionInfo.updatedRecipient) },
                { Property: "updatedMessageHash", Value: event.data.relayExecutionInfo.updatedMessageHash.toString() },
                { Property: "updatedOutputAmount", Value: event.data.relayExecutionInfo.updatedOutputAmount.toString() },
                { Property: "fillType", Value: event.data.relayExecutionInfo.fillType },
            ]);
        });
    }
    catch (error) {
        console.error("An error occurred while fetching the fill events:", error);
    }
}
// Run the queryFills function
queryFills();
