UNPKG

@mean-dao/payment-streaming

Version:
341 lines (340 loc) 18 kB
/** * Solana */ import { Commitment, Connection, Finality, PublicKey, Transaction } from '@solana/web3.js'; import { BN } from '@project-serum/anchor'; /** * MSP */ import { Category, ListStreamParams, PaymentStreamingAccount, AccountType, Stream, StreamEventData, StreamTemplate, SubCategory, TimeUnit, AccountActivity, StreamActivity, TransferTransactionAccounts, ScheduleTransferTransactionAccounts, StreamPaymentTransactionAccounts, CreateAccountTransactionAccounts, CreateStreamTransactionAccounts, CreateVestingAccountTransactionAccounts, UpdateVestingTemplateTransactionAccounts, CreateVestingStreamTransactionAccounts, AddFundsToAccountTransactionAccounts, AllocateFundsToStreamTransactionAccounts, FundStreamTransactionAccounts, WithdrawFromAccountTransactionAccounts, RefreshAccountDataTransactionAccounts, CloseAccountTransactionAccounts, WithdrawFromStreamTransactionAccounts, PauseResumeStreamTransactionAccounts, TransferStreamTransactionAccounts, CloseStreamTransactionAccounts } from './types'; import { WARNING_TYPES } from './constants'; /** * TS Client to interact with the Payment Streaming (PS) program. */ export declare class PaymentStreaming { private connection; private program; private blockhashCommitment; /** * Creates a Payment Streaming client * * @param connection Connectin to use * @param programId Payment Streaming program ID. By default is the mainnet ID * @param blockhashCommitment Commitment used to fetch the latest `blockhash` * and corresponding `lastValidBlockHeight` */ constructor(connection: Connection, programId?: PublicKey, blockhashCommitment?: Commitment); getStream(id: PublicKey): Promise<Stream | null>; getStreamRaw(id: PublicKey): Promise<StreamEventData | null>; refreshStream(streamInfo: Stream, hardUpdate?: boolean): Promise<Stream | null>; listStreams({ psAccountOwner, psAccount, beneficiary, category, subCategory, }: ListStreamParams): Promise<Stream[]>; refreshStreams(streamInfoList: Stream[], psAccountOwner?: PublicKey | undefined, psAccount?: PublicKey | undefined, beneficiary?: PublicKey | undefined, hardUpdate?: boolean): Promise<Stream[]>; /** * * @param id The address of the stream * @param before The signature to start searching backwards from. * @param limit The max amount of elements to retrieve * @param commitment Commitment to query the stream activity * @returns */ listStreamActivity(id: PublicKey, before?: string, limit?: number, commitment?: Finality | undefined): Promise<StreamActivity[]>; getAccount(id: PublicKey, commitment?: Commitment | undefined): Promise<PaymentStreamingAccount>; listAccounts(owner: PublicKey | undefined, excludeAutoClose?: boolean, category?: Category, subCategory?: SubCategory): Promise<PaymentStreamingAccount[]>; getStreamTemplate(psAccount: PublicKey): Promise<StreamTemplate>; private prepareTransaction; private createTransaction; /** * Contructs a transaction to perform a simple transfer of tokens to a * beneficiary using the Token program. * * @param accounts - Transaction accounts * @param amount - The token amount to be sent */ buildTransferTransaction({ sender, feePayer, beneficiary, mint }: TransferTransactionAccounts, amount: string | number): Promise<{ transaction: Transaction; }>; /** * Returns a transaction for scheduling a transfer as a stream without rate. * * @param accounts - Transaction accoutns * @param amount - The token amount to be allocated to the stream * @param startUtc - The date on which the transfer will be executed * @param streamName - The name of the transfer * @param tokenFeePayedByOwner - If true, the protocol token fees will be paid by the * {@link owner}, otherwise by the {@link beneficiary} at withdraw time */ buildScheduleTransferTransaction({ owner, feePayer, beneficiary, mint }: ScheduleTransferTransactionAccounts, amount: string | number, startUtc?: Date, streamName?: string, tokenFeePayedByOwner?: boolean): Promise<{ transaction: Transaction; stream: PublicKey; }>; /** * Constructs a transaction to create a recurring payment at a given rate to * start immediately or scheduled. * * @param owner - Transaction accounts * @param streamName - The name of the transfer * @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 - The token amount to be allocated to the stream * @param startUtc - The date and time on which the transfer will be executed * @param tokenFeePayedByOwner - If true, the protocol token fees will be * paid from {@link psAccountToken} and deposited upfront by the owner. If * false, the beneficiary will paid the token fees at withdraw time */ buildStreamPaymentTransaction({ owner, feePayer, beneficiary, mint }: StreamPaymentTransactionAccounts, streamName: string, rateAmount: string | number, rateIntervalInSeconds: number, allocationAssigned: string | number, startUtc?: Date, tokenFeePayedByOwner?: boolean): Promise<{ transaction: Transaction; psAccount: PublicKey; psAccountToken: PublicKey; stream: PublicKey; }>; /** * Constructs a transaction for creating a PS account. * * @param accounts - Transaction 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 solFeePayedFromAccount * @param category * @param subCategory * @returns */ buildCreateAccountTransaction({ owner, feePayer, mint }: CreateAccountTransactionAccounts, name: string, type: AccountType, solFeePayedFromAccount?: boolean, category?: Category, subCategory?: SubCategory): Promise<{ transaction: Transaction; psAccount: PublicKey; psAccountToken: PublicKey; }>; /** * Constructs a transaction for creating a stream under a PS account. * * @param accounts - Transaction accounts * @param streamName - 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 startUtc - Date and time when the stream will start * @param cliffVestAmount - Token amount that is immediatelly withdrawable * by the {@link beneficiary} as soon as the stream starts. When * {@link cliffVestPercent} is greater than zero, this value will be ignored * @param cliffVestPercent - The 0-100 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 * @param tokenFeePayedFromAccount - If true, the protocol token fees will be * paid from the PS account ATA 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 */ buildCreateStreamTransaction({ psAccount, owner, feePayer, beneficiary, }: CreateStreamTransactionAccounts, streamName: string, rateAmount: number | string, rateIntervalInSeconds: number, allocationAssigned: number | string, startUtc?: Date, cliffVestAmount?: number | string, cliffVestPercent?: number, tokenFeePayedFromAccount?: boolean, usePda?: boolean): Promise<{ transaction: Transaction; stream: PublicKey; }>; /** * Constructs a transaction to create vesting contract account together with a * configuration account (template) for creating vesting streams. * * @param accounts - Transaction accounts * @param name - Name for the vesting contract 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 solFeePayedFromAccount - If true, protocol SOL fees will be payed * from the newly created account, otherwise from the {@link feePayer} account * @param numberOfIntervals - Number of intervals of duration * {@link rateIntervalInSeconds} in which the allocation assigned will be * streamed * @param intervalUnit - Duration of each interval (E.g. for 1 minute * intervals pass {@link TimeUnit.Minute}, for 1 hour intervals pass * {@link TimeUnit.Hour} and so on). See {@link TimeUnit} enum * @param fundingAmount - The token amount to fund the newly creawted * vesting account if a value greater than 0 is provided * @param vestingCategory - Category for the vesting contract account * @param startUtc - The vesting contract start date * @param cliffVestPercent - When a vesting stream is created using this * template, this is the 0-100 percentage of the allocation assigned to the * stream that is immediatelly withdrawable by the beneficiary as soon as the * vesting stream starts * @param tokenFeePayedFromAccount - If true, the protocol token fees will be * paid from PS account ATA and deposited upfront during stream * creation or allocation. If false, the beneficiary will pay for token fees * at withdraw time */ buildCreateVestingAccountTransaction({ owner, feePayer, mint }: CreateVestingAccountTransactionAccounts, name: string, type: AccountType, solFeePayedFromAccount: boolean, numberOfIntervals: number, intervalUnit: TimeUnit, fundingAmount: string | number, vestingCategory: SubCategory, startUtc?: Date, cliffVestPercent?: number, tokenFeePayedFromAccount?: boolean): Promise<{ transaction: Transaction; vestingAccount: PublicKey; vestingAccountToken: PublicKey; template: PublicKey; }>; /** * Constructs a transaction for updating values of vesting account * template if no streams have been created yet. * * @param accounts - Transaction accounts * @param numberOfIntervals - The new number of intervals * @param intervalUnit - The new interval unit * @param startUtc - The new start date * @param cliffVestPercent - The new cliff percentage * @param tokenFeePayedFromAccount - The new tokenFeePayedFromAccount */ buildUpdateVestingTemplateTransaction({ owner, feePayer, vestingAccount, }: UpdateVestingTemplateTransactionAccounts, numberOfIntervals?: number, intervalUnit?: TimeUnit, startUtc?: Date, cliffVestPercent?: number, tokenFeePayedFromAccount?: boolean): Promise<{ transaction: Transaction; }>; /** * Returns a list with the activity of a vesting account. * * @param vestingAccount - The vesting account * @param before - The signature to start searching backwards from. * @param limit - The max amount of elements to retrieve * @param commitment - Commitment to query the vesting account activity */ listAccountActivity(vestingAccount: PublicKey, before?: string, limit?: number, commitment?: Finality | undefined): Promise<AccountActivity[]>; /** * Gets the flowing rate of a vesting contract. * * @param vestingAccount - The address of the vesting contract account * @param onlyRunning - If true, only running streams will be accounted */ getVestingAccountFlowRate(vestingAccount: PublicKey, onlyRunning?: boolean): Promise<{ rateAmount: BN; intervalUnit: TimeUnit; totalAllocation: BN; }>; /** * Creates a vesting stream based on the vesting contract template. * * @param accounts - Transaction accounts * @param streamName - A name for the new stream * @param allocationAssigned - Total token amount allocated to the new stream * out of the containing vesting account's unallocated balance */ buildCreateVestingStreamTransaction({ vestingAccount, owner, feePayer, beneficiary, }: CreateVestingStreamTransactionAccounts, allocationAssigned: string | number, streamName?: string, usePda?: boolean): Promise<{ transaction: Transaction; stream: PublicKey; template: PublicKey; }>; /** * Constructs a transaction to add funds to a PS account. The funds are * added as unallocated balance. * * @param accounts - Transaction accounts * @param amount - Token amount to add */ buildAddFundsToAccountTransaction({ psAccount, psAccountMint, contributor, feePayer, }: AddFundsToAccountTransactionAccounts, amount: string | number): Promise<{ transaction: Transaction; }>; /** * Constructs a transaction to allocate funds to a stream from the PS * account unallocated balance. * * @param accounts - Transaction accounts * @param amount - Token amount to allocate */ buildAllocateFundsToStreamTransaction({ psAccount, owner, feePayer, stream, }: AllocateFundsToStreamTransactionAccounts, amount: string | number): Promise<{ transaction: Transaction; }>; /** * Constructs a transaction which does both: adding funds to a PS account * and allocating the funds to the specified stream. * * @param accounts - Transaction accounts * @param amount * @param autoWSol - Whether a wrap SOL instruction should be included in * the transaction if necessary */ buildFundStreamTransaction({ psAccount, owner, feePayer, stream }: FundStreamTransactionAccounts, amount: string | number, autoWSol?: boolean): Promise<{ transaction: Transaction; }>; /** * Constructs a transaction to withdraw funds from a Payment Streaming * account. * * @param accounts - Transaction accounts * @param amount - Token amount to withdraw * @param autoWSol - Whether a wrap SOL instruction should be included in * the transaction if necessary */ buildWithdrawFromAccountTransaction({ psAccount, feePayer, destination, }: WithdrawFromAccountTransactionAccounts, amount: number | string, autoWSol?: boolean): Promise<{ transaction: Transaction; }>; /** * Constructs a transaction 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 accounts - Transaction accounts */ buildRefreshAccountDataTransaction({ psAccount, feePayer, }: RefreshAccountDataTransactionAccounts): Promise<{ transaction: Transaction; }>; /** * Constructs a transaction to close a Payment Streaming account. * * @param accounts - Transaction accounts * @param autoWSol - Whether a wrap SOL instruction should be included in * the transaction if necessary */ buildCloseAccountTransaction({ psAccount, feePayer, destination }: CloseAccountTransactionAccounts, autoWSol?: boolean): Promise<{ transaction: Transaction; }>; /** * Constructs a transaction to withdraw funds from a stream. * * @param accounts - Transaction accounts * @param amount - The token amount to withdraw * @param autoWSol - Whether a wrap SOL instruction should be included in * the transaction if necessary */ buildWithdrawFromStreamTransaction({ stream, feePayer }: WithdrawFromStreamTransactionAccounts, amount: number | string, autoWSol?: boolean): Promise<{ transaction: Transaction; }>; /** * Constructs a transaction to pause a running stream. * * @param accounts - Transaction accounts */ buildPauseStreamTransaction({ stream, owner, feePayer, }: PauseResumeStreamTransactionAccounts): Promise<{ transaction: Transaction; }>; /** * Constructs a transaction to resume a paused stream. * * @param accounts - Transaction accounts */ buildResumeStreamTransaction({ stream, owner, feePayer, }: PauseResumeStreamTransactionAccounts): Promise<{ transaction: Transaction; }>; /** * Constructs a transaction to transfer a stream. * * @param accounts - Transaction accounts */ buildTransferStreamTransaction({ stream, beneficiary, newBeneficiary, feePayer, }: TransferStreamTransactionAccounts): Promise<{ transaction: Transaction; }>; /** * Constructs a transaction to close a stream. * * @param accounts - Transaction accounts * @param autoWSol - Whether a wrap SOL instruction should be included in * the transaction if necessary * @param autoCloseAccount - If true, an instruction will be included in * the resulting transaction to close the containing PS account */ buildCloseStreamTransaction({ stream, feePayer, destination }: CloseStreamTransactionAccounts, autoCloseAccount?: boolean, autoWSol?: boolean): Promise<{ transaction: Transaction; }>; private ensureAutoWrapSolInstructions; /** * Validates the given address * @param address Solana public address * @returns one of the WARNING_TYPES as result */ checkAddressForWarnings(address: string): Promise<WARNING_TYPES>; }