Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

Commit b13a58b

Browse files
authored
token-client: Add compute unit price and limit fields (#6493)
1 parent 9f210c1 commit b13a58b

File tree

3 files changed

+38
-38
lines changed

3 files changed

+38
-38
lines changed

token/client/src/token.rs

Lines changed: 30 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ use {
88
solana_program_test::tokio::time,
99
solana_sdk::{
1010
account::Account as BaseAccount,
11+
compute_budget::ComputeBudgetInstruction,
1112
hash::Hash,
1213
instruction::{AccountMeta, Instruction},
1314
message::Message,
@@ -339,6 +340,8 @@ pub struct Token<T> {
339340
nonce_blockhash: Option<Hash>,
340341
memo: Arc<RwLock<Option<TokenMemo>>>,
341342
transfer_hook_accounts: Option<Vec<AccountMeta>>,
343+
compute_unit_price: Option<u64>,
344+
compute_unit_limit: Option<u32>,
342345
}
343346

344347
impl<T> fmt::Debug for Token<T> {
@@ -356,6 +359,8 @@ impl<T> fmt::Debug for Token<T> {
356359
.field("nonce_blockhash", &self.nonce_blockhash)
357360
.field("memo", &self.memo.read().unwrap())
358361
.field("transfer_hook_accounts", &self.transfer_hook_accounts)
362+
.field("compute_unit_price", &self.compute_unit_price)
363+
.field("compute_unit_limit", &self.compute_unit_limit)
359364
.finish()
360365
}
361366
}
@@ -402,6 +407,8 @@ where
402407
nonce_blockhash: None,
403408
memo: Arc::new(RwLock::new(None)),
404409
transfer_hook_accounts: None,
410+
compute_unit_price: None,
411+
compute_unit_limit: None,
405412
}
406413
}
407414

@@ -451,6 +458,16 @@ where
451458
self
452459
}
453460

461+
pub fn with_compute_unit_price(mut self, compute_unit_price: u64) -> Self {
462+
self.compute_unit_price = Some(compute_unit_price);
463+
self
464+
}
465+
466+
pub fn with_compute_unit_limit(mut self, compute_unit_limit: u32) -> Self {
467+
self.compute_unit_limit = Some(compute_unit_limit);
468+
self
469+
}
470+
454471
pub fn with_memo<M: AsRef<str>>(&self, memo: M, signers: Vec<Pubkey>) -> &Self {
455472
let mut w_memo = self.memo.write().unwrap();
456473
*w_memo = Some(TokenMemo {
@@ -508,7 +525,6 @@ where
508525
async fn construct_tx<S: Signers>(
509526
&self,
510527
token_instructions: &[Instruction],
511-
additional_compute_budget: Option<u32>,
512528
signing_keypairs: &S,
513529
) -> TokenResult<Transaction> {
514530
let mut instructions = vec![];
@@ -533,12 +549,16 @@ where
533549

534550
instructions.extend_from_slice(token_instructions);
535551

536-
if let Some(additional_compute_budget) = additional_compute_budget {
537-
instructions.push(
538-
solana_sdk::compute_budget::ComputeBudgetInstruction::set_compute_unit_limit(
539-
additional_compute_budget,
540-
),
541-
);
552+
if let Some(compute_unit_limit) = self.compute_unit_limit {
553+
instructions.push(ComputeBudgetInstruction::set_compute_unit_limit(
554+
compute_unit_limit,
555+
));
556+
}
557+
558+
if let Some(compute_unit_price) = self.compute_unit_price {
559+
instructions.push(ComputeBudgetInstruction::set_compute_unit_price(
560+
compute_unit_price,
561+
));
542562
}
543563

544564
let (message, blockhash) =
@@ -598,7 +618,7 @@ where
598618
signing_keypairs: &S,
599619
) -> TokenResult<T::SimulationOutput> {
600620
let transaction = self
601-
.construct_tx(token_instructions, None, signing_keypairs)
621+
.construct_tx(token_instructions, signing_keypairs)
602622
.await?;
603623

604624
self.client
@@ -613,27 +633,7 @@ where
613633
signing_keypairs: &S,
614634
) -> TokenResult<T::Output> {
615635
let transaction = self
616-
.construct_tx(token_instructions, None, signing_keypairs)
617-
.await?;
618-
619-
self.client
620-
.send_transaction(&transaction)
621-
.await
622-
.map_err(TokenError::Client)
623-
}
624-
625-
pub async fn process_ixs_with_additional_compute_budget<S: Signers>(
626-
&self,
627-
token_instructions: &[Instruction],
628-
additional_compute_budget: u32,
629-
signing_keypairs: &S,
630-
) -> TokenResult<T::Output> {
631-
let transaction = self
632-
.construct_tx(
633-
token_instructions,
634-
Some(additional_compute_budget),
635-
signing_keypairs,
636-
)
636+
.construct_tx(token_instructions, signing_keypairs)
637637
.await?;
638638

639639
self.client
@@ -2728,9 +2728,6 @@ where
27282728
.new_decryptable_available_balance(transfer_amount, source_aes_key)
27292729
.map_err(|_| TokenError::AccountDecryption)?;
27302730

2731-
// additional compute budget required for `VerifyTransferWithFee`
2732-
const TRANSFER_WITH_FEE_COMPUTE_BUDGET: u32 = 500_000;
2733-
27342731
let mut instructions = confidential_transfer::instruction::transfer_with_fee(
27352732
&self.program_id,
27362733
source_account,
@@ -2756,12 +2753,7 @@ where
27562753
)
27572754
.await
27582755
.map_err(|_| TokenError::AccountNotFound)?;
2759-
self.process_ixs_with_additional_compute_budget(
2760-
&instructions,
2761-
TRANSFER_WITH_FEE_COMPUTE_BUDGET,
2762-
signing_keypairs,
2763-
)
2764-
.await
2756+
self.process_ixs(&instructions, signing_keypairs).await
27652757
}
27662758

27672759
/// Transfer tokens confidentially with fee using split proofs.

token/program-2022-test/tests/confidential_transfer.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1110,6 +1110,7 @@ async fn confidential_transfer_transfer_with_fee() {
11101110
let bob_meta = ConfidentialTokenAccountMeta::new(&token, &bob, None, false, true).await;
11111111

11121112
// Self-transfer of 0 tokens
1113+
let token = token.with_compute_unit_limit(500_000);
11131114
token
11141115
.confidential_transfer_transfer_with_fee(
11151116
&alice_meta.token_account,
@@ -2523,6 +2524,7 @@ async fn confidential_transfer_transfer_with_split_proof_contexts_in_parallel()
25232524
&range_proof_context_state_account,
25242525
&context_state_authority,
25252526
];
2527+
let token = token.with_compute_unit_limit(500_000);
25262528
token
25272529
.confidential_transfer_transfer_with_split_proofs_in_parallel(
25282530
&alice_meta.token_account,
@@ -2941,6 +2943,7 @@ async fn confidential_transfer_transfer_with_fee_and_split_proof_context_in_para
29412943
&range_proof_context_state_account,
29422944
&context_state_authority,
29432945
];
2946+
let token = token.with_compute_unit_limit(500_000);
29442947
token
29452948
.confidential_transfer_transfer_with_fee_and_split_proofs_in_parallel(
29462949
&alice_meta.token_account,

token/program-2022-test/tests/confidential_transfer_fee.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,7 @@ async fn confidential_transfer_withdraw_withheld_tokens_from_mint() {
513513
};
514514

515515
// Test fee is 2.5% so the withheld fees should be 3
516+
let token = token.with_compute_unit_limit(500_000);
516517
token
517518
.confidential_transfer_transfer_with_fee(
518519
&alice_meta.token_account,
@@ -671,6 +672,7 @@ async fn confidential_transfer_withdraw_withheld_tokens_from_accounts() {
671672
};
672673

673674
// Test fee is 2.5% so the withheld fees should be 3
675+
let token = token.with_compute_unit_limit(500_000);
674676
token
675677
.confidential_transfer_transfer_with_fee(
676678
&alice_meta.token_account,
@@ -801,6 +803,7 @@ async fn confidential_transfer_withdraw_withheld_tokens_from_mint_with_proof_con
801803
};
802804

803805
// Test fee is 2.5% so the withheld fees should be 3
806+
let token = token.with_compute_unit_limit(500_000);
804807
token
805808
.confidential_transfer_transfer_with_fee(
806809
&alice_meta.token_account,
@@ -969,6 +972,7 @@ async fn confidential_transfer_withdraw_withheld_tokens_from_accounts_with_proof
969972
};
970973

971974
// Test fee is 2.5% so the withheld fees should be 3
975+
let token = token.with_compute_unit_limit(500_000);
972976
token
973977
.confidential_transfer_transfer_with_fee(
974978
&alice_meta.token_account,
@@ -1159,6 +1163,7 @@ async fn confidential_transfer_harvest_withheld_tokens_to_mint() {
11591163
.unwrap();
11601164

11611165
// Test fee is 2.5% so the withheld fees should be 3
1166+
let token = token.with_compute_unit_limit(500_000);
11621167
token
11631168
.confidential_transfer_transfer_with_fee(
11641169
&alice_meta.token_account,

0 commit comments

Comments
 (0)