var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
    function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
    return new (P || (P = Promise))(function (resolve, reject) {
        function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
        function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
        function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
        step((generator = generator.apply(thisArg, _arguments || [])).next());
    });
};
import { Keypair, SystemProgram } from '@solarti/web3.js';
import { assertSol, useOperation, } from '../../../types';
import { TransactionBuilder } from '../../../utils';
// -----------------
// Operation
// -----------------
const Key = 'CreateAccountOperation';
/**
 * Creates a new uninitialized Solana account.
 *
 * ```ts
 * const { newAccount } = await metaplex
 *   .system()
 *   .createAccount({ space: 100 }); // 100 bytes
 * ```
 *
 * @group Operations
 * @category Constructors
 */
export const createAccountOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const createAccountOperationHandler = {
    handle(operation, metaplex, scope) {
        return __awaiter(this, void 0, void 0, function* () {
            const builder = yield createAccountBuilder(metaplex, operation.input, scope);
            scope.throwIfCanceled();
            return builder.sendAndConfirm(metaplex, scope.confirmOptions);
        });
    },
};
/**
 * Creates a new uninitialized Solana account.
 *
 * ```ts
 * const transactionBuilder = await metaplex
 *   .system()
 *   .builders()
 *   .createAccount({ space: 100 }); // 100 bytes
 * ```
 *
 * Note that accessing this transaction builder is asynchronous
 * because we may need to contact the cluster to get the
 * rent-exemption for the provided space.
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const createAccountBuilder = (metaplex, params, options = {}) => __awaiter(void 0, void 0, void 0, function* () {
    var _a, _b;
    const { payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const { space, newAccount = Keypair.generate(), program = SystemProgram.programId, } = params;
    const lamports = (_a = params.lamports) !== null && _a !== void 0 ? _a : (yield metaplex.rpc().getRent(space));
    assertSol(lamports);
    return TransactionBuilder.make()
        .setFeePayer(payer)
        .setContext({
        newAccount,
        lamports,
    })
        .add({
        instruction: SystemProgram.createAccount({
            fromPubkey: payer.publicKey,
            newAccountPubkey: newAccount.publicKey,
            space,
            lamports: lamports.basisPoints.toNumber(),
            programId: program,
        }),
        signers: [payer, newAccount],
        key: (_b = params.instructionKey) !== null && _b !== void 0 ? _b : 'createAccount',
    });
});
//# sourceMappingURL=createAccount.js.map