Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

STREAM-1060: check all token accounts ATA #118

Merged
merged 6 commits into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.9.0",
"version": "5.9.1",
"$schema": "node_modules/lerna/schemas/lerna-schema.json"
}
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

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.9.0",
"version": "5.9.1",
"description": "JavaScript SDK to interact with Streamflow protocol.",
"main": "dist/index.js",
"homepage": "https://github.com/streamflow-finance/js-sdk/",
Expand Down
58 changes: 39 additions & 19 deletions packages/stream/solana/StreamClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import {
ICreateStreamSolanaExt,
IInteractStreamSolanaExt,
ITopUpStreamSolanaExt,
CheckAssociatedTokenAccountData,
CheckAssociatedTokenAccountsData,
} from "./types";
import {
ata,
Expand Down Expand Up @@ -479,7 +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);
await this.checkAssociatedTokenAccounts(data, { invoker }, ixs);

ixs.push(
withdrawStreamInstruction(amount, this.programId, {
Expand Down Expand Up @@ -534,7 +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);
await this.checkAssociatedTokenAccounts(data, { invoker }, ixs);

ixs.push(
cancelStreamInstruction(this.programId, {
Expand Down Expand Up @@ -947,25 +947,45 @@ export default class SolanaStreamClient extends BaseStreamClient {
}

/**
* Utility function that checks whether associated token account for the recipient exists and adds an instruction to add if not
* Utility function that checks whether associated token accounts still exist and adds instructions to add them if not
*/
private async checkAssociatedTokenAccount(
data: CheckAssociatedTokenAccountData,
{ invoker }: IInteractStreamSolanaExt,
private async checkAssociatedTokenAccounts(
data: CheckAssociatedTokenAccountsData,
{ invoker, checkTokenAccounts }: 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
)
);
if (!checkTokenAccounts) {
return;
}
const checkedKeys: Set<PublicKey> = new Set();
const accountArrays = [
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's create a proper object out of this?
We could just have a type and cast data to it.

Much easier then to work with indexes

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What do you mean here? Seems complex.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's easier to read then associating [1] to senderTokens for instance.

[data.sender, data.senderTokens],
[data.recipient, data.recipientTokens],
[data.partner, data.partnerTokens],
[data.streamflowTreasury, data.streamflowTreasuryTokens],
].filter((value) => {
if (checkedKeys.has(value[1])) {
return false;
}
checkedKeys.add(value[1]);
return true;
});
const response = await this.connection.getMultipleAccountsInfo(
accountArrays.map((item) => item[1])
);
for (let i = 0; i < response.length; i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we iterate here over accountArrays in forof loop?

for accountArray of accountArrays ...

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why should we?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More clean then traditional for loop with indexes :)

Copy link
Collaborator Author

@Yolley Yolley Dec 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I need it here because every index in response corresponds to index in accountArrays

if (!response[1]) {
Yolley marked this conversation as resolved.
Show resolved Hide resolved
ixs.push(
createAssociatedTokenAccountInstruction(
invoker.publicKey!,
accountArrays[i][0],
accountArrays[i][1],
data.mint,
TOKEN_PROGRAM_ID,
ASSOCIATED_TOKEN_PROGRAM_ID
)
);
}
}
}

Expand Down
9 changes: 8 additions & 1 deletion packages/stream/solana/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ export interface ICreateStreamSolanaExt {

export interface IInteractStreamSolanaExt {
invoker: SignerWalletAdapter | Keypair;
checkTokenAccounts?: boolean;
}

export interface ITopUpStreamSolanaExt {
Expand Down Expand Up @@ -426,9 +427,15 @@ export interface CreateMultipleStreamsValues {
recipients: Recipient[];
}

export interface CheckAssociatedTokenAccountData {
export interface CheckAssociatedTokenAccountsData {
sender: PublicKey;
senderTokens: PublicKey;
recipient: PublicKey;
recipientTokens: PublicKey;
partner: PublicKey;
partnerTokens: PublicKey;
streamflowTreasury: PublicKey;
streamflowTreasuryTokens: PublicKey;
mint: PublicKey;
}

Expand Down