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 { SystemProgram } from '@solarti/web3.js';
import { assertSol, useOperation, } from '../../../types';
import { TransactionBuilder } from '../../../utils';
// -----------------
// Operation
// -----------------
const Key = 'TransferSolOperation';
/**
 * Transfers some SOL from one account to another.
 *
 * ```ts
 * await metaplex
 *   .system()
 *   .transferSol({
 *     to: new PublicKey("..."),
 *     amount: sol(1.5),
 *   };
 * ````
 *
 * @group Operations
 * @category Constructors
 */
export const transferSolOperation = useOperation(Key);
/**
 * @group Operations
 * @category Handlers
 */
export const transferSolOperationHandler = {
    handle(operation, metaplex, scope) {
        return __awaiter(this, void 0, void 0, function* () {
            const builder = transferSolBuilder(metaplex, operation.input, scope);
            return builder.sendAndConfirm(metaplex, scope.confirmOptions);
        });
    },
};
/**
 * Transfers some SOL from one account to another.
 *
 * ```ts
 * const transactionBuilder = metaplex
 *   .system()
 *   .builders()
 *   .transferSol({
 *     to: new PublicKey("..."),
 *     amount: sol(1.5),
 *   });
 * ````
 *
 * @group Transaction Builders
 * @category Constructors
 */
export const transferSolBuilder = (metaplex, params, options = {}) => {
    var _a;
    const { programs, payer = metaplex.rpc().getDefaultFeePayer() } = options;
    const { from = metaplex.identity(), to, amount, basePubkey, seed } = params;
    assertSol(amount);
    return TransactionBuilder.make()
        .setFeePayer(payer)
        .add({
        instruction: SystemProgram.transfer(Object.assign(Object.assign({ fromPubkey: from.publicKey, toPubkey: to, lamports: amount.basisPoints.toNumber() }, (basePubkey ? { basePubkey, seed } : {})), { programId: metaplex.programs().getSystem(programs).address })),
        signers: [from],
        key: (_a = params.instructionKey) !== null && _a !== void 0 ? _a : 'transferSol',
    });
};
//# sourceMappingURL=transferSol.js.map