From 565f429f7993f7134627d8da703d80d6b81f16bd Mon Sep 17 00:00:00 2001 From: Michael Huang Date: Mon, 28 Mar 2022 19:34:52 -0500 Subject: [PATCH] Add helper to split out ATAIXs (#535) --- .../src/transaction/TransactionEnvelope.ts | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/packages/solana-contrib/src/transaction/TransactionEnvelope.ts b/packages/solana-contrib/src/transaction/TransactionEnvelope.ts index a347d4ac2..a6fce9730 100644 --- a/packages/solana-contrib/src/transaction/TransactionEnvelope.ts +++ b/packages/solana-contrib/src/transaction/TransactionEnvelope.ts @@ -577,6 +577,30 @@ export class TransactionEnvelope { return new TransactionEnvelope(this.provider, instructions, this.signers); } + /** + * Split out ATA instructions to a separate transaction envelope. + */ + splitATAIXs(): { + ataIXs: TransactionEnvelope; + tx: TransactionEnvelope; + } { + const ataIXs = new TransactionEnvelope(this.provider, [], this.signers); + const newTx = new TransactionEnvelope(this.provider, [], this.signers); + + for (const ix of this.instructions) { + if (ix.programId.equals(ASSOCIATED_TOKEN_PROGRAM_ID)) { + ataIXs.instructions.push(ix); + } else { + newTx.instructions.push(ix); + } + } + + return { + ataIXs: ataIXs.dedupeATAIXs(), + tx: newTx, + }; + } + /** * Get an instruction from the transaction envelope by index. */