Skip to content

Commit 1484e16

Browse files
committed
fix: add configurable treasuryId, set tighter vaa split, improve examples
1 parent 1c08033 commit 1484e16

File tree

4 files changed

+53
-19
lines changed

4 files changed

+53
-19
lines changed

target_chains/solana/sdk/js/pyth_solana_receiver/examples/post_price_update.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,18 @@ async function main() {
2626
`Sending transactions from account: ${keypair.publicKey.toBase58()}`
2727
);
2828
const wallet = new Wallet(keypair);
29-
const pythSolanaReceiver = new PythSolanaReceiver({ connection, wallet });
29+
// Optionally use an account lookup table to reduce tx sizes.
30+
const addressLookupTableAccount = new PublicKey(
31+
"5DNCErWQFBdvCxWQXaC1mrEFsvL3ftrzZ2gVZWNybaSX"
32+
);
33+
// Use a stable treasury ID of 0, since its address is indexed in the address lookup table.
34+
// This is a tx size optimization and is optional. If not provided, a random treasury account will be used.
35+
const treasuryId = 1;
36+
const pythSolanaReceiver = new PythSolanaReceiver({
37+
connection,
38+
wallet,
39+
treasuryId,
40+
});
3041

3142
// Get the price update from hermes
3243
const priceUpdateData = await getPriceUpdateData();
@@ -35,9 +46,15 @@ async function main() {
3546
// If closeUpdateAccounts = true, the builder will automatically generate instructions to close the ephemeral price update accounts
3647
// at the end of the transaction. Closing the accounts will reclaim their rent.
3748
// The example is using closeUpdateAccounts = false so you can easily look up the price update account in an explorer.
38-
const transactionBuilder = pythSolanaReceiver.newTransactionBuilder({
39-
closeUpdateAccounts: false,
40-
});
49+
const lookupTableAccount =
50+
(await connection.getAddressLookupTable(addressLookupTableAccount)).value ??
51+
undefined;
52+
const transactionBuilder = pythSolanaReceiver.newTransactionBuilder(
53+
{
54+
closeUpdateAccounts: false,
55+
},
56+
lookupTableAccount
57+
);
4158
// Post the price updates to ephemeral accounts, one per price feed.
4259
await transactionBuilder.addPostPriceUpdates(priceUpdateData);
4360
console.log(

target_chains/solana/sdk/js/pyth_solana_receiver/examples/update_price_feed.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ const ETH_PRICE_FEED_ID =
1313
"0xff61491a931112ddf1bd8147cd1b641375f79f5825126d665480874634fd0ace";
1414
const PRICE_FEED_IDS = [SOL_PRICE_FEED_ID, ETH_PRICE_FEED_ID];
1515

16-
// Optionally use an account lookup table to reduce tx sizes
17-
const addressLookupTableAccount = new PublicKey(
18-
"5DNCErWQFBdvCxWQXaC1mrEFsvL3ftrzZ2gVZWNybaSX"
19-
);
20-
2116
let keypairFile = "";
2217
if (process.env["SOLANA_KEYPAIR"]) {
2318
keypairFile = process.env["SOLANA_KEYPAIR"];
@@ -32,11 +27,23 @@ async function main() {
3227
`Sending transactions from account: ${keypair.publicKey.toBase58()}`
3328
);
3429
const wallet = new Wallet(keypair);
35-
const pythSolanaReceiver = new PythSolanaReceiver({ connection, wallet });
30+
31+
// Optionally use an account lookup table to reduce tx sizes.
32+
const addressLookupTableAccount = new PublicKey(
33+
"5DNCErWQFBdvCxWQXaC1mrEFsvL3ftrzZ2gVZWNybaSX"
34+
);
35+
// Use a stable treasury ID of 0, since its address is indexed in the address lookup table.
36+
// This is a tx size optimization and is optional. If not provided, a random treasury account will be used.
37+
const treasuryId = 1;
38+
const pythSolanaReceiver = new PythSolanaReceiver({
39+
connection,
40+
wallet,
41+
treasuryId,
42+
});
3643

3744
// Get the price update from hermes
3845
const priceUpdateData = await getPriceUpdateData(PRICE_FEED_IDS);
39-
// console.log(`Posting price update: ${priceUpdateData}`);
46+
console.log(`Posting price update: ${priceUpdateData}`);
4047

4148
// The shard indicates which set of price feed accounts you wish to update.
4249
const shardId = 1;

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

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -436,20 +436,29 @@ export class PythSolanaReceiver {
436436
readonly receiver: Program<PythSolanaReceiverProgram>;
437437
readonly wormhole: Program<WormholeCoreBridgeSolana>;
438438
readonly pushOracle: Program<PythPushOracle>;
439-
439+
readonly treasuryId?: number;
440440
constructor({
441441
connection,
442442
wallet,
443443
wormholeProgramId = DEFAULT_WORMHOLE_PROGRAM_ID,
444444
receiverProgramId = DEFAULT_RECEIVER_PROGRAM_ID,
445445
pushOracleProgramId = DEFAULT_PUSH_ORACLE_PROGRAM_ID,
446+
treasuryId = undefined,
446447
}: {
447448
connection: Connection;
448449
wallet: Wallet;
449450
wormholeProgramId?: PublicKey;
450451
receiverProgramId?: PublicKey;
451452
pushOracleProgramId?: PublicKey;
453+
// Optionally provide a treasuryId to always use a specific treasury account.
454+
// This can be useful when using an ALT to reduce tx size.
455+
// If not provided, treasury accounts will be randomly selected.
456+
treasuryId?: number;
452457
}) {
458+
if (treasuryId !== undefined && (treasuryId < 0 || treasuryId > 255)) {
459+
throw new Error("treasuryId must be between 0 and 255");
460+
}
461+
453462
this.connection = connection;
454463
this.wallet = wallet;
455464
this.provider = new AnchorProvider(this.connection, this.wallet, {
@@ -470,16 +479,17 @@ export class PythSolanaReceiver {
470479
pushOracleProgramId,
471480
this.provider
472481
);
482+
this.treasuryId = treasuryId;
473483
}
474484

475485
/**
476486
* Get a new transaction builder to build transactions that interact with the Pyth Solana Receiver program and consume price updates
477487
*/
478488
newTransactionBuilder(
479489
config: PythTransactionBuilderConfig,
480-
address_lookup_account?: AddressLookupTableAccount
490+
addressLookupAccount?: AddressLookupTableAccount
481491
): PythTransactionBuilder {
482-
return new PythTransactionBuilder(this, config, address_lookup_account);
492+
return new PythTransactionBuilder(this, config, addressLookupAccount);
483493
}
484494

485495
/**
@@ -504,7 +514,7 @@ export class PythSolanaReceiver {
504514
const priceFeedIdToPriceUpdateAccount: Record<string, PublicKey> = {};
505515
const closeInstructions: InstructionWithEphemeralSigners[] = [];
506516

507-
const treasuryId = DEFAULT_TREASURY_ID;
517+
const treasuryId = this.treasuryId ?? getRandomTreasuryId();
508518

509519
for (const priceUpdateData of priceUpdateDataArray) {
510520
const accumulatorUpdateData = parseAccumulatorUpdateData(
@@ -572,7 +582,7 @@ export class PythSolanaReceiver {
572582
const priceFeedIdToPriceUpdateAccount: Record<string, PublicKey> = {};
573583
const closeInstructions: InstructionWithEphemeralSigners[] = [];
574584

575-
const treasuryId = getRandomTreasuryId();
585+
const treasuryId = this.treasuryId ?? getRandomTreasuryId();
576586

577587
for (const priceUpdateData of priceUpdateDataArray) {
578588
const accumulatorUpdateData = parseAccumulatorUpdateData(
@@ -643,7 +653,7 @@ export class PythSolanaReceiver {
643653
const priceFeedIdToTwapUpdateAccount: Record<string, PublicKey> = {};
644654
const closeInstructions: InstructionWithEphemeralSigners[] = [];
645655

646-
const treasuryId = getRandomTreasuryId();
656+
const treasuryId = this.treasuryId ?? getRandomTreasuryId();
647657

648658
if (twapUpdateDataArray.length !== 2) {
649659
throw new Error(
@@ -737,7 +747,7 @@ export class PythSolanaReceiver {
737747
const priceFeedIdToPriceUpdateAccount: Record<string, PublicKey> = {};
738748
const closeInstructions: InstructionWithEphemeralSigners[] = [];
739749

740-
const treasuryId = DEFAULT_TREASURY_ID;
750+
const treasuryId = this.treasuryId ?? getRandomTreasuryId();
741751

742752
for (const priceUpdateData of priceUpdateDataArray) {
743753
const accumulatorUpdateData = parseAccumulatorUpdateData(

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export const VAA_START = 46;
4646
* `initEncodedVaa` and `writeEncodedVaa` in a single Solana transaction, while using an address lookup table.
4747
* This way, the packing of the instructions to post an encoded vaa is more efficient.
4848
*/
49-
export const VAA_SPLIT_INDEX = 700;
49+
export const VAA_SPLIT_INDEX = 721;
5050

5151
/**
5252
* Trim the number of signatures of a VAA.

0 commit comments

Comments
 (0)