UNPKG

fsl-js-sdk

Version:
77 lines (76 loc) 2.93 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.createAssociatedTokenAccountInstruction = exports.createWSOLAccountInstructions = void 0; var spl_token_v0_1 = require("@solana/spl-token-v0"); var web3_js_1 = require("@solana/web3.js"); var __1 = require("../.."); var createWSOLAccountInstructions = function (owner, solMint, amountIn, rentExemptLamports) { var tempAccount = new web3_js_1.Keypair(); var createAccountInstruction = web3_js_1.SystemProgram.createAccount({ fromPubkey: owner, newAccountPubkey: tempAccount.publicKey, lamports: amountIn.toNumber() + rentExemptLamports, space: spl_token_v0_1.AccountLayout.span, programId: spl_token_v0_1.TOKEN_PROGRAM_ID, }); var initAccountInstruction = spl_token_v0_1.Token.createInitAccountInstruction(spl_token_v0_1.TOKEN_PROGRAM_ID, solMint, tempAccount.publicKey, owner); var closeWSOLAccountInstruction = spl_token_v0_1.Token.createCloseAccountInstruction(spl_token_v0_1.TOKEN_PROGRAM_ID, tempAccount.publicKey, owner, owner, []); return { address: tempAccount.publicKey, instructions: [createAccountInstruction, initAccountInstruction], cleanupInstructions: [closeWSOLAccountInstruction], signers: [tempAccount], }; }; exports.createWSOLAccountInstructions = createWSOLAccountInstructions; function createAssociatedTokenAccountInstruction(associatedTokenAddress, fundSource, destination, tokenMint, fundAddressOwner) { var systemProgramId = new web3_js_1.PublicKey('11111111111111111111111111111111'); var keys = [ { pubkey: fundSource, isSigner: true, isWritable: true, }, { pubkey: associatedTokenAddress, isSigner: false, isWritable: true, }, { pubkey: destination, isSigner: false, isWritable: false, }, { pubkey: tokenMint, isSigner: false, isWritable: false, }, { pubkey: systemProgramId, isSigner: false, isWritable: false, }, { pubkey: spl_token_v0_1.TOKEN_PROGRAM_ID, isSigner: false, isWritable: false, }, { pubkey: web3_js_1.SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false, }, ]; var createATAInstruction = new web3_js_1.TransactionInstruction({ keys: keys, programId: __1.SPL_ASSOCIATED_TOKEN_ACCOUNT_PROGRAM_ID, data: Buffer.from([]), }); return { instructions: [createATAInstruction], cleanupInstructions: [], signers: fundAddressOwner.signer ? [fundAddressOwner.signer] : [], }; } exports.createAssociatedTokenAccountInstruction = createAssociatedTokenAccountInstruction;