Skip to content

Commit 12c9f5d

Browse files
authored
Merge pull request #125 from Arrowana/fix/triton-take-average-of-last-20-slots
fix: Triton average over window of recent slots instead of last
2 parents 5e6ef7e + a4dce0a commit 12c9f5d

File tree

2 files changed

+40
-9
lines changed

2 files changed

+40
-9
lines changed

Cargo.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/dynamic_fee.rs

Lines changed: 36 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use crate::Miner;
33
use ore_api::consts::BUS_ADDRESSES;
44
use reqwest::Client;
55
use serde_json::{json, Value};
6+
use solana_client::rpc_response::RpcPrioritizationFee;
67
use url::Url;
78

89
enum FeeStrategy {
@@ -129,11 +130,17 @@ impl Miner {
129130
)
130131
})
131132
.ok_or_else(|| format!("Failed to parse priority fee response: {:?}", response)),
132-
FeeStrategy::Triton => response["result"]
133-
.as_array()
134-
.and_then(|arr| arr.last())
135-
.and_then(|last| last["prioritizationFee"].as_u64())
136-
.ok_or_else(|| format!("Failed to parse priority fee response: {:?}", response)),
133+
FeeStrategy::Triton => {
134+
serde_json::from_value::<Vec<RpcPrioritizationFee>>(response["result"].clone())
135+
.map(|prioritization_fees| {
136+
estimate_prioritization_fee_micro_lamports(prioritization_fees)
137+
})
138+
.ok_or_else(|error: serde_json::Error| {
139+
Err(format!(
140+
"Failed to parse priority fee. Response: {response:?}, error: {error}"
141+
))
142+
})
143+
}
137144
};
138145

139146
// Check if the calculated fee is higher than max
@@ -149,3 +156,27 @@ impl Miner {
149156
}
150157
}
151158
}
159+
160+
/// Our estimate is the average over the last 20 slots
161+
pub fn estimate_prioritization_fee_micro_lamports(
162+
prioritization_fees: Vec<RpcPrioritizationFee>,
163+
) -> u64 {
164+
let prioritization_fees = prioritization_fees
165+
.into_iter()
166+
.rev()
167+
.take(20)
168+
.map(
169+
|RpcPrioritizationFee {
170+
prioritization_fee, ..
171+
}| prioritization_fee,
172+
)
173+
.collect::<Vec<_>>();
174+
if prioritization_fees.is_empty() {
175+
panic!("Response does not contain any prioritization fees");
176+
}
177+
178+
let prioritization_fee =
179+
prioritization_fees.iter().sum::<u64>() / prioritization_fees.len() as u64;
180+
181+
prioritization_fee
182+
}

0 commit comments

Comments
 (0)