"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.createDefaultTransaction = exports.signAndSendTransaction = void 0;
exports.sendTransactionWithLookupTable = sendTransactionWithLookupTable;
exports.createLookupTable = createLookupTable;
exports.extendLookupTable = extendLookupTable;
var tslib_1 = require("tslib");
var kit_1 = require("@solana/kit");
var address_lookup_table_1 = require("@solana-program/address-lookup-table");
var signAndSendTransaction = function (rpcClient_1, transactionMessage_1) {
    var args_1 = [];
    for (var _i = 2; _i < arguments.length; _i++) {
        args_1[_i - 2] = arguments[_i];
    }
    return tslib_1.__awaiter(void 0, tslib_1.__spreadArray([rpcClient_1, transactionMessage_1], args_1, true), void 0, function (rpcClient, transactionMessage, commitment) {
        var signedTransaction, signature;
        if (commitment === void 0) { commitment = "confirmed"; }
        return tslib_1.__generator(this, function (_a) {
            switch (_a.label) {
                case 0: return [4, (0, kit_1.signTransactionMessageWithSigners)(transactionMessage)];
                case 1:
                    signedTransaction = _a.sent();
                    (0, kit_1.assertIsTransactionWithBlockhashLifetime)(signedTransaction);
                    (0, kit_1.assertIsTransactionWithinSizeLimit)(signedTransaction);
                    signature = (0, kit_1.getSignatureFromTransaction)(signedTransaction);
                    return [4, (0, kit_1.sendAndConfirmTransactionFactory)(rpcClient)(signedTransaction, {
                            commitment: commitment,
                        })];
                case 2:
                    _a.sent();
                    return [2, signature];
            }
        });
    });
};
exports.signAndSendTransaction = signAndSendTransaction;
var createDefaultTransaction = function (rpcClient, signer) { return tslib_1.__awaiter(void 0, void 0, void 0, function () {
    var latestBlockhash;
    return tslib_1.__generator(this, function (_a) {
        switch (_a.label) {
            case 0: return [4, rpcClient.rpc.getLatestBlockhash().send()];
            case 1:
                latestBlockhash = (_a.sent()).value;
                return [2, (0, kit_1.pipe)((0, kit_1.createTransactionMessage)({ version: 0 }), function (tx) { return (0, kit_1.setTransactionMessageFeePayerSigner)(signer, tx); }, function (tx) { return (0, kit_1.setTransactionMessageLifetimeUsingBlockhash)(latestBlockhash, tx); })];
        }
    });
}); };
exports.createDefaultTransaction = createDefaultTransaction;
function sendTransactionWithLookupTable(client, payer, instructions, addressesByLookupTableAddress) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var _a;
        var _this = this;
        return tslib_1.__generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    _a = kit_1.pipe;
                    return [4, (0, exports.createDefaultTransaction)(client, payer)];
                case 1: return [2, _a.apply(void 0, [_b.sent(), function (tx) { return (0, kit_1.appendTransactionMessageInstructions)(instructions, tx); },
                        function (tx) { return (0, kit_1.compressTransactionMessageUsingAddressLookupTables)(tx, addressesByLookupTableAddress); },
                        function (tx) { return (0, kit_1.signTransactionMessageWithSigners)(tx); },
                        function (tx) { return tslib_1.__awaiter(_this, void 0, void 0, function () {
                            var signedTx;
                            return tslib_1.__generator(this, function (_a) {
                                switch (_a.label) {
                                    case 0: return [4, tx];
                                    case 1:
                                        signedTx = _a.sent();
                                        (0, kit_1.assertIsTransactionWithBlockhashLifetime)(signedTx);
                                        (0, kit_1.assertIsTransactionWithinSizeLimit)(signedTx);
                                        return [4, (0, kit_1.sendAndConfirmTransactionFactory)(client)(signedTx, {
                                                commitment: "confirmed",
                                                skipPreflight: false,
                                            })];
                                    case 2:
                                        _a.sent();
                                        return [2, (0, kit_1.getSignatureFromTransaction)(signedTx)];
                                }
                            });
                        }); }])];
            }
        });
    });
}
function createLookupTable(client, authority) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var recentSlot, alt, createAltIx, _a;
        return tslib_1.__generator(this, function (_b) {
            switch (_b.label) {
                case 0: return [4, client.rpc.getSlot({ commitment: "finalized" }).send()];
                case 1:
                    recentSlot = _b.sent();
                    return [4, (0, address_lookup_table_1.findAddressLookupTablePda)({
                            authority: authority.address,
                            recentSlot: recentSlot,
                        })];
                case 2:
                    alt = (_b.sent())[0];
                    return [4, (0, address_lookup_table_1.getCreateLookupTableInstructionAsync)({
                            authority: authority,
                            recentSlot: recentSlot,
                        })];
                case 3:
                    createAltIx = _b.sent();
                    _a = kit_1.pipe;
                    return [4, (0, exports.createDefaultTransaction)(client, authority)];
                case 4: return [4, _a.apply(void 0, [_b.sent(), function (tx) { return (0, kit_1.appendTransactionMessageInstruction)(createAltIx, tx); },
                        function (tx) { return (0, exports.signAndSendTransaction)(client, tx); }])];
                case 5:
                    _b.sent();
                    return [2, alt];
            }
        });
    });
}
function extendLookupTable(client, authority, alt, addresses) {
    return tslib_1.__awaiter(this, void 0, void 0, function () {
        var extendAltIx, _a, altAccount, addressesByLookupTableAddress;
        return tslib_1.__generator(this, function (_b) {
            switch (_b.label) {
                case 0:
                    extendAltIx = (0, address_lookup_table_1.getExtendLookupTableInstruction)({
                        address: alt,
                        authority: authority,
                        payer: authority,
                        addresses: addresses,
                    });
                    _a = kit_1.pipe;
                    return [4, (0, exports.createDefaultTransaction)(client, authority)];
                case 1: return [4, _a.apply(void 0, [_b.sent(), function (tx) { return (0, kit_1.appendTransactionMessageInstruction)(extendAltIx, tx); },
                        function (tx) { return (0, exports.signAndSendTransaction)(client, tx); }])];
                case 2:
                    _b.sent();
                    return [4, (0, address_lookup_table_1.fetchAddressLookupTable)(client.rpc, alt)];
                case 3:
                    altAccount = _b.sent();
                    addressesByLookupTableAddress = {};
                    addressesByLookupTableAddress[alt] = altAccount.data.addresses;
                    return [4, new Promise(function (resolve) { return setTimeout(resolve, 1000); })];
                case 4:
                    _b.sent();
                    return [2, addressesByLookupTableAddress];
            }
        });
    });
}
//# sourceMappingURL=transactionUtils.js.map