Skip to content

Commit 78aee7b

Browse files
committed
refactor: extract shared VAA instruction building into generateVaaInstructionGroups, allowing for flexible ix ordering and batching while sharing core logic
1 parent 82dbaba commit 78aee7b

File tree

2 files changed

+243
-317
lines changed

2 files changed

+243
-317
lines changed

target_chains/solana/sdk/js/pyth_solana_receiver/src/PythSolanaReceiver.ts

Lines changed: 17 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,9 @@ import {
4040
} from "./compute_budget";
4141
import { Wallet } from "@coral-xyz/anchor";
4242
import {
43-
buildEncodedVaaCreateInstruction,
43+
buildCloseEncodedVaaInstruction,
44+
buildPostEncodedVaaInstructions,
4445
buildPostEncodedVaasForTwapInstructions,
45-
buildWriteEncodedVaaWithSplitInstructions,
4646
findEncodedVaaAccountsByWriteAuthority,
4747
getGuardianSetIndex,
4848
trimSignatures,
@@ -549,77 +549,6 @@ export class PythSolanaReceiver {
549549
};
550550
}
551551

552-
/**
553-
* Build a series of helper instructions that post a VAA in an encoded VAA account. This function is bespoke for posting Pyth VAAs and might not work for other usecases.
554-
*
555-
* @param vaa a Wormhole VAA
556-
* @returns `postInstructions`: the instructions to post the VAA
557-
* @returns `encodedVaaAddress`: the address of the encoded VAA account where the VAA will be posted
558-
* @returns `closeInstructions`: the instructions to close the encoded VAA account
559-
*/
560-
async buildPostEncodedVaaInstructions(vaa: Buffer): Promise<{
561-
postInstructions: InstructionWithEphemeralSigners[];
562-
encodedVaaAddress: PublicKey;
563-
closeInstructions: InstructionWithEphemeralSigners[];
564-
}> {
565-
const trimmedVaa = trimSignatures(vaa, 13);
566-
const postInstructions: InstructionWithEphemeralSigners[] = [];
567-
const closeInstructions: InstructionWithEphemeralSigners[] = [];
568-
const encodedVaaKeypair = new Keypair();
569-
const guardianSetIndex = getGuardianSetIndex(trimmedVaa);
570-
571-
postInstructions.push(
572-
await buildEncodedVaaCreateInstruction(
573-
this.wormhole,
574-
trimmedVaa,
575-
encodedVaaKeypair
576-
)
577-
);
578-
579-
postInstructions.push({
580-
instruction: await this.wormhole.methods
581-
.initEncodedVaa()
582-
.accounts({
583-
encodedVaa: encodedVaaKeypair.publicKey,
584-
})
585-
.instruction(),
586-
signers: [],
587-
computeUnits: INIT_ENCODED_VAA_COMPUTE_BUDGET,
588-
});
589-
590-
const writeInstructions = await buildWriteEncodedVaaWithSplitInstructions(
591-
this.wormhole,
592-
trimmedVaa,
593-
encodedVaaKeypair.publicKey
594-
);
595-
postInstructions.push(...writeInstructions);
596-
597-
postInstructions.push({
598-
instruction: await this.wormhole.methods
599-
.verifyEncodedVaaV1()
600-
.accounts({
601-
guardianSet: getGuardianSetPda(
602-
guardianSetIndex,
603-
this.wormhole.programId
604-
),
605-
draftVaa: encodedVaaKeypair.publicKey,
606-
})
607-
.instruction(),
608-
signers: [],
609-
computeUnits: VERIFY_ENCODED_VAA_COMPUTE_BUDGET,
610-
});
611-
612-
closeInstructions.push(
613-
await this.buildCloseEncodedVaaInstruction(encodedVaaKeypair.publicKey)
614-
);
615-
616-
return {
617-
postInstructions,
618-
encodedVaaAddress: encodedVaaKeypair.publicKey,
619-
closeInstructions,
620-
};
621-
}
622-
623552
/**
624553
* Build a series of helper instructions that post price updates to the Pyth Solana Receiver program and another series to close the encoded vaa accounts and the price update accounts.
625554
*
@@ -859,20 +788,19 @@ export class PythSolanaReceiver {
859788
}
860789

861790
/**
862-
* Build an instruction to close an encoded VAA account, recovering the rent.
791+
* Build a series of helper instructions that post a VAA in an encoded VAA account. This function is bespoke for posting Pyth VAAs and might not work for other usecases.
792+
*
793+
* @param vaa a Wormhole VAA
794+
* @returns `encodedVaaAddress`: the address of the encoded VAA account where the VAA will be posted
795+
* @returns `postInstructions`: the instructions to post the VAA
796+
* @returns `closeInstructions`: the instructions to close the encoded VAA account
863797
*/
864-
async buildCloseEncodedVaaInstruction(
865-
encodedVaa: PublicKey
866-
): Promise<InstructionWithEphemeralSigners> {
867-
const instruction = await this.wormhole.methods
868-
.closeEncodedVaa()
869-
.accounts({ encodedVaa })
870-
.instruction();
871-
return {
872-
instruction,
873-
signers: [],
874-
computeUnits: CLOSE_ENCODED_VAA_COMPUTE_BUDGET,
875-
};
798+
async buildPostEncodedVaaInstructions(vaa: Buffer): Promise<{
799+
encodedVaaAddress: PublicKey;
800+
postInstructions: InstructionWithEphemeralSigners[];
801+
closeInstructions: InstructionWithEphemeralSigners[];
802+
}> {
803+
return buildPostEncodedVaaInstructions(this.wormhole, vaa);
876804
}
877805

878806
/**
@@ -884,7 +812,9 @@ export class PythSolanaReceiver {
884812
const encodedVaas = await this.findOwnedEncodedVaaAccounts();
885813
const instructions = [];
886814
for (const encodedVaa of encodedVaas) {
887-
instructions.push(await this.buildCloseEncodedVaaInstruction(encodedVaa));
815+
instructions.push(
816+
await buildCloseEncodedVaaInstruction(this.wormhole, encodedVaa)
817+
);
888818
}
889819
return instructions.slice(0, maxInstructions);
890820
}

0 commit comments

Comments
 (0)