Skip to content

Commit

Permalink
create ata on withdrawal if it does not exist (#115)
Browse files Browse the repository at this point in the history
  • Loading branch information
Yolley authored Dec 25, 2023
1 parent 350ddca commit 977658b
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lerna.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
"packages": [
"packages/*"
],
"version": "5.8.1",
"version": "5.9.0",
"$schema": "node_modules/lerna/schemas/lerna-schema.json"
}
2 changes: 1 addition & 1 deletion packages/stream/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@streamflow/stream",
"version": "5.8.1",
"version": "5.9.0",
"description": "JavaScript SDK to interact with Streamflow protocol.",
"main": "dist/index.js",
"homepage": "https://github.com/streamflow-finance/js-sdk/",
Expand Down
27 changes: 27 additions & 0 deletions packages/stream/solana/StreamClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ import {
sendAndConfirmRawTransaction,
BlockheightBasedTransactionConfirmationStrategy,
} from "@solana/web3.js";
import { createAssociatedTokenAccountInstruction } from "@solana/spl-token";
import * as borsh from "borsh";

import {
Expand All @@ -30,6 +31,7 @@ import {
ICreateStreamSolanaExt,
IInteractStreamSolanaExt,
ITopUpStreamSolanaExt,
CheckAssociatedTokenAccountData,
} from "./types";
import {
ata,
Expand Down Expand Up @@ -477,6 +479,7 @@ export default class SolanaStreamClient extends BaseStreamClient {

const streamflowTreasuryTokens = await ata(data.mint, STREAMFLOW_TREASURY_PUBLIC_KEY);
const partnerTokens = await ata(data.mint, data.partner);
await this.checkAssociatedTokenAccount(data, { invoker }, ixs);

ixs.push(
withdrawStreamInstruction(amount, this.programId, {
Expand Down Expand Up @@ -531,6 +534,7 @@ export default class SolanaStreamClient extends BaseStreamClient {
const partnerTokens = await ata(data.mint, data.partner);

const ixs: TransactionInstruction[] = [];
await this.checkAssociatedTokenAccount(data, { invoker }, ixs);

ixs.push(
cancelStreamInstruction(this.programId, {
Expand Down Expand Up @@ -942,6 +946,29 @@ export default class SolanaStreamClient extends BaseStreamClient {
return { tx, metadataPubKey };
}

/**
* Utility function that checks whether associated token account for the recipient exists and adds an instruction to add if not
*/
private async checkAssociatedTokenAccount(
data: CheckAssociatedTokenAccountData,
{ invoker }: IInteractStreamSolanaExt,
ixs: TransactionInstruction[]
) {
const accountExists = await this.connection.getAccountInfo(data.recipientTokens);
if (!accountExists?.data) {
ixs.push(
createAssociatedTokenAccountInstruction(
invoker.publicKey!,
data.recipientTokens,
data.recipient,
data.mint,
TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID
)
);
}
}

/**
* Utility function to generate metadata for a Contract or return existing Pubkey
*/
Expand Down
6 changes: 6 additions & 0 deletions packages/stream/solana/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,12 @@ export interface CreateMultipleStreamsValues {
recipients: Recipient[];
}

export interface CheckAssociatedTokenAccountData {
recipient: PublicKey;
recipientTokens: PublicKey;
mint: PublicKey;
}

export interface BatchItem {
recipient: string;
tx: Transaction;
Expand Down

0 comments on commit 977658b

Please sign in to comment.