Rust client: Add prioritization fee option (#433)

This commit is contained in:
Christian Kamm 2023-02-02 14:23:33 +01:00 committed by GitHub
parent 628feafcb0
commit 51a649ac60
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 35 additions and 10 deletions

View File

@ -115,6 +115,7 @@ impl Rpc {
commitment: solana_sdk::commitment_config::CommitmentConfig::confirmed(),
fee_payer: Arc::new(fee_payer),
timeout: None,
prioritization_micro_lamports: 5,
})
}
}

View File

@ -47,6 +47,7 @@ pub struct Client {
pub fee_payer: Arc<Keypair>,
pub commitment: CommitmentConfig,
pub timeout: Option<Duration>,
pub prioritization_micro_lamports: u64,
}
impl Client {
@ -55,12 +56,14 @@ impl Client {
commitment: CommitmentConfig,
fee_payer: &Keypair,
timeout: Option<Duration>,
prioritization_micro_lamports: u64,
) -> Self {
Self {
cluster,
fee_payer: Arc::new(fee_payer.clone()),
commitment,
timeout,
prioritization_micro_lamports,
}
}
@ -168,8 +171,6 @@ impl MangoClient {
account_num: u32,
mango_account_name: &str,
) -> anyhow::Result<(Pubkey, Signature)> {
let rpc = client.rpc_async();
let account = Pubkey::find_program_address(
&[
group.as_ref(),
@ -208,7 +209,7 @@ impl MangoClient {
payer: payer.pubkey(),
signers: vec![owner, payer],
}
.send_and_confirm(&rpc)
.send_and_confirm(&client)
.await?;
Ok((account, txsig))
@ -1351,7 +1352,6 @@ impl MangoClient {
}),
});
let rpc = self.client.rpc_async();
let payer = self.owner.pubkey(); // maybe use fee_payer? but usually it's the same
let mut address_lookup_tables = self.mango_address_lookup_tables().await?;
address_lookup_tables.extend(jup_alts.into_iter());
@ -1362,7 +1362,7 @@ impl MangoClient {
payer,
signers: vec![&self.owner],
}
.send_and_confirm(&rpc)
.send_and_confirm(&self.client)
.await
}
@ -1447,7 +1447,7 @@ impl MangoClient {
payer: self.client.fee_payer.pubkey(),
signers: vec![&self.owner, &*self.client.fee_payer],
}
.send_and_confirm(&self.client.rpc_async())
.send_and_confirm(&self.client)
.await
}
@ -1461,7 +1461,7 @@ impl MangoClient {
payer: self.client.fee_payer.pubkey(),
signers: vec![&*self.client.fee_payer],
}
.send_and_confirm(&self.client.rpc_async())
.send_and_confirm(&self.client)
.await
}
}
@ -1516,8 +1516,17 @@ impl<'a> TransactionBuilder<'a> {
Ok(tx)
}
pub async fn send_and_confirm(self, rpc: &RpcClientAsync) -> anyhow::Result<Signature> {
let tx = self.transaction(rpc).await?;
pub async fn send_and_confirm(mut self, client: &Client) -> anyhow::Result<Signature> {
let rpc = client.rpc_async();
if client.prioritization_micro_lamports != 0 {
self.instructions.insert(
0,
solana_sdk::compute_budget::ComputeBudgetInstruction::set_compute_unit_price(
client.prioritization_micro_lamports,
),
)
}
let tx = self.transaction(&rpc).await?;
rpc.send_and_confirm_transaction(&tx)
.await
.map_err(prettify_solana_client_error)

View File

@ -59,6 +59,10 @@ struct Cli {
#[clap(long, env, default_value_t = 10)]
timeout: u64,
/// prioritize each transaction with this many microlamports/cu
#[clap(long, env, default_value = "0")]
prioritization_micro_lamports: u64,
}
#[derive(Subcommand, Debug, Clone)]
@ -100,6 +104,7 @@ async fn main() -> Result<(), anyhow::Error> {
commitment,
&owner,
Some(Duration::from_secs(cli.timeout)),
cli.prioritization_micro_lamports,
),
cli.mango_account,
owner,

View File

@ -82,6 +82,10 @@ struct Cli {
#[clap(long, env, default_value = "1")]
rebalance_slippage: f64,
/// prioritize each transaction with this many microlamports/cu
#[clap(long, env, default_value = "0")]
prioritization_micro_lamports: u64,
}
pub fn encode_address(addr: &Pubkey) -> String {
@ -107,7 +111,13 @@ async fn main() -> anyhow::Result<()> {
let rpc_timeout = Duration::from_secs(10);
let cluster = Cluster::Custom(rpc_url.clone(), ws_url.clone());
let commitment = CommitmentConfig::processed();
let client = Client::new(cluster.clone(), commitment, &liqor_owner, Some(rpc_timeout));
let client = Client::new(
cluster.clone(),
commitment,
&liqor_owner,
Some(rpc_timeout),
cli.prioritization_micro_lamports,
);
// The representation of current on-chain account data
let chain_data = Arc::new(RwLock::new(chain_data::ChainData::new()));