UNPKG

@mean-dao/payment-streaming

Version:
647 lines (646 loc) 31.3 kB
"use strict"; 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()); }); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.buildCreateAccountInstruction = buildCreateAccountInstruction; exports.buildAddFundsInstruction = buildAddFundsInstruction; exports.buildCreateStreamInstruction = buildCreateStreamInstruction; exports.buildCreateAccountAndTemplateInstruction = buildCreateAccountAndTemplateInstruction; exports.buildUpdateStreamTemplateInstruction = buildUpdateStreamTemplateInstruction; exports.buildCreateStreamWithTemplateInstruction = buildCreateStreamWithTemplateInstruction; exports.buildAllocateFundsToStreamInstruction = buildAllocateFundsToStreamInstruction; exports.buildWithdrawFromAccountInstruction = buildWithdrawFromAccountInstruction; exports.buildRefreshAccountDataInstruction = buildRefreshAccountDataInstruction; exports.buildCloseAccountInstruction = buildCloseAccountInstruction; exports.buildWithdrawFromStreamInstruction = buildWithdrawFromStreamInstruction; exports.buildPauseStreamInstruction = buildPauseStreamInstruction; exports.buildResumeStreamInstruction = buildResumeStreamInstruction; exports.buildTransferStreamInstruction = buildTransferStreamInstruction; exports.buildCloseStreamInstruction = buildCloseStreamInstruction; const anchor_1 = require("@project-serum/anchor"); const spl_token_1 = require("@solana/spl-token"); const web3_js_1 = require("@solana/web3.js"); const constants_1 = require("./constants"); const types_1 = require("./types"); /** * Constructs a CreateAccount instruction. * * @param program - Anchor program created from the PS program IDL * @param accounts - Instruction accounts * @param name - Name for the new account * @param type - Either Open or Lock. Under locked accounts, once a stream * starts it cannot be paused or closed, they will run until out of funds * @param autoClose - If true, this account will be closed after the last * stream in it is closed * @param solFeePayedFromAccount - If true, protocol SOL fees will be payed * from the newly created account, otherwise from the {@link feePayer} account * @param category - Category of the new account * @param subCategory - Subcategory of the new account */ function buildCreateAccountInstruction(program_1, _a, name_1, type_1, autoClose_1, solFeePayedFromAccount_1) { return __awaiter(this, arguments, void 0, function* (program, { owner, mint, feePayer }, name, type, autoClose, solFeePayedFromAccount, category = types_1.Category.default, subCategory = types_1.SubCategory.default) { const [slotBn, psAccountSeeds] = yield getAccountSeeds(program.provider.connection, owner); const [psAccount] = web3_js_1.PublicKey.findProgramAddressSync(psAccountSeeds, program.programId); const psAccountToken = yield spl_token_1.Token.getAssociatedTokenAddress(constants_1.ASSOCIATED_TOKEN_PROGRAM_ID, constants_1.TOKEN_PROGRAM_ID, mint, psAccount, true); const instruction = yield program.methods .createTreasury(constants_1.LATEST_IDL_FILE_VERSION, slotBn, name || '', type, autoClose, solFeePayedFromAccount, { [types_1.Category[category]]: {} }, { [types_1.SubCategory[subCategory]]: {} }) .accounts({ payer: feePayer, treasurer: owner, treasury: psAccount, treasuryToken: psAccountToken, associatedToken: mint, feeTreasury: constants_1.FEE_ACCOUNT, associatedTokenProgram: constants_1.ASSOCIATED_TOKEN_PROGRAM_ID, tokenProgram: constants_1.TOKEN_PROGRAM_ID, systemProgram: constants_1.SYSTEM_PROGRAM_ID, rent: constants_1.SYSVAR_RENT_PUBKEY, }) .instruction(); return { instruction: instruction, psAccount: psAccount, psAccountToken: psAccountToken, }; }); } /** * Constructs an AddFunds instruction. * * @param program - Anchor program created from the PS program IDL * @param accounts - Instruction accounts * @param amount - Token amount to add */ function buildAddFundsInstruction(program_1, _a, amount_1) { return __awaiter(this, arguments, void 0, function* (program, { psAccount, psAccountMint, psAccountToken, contributor, contributorToken, feePayer, feeAccountToken, }, amount) { psAccountToken = yield ensureAssociatedTokenAddress(psAccountToken, psAccountMint, psAccount); contributorToken = yield ensureAssociatedTokenAddress(contributorToken, psAccountMint, contributor); feeAccountToken = yield ensureAssociatedTokenAddress(feeAccountToken, psAccountMint, constants_1.FEE_ACCOUNT); const instruction = yield program.methods .addFunds(constants_1.LATEST_IDL_FILE_VERSION, amount) .accounts({ payer: feePayer, contributor: contributor, contributorToken: contributorToken, treasury: psAccount, treasuryToken: psAccountToken, associatedToken: psAccountMint, feeTreasury: constants_1.FEE_ACCOUNT, feeTreasuryToken: feeAccountToken, associatedTokenProgram: constants_1.ASSOCIATED_TOKEN_PROGRAM_ID, tokenProgram: constants_1.TOKEN_PROGRAM_ID, systemProgram: constants_1.SYSTEM_PROGRAM_ID, rent: constants_1.SYSVAR_RENT_PUBKEY, }) .instruction(); return { instruction, psAccountToken, contributorToken, feeAccountToken, }; }); } /** * Constructs a crate stream instruction. * * @param program - Anchor program created from the PS program IDL * @param accounts - Instruction accounts * @param name - A name for the new stream * @param rateAmount - Token amount that will be streamed in every * {@link rateIntervalInSeconds} period * @param rateIntervalInSeconds - Period of time in seconds in which the * {@link rateAmount} will be streamed progressively second by second * @param allocationAssigned - Total token amount allocated to the new stream * out of the containing PS account's unallocated balance * @param startTs - Unix timestamp when the stream will start * @param cliffVestAmount - Token amount that is immediatelly withdrawable * by the beneficiary as soon as the stream starts. When * {@link cliffVestPercent} is greater than zero, this value will be ignored * @param cliffVestPercent - Percentage of {@link allocationAssigned} that is * immediatelly withdrawable by the {@link beneficiary} as soon as the * stream starts. It takes precedence over {@link cliffVestAmount}, i.e. when * this value is greater than zero, {@link cliffVestAmount} will be ignored. * This value will be provided in a range from 0 (0%) to 1_000_000 (100%) * @param tokenFeePayedFromAccount - If true, the protocol token fees will be * paid from {@link psAccountToken} and deposited upfront during stream * creation or allocation. If false, the beneficiary will pay for token fees * at withdraw time * @param usePda - If true, the new stream will be created at an address * derived from the program */ function buildCreateStreamInstruction(program_1, _a, name_1, rateAmount_1, rateIntervalInSeconds_1, allocationAssigned_1, startTs_1, cliffVestAmount_1, cliffVestPercent_1, tokenFeePayedFromAccount_1, usePda_1) { return __awaiter(this, arguments, void 0, function* (program, { psAccount, psAccountMint, psAccountToken, owner, beneficiary, feePayer, feeAccountToken, }, name, rateAmount, rateIntervalInSeconds, allocationAssigned, startTs, cliffVestAmount, cliffVestPercent, tokenFeePayedFromAccount, usePda) { psAccountToken = yield ensureAssociatedTokenAddress(psAccountToken, psAccountMint, psAccount); feeAccountToken = yield ensureAssociatedTokenAddress(feeAccountToken, psAccountMint, constants_1.FEE_ACCOUNT); if (usePda) { const streamPdaSeed = web3_js_1.Keypair.generate().publicKey; const [streamPda] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from('stream'), psAccount.toBuffer(), streamPdaSeed.toBuffer()], program.programId); const instruction = yield program.methods .createStreamPda(constants_1.LATEST_IDL_FILE_VERSION, name, startTs, rateAmount, rateIntervalInSeconds, allocationAssigned, cliffVestAmount, cliffVestPercent, tokenFeePayedFromAccount, streamPdaSeed) .accounts({ payer: feePayer, treasurer: owner, treasury: psAccount, treasuryToken: psAccountToken, associatedToken: psAccountMint, beneficiary: beneficiary, stream: streamPda, feeTreasury: constants_1.FEE_ACCOUNT, feeTreasuryToken: feeAccountToken, associatedTokenProgram: constants_1.ASSOCIATED_TOKEN_PROGRAM_ID, tokenProgram: constants_1.TOKEN_PROGRAM_ID, systemProgram: constants_1.SYSTEM_PROGRAM_ID, rent: constants_1.SYSVAR_RENT_PUBKEY, }) .instruction(); return { instruction, stream: streamPda, isPda: false, psAccountToken, feeAccountToken, }; } const streamKey = web3_js_1.Keypair.generate(); const instruction = yield program.methods .createStream(constants_1.LATEST_IDL_FILE_VERSION, name, startTs, rateAmount, rateIntervalInSeconds, allocationAssigned, cliffVestAmount, cliffVestPercent, tokenFeePayedFromAccount) .accounts({ payer: feePayer, treasurer: owner, treasury: psAccount, treasuryToken: psAccountToken, associatedToken: psAccountMint, beneficiary: beneficiary, stream: streamKey.publicKey, feeTreasury: constants_1.FEE_ACCOUNT, feeTreasuryToken: feeAccountToken, associatedTokenProgram: constants_1.ASSOCIATED_TOKEN_PROGRAM_ID, tokenProgram: constants_1.TOKEN_PROGRAM_ID, systemProgram: constants_1.SYSTEM_PROGRAM_ID, rent: constants_1.SYSVAR_RENT_PUBKEY, }) .signers([streamKey]) .instruction(); return { instruction, stream: streamKey.publicKey, streamKey, isPda: true, psAccountToken, feeAccountToken, }; }); } /** * Constructs an instruction to create a PS account together with a * configuration account (template) for creating streams. * * @param program - Anchor program created from the PS program IDL * @param accounts - Instruction accounts * @param accountName - Name for the new account * @param accountType - Either Open or Lock. Under locked accounts, once a stream * starts it cannot be paused or closed, they will run until out of funds * @param solFeePayedFromAccount - If true, protocol SOL fees will be payed * from the newly created account, otherwise from the {@link feePayer} account * @param streamTemplateOptions - Parameters for the stream template account * @param category - Category of the new account * @param subCategory - Subcategory of the new account */ function buildCreateAccountAndTemplateInstruction(program_1, _a, accountName_1, accountType_1, solFeePayedFromAccount_1, _b) { return __awaiter(this, arguments, void 0, function* (program, { owner, mint, feePayer }, accountName, accountType, solFeePayedFromAccount, { rateIntervalInSeconds, numberOfIntervals, startTs, cliffVestPercent, tokenFeePayedFromAccount, }, category = types_1.Category.default, subCategory = types_1.SubCategory.default) { const [slotBn, psAccountSeeds] = yield getAccountSeeds(program.provider.connection, owner); const [psAccount] = web3_js_1.PublicKey.findProgramAddressSync(psAccountSeeds, program.programId); const psAccountToken = yield spl_token_1.Token.getAssociatedTokenAddress(constants_1.ASSOCIATED_TOKEN_PROGRAM_ID, constants_1.TOKEN_PROGRAM_ID, mint, psAccount, true); // Template address const [template] = web3_js_1.PublicKey.findProgramAddressSync([anchor_1.utils.bytes.utf8.encode('template'), psAccount.toBuffer()], program.programId); const instruction = yield program.methods .createTreasuryAndTemplate(constants_1.LATEST_IDL_FILE_VERSION, accountName || '', accountType, false, solFeePayedFromAccount, { [types_1.Category[category]]: {} }, { [types_1.SubCategory[subCategory]]: {} }, startTs, rateIntervalInSeconds, numberOfIntervals, cliffVestPercent, tokenFeePayedFromAccount, slotBn) .accounts({ payer: feePayer, treasurer: owner, treasury: psAccount, treasuryToken: psAccountToken, template, associatedToken: mint, feeTreasury: constants_1.FEE_ACCOUNT, associatedTokenProgram: constants_1.ASSOCIATED_TOKEN_PROGRAM_ID, tokenProgram: constants_1.TOKEN_PROGRAM_ID, systemProgram: constants_1.SYSTEM_PROGRAM_ID, rent: constants_1.SYSVAR_RENT_PUBKEY, }) .instruction(); return { instruction: instruction, psAccount: psAccount, psAccountToken: psAccountToken, template: template, }; }); } /** * Constructs an instruction to update a stream template. * * @param program - Anchor program created from the PS program IDL * @param accounts - Instruction accounts * @param newRateIntervalInSeconds * @param newNumberOfIntervals * @param newStartTs * @param newCliffVestPercent * @param newTokenFeePayedFromAccount */ function buildUpdateStreamTemplateInstruction(program_1, _a, newRateIntervalInSeconds_1, newNumberOfIntervals_1, newStartTs_1, newCliffVestPercent_1, newTokenFeePayedFromAccount_1) { return __awaiter(this, arguments, void 0, function* (program, { psAccount, template, owner, feePayer, }, newRateIntervalInSeconds, newNumberOfIntervals, newStartTs, newCliffVestPercent, newTokenFeePayedFromAccount) { const instruction = yield program.methods .modifyStreamTemplate(constants_1.LATEST_IDL_FILE_VERSION, newStartTs, newRateIntervalInSeconds, newNumberOfIntervals, newCliffVestPercent, newTokenFeePayedFromAccount) .accounts({ payer: feePayer, template: template, treasurer: owner, treasury: psAccount, }) .instruction(); return { instruction: instruction, }; }); } /** * Constructs a crate stream instruction using the configuration * from a template account. This is similar to * {@link buildCreateStreamInstruction} but only {@link beneficiary}, * {@link allocationAssigned} and {@link name} are provided, the rest * is taken from the template. * * @param program - Anchor program created from the PS program IDL * @param accounts - Instruction accounts * @param allocationAssigned - Total token amount allocated to the new stream * out of the containing PS account's unallocated balance * @param name - A name for the new stream * @param usePda - If true, the new stream will be created at an address * derived from the program */ function buildCreateStreamWithTemplateInstruction(program_1, _a, allocationAssigned_1, name_1, usePda_1) { return __awaiter(this, arguments, void 0, function* (program, { psAccount, psAccountMint, psAccountToken, template, owner, feePayer, beneficiary, feeAccountToken, }, allocationAssigned, name, usePda) { psAccountToken = yield ensureAssociatedTokenAddress(psAccountToken, psAccountMint, psAccount); feeAccountToken = yield ensureAssociatedTokenAddress(feeAccountToken, psAccountMint, constants_1.FEE_ACCOUNT); if (usePda) { const streamPdaSeed = web3_js_1.Keypair.generate().publicKey; const [streamPda] = web3_js_1.PublicKey.findProgramAddressSync([Buffer.from('stream'), psAccount.toBuffer(), streamPdaSeed.toBuffer()], program.programId); const instruction = yield program.methods .createStreamPdaWithTemplate(constants_1.LATEST_IDL_FILE_VERSION, name, new anchor_1.BN(allocationAssigned), streamPdaSeed) .accounts({ payer: feePayer, treasurer: owner, treasury: psAccount, treasuryToken: psAccountToken, associatedToken: psAccountMint, beneficiary: beneficiary, template: template, stream: streamPda, feeTreasury: constants_1.FEE_ACCOUNT, feeTreasuryToken: feeAccountToken, associatedTokenProgram: constants_1.ASSOCIATED_TOKEN_PROGRAM_ID, tokenProgram: constants_1.TOKEN_PROGRAM_ID, systemProgram: constants_1.SYSTEM_PROGRAM_ID, rent: constants_1.SYSVAR_RENT_PUBKEY, }) .instruction(); return { instruction, stream: streamPda, isPda: false, psAccountToken, feeAccountToken, }; } const streamKey = web3_js_1.Keypair.generate(); const instruction = yield program.methods .createStreamWithTemplate(constants_1.LATEST_IDL_FILE_VERSION, name, new anchor_1.BN(allocationAssigned)) .accounts({ payer: feePayer, treasurer: owner, treasury: psAccount, treasuryToken: psAccountToken, associatedToken: psAccountMint, beneficiary: beneficiary, template: template, stream: streamKey.publicKey, feeTreasury: constants_1.FEE_ACCOUNT, feeTreasuryToken: feeAccountToken, associatedTokenProgram: constants_1.ASSOCIATED_TOKEN_PROGRAM_ID, tokenProgram: constants_1.TOKEN_PROGRAM_ID, systemProgram: constants_1.SYSTEM_PROGRAM_ID, rent: constants_1.SYSVAR_RENT_PUBKEY, }) .instruction(); return { instruction, stream: streamKey.publicKey, streamKey, isPda: true, psAccountToken, feeAccountToken, }; }); } /** * Constructs an Allocate instruction. * * @param program - Anchor program created from the PS program IDL * @param accounts - Instruction accounts * @param amount - Token amount to allocate out of the containing PS account * unallocated balance. */ function buildAllocateFundsToStreamInstruction(program_1, _a, amount_1) { return __awaiter(this, arguments, void 0, function* (program, { psAccount, psAccountMint, owner, feePayer, stream, psAccountToken, feeAccountToken, }, amount) { psAccountToken = yield ensureAssociatedTokenAddress(psAccountToken, psAccountMint, psAccount); feeAccountToken = yield ensureAssociatedTokenAddress(feeAccountToken, psAccountMint, constants_1.FEE_ACCOUNT); const instruction = yield program.methods .allocate(constants_1.LATEST_IDL_FILE_VERSION, amount) .accounts({ payer: feePayer, treasurer: owner, treasury: psAccount, treasuryToken: psAccountToken, associatedToken: psAccountMint, stream: stream, feeTreasury: constants_1.FEE_ACCOUNT, feeTreasuryToken: feeAccountToken, associatedTokenProgram: constants_1.ASSOCIATED_TOKEN_PROGRAM_ID, tokenProgram: constants_1.TOKEN_PROGRAM_ID, systemProgram: constants_1.SYSTEM_PROGRAM_ID, rent: constants_1.SYSVAR_RENT_PUBKEY, }) .instruction(); return { instruction, }; }); } /** * Constructs an instruction to withdraw funs from a Payment Streaing account. * * @param program - Anchor program created from the PS program IDL * @param accounts - Instruction accounts * @param amount - Token amount to withdraw */ function buildWithdrawFromAccountInstruction(program_1, _a, amount_1) { return __awaiter(this, arguments, void 0, function* (program, { psAccount, psAccountMint, owner, feePayer, destination, destinationToken, psAccountToken, feeAccountToken, }, amount) { destinationToken = yield ensureAssociatedTokenAddress(destinationToken, psAccountMint, destination); psAccountToken = yield ensureAssociatedTokenAddress(psAccountToken, psAccountMint, psAccount); feeAccountToken = yield ensureAssociatedTokenAddress(feeAccountToken, psAccountMint, constants_1.FEE_ACCOUNT); const instruction = yield program.methods .treasuryWithdraw(constants_1.LATEST_IDL_FILE_VERSION, amount) .accounts({ payer: feePayer, treasurer: owner, destinationAuthority: destination, destinationTokenAccount: destinationToken, associatedToken: psAccountMint, treasury: psAccount, treasuryToken: psAccountToken, feeTreasury: constants_1.FEE_ACCOUNT, feeTreasuryToken: feeAccountToken, associatedTokenProgram: constants_1.ASSOCIATED_TOKEN_PROGRAM_ID, tokenProgram: constants_1.TOKEN_PROGRAM_ID, systemProgram: constants_1.SYSTEM_PROGRAM_ID, rent: constants_1.SYSVAR_RENT_PUBKEY, }) .instruction(); return { instruction, destinationToken, psAccountToken, feeAccountToken, }; }); } /** * Constructs an instruction to refresh a Payment Streaming account after * funds are sent to it from outside of the program, i.e. using the * Token program directly. * * @param program - Anchor program created from the PS program IDL * @param accounts - Instruction accounts */ function buildRefreshAccountDataInstruction(program_1, _a) { return __awaiter(this, arguments, void 0, function* (program, { psAccount, psAccountMint, psAccountToken, }) { psAccountToken = yield ensureAssociatedTokenAddress(psAccountToken, psAccountMint, psAccount); const instruction = yield program.methods .refreshTreasuryData(constants_1.LATEST_IDL_FILE_VERSION) .accounts({ associatedToken: psAccountMint, treasury: psAccount, treasuryToken: psAccountToken, }) .instruction(); return { instruction, psAccountToken, }; }); } /** * Constructs an instruction to close a Payment Streaming account. * * @param program - Anchor program created from the PS program IDL * @param accounts - Instruction accounts */ function buildCloseAccountInstruction(program_1, _a) { return __awaiter(this, arguments, void 0, function* (program, { psAccount, psAccountMint, owner, feePayer, destination, destinationToken, psAccountToken, feeAccountToken, }) { destinationToken = yield ensureAssociatedTokenAddress(destinationToken, psAccountMint, destination); psAccountToken = yield ensureAssociatedTokenAddress(psAccountToken, psAccountMint, psAccount); feeAccountToken = yield ensureAssociatedTokenAddress(feeAccountToken, psAccountMint, constants_1.FEE_ACCOUNT); const instruction = yield program.methods .closeTreasury(constants_1.LATEST_IDL_FILE_VERSION) .accounts({ payer: feePayer, treasurer: owner, destinationAuthority: destination, destinationTokenAccount: destinationToken, associatedToken: psAccountMint, treasury: psAccount, treasuryToken: psAccountToken, feeTreasury: constants_1.FEE_ACCOUNT, feeTreasuryToken: feeAccountToken, associatedTokenProgram: constants_1.ASSOCIATED_TOKEN_PROGRAM_ID, tokenProgram: constants_1.TOKEN_PROGRAM_ID, systemProgram: constants_1.SYSTEM_PROGRAM_ID, rent: constants_1.SYSVAR_RENT_PUBKEY, }) .instruction(); return { instruction, destinationToken, psAccountToken, feeAccountToken, }; }); } /** * Constructs an instruction to withdraw funs from a stream. * * @param program - Anchor program created from the PS program IDL * @param accounts - Instruction accounts * @param amount */ function buildWithdrawFromStreamInstruction(program_1, _a, amount_1) { return __awaiter(this, arguments, void 0, function* (program, { psAccount, psAccountMint, stream, beneficiary, feePayer, beneficiaryToken, psAccountToken, feeAccountToken, }, amount) { beneficiaryToken = yield ensureAssociatedTokenAddress(beneficiaryToken, psAccountMint, beneficiary); psAccountToken = yield ensureAssociatedTokenAddress(psAccountToken, psAccountMint, psAccount); feeAccountToken = yield ensureAssociatedTokenAddress(feeAccountToken, psAccountMint, constants_1.FEE_ACCOUNT); const instruction = yield program.methods .withdraw(constants_1.LATEST_IDL_FILE_VERSION, amount) .accounts({ payer: feePayer, beneficiary: beneficiary, beneficiaryToken: beneficiaryToken, associatedToken: psAccountMint, treasury: psAccount, treasuryToken: psAccountToken, stream: stream, feeTreasury: constants_1.FEE_ACCOUNT, feeTreasuryToken: feeAccountToken, associatedTokenProgram: constants_1.ASSOCIATED_TOKEN_PROGRAM_ID, tokenProgram: constants_1.TOKEN_PROGRAM_ID, systemProgram: constants_1.SYSTEM_PROGRAM_ID, rent: constants_1.SYSVAR_RENT_PUBKEY, }) .instruction(); return { instruction, beneficiaryToken, psAccountToken, feeAccountToken, }; }); } /** * Constructs an instruction to pause a running stream. * * @param program - Anchor program created from the PS program IDL * @param accounts - Instruction accounts */ function buildPauseStreamInstruction(program_1, _a) { return __awaiter(this, arguments, void 0, function* (program, { psAccount, owner, stream }) { const instruction = yield program.methods .pauseStream(constants_1.LATEST_IDL_FILE_VERSION) .accounts({ initializer: owner, treasury: psAccount, stream: stream, }) .instruction(); return { instruction, }; }); } /** * Constructs an instruction to resume a paused stream. * * @param program - Anchor program created from the PS program IDL * @param accounts - Instruction accounts */ function buildResumeStreamInstruction(program_1, _a) { return __awaiter(this, arguments, void 0, function* (program, { psAccount, owner, stream }) { const instruction = yield program.methods .resumeStream(constants_1.LATEST_IDL_FILE_VERSION) .accounts({ initializer: owner, treasury: psAccount, stream: stream, }) .instruction(); return { instruction, }; }); } /** * Constructs an instruction to transfer a stream to a new beneficiary. * * @param program - Anchor program created from the PS program IDL * @param accounts - Instruction accounts */ function buildTransferStreamInstruction(program_1, _a) { return __awaiter(this, arguments, void 0, function* (program, { stream, beneficiary, newBeneficiary }) { const instruction = yield program.methods .transferStream(constants_1.LATEST_IDL_FILE_VERSION, newBeneficiary) .accounts({ beneficiary: beneficiary, stream: stream, feeTreasury: constants_1.FEE_ACCOUNT, systemProgram: constants_1.SYSTEM_PROGRAM_ID, }) .instruction(); return { instruction, }; }); } /** * Constructs an instruction to close a stream. * * @param program - Anchor program created from the PS program IDL * @param accounts - Instruction accounts */ function buildCloseStreamInstruction(program_1, _a) { return __awaiter(this, arguments, void 0, function* (program, { psAccount, psAccountMint, owner, stream, beneficiary, feePayer, beneficiaryToken, psAccountToken, feeAccountToken, }) { beneficiaryToken = yield ensureAssociatedTokenAddress(beneficiaryToken, psAccountMint, beneficiary); psAccountToken = yield ensureAssociatedTokenAddress(psAccountToken, psAccountMint, psAccount); feeAccountToken = yield ensureAssociatedTokenAddress(feeAccountToken, psAccountMint, constants_1.FEE_ACCOUNT); const instruction = yield program.methods .closeStream(constants_1.LATEST_IDL_FILE_VERSION) .accounts({ payer: feePayer, treasurer: owner, beneficiary: beneficiary, beneficiaryToken: beneficiaryToken, associatedToken: psAccountMint, treasury: psAccount, treasuryToken: psAccountToken, stream: stream, feeTreasury: constants_1.FEE_ACCOUNT, feeTreasuryToken: feeAccountToken, associatedTokenProgram: constants_1.ASSOCIATED_TOKEN_PROGRAM_ID, tokenProgram: constants_1.TOKEN_PROGRAM_ID, systemProgram: constants_1.SYSTEM_PROGRAM_ID, rent: constants_1.SYSVAR_RENT_PUBKEY, }) .instruction(); return { instruction, beneficiaryToken, psAccountToken, feeAccountToken, }; }); } //#region UTILS function getAccountSeeds(connection, owner) { return __awaiter(this, void 0, void 0, function* () { const slot = yield connection.getSlot(); const slotBn = new anchor_1.BN(slot); const slotBuffer = slotBn.toArrayLike(Buffer, 'le', 8); const psAccountSeeds = [owner.toBuffer(), slotBuffer]; return [slotBn, psAccountSeeds]; }); } function ensureAssociatedTokenAddress(associatedToken, mint, owner) { return __awaiter(this, void 0, void 0, function* () { if (associatedToken) { return associatedToken; } return spl_token_1.Token.getAssociatedTokenAddress(constants_1.ASSOCIATED_TOKEN_PROGRAM_ID, constants_1.TOKEN_PROGRAM_ID, mint, owner, true); }); } //#endregion