"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.loadExecuteRelayerRefundLeafParams = loadExecuteRelayerRefundLeafParams;
exports.closeInstructionParams = closeInstructionParams;
exports.createFillRelayParamsInstructions = createFillRelayParamsInstructions;
exports.loadFillRelayParams = loadFillRelayParams;
exports.loadRequestSlowFillParams = loadRequestSlowFillParams;
exports.loadExecuteSlowRelayLeafParams = loadExecuteSlowRelayLeafParams;
var tslib_1 = require("tslib");
var web3_js_1 = require("@solana/web3.js");
var coders_1 = require("./coders");
function loadExecuteRelayerRefundLeafParams(program, caller, rootBundleId, relayerRefundLeaf, proof) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var maxInstructionParamsFragment, instructionParams, accountInfo, accountCoder, instructionParamsBytes, i, fragment;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    maxInstructionParamsFragment = 900;
                    instructionParams = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("instruction_params"), caller.toBuffer()], program.programId)[0];
                    return [4, program.provider.connection.getAccountInfo(instructionParams)];
                case 1:
                    accountInfo = _a.sent();
                    if (!(accountInfo !== null)) return [3, 3];
                    return [4, program.methods.closeInstructionParams().rpc()];
                case 2:
                    _a.sent();
                    _a.label = 3;
                case 3:
                    accountCoder = new coders_1.LargeAccountsCoder(program.idl);
                    return [4, accountCoder.encode("executeRelayerRefundLeafParams", {
                            rootBundleId: rootBundleId,
                            relayerRefundLeaf: relayerRefundLeaf,
                            proof: proof,
                        })];
                case 4:
                    instructionParamsBytes = _a.sent();
                    return [4, program.methods.initializeInstructionParams(instructionParamsBytes.length).rpc()];
                case 5:
                    _a.sent();
                    i = 0;
                    _a.label = 6;
                case 6:
                    if (!(i < instructionParamsBytes.length)) return [3, 9];
                    fragment = instructionParamsBytes.slice(i, i + maxInstructionParamsFragment);
                    return [4, program.methods.writeInstructionParamsFragment(i, fragment).rpc()];
                case 7:
                    _a.sent();
                    _a.label = 8;
                case 8:
                    i += maxInstructionParamsFragment;
                    return [3, 6];
                case 9: return [2, instructionParams];
            }
        });
    });
}
function closeInstructionParams(program, signer) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var instructionParams, accountInfo, closeIx;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0:
                    instructionParams = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from("instruction_params"), signer.publicKey.toBuffer()], program.programId)[0];
                    return [4, program.provider.connection.getAccountInfo(instructionParams)];
                case 1:
                    accountInfo = _a.sent();
                    if (!(accountInfo !== null)) return [3, 4];
                    return [4, program.methods.closeInstructionParams().accounts({ signer: signer.publicKey }).instruction()];
                case 2:
                    closeIx = _a.sent();
                    return [4, (0, web3_js_1.sendAndConfirmTransaction)(program.provider.connection, new web3_js_1.Transaction().add(closeIx), [signer])];
                case 3:
                    _a.sent();
                    _a.label = 4;
                case 4: return [2];
            }
        });
    });
}
function createFillRelayParamsInstructions(program, signer, relayData, repaymentChainId, repaymentAddress) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var maxInstructionParamsFragment, accountCoder, instructionParamsBytes, loadInstructions, _a, _b, i, fragment, _c, _d, closeInstruction;
        return tslib_1.__generator(this, function (_e) {
            switch (_e.label) {
                case 0:
                    maxInstructionParamsFragment = 900;
                    accountCoder = new coders_1.LargeAccountsCoder(program.idl);
                    return [4, accountCoder.encode("fillRelayParams", {
                            relayData: relayData,
                            repaymentChainId: repaymentChainId,
                            repaymentAddress: repaymentAddress,
                        })];
                case 1:
                    instructionParamsBytes = _e.sent();
                    loadInstructions = [];
                    _b = (_a = loadInstructions).push;
                    return [4, program.methods.initializeInstructionParams(instructionParamsBytes.length).accounts({ signer: signer }).instruction()];
                case 2:
                    _b.apply(_a, [_e.sent()]);
                    i = 0;
                    _e.label = 3;
                case 3:
                    if (!(i < instructionParamsBytes.length)) return [3, 6];
                    fragment = instructionParamsBytes.slice(i, i + maxInstructionParamsFragment);
                    _d = (_c = loadInstructions).push;
                    return [4, program.methods.writeInstructionParamsFragment(i, fragment).accounts({ signer: signer }).instruction()];
                case 4:
                    _d.apply(_c, [_e.sent()]);
                    _e.label = 5;
                case 5:
                    i += maxInstructionParamsFragment;
                    return [3, 3];
                case 6: return [4, program.methods.closeInstructionParams().accounts({ signer: signer }).instruction()];
                case 7:
                    closeInstruction = _e.sent();
                    return [2, { loadInstructions: loadInstructions, closeInstruction: closeInstruction }];
            }
        });
    });
}
function loadFillRelayParams(program, signer, relayData, repaymentChainId, repaymentAddress) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var loadInstructions, i;
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4, closeInstructionParams(program, signer)];
                case 1:
                    _a.sent();
                    return [4, createFillRelayParamsInstructions(program, signer.publicKey, relayData, repaymentChainId, repaymentAddress)];
                case 2:
                    loadInstructions = (_a.sent()).loadInstructions;
                    i = 0;
                    _a.label = 3;
                case 3:
                    if (!(i < loadInstructions.length)) return [3, 6];
                    return [4, (0, web3_js_1.sendAndConfirmTransaction)(program.provider.connection, new web3_js_1.Transaction().add(loadInstructions[i]), [
                            signer,
                        ])];
                case 4:
                    _a.sent();
                    _a.label = 5;
                case 5:
                    i += 1;
                    return [3, 3];
                case 6: return [2];
            }
        });
    });
}
function loadRequestSlowFillParams(program, signer, relayData) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var maxInstructionParamsFragment, accountCoder, instructionParamsBytes, loadInstructions, _a, _b, i, fragment, _c, _d;
        return tslib_1.__generator(this, function (_e) {
            switch (_e.label) {
                case 0: return [4, closeInstructionParams(program, signer)];
                case 1:
                    _e.sent();
                    maxInstructionParamsFragment = 900;
                    accountCoder = new coders_1.LargeAccountsCoder(program.idl);
                    return [4, accountCoder.encode("requestSlowFillParams", { relayData: relayData })];
                case 2:
                    instructionParamsBytes = _e.sent();
                    loadInstructions = [];
                    _b = (_a = loadInstructions).push;
                    return [4, program.methods
                            .initializeInstructionParams(instructionParamsBytes.length)
                            .accounts({ signer: signer.publicKey })
                            .instruction()];
                case 3:
                    _b.apply(_a, [_e.sent()]);
                    i = 0;
                    _e.label = 4;
                case 4:
                    if (!(i < instructionParamsBytes.length)) return [3, 7];
                    fragment = instructionParamsBytes.slice(i, i + maxInstructionParamsFragment);
                    _d = (_c = loadInstructions).push;
                    return [4, program.methods
                            .writeInstructionParamsFragment(i, fragment)
                            .accounts({ signer: signer.publicKey })
                            .instruction()];
                case 5:
                    _d.apply(_c, [_e.sent()]);
                    _e.label = 6;
                case 6:
                    i += maxInstructionParamsFragment;
                    return [3, 4];
                case 7: return [2, loadInstructions];
            }
        });
    });
}
function loadExecuteSlowRelayLeafParams(program, signer, slowFillLeaf, rootBundleId, proof) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var maxInstructionParamsFragment, accountCoder, instructionParamsBytes, loadInstructions, _a, _b, i, fragment, _c, _d;
        return tslib_1.__generator(this, function (_e) {
            switch (_e.label) {
                case 0: return [4, closeInstructionParams(program, signer)];
                case 1:
                    _e.sent();
                    maxInstructionParamsFragment = 900;
                    accountCoder = new coders_1.LargeAccountsCoder(program.idl);
                    return [4, accountCoder.encode("executeSlowRelayLeafParams", {
                            slowFillLeaf: slowFillLeaf,
                            rootBundleId: rootBundleId,
                            proof: proof,
                        })];
                case 2:
                    instructionParamsBytes = _e.sent();
                    loadInstructions = [];
                    _b = (_a = loadInstructions).push;
                    return [4, program.methods
                            .initializeInstructionParams(instructionParamsBytes.length)
                            .accounts({ signer: signer.publicKey })
                            .instruction()];
                case 3:
                    _b.apply(_a, [_e.sent()]);
                    i = 0;
                    _e.label = 4;
                case 4:
                    if (!(i < instructionParamsBytes.length)) return [3, 7];
                    fragment = instructionParamsBytes.slice(i, i + maxInstructionParamsFragment);
                    _d = (_c = loadInstructions).push;
                    return [4, program.methods
                            .writeInstructionParamsFragment(i, fragment)
                            .accounts({ signer: signer.publicKey })
                            .instruction()];
                case 5:
                    _d.apply(_c, [_e.sent()]);
                    _e.label = 6;
                case 6:
                    i += maxInstructionParamsFragment;
                    return [3, 4];
                case 7: return [2, loadInstructions];
            }
        });
    });
}
//# sourceMappingURL=instructionParamsUtils.js.map