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-1454: support Token2022 #162

Merged
merged 3 commits into from
Apr 8, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 3 additions & 0 deletions packages/common/solana/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,9 @@ export async function checkOrCreateAtaBatch(
programId?: PublicKey,
): Promise<TransactionInstruction[]> {
const ixs: TransactionInstruction[] = [];
if (!programId) {
programId = (await getMintAndProgram(connection, mint)).programId;
}
// TODO: optimize fetching and maps/arrays
const atas: PublicKey[] = [];
for (const owner of owners) {
Expand Down
67 changes: 37 additions & 30 deletions packages/stream/solana/StreamClient.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import BN from "bn.js";
import { Buffer } from "buffer";
import { ASSOCIATED_TOKEN_PROGRAM_ID, NATIVE_MINT, TOKEN_PROGRAM_ID } from "@solana/spl-token";
import { ASSOCIATED_TOKEN_PROGRAM_ID, NATIVE_MINT } from "@solana/spl-token";
import {
Connection,
Keypair,
Expand All @@ -24,6 +24,7 @@ import {
prepareWrappedAccount,
prepareTransaction,
prepareBaseInstructions,
getMintAndProgram,
} from "@streamflow/common/solana";
import * as borsh from "borsh";

Expand Down Expand Up @@ -199,13 +200,14 @@ export default class SolanaStreamClient extends BaseStreamClient {
this.programId,
);

const senderTokens = await ata(mintPublicKey, sender.publicKey);
const recipientTokens = await ata(mintPublicKey, recipientPublicKey);
const streamflowTreasuryTokens = await ata(mintPublicKey, STREAMFLOW_TREASURY_PUBLIC_KEY);
const { programId } = await getMintAndProgram(this.connection, mintPublicKey);
Yolley marked this conversation as resolved.
Show resolved Hide resolved
const senderTokens = await ata(mintPublicKey, sender.publicKey, programId);
const recipientTokens = await ata(mintPublicKey, recipientPublicKey, programId);
const streamflowTreasuryTokens = await ata(mintPublicKey, STREAMFLOW_TREASURY_PUBLIC_KEY, programId);

const partnerPublicKey = partner ? new PublicKey(partner) : STREAMFLOW_TREASURY_PUBLIC_KEY;

const partnerTokens = await ata(mintPublicKey, partnerPublicKey);
const partnerTokens = await ata(mintPublicKey, partnerPublicKey, programId);

if (isNative) {
const totalFee = await this.getTotalFee({
Expand Down Expand Up @@ -249,7 +251,7 @@ export default class SolanaStreamClient extends BaseStreamClient {
feeOracle: FEE_ORACLE_PUBLIC_KEY,
rent: SYSVAR_RENT_PUBKEY,
timelockProgram: this.programId,
tokenProgram: TOKEN_PROGRAM_ID,
tokenProgram: programId,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
Copy link
Contributor

Choose a reason for hiding this comment

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

I see this is also imported from spl package. It is the same for both token programs?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

ATA is another program, it works with an valid token program.

withdrawor: WITHDRAWOR_PUBLIC_KEY,
systemProgram: SystemProgram.programId,
Expand Down Expand Up @@ -339,7 +341,8 @@ export default class SolanaStreamClient extends BaseStreamClient {
this.programId,
);

const senderTokens = await ata(mintPublicKey, sender.publicKey);
const { programId } = await getMintAndProgram(this.connection, mintPublicKey);
const senderTokens = await ata(mintPublicKey, sender.publicKey, programId);

const partnerPublicKey = partner ? new PublicKey(partner) : STREAMFLOW_TREASURY_PUBLIC_KEY;

Expand Down Expand Up @@ -382,7 +385,7 @@ export default class SolanaStreamClient extends BaseStreamClient {
feeOracle: FEE_ORACLE_PUBLIC_KEY,
rent: SYSVAR_RENT_PUBKEY,
timelockProgram: this.programId,
tokenProgram: TOKEN_PROGRAM_ID,
tokenProgram: programId,
withdrawor: WITHDRAWOR_PUBLIC_KEY,
systemProgram: SystemProgram.programId,
},
Expand Down Expand Up @@ -554,9 +557,10 @@ export default class SolanaStreamClient extends BaseStreamClient {
}

const data = decodeStream(escrow.data);
const streamflowTreasuryTokens = await ata(data.mint, STREAMFLOW_TREASURY_PUBLIC_KEY);
const partnerTokens = await ata(data.mint, data.partner);
await this.checkAssociatedTokenAccounts(data, { invoker, checkTokenAccounts }, ixs);
const { programId } = await getMintAndProgram(this.connection, data.mint);
const streamflowTreasuryTokens = await ata(data.mint, STREAMFLOW_TREASURY_PUBLIC_KEY, programId);
const partnerTokens = await ata(data.mint, data.partner, programId);
await this.checkAssociatedTokenAccounts(data, { invoker, checkTokenAccounts }, ixs, programId);

ixs.push(
withdrawStreamInstruction(amount, this.programId, {
Expand All @@ -570,7 +574,7 @@ export default class SolanaStreamClient extends BaseStreamClient {
partner: data.partner,
partnerTokens,
mint: data.mint,
tokenProgram: TOKEN_PROGRAM_ID,
tokenProgram: programId,
}),
);

Expand Down Expand Up @@ -611,14 +615,15 @@ export default class SolanaStreamClient extends BaseStreamClient {

const data = decodeStream(escrowAcc?.data);

const streamflowTreasuryTokens = await ata(data.mint, STREAMFLOW_TREASURY_PUBLIC_KEY);
const partnerTokens = await ata(data.mint, data.partner);
const { programId } = await getMintAndProgram(this.connection, data.mint);
const streamflowTreasuryTokens = await ata(data.mint, STREAMFLOW_TREASURY_PUBLIC_KEY, programId);
const partnerTokens = await ata(data.mint, data.partner, programId);

const ixs: TransactionInstruction[] = prepareBaseInstructions(this.connection, {
computePrice,
computeLimit,
});
await this.checkAssociatedTokenAccounts(data, { invoker, checkTokenAccounts }, ixs);
await this.checkAssociatedTokenAccounts(data, { invoker, checkTokenAccounts }, ixs, programId);

ixs.push(
cancelStreamInstruction(this.programId, {
Expand All @@ -634,7 +639,7 @@ export default class SolanaStreamClient extends BaseStreamClient {
partner: data.partner,
partnerTokens,
mint: data.mint,
tokenProgram: TOKEN_PROGRAM_ID,
tokenProgram: programId,
}),
);

Expand Down Expand Up @@ -683,8 +688,9 @@ export default class SolanaStreamClient extends BaseStreamClient {
throw new Error("Couldn't get account info");
}
const { mint } = decodeStream(escrow?.data);
const { programId } = await getMintAndProgram(this.connection, mint);

const newRecipientTokens = await ata(mint, newRecipientPublicKey);
const newRecipientTokens = await ata(mint, newRecipientPublicKey, programId);

ixs.push(
transferStreamInstruction(this.programId, {
Expand All @@ -694,7 +700,7 @@ export default class SolanaStreamClient extends BaseStreamClient {
metadata: stream,
mint,
rent: SYSVAR_RENT_PUBKEY,
tokenProgram: TOKEN_PROGRAM_ID,
tokenProgram: programId,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
systemProgram: SystemProgram.programId,
}),
Expand Down Expand Up @@ -740,8 +746,9 @@ export default class SolanaStreamClient extends BaseStreamClient {
}
const { mint, partner, senderTokens, escrowTokens } = decodeStream(escrow?.data);

const streamflowTreasuryTokens = await ata(mint, STREAMFLOW_TREASURY_PUBLIC_KEY);
const partnerTokens = await ata(mint, partner);
const { programId } = await getMintAndProgram(this.connection, mint);
const streamflowTreasuryTokens = await ata(mint, STREAMFLOW_TREASURY_PUBLIC_KEY, programId);
const partnerTokens = await ata(mint, partner, programId);

if (isNative) {
ixs.push(...(await prepareWrappedAccount(this.connection, invoker.publicKey, amount)));
Expand All @@ -758,7 +765,7 @@ export default class SolanaStreamClient extends BaseStreamClient {
partner: partner,
partnerTokens: partnerTokens,
mint,
tokenProgram: TOKEN_PROGRAM_ID,
tokenProgram: programId,
withdrawor: WITHDRAWOR_PUBLIC_KEY,
systemProgram: SystemProgram.programId,
}),
Expand Down Expand Up @@ -949,13 +956,12 @@ export default class SolanaStreamClient extends BaseStreamClient {
this.programId,
);

const senderTokens = await ata(mintPublicKey, sender.publicKey);
const recipientTokens = await ata(mintPublicKey, recipientPublicKey);
const streamflowTreasuryTokens = await ata(mintPublicKey, STREAMFLOW_TREASURY_PUBLIC_KEY);

const partnerPublicKey = partner ? new PublicKey(partner) : STREAMFLOW_TREASURY_PUBLIC_KEY;

const partnerTokens = await ata(mintPublicKey, partnerPublicKey);
const { programId } = await getMintAndProgram(this.connection, mintPublicKey);
const senderTokens = await ata(mintPublicKey, sender.publicKey, programId);
const recipientTokens = await ata(mintPublicKey, recipientPublicKey, programId);
const streamflowTreasuryTokens = await ata(mintPublicKey, STREAMFLOW_TREASURY_PUBLIC_KEY, programId);
const partnerPublicKey = partner ? new PublicKey(partner) : WITHDRAWOR_PUBLIC_KEY;
const partnerTokens = await ata(mintPublicKey, partnerPublicKey, programId);

ixs.push(
createStreamInstruction(
Expand Down Expand Up @@ -991,7 +997,7 @@ export default class SolanaStreamClient extends BaseStreamClient {
feeOracle: FEE_ORACLE_PUBLIC_KEY,
rent: SYSVAR_RENT_PUBKEY,
timelockProgram: this.programId,
tokenProgram: TOKEN_PROGRAM_ID,
tokenProgram: programId,
associatedTokenProgram: ASSOCIATED_TOKEN_PROGRAM_ID,
withdrawor: WITHDRAWOR_PUBLIC_KEY,
systemProgram: SystemProgram.programId,
Expand Down Expand Up @@ -1025,6 +1031,7 @@ export default class SolanaStreamClient extends BaseStreamClient {
data: CheckAssociatedTokenAccountsData,
{ invoker, checkTokenAccounts }: IInteractStreamSolanaExt,
ixs: TransactionInstruction[],
programId: PublicKey,
) {
if (!checkTokenAccounts) {
return;
Expand All @@ -1038,6 +1045,6 @@ export default class SolanaStreamClient extends BaseStreamClient {
]),
(address) => new PublicKey(address),
);
ixs.push(...(await checkOrCreateAtaBatch(this.connection, owners, data.mint, invoker)));
ixs.push(...(await checkOrCreateAtaBatch(this.connection, owners, data.mint, invoker, programId)));
}
}