Skip to content

Commit

Permalink
Handle arrays
Browse files Browse the repository at this point in the history
  • Loading branch information
guibescos committed Feb 28, 2024
1 parent 99b76b6 commit 117a166
Showing 1 changed file with 40 additions and 51 deletions.
91 changes: 40 additions & 51 deletions target_chains/solana/sdk/js/pyth_solana_receiver/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -85,30 +85,15 @@ export class PythSolanaReceiver {
);

const {
instructions,
postInstructions: instructions,
priceFeedIdToPriceUpdateAccount: priceFeedIdToPriceUpdateAccount,
encodedVaaAddresses,
priceUpdateAddresses,
cleanupInstructions,
} = await this.buildPostPriceUpdateInstructions(priceUpdateDataArray);
builder.addInstructions(instructions);
builder.addInstructions(
await getInstructions(priceFeedIdToPriceUpdateAccount)
);

await Promise.all(
encodedVaaAddresses.map(async (encodedVaaAddress) =>
builder.addInstruction(
await this.buildCloseEncodedVaaInstruction(encodedVaaAddress)
)
)
);
await Promise.all(
priceUpdateAddresses.map(async (priceUpdateAccount) =>
builder.addInstruction(
await this.buildClosePriceUpdateInstruction(priceUpdateAccount)
)
)
);
builder.addInstructions(cleanupInstructions);
return builder.getVersionedTransactions(priorityFeeConfig ?? {});
}

Expand All @@ -124,34 +109,28 @@ export class PythSolanaReceiver {
this.connection
);
const {
instructions,
postInstructions: instructions,
priceFeedIdToPriceUpdateAccount,
priceUpdateAddresses,
cleanupInstructions,
} = await this.buildPostPriceUpdateAtomicInstructions(priceUpdateDataArray);
builder.addInstructions(instructions);
builder.addInstructions(
await getInstructions(priceFeedIdToPriceUpdateAccount)
);
await Promise.all(
priceUpdateAddresses.map(async (priceUpdateAccount) =>
builder.addInstruction(
await this.buildClosePriceUpdateInstruction(priceUpdateAccount)
)
)
);
builder.addInstructions(cleanupInstructions);
return builder.getVersionedTransactions(priorityFeeConfig ?? {});
}

async buildPostPriceUpdateAtomicInstructions(
priceUpdateDataArray: string[]
): Promise<{
instructions: InstructionWithEphemeralSigners[];
postInstructions: InstructionWithEphemeralSigners[];
priceFeedIdToPriceUpdateAccount: Record<string, PublicKey>;
priceUpdateAddresses: PublicKey[];
cleanupInstructions: InstructionWithEphemeralSigners[];
}> {
const instructions: InstructionWithEphemeralSigners[] = [];
const postInstructions: InstructionWithEphemeralSigners[] = [];
const priceFeedIdToPriceUpdateAccount: Record<string, PublicKey> = {};
const priceUpdateAddresses: PublicKey[] = [];
const cleanupInstructions: InstructionWithEphemeralSigners[] = [];

for (const priceUpdateData of priceUpdateDataArray) {
const accumulatorUpdateData = parseAccumulatorUpdateData(
Expand All @@ -165,7 +144,7 @@ export class PythSolanaReceiver {

for (const update of accumulatorUpdateData.updates) {
const priceUpdateKeypair = new Keypair();
instructions.push({
postInstructions.push({
instruction: await this.receiver.methods
.postUpdateAtomic({
vaa: trimmedVaa,
Expand All @@ -185,28 +164,31 @@ export class PythSolanaReceiver {
priceFeedIdToPriceUpdateAccount[
"0x" + parsePriceFeedMessage(update.message).feedId.toString("hex")
] = priceUpdateKeypair.publicKey;
priceUpdateAddresses.push(priceUpdateKeypair.publicKey);

cleanupInstructions.push(
await this.buildClosePriceUpdateInstruction(
priceUpdateKeypair.publicKey
)
);
}
}
return {
instructions,
postInstructions,
priceFeedIdToPriceUpdateAccount,
priceUpdateAddresses,
cleanupInstructions,
};
}

async buildPostPriceUpdateInstructions(
priceUpdateDataArray: string[]
): Promise<{
instructions: InstructionWithEphemeralSigners[];
postInstructions: InstructionWithEphemeralSigners[];
priceFeedIdToPriceUpdateAccount: Record<string, PublicKey>;
encodedVaaAddresses: PublicKey[];
priceUpdateAddresses: PublicKey[];
cleanupInstructions: InstructionWithEphemeralSigners[];
}> {
const instructions: InstructionWithEphemeralSigners[] = [];
const postInstructions: InstructionWithEphemeralSigners[] = [];
const priceFeedIdToPriceUpdateAccount: Record<string, PublicKey> = {};
const priceUpdateAddresses: PublicKey[] = [];
const encodedVaaAddresses: PublicKey[] = [];
const cleanupInstructions: InstructionWithEphemeralSigners[] = [];

for (const priceUpdateData of priceUpdateDataArray) {
const accumulatorUpdateData = parseAccumulatorUpdateData(
Expand All @@ -218,15 +200,15 @@ export class PythSolanaReceiver {

const guardianSetIndex = getGuardianSetIndex(accumulatorUpdateData.vaa);

instructions.push({
postInstructions.push({
instruction: await this.wormhole.account.encodedVaa.createInstruction(
encodedVaaKeypair,
encodedVaaSize
),
signers: [encodedVaaKeypair],
});

instructions.push({
postInstructions.push({
instruction: await this.wormhole.methods
.initEncodedVaa()
.accounts({
Expand All @@ -236,7 +218,7 @@ export class PythSolanaReceiver {
signers: [],
});

instructions.push({
postInstructions.push({
instruction: await this.wormhole.methods
.writeEncodedVaa({
index: 0,
Expand All @@ -249,7 +231,7 @@ export class PythSolanaReceiver {
signers: [],
});

instructions.push({
postInstructions.push({
instruction: await this.wormhole.methods
.writeEncodedVaa({
index: VAA_SPLIT_INDEX,
Expand All @@ -262,7 +244,7 @@ export class PythSolanaReceiver {
signers: [],
});

instructions.push({
postInstructions.push({
instruction: await this.wormhole.methods
.verifyEncodedVaaV1()
.accounts({
Expand All @@ -274,9 +256,13 @@ export class PythSolanaReceiver {
computeUnits: VERIFY_ENCODED_VAA_COMPUTE_BUDGET,
});

cleanupInstructions.push(
await this.buildCloseEncodedVaaInstruction(encodedVaaKeypair.publicKey)
);

for (const update of accumulatorUpdateData.updates) {
const priceUpdateKeypair = new Keypair();
instructions.push({
postInstructions.push({
instruction: await this.receiver.methods
.postUpdate({
merklePriceUpdate: update,
Expand All @@ -296,15 +282,18 @@ export class PythSolanaReceiver {
priceFeedIdToPriceUpdateAccount[
"0x" + parsePriceFeedMessage(update.message).feedId.toString("hex")
] = priceUpdateKeypair.publicKey;
priceUpdateAddresses.push(priceUpdateKeypair.publicKey);
cleanupInstructions.push(
await this.buildClosePriceUpdateInstruction(
priceUpdateKeypair.publicKey
)
);
}
}

return {
instructions,
postInstructions,
priceFeedIdToPriceUpdateAccount,
encodedVaaAddresses,
priceUpdateAddresses,
cleanupInstructions,
};
}

Expand Down

0 comments on commit 117a166

Please sign in to comment.