Skip to content

Commit 97e2e53

Browse files
committed
Fix up priority fee computation
1 parent 7cd5341 commit 97e2e53

File tree

1 file changed

+32
-19
lines changed

1 file changed

+32
-19
lines changed

src/main.rs

Lines changed: 32 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -208,14 +208,14 @@ fn apply_priority_fee(
208208
instructions: &mut Vec<Instruction>,
209209
compute_unit_limit: u32,
210210
priority_fee: PriorityFee,
211-
) -> Result<u64, Box<dyn std::error::Error>> {
212-
let priority_fee_lamports = if let Some(exact_lamports) = priority_fee.exact_lamports() {
213-
exact_lamports
211+
) -> Result<(), Box<dyn std::error::Error>> {
212+
let compute_budget = if let Some(exact_lamports) = priority_fee.exact_lamports() {
213+
ComputeBudget::new(compute_unit_limit, exact_lamports)
214214
} else {
215-
let prioritization_fees =
215+
let recent_compute_unit_prices =
216216
rpc_client_utils::get_recent_priority_fees_for_instructions(rpc_client, instructions)?;
217217

218-
let max_compute_unit_price_micro_lamports = prioritization_fees
218+
let compute_unit_price_micro_lamports = recent_compute_unit_prices
219219
.iter()
220220
.max()
221221
.copied()
@@ -226,23 +226,36 @@ fn apply_priority_fee(
226226
helius_rpc::HeliusPriorityLevel::High,
227227
instructions,
228228
) {
229-
println!("Note: helius compute unit price (high) estimate is {priority_fee_estimate}. `sys` computed {max_compute_unit_price_micro_lamports}");
229+
println!(
230+
"Note: helius compute unit price (high) estimate is {priority_fee_estimate}. \
231+
`sys` computed {compute_unit_price_micro_lamports}"
232+
);
230233
}
231234

232-
max_compute_unit_price_micro_lamports
233-
};
235+
let compute_budget = ComputeBudget {
236+
compute_unit_price_micro_lamports,
237+
compute_unit_limit,
238+
};
234239

235-
if priority_fee_lamports > priority_fee.max_lamports() {
236-
return Err(format!(
237-
"Transaction too expensive. Priority fee of {} is greater than max fee of {}",
238-
Sol(priority_fee_lamports),
239-
Sol(priority_fee.max_lamports())
240-
)
241-
.into());
242-
}
243-
println!("Priority fee: {}", Sol(priority_fee_lamports));
240+
if compute_budget.priority_fee_lamports() > priority_fee.max_lamports() {
241+
println!(
242+
"Note: Computed priority fee of {} is greater than max fee",
243+
Sol(priority_fee.max_lamports())
244+
);
245+
ComputeBudget::new(compute_unit_limit, priority_fee.max_lamports())
246+
} else {
247+
compute_budget
248+
}
249+
};
244250

245-
let compute_budget = ComputeBudget::new(compute_unit_limit, priority_fee_lamports);
251+
println!(
252+
"Priority fee: {}",
253+
Sol(compute_budget.priority_fee_lamports())
254+
);
255+
assert!(
256+
0.01 > lamports_to_sol(compute_budget.priority_fee_lamports()),
257+
"Priority fee too large, Bug?"
258+
);
246259

247260
instructions.push(
248261
compute_budget::ComputeBudgetInstruction::set_compute_unit_limit(
@@ -256,7 +269,7 @@ fn apply_priority_fee(
256269
),
257270
);
258271

259-
Ok(priority_fee_lamports)
272+
Ok(())
260273
}
261274

262275
fn add_exchange_deposit_address_to_db(

0 commit comments

Comments
 (0)