Allow different commitment level through utilities

Change-Id: I7269e5c64c82dceaf1d70c74fbdfe6e44c247ec5
This commit is contained in:
Reisen 2021-07-07 08:48:37 +00:00 committed by Hendrik Hofstadt
parent 768f768001
commit 007098aaec
1 changed files with 29 additions and 2 deletions

View File

@ -94,18 +94,19 @@ use solitaire::{
pub use helpers::*; pub use helpers::*;
/// Simple API wrapper for quickly preparing and sending transactions. /// Simple API wrapper for quickly preparing and sending transactions.
fn execute( pub fn execute(
client: &RpcClient, client: &RpcClient,
payer: &Keypair, payer: &Keypair,
signers: &[&Keypair], signers: &[&Keypair],
instructions: &[Instruction], instructions: &[Instruction],
commitment_level: CommitmentConfig,
) -> Result<Signature, ClientError> { ) -> Result<Signature, ClientError> {
let mut transaction = Transaction::new_with_payer(instructions, Some(&payer.pubkey())); let mut transaction = Transaction::new_with_payer(instructions, Some(&payer.pubkey()));
let recent_blockhash = client.get_recent_blockhash().unwrap().0; let recent_blockhash = client.get_recent_blockhash().unwrap().0;
transaction.sign(&signers.to_vec(), recent_blockhash); transaction.sign(&signers.to_vec(), recent_blockhash);
client.send_and_confirm_transaction_with_spinner_and_config( client.send_and_confirm_transaction_with_spinner_and_config(
&transaction, &transaction,
CommitmentConfig::processed(), commitment_level,
RpcSendTransactionConfig { RpcSendTransactionConfig {
skip_preflight: true, skip_preflight: true,
preflight_commitment: None, preflight_commitment: None,
@ -131,6 +132,23 @@ mod helpers {
(payer, rpc, program) (payer, rpc, program)
} }
/// Wait for a single transaction to fully finalize, guaranteeing chain state has been
/// confirmed. Useful for consistently fetching data during state checks.
pub fn sync(client: &RpcClient, payer: &Keypair) {
execute(
client,
payer,
&[payer],
&[system_instruction::transfer(
&payer.pubkey(),
&payer.pubkey(),
1,
)],
CommitmentConfig::finalized(),
)
.unwrap();
}
/// Fetch account data, the loop is there to re-attempt until data is available. /// Fetch account data, the loop is there to re-attempt until data is available.
pub fn get_account_data<T: BorshDeserialize>( pub fn get_account_data<T: BorshDeserialize>(
client: &RpcClient, client: &RpcClient,
@ -233,6 +251,7 @@ mod helpers {
from, from,
&[from], &[from],
&[system_instruction::transfer(&from.pubkey(), to, lamports)], &[system_instruction::transfer(&from.pubkey(), to, lamports)],
CommitmentConfig::processed(),
) )
} }
@ -255,6 +274,7 @@ mod helpers {
initial_guardians, initial_guardians,
) )
.unwrap()], .unwrap()],
CommitmentConfig::processed(),
) )
} }
@ -290,6 +310,7 @@ mod helpers {
system_instruction::transfer(&payer.pubkey(), &fee_collector, fee), system_instruction::transfer(&payer.pubkey(), &fee_collector, fee),
instruction, instruction,
], ],
CommitmentConfig::processed(),
)?; )?;
Ok(message_key) Ok(message_key)
@ -328,6 +349,7 @@ mod helpers {
) )
.unwrap(), .unwrap(),
], ],
CommitmentConfig::processed(),
)?; )?;
} }
Ok(()) Ok(())
@ -344,6 +366,7 @@ mod helpers {
payer, payer,
&[payer], &[payer],
&[instructions::post_vaa(*program, payer.pubkey(), vaa)], &[instructions::post_vaa(*program, payer.pubkey(), vaa)],
CommitmentConfig::processed(),
) )
} }
@ -364,6 +387,7 @@ mod helpers {
payload_message, payload_message,
spill, spill,
)], )],
CommitmentConfig::processed(),
) )
} }
@ -390,6 +414,7 @@ mod helpers {
new_index, new_index,
sequence, sequence,
)], )],
CommitmentConfig::processed(),
) )
} }
@ -412,6 +437,7 @@ mod helpers {
emitter, emitter,
sequence, sequence,
)], )],
CommitmentConfig::processed(),
) )
} }
@ -436,6 +462,7 @@ mod helpers {
sequence, sequence,
recipient, recipient,
)], )],
CommitmentConfig::processed(),
) )
} }
} }