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

Commit 222ec66

Browse files
mvinesmergify[bot]
authored andcommitted
Use RpcClient::new_with_commitment()
1 parent c6a4ac9 commit 222ec66

File tree

1 file changed

+77
-131
lines changed

1 file changed

+77
-131
lines changed

token/cli/src/main.rs

Lines changed: 77 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,7 @@ use solana_clap_utils::{
2020
};
2121
use solana_cli_output::{display::println_name_value, return_signers, OutputFormat};
2222
use solana_client::{
23-
blockhash_query::BlockhashQuery, rpc_client::RpcClient, rpc_config::RpcSendTransactionConfig,
24-
rpc_request::TokenAccountsFilter,
23+
blockhash_query::BlockhashQuery, rpc_client::RpcClient, rpc_request::TokenAccountsFilter,
2524
};
2625
use solana_remote_wallet::remote_wallet::RemoteWalletManager;
2726
use solana_sdk::{
@@ -146,7 +145,6 @@ struct Config<'a> {
146145
verbose: bool,
147146
owner: Pubkey,
148147
fee_payer: Pubkey,
149-
commitment_config: CommitmentConfig,
150148
default_signer: DefaultSigner,
151149
nonce_account: Option<Pubkey>,
152150
nonce_authority: Option<Pubkey>,
@@ -165,10 +163,7 @@ fn new_throwaway_signer() -> (Option<Box<dyn Signer>>, Option<Pubkey>) {
165163
}
166164

167165
fn check_fee_payer_balance(config: &Config, required_balance: u64) -> Result<(), Error> {
168-
let balance = config
169-
.rpc_client
170-
.get_balance_with_commitment(&config.fee_payer, config.commitment_config)?
171-
.value;
166+
let balance = config.rpc_client.get_balance(&config.fee_payer)?;
172167
if balance < required_balance {
173168
Err(format!(
174169
"Fee payer, {}, has insufficient balance: {} required, {} available",
@@ -183,10 +178,7 @@ fn check_fee_payer_balance(config: &Config, required_balance: u64) -> Result<(),
183178
}
184179

185180
fn check_owner_balance(config: &Config, required_balance: u64) -> Result<(), Error> {
186-
let balance = config
187-
.rpc_client
188-
.get_balance_with_commitment(&config.owner, config.commitment_config)?
189-
.value;
181+
let balance = config.rpc_client.get_balance(&config.owner)?;
190182
if balance < required_balance {
191183
Err(format!(
192184
"Owner, {}, has insufficient balance: {} required, {} available",
@@ -306,14 +298,9 @@ fn command_create_account(
306298
)
307299
};
308300

309-
if let Some(account_data) = config
310-
.rpc_client
311-
.get_account_with_commitment(&account, config.commitment_config)?
312-
.value
313-
{
314-
if !(account_data.owner == system_program::id() && system_account_ok) {
315-
return Err(format!("Error: Account already exists: {}", account).into());
316-
}
301+
let account_data = config.rpc_client.get_account(&account)?;
302+
if !(account_data.owner == system_program::id() && system_account_ok) {
303+
return Err(format!("Error: Account already exists: {}", account).into());
317304
}
318305

319306
Ok(Some((
@@ -407,8 +394,7 @@ fn resolve_mint_info(
407394
if !config.sign_only {
408395
let source_account = config
409396
.rpc_client
410-
.get_token_account_with_commitment(&token_account, config.commitment_config)?
411-
.value
397+
.get_token_account(&token_account)?
412398
.ok_or_else(|| format!("Could not find token account {}", token_account))?;
413399
Ok((
414400
Pubkey::from_str(&source_account.mint)?,
@@ -462,64 +448,55 @@ fn command_transfer(
462448
let mut recipient_token_account = recipient;
463449
let mut minimum_balance_for_rent_exemption = 0;
464450

465-
if let Some(account_data) = config
466-
.rpc_client
467-
.get_account_with_commitment(&recipient, config.commitment_config)?
468-
.value
469-
{
470-
if account_data.owner == system_program::id() {
471-
recipient_token_account = get_associated_token_address(&recipient, &mint_pubkey);
472-
println!(
473-
" Recipient associated token account: {}",
474-
recipient_token_account
475-
);
451+
let account_data = config.rpc_client.get_account(&recipient)?;
452+
if account_data.owner == system_program::id() {
453+
recipient_token_account = get_associated_token_address(&recipient, &mint_pubkey);
454+
println!(
455+
" Recipient associated token account: {}",
456+
recipient_token_account
457+
);
476458

477-
let needs_funding = if let Some(recipient_token_account_data) = config
478-
.rpc_client
479-
.get_account_with_commitment(&recipient_token_account, config.commitment_config)?
480-
.value
481-
{
482-
if recipient_token_account_data.owner == system_program::id() {
483-
true
484-
} else if recipient_token_account_data.owner == spl_token::id() {
485-
false
486-
} else {
487-
return Err(
488-
format!("Error: Unsupported recipient address: {}", recipient).into(),
489-
);
490-
}
491-
} else {
459+
let needs_funding = if let Some(recipient_token_account_data) = config
460+
.rpc_client
461+
.get_account_with_commitment(&recipient_token_account, config.rpc_client.commitment())?
462+
.value
463+
{
464+
if recipient_token_account_data.owner == system_program::id() {
492465
true
493-
};
466+
} else if recipient_token_account_data.owner == spl_token::id() {
467+
false
468+
} else {
469+
return Err(format!("Error: Unsupported recipient address: {}", recipient).into());
470+
}
471+
} else {
472+
true
473+
};
494474

495-
if needs_funding {
496-
if fund_recipient {
497-
minimum_balance_for_rent_exemption += config
498-
.rpc_client
499-
.get_minimum_balance_for_rent_exemption(Account::LEN)?;
500-
println!(
501-
" Funding recipient: {} ({} SOL)",
502-
recipient_token_account,
503-
lamports_to_sol(minimum_balance_for_rent_exemption)
504-
);
505-
instructions.push(create_associated_token_account(
506-
&config.fee_payer,
507-
&recipient,
508-
&mint_pubkey,
509-
));
510-
} else {
511-
return Err(
512-
"Error: Recipient's associated token account does not exist. \
475+
if needs_funding {
476+
if fund_recipient {
477+
minimum_balance_for_rent_exemption += config
478+
.rpc_client
479+
.get_minimum_balance_for_rent_exemption(Account::LEN)?;
480+
println!(
481+
" Funding recipient: {} ({} SOL)",
482+
recipient_token_account,
483+
lamports_to_sol(minimum_balance_for_rent_exemption)
484+
);
485+
instructions.push(create_associated_token_account(
486+
&config.fee_payer,
487+
&recipient,
488+
&mint_pubkey,
489+
));
490+
} else {
491+
return Err(
492+
"Error: Recipient's associated token account does not exist. \
513493
Add `--fund-recipient` to fund their account"
514-
.into(),
515-
);
516-
}
494+
.into(),
495+
);
517496
}
518-
} else if account_data.owner != spl_token::id() {
519-
return Err(format!("Error: Unsupported recipient address: {}", recipient).into());
520497
}
521-
} else {
522-
return Err(format!("Error: Recipient does not exist: {}", recipient).into());
498+
} else if account_data.owner != spl_token::id() {
499+
return Err(format!("Error: Unsupported recipient address: {}", recipient).into());
523500
}
524501

525502
instructions.push(transfer_checked(
@@ -649,12 +626,7 @@ fn command_unwrap(config: &Config, address: Pubkey) -> CommandResult {
649626
if !config.sign_only {
650627
println!(
651628
" Amount: {} SOL",
652-
lamports_to_sol(
653-
config
654-
.rpc_client
655-
.get_balance_with_commitment(&address, config.commitment_config)?
656-
.value
657-
),
629+
lamports_to_sol(config.rpc_client.get_balance(&address)?),
658630
);
659631
}
660632
println!(" Recipient: {}", &config.owner);
@@ -702,8 +674,7 @@ fn command_revoke(config: &Config, account: Pubkey, delegate: Option<Pubkey>) ->
702674
let delegate = if !config.sign_only {
703675
let source_account = config
704676
.rpc_client
705-
.get_token_account_with_commitment(&account, config.commitment_config)?
706-
.value
677+
.get_token_account(&account)?
707678
.ok_or_else(|| format!("Could not find token account {}", account))?;
708679
if let Some(string) = source_account.delegate {
709680
Some(Pubkey::from_str(&string)?)
@@ -736,8 +707,7 @@ fn command_close(config: &Config, account: Pubkey, destination: Pubkey) -> Comma
736707
if !config.sign_only {
737708
let source_account = config
738709
.rpc_client
739-
.get_token_account_with_commitment(&account, config.commitment_config)?
740-
.value
710+
.get_token_account(&account)?
741711
.ok_or_else(|| format!("Could not find token account {}", account))?;
742712

743713
if !source_account.is_native && source_account.token_amount.ui_amount > 0.0 {
@@ -760,10 +730,7 @@ fn command_close(config: &Config, account: Pubkey, destination: Pubkey) -> Comma
760730
}
761731

762732
fn command_balance(config: &Config, address: Pubkey) -> CommandResult {
763-
let balance = config
764-
.rpc_client
765-
.get_token_account_balance_with_commitment(&address, config.commitment_config)?
766-
.value;
733+
let balance = config.rpc_client.get_token_account_balance(&address)?;
767734

768735
if config.verbose {
769736
println!("ui amount: {}", balance.ui_amount);
@@ -776,27 +743,20 @@ fn command_balance(config: &Config, address: Pubkey) -> CommandResult {
776743
}
777744

778745
fn command_supply(config: &Config, address: Pubkey) -> CommandResult {
779-
let supply = config
780-
.rpc_client
781-
.get_token_supply_with_commitment(&address, config.commitment_config)?
782-
.value;
746+
let supply = config.rpc_client.get_token_supply(&address)?;
783747

784748
println!("{}", supply.ui_amount);
785749
Ok(None)
786750
}
787751

788752
fn command_accounts(config: &Config, token: Option<Pubkey>) -> CommandResult {
789-
let accounts = config
790-
.rpc_client
791-
.get_token_accounts_by_owner_with_commitment(
792-
&config.owner,
793-
match token {
794-
Some(token) => TokenAccountsFilter::Mint(token),
795-
None => TokenAccountsFilter::ProgramId(spl_token::id()),
796-
},
797-
config.commitment_config,
798-
)?
799-
.value;
753+
let accounts = config.rpc_client.get_token_accounts_by_owner(
754+
&config.owner,
755+
match token {
756+
Some(token) => TokenAccountsFilter::Mint(token),
757+
None => TokenAccountsFilter::ProgramId(spl_token::id()),
758+
},
759+
)?;
800760
if accounts.is_empty() {
801761
println!("None");
802762
}
@@ -853,14 +813,10 @@ fn command_accounts(config: &Config, token: Option<Pubkey>) -> CommandResult {
853813

854814
fn command_gc(config: &Config) -> CommandResult {
855815
println!("Fetching token accounts");
856-
let accounts = config
857-
.rpc_client
858-
.get_token_accounts_by_owner_with_commitment(
859-
&config.owner,
860-
TokenAccountsFilter::ProgramId(spl_token::id()),
861-
config.commitment_config,
862-
)?
863-
.value;
816+
let accounts = config.rpc_client.get_token_accounts_by_owner(
817+
&config.owner,
818+
TokenAccountsFilter::ProgramId(spl_token::id()),
819+
)?;
864820
if accounts.is_empty() {
865821
println!("Nothing to do");
866822
return Ok(None);
@@ -1005,11 +961,7 @@ fn stringify_ui_token_amount_trimmed(amount: &UiTokenAmount) -> String {
1005961
}
1006962

1007963
fn command_account_info(config: &Config, address: Pubkey) -> CommandResult {
1008-
let account = config
1009-
.rpc_client
1010-
.get_token_account_with_commitment(&address, config.commitment_config)?
1011-
.value
1012-
.unwrap();
964+
let account = config.rpc_client.get_token_account(&address)?.unwrap();
1013965
println!();
1014966
println_name_value("Address:", &address.to_string());
1015967
println_name_value(
@@ -1043,11 +995,7 @@ fn command_account_info(config: &Config, address: Pubkey) -> CommandResult {
1043995
}
1044996

1045997
fn get_multisig(config: &Config, address: &Pubkey) -> Result<Multisig, Error> {
1046-
let account = config
1047-
.rpc_client
1048-
.get_account_with_commitment(&address, config.commitment_config)?
1049-
.value
1050-
.ok_or(format!("account does not exist: {}", address))?;
998+
let account = config.rpc_client.get_account(&address)?;
1051999
Multisig::unpack(&account.data).map_err(|e| e.into())
10521000
}
10531001

@@ -1709,11 +1657,13 @@ fn main() {
17091657
let multisigner_pubkeys = multisigner_ids.iter().collect::<Vec<_>>();
17101658

17111659
Config {
1712-
rpc_client: RpcClient::new(json_rpc_url),
1660+
rpc_client: RpcClient::new_with_commitment(
1661+
json_rpc_url,
1662+
CommitmentConfig::single_gossip(),
1663+
),
17131664
verbose,
17141665
owner,
17151666
fee_payer,
1716-
commitment_config: CommitmentConfig::single_gossip(),
17171667
default_signer,
17181668
nonce_account,
17191669
nonce_authority,
@@ -1990,7 +1940,10 @@ fn main() {
19901940
};
19911941
let (recent_blockhash, fee_calculator) = config
19921942
.blockhash_query
1993-
.get_blockhash_and_fee_calculator(&config.rpc_client, config.commitment_config)
1943+
.get_blockhash_and_fee_calculator(
1944+
&config.rpc_client,
1945+
config.rpc_client.commitment(),
1946+
)
19941947
.unwrap_or_else(|e| {
19951948
eprintln!("error: {}", e);
19961949
exit(1);
@@ -2012,14 +1965,7 @@ fn main() {
20121965
transaction.try_sign(&signer_info.signers, recent_blockhash)?;
20131966
let signature = config
20141967
.rpc_client
2015-
.send_and_confirm_transaction_with_spinner_and_config(
2016-
&transaction,
2017-
config.commitment_config,
2018-
RpcSendTransactionConfig {
2019-
preflight_commitment: Some(config.commitment_config.commitment),
2020-
..RpcSendTransactionConfig::default()
2021-
},
2022-
)?;
1968+
.send_and_confirm_transaction_with_spinner(&transaction)?;
20231969
println!("Signature: {}", signature);
20241970
}
20251971
}

0 commit comments

Comments
 (0)