Skip to content

Commit

Permalink
rust client: fix TransactionBuilder append to correctly handle CU (#960)
Browse files Browse the repository at this point in the history
  • Loading branch information
farnyser authored May 15, 2024
1 parent 2d9f248 commit fdb3e98
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
1 change: 1 addition & 0 deletions bin/settler/src/settle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ impl<'a> SettleBatchProcessor<'a> {
payer: fee_payer.pubkey(),
signers: vec![fee_payer],
config: client.config().transaction_builder_config.clone(),
additional_cus: vec![],
}
.transaction_with_blockhash(self.blockhash)
}
Expand Down
19 changes: 15 additions & 4 deletions lib/client/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,7 @@ impl MangoClient {
payer: payer.pubkey(),
signers: vec![owner, payer],
config: client.config.transaction_builder_config.clone(),
additional_cus: vec![],
}
.send_and_confirm(&client)
.await?;
Expand Down Expand Up @@ -2395,6 +2396,7 @@ impl MangoClient {
payer: fee_payer.pubkey(),
signers: vec![fee_payer],
config: self.client.config.transaction_builder_config.clone(),
additional_cus: vec![],
})
}

Expand All @@ -2409,6 +2411,7 @@ impl MangoClient {
payer: fee_payer.pubkey(),
signers: vec![fee_payer],
config: self.client.config.transaction_builder_config.clone(),
additional_cus: vec![],
}
.simulate(&self.client)
.await
Expand Down Expand Up @@ -2518,6 +2521,7 @@ pub struct TransactionBuilder {
pub signers: Vec<Arc<Keypair>>,
pub payer: Pubkey,
pub config: TransactionBuilderConfig,
pub additional_cus: Vec<u32>,
}

pub type SimulateTransactionResponse =
Expand Down Expand Up @@ -2558,10 +2562,15 @@ impl TransactionBuilder {

let cu_per_ix = self.config.compute_budget_per_instruction.unwrap_or(0);
if !has_compute_unit_limit && cu_per_ix > 0 {
let ix_count: u32 = (ixs.len() - cu_instructions).try_into().unwrap();
let ix_count: u32 = (ixs.len() - cu_instructions - self.additional_cus.len())
.try_into()
.unwrap();
let additional_cu_sum: u32 = self.additional_cus.iter().sum();
ixs.insert(
0,
ComputeBudgetInstruction::set_compute_unit_limit(cu_per_ix * ix_count),
ComputeBudgetInstruction::set_compute_unit_limit(
cu_per_ix * ix_count + additional_cu_sum,
),
);
}

Expand Down Expand Up @@ -2649,8 +2658,10 @@ impl TransactionBuilder {
}

pub fn append(&mut self, prepared_instructions: PreparedInstructions) {
self.instructions
.extend(prepared_instructions.to_instructions());
// Do NOT use to instruction s it would add a CU limit
// That CU limit would overwrite the one computed by the transaction builder
self.instructions.extend(prepared_instructions.instructions);
self.additional_cus.push(prepared_instructions.cu);
}
}

Expand Down
2 changes: 2 additions & 0 deletions lib/client/src/swap/jupiter_v6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,7 @@ impl<'a> JupiterV6<'a> {
.send()
.await
.context("quote request to jupiter")?;

let quote: QuoteResponse =
util::http_error_handling(response).await.with_context(|| {
format!("error requesting jupiter route between {input_mint} and {output_mint}")
Expand Down Expand Up @@ -392,6 +393,7 @@ impl<'a> JupiterV6<'a> {
.config()
.transaction_builder_config
.clone(),
additional_cus: vec![],
})
}

Expand Down
1 change: 1 addition & 0 deletions lib/client/src/swap/sanctum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,7 @@ impl<'a> Sanctum<'a> {
.config()
.transaction_builder_config
.clone(),
additional_cus: vec![],
})
}

Expand Down
12 changes: 10 additions & 2 deletions lib/client/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use solana_sdk::instruction::Instruction;

use anchor_lang::prelude::{AccountMeta, Pubkey};
use anyhow::Context;
use tracing::warn;

/// Some Result<> types don't convert to anyhow::Result nicely. Force them through stringification.
pub trait AnyhowWrap {
Expand Down Expand Up @@ -80,8 +81,15 @@ pub async fn http_error_handling<T: serde::de::DeserializeOwned>(
if !status.is_success() {
anyhow::bail!("http request failed, status: {status}, body: {response_text}");
}
serde_json::from_str::<T>(&response_text)
.with_context(|| format!("response has unexpected format, body: {response_text}"))
let object = serde_json::from_str::<T>(&response_text);

match object {
Ok(o) => Ok(o),
Err(e) => {
warn!("{}", e);
anyhow::bail!("response has unexpected format, body: {response_text}")
}
}
}

pub fn to_readonly_account_meta(pubkey: Pubkey) -> AccountMeta {
Expand Down

0 comments on commit fdb3e98

Please sign in to comment.