Skip to content

Commit

Permalink
priority fee cap signal
Browse files Browse the repository at this point in the history
  • Loading branch information
miralandlabs committed Aug 10, 2024
1 parent 55520e2 commit 03dae5e
Show file tree
Hide file tree
Showing 7 changed files with 145 additions and 28 deletions.
85 changes: 80 additions & 5 deletions src/components/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use solana_client_wasm::solana_sdk::native_token::lamports_to_sol;

use crate::{
components::{Appearance, BackupKeypairWarning, Copyable},
gateway::{FEE_URL, RPC_URL},
gateway::{FEE_URL, PRIORITY_FEE_CAP, RPC_URL},
hooks::{
use_appearance, use_explorer, use_pubkey, use_fee_url, use_rpc_url,
use_show_backup_warning, use_sol_balance, Explorer, FeeUrl, RpcUrl,
use_appearance, use_explorer, use_pubkey, use_fee_url, use_rpc_url, use_priority_fee_cap,
use_show_backup_warning, use_sol_balance, Explorer, FeeUrl, PriorityFeeCap, RpcUrl,
},
route::Route,
};
Expand All @@ -32,6 +32,12 @@ pub fn Settings() -> Element {
let mut fee_url_error = use_signal::<Option<String>>(|| None);
let is_fee_url_edited = fee_url.read().0.ne(&*fee_url_input.read());

// MI
let mut priority_fee_cap = use_priority_fee_cap();
let mut priority_fee_cap_input = use_signal(|| priority_fee_cap.read().0.clone());
let mut priority_fee_cap_error = use_signal::<Option<u64>>(|| None);
let is_priority_fee_cap_edited = priority_fee_cap.read().0.ne(&*priority_fee_cap_input.read());

let container_class = "flex flex-row gap-8 justify-between w-full sm:px-1";
let section_title_class = "text-lg md:text-2xl font-bold";
let data_title_class = "font-medium text-sm opacity-50 my-auto";
Expand Down Expand Up @@ -238,10 +244,10 @@ pub fn Settings() -> Element {
div {
p {
class: "{data_title_class}",
"Priority Fee Estimate"
"Priority Fee Estimate URL"
}
p {
class: "text-left text-white max-w-144",
class: "text-left dark:text-white max-w-144",
"This url is free for now and provided as-is. You are encouraged to use your own fee estimation url instead."
}
}
Expand Down Expand Up @@ -296,6 +302,75 @@ pub fn Settings() -> Element {
}
}
}
div {
class: "{container_class} flex-auto",
div {
p {
class: "{data_title_class}",
"Priority Fee Cap"
}
p {
class: "text-left dark:text-white max-w-144",
"You can set your own priority fee max value."
}
}
div {
class: "flex flex-auto flex-col gap-2",
div {
class: "flex flex-row flex-shrink h-min gap-1 shrink mb-auto",
input {
class: "bg-transparent disabled:opacity-50 dark:text-white text-right px-1 mb-auto rounded font-semibold hover:bg-green-600 transition-colors",
dir: "rtl",
step: 100_000,
min: 0,
max: 10_000_000,
r#type: "number",
value: "{priority_fee_cap.read().0}",
oninput: move |e| {
if let Ok(v) = e.value().parse::<u64>() {
priority_fee_cap.set(PriorityFeeCap(v));
}
}
}
p {
class: "my-auto font-semibold",
"microlamports"
}
}
div {
class: "flex flex-shrink gap-2 justify-end",
if let Some(err_str) = priority_fee_cap_error.read().clone() {
p {
class: "text-sm text-red-500 text-right",
"{err_str}"
}
}
div {
class: "flex flex-row gap-2",
if priority_fee_cap.read().0.ne(&PRIORITY_FEE_CAP) {
button {
class: "hover-100 active-200 rounded shrink ml-auto transition-colors px-2 py-1 font-semibold",
onclick: move |_| {
priority_fee_cap.set(PriorityFeeCap(PRIORITY_FEE_CAP));
priority_fee_cap_input.set(PRIORITY_FEE_CAP);
priority_fee_cap_error.set(None);
},
"Reset to default"
}
}
if is_priority_fee_cap_edited && priority_fee_cap_error.read().is_none() {
button {
class: "bg-green-500 hover:bg-green-600 active:bg-green-700 text-white rounded shrink ml-auto transition-colors px-2 py-1",
onclick: move |_| {
priority_fee_cap.set(PriorityFeeCap(priority_fee_cap_input.read().clone()));
},
"Save"
}
}
}
}
}
}
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/gateway/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub const FEE_URL: &str = "https://mainnet.helius-rpc.com/?api-key=cb135900-fab9
// royal: ore-app-classic ironforge RPC Endpoint
pub const RPC_URL: &str = "https://rpc.ironforge.network/mainnet?apiKey=01J3ZM0ECN63VB741S74YPCFWS";

pub const PRIORITY_FEE_CAP: u64 = 1_000_000; // microlamport

pub const CU_LIMIT_CREATE_ATA: u32 = 85_000; // MI added
pub const CU_LIMIT_CLAIM: u32 = 12_000;
pub const CU_LIMIT_STAKE: u32 = 12_000; // MI added
Expand Down
46 changes: 29 additions & 17 deletions src/gateway/pfee.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::hooks::use_fee_url;
use crate::hooks::{use_fee_url, use_priority_fee, use_priority_fee_cap};
use dioxus::signals::Readable;
use ore_api::consts::BUS_ADDRESSES;
use reqwest::Client;
Expand All @@ -24,6 +24,8 @@ pub struct RpcPrioritizationFee {
pub async fn get_recent_priority_fee_estimate() -> Result<u64, String> {
// Get url
let fee_url = use_fee_url();
let priority_fee = use_priority_fee();
let priority_fee_cap = use_priority_fee_cap();

// Select fee estiamte strategy
let host = Url::parse(&fee_url.read().0)
Expand Down Expand Up @@ -98,16 +100,35 @@ pub async fn get_recent_priority_fee_estimate() -> Result<u64, String> {
}
};

// Send request
let response: Value = client
// // Send request
// let response: Value = client
// .post(fee_url.read().0.clone())
// .json(&body)
// .send()
// .await
// .unwrap()
// .json()
// .await
// .unwrap();

// MI, Send request in two steps
// split json from send
// 1) handle response
let Ok(resp) = client
.post(fee_url.read().0.clone())
.json(&body)
.send()
.await
.unwrap()
.json()
.await
.unwrap();
else {
eprintln!("didn't get dynamic fee estimate, use default instead.");
return Ok(priority_fee.read().0);
};

// 2) handle json
let Ok(response) = resp.json::<Value>().await else {
eprintln!("didn't get json data from fee estimate response, use default instead.");
return Ok(priority_fee.read().0);
};

// Parse response
let calculated_fee = match strategy {
Expand Down Expand Up @@ -152,16 +173,7 @@ pub async fn get_recent_priority_fee_estimate() -> Result<u64, String> {
// Check if the calculated fee is higher than max
match calculated_fee {
Err(err) => Err(err),
Ok(fee) => {
// if let Some(max_fee) = self.priority_fee {
// Ok(fee.min(max_fee))
// } else {
// Ok(fee)
// }

// MI, set fee cap 300_000
Ok(fee.min(300_000))
}
Ok(fee) => Ok(fee.min(priority_fee_cap.read().0)),
}
}

Expand Down
2 changes: 2 additions & 0 deletions src/hooks/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ mod use_ore_supply;
mod use_persistent;
mod use_power_level;
mod use_priority_fee;
mod use_priority_fee_cap;
mod use_priority_fee_strategy;
mod use_proof;
mod use_rpc;
Expand All @@ -38,6 +39,7 @@ pub use use_ore_balances::*;
pub use use_ore_supply::*;
pub use use_power_level::*;
pub use use_priority_fee::*;
pub use use_priority_fee_cap::*;
pub use use_priority_fee_strategy::*;
pub use use_proof::*;
pub use use_rpc::*;
Expand Down
7 changes: 5 additions & 2 deletions src/hooks/use_priority_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,20 @@ use crate::hooks::use_persistent::use_persistent;

const KEY: &str = "priority_fee";

pub const DEFAULT_PRIORITY_FEE: u64 = 5000; // microlamports

#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq)]
pub struct PriorityFee(pub u64);

// initialize to 5000 microlamports
pub fn use_priority_fee() -> Signal<PriorityFee> {
let priority_fee = use_context::<Signal<PriorityFee>>();
let mut priority_fee_persistent = use_persistent(KEY, || PriorityFee(0));
let mut priority_fee_persistent = use_persistent(KEY, || PriorityFee(DEFAULT_PRIORITY_FEE));
use_effect(move || priority_fee_persistent.set(*priority_fee.read()));
priority_fee
}

pub fn use_priority_fee_provider() {
let priority_fee = use_persistent(KEY, || PriorityFee(0)).get();
let priority_fee = use_persistent(KEY, || PriorityFee(DEFAULT_PRIORITY_FEE)).get();
use_context_provider(|| Signal::new(priority_fee));
}
21 changes: 21 additions & 0 deletions src/hooks/use_priority_fee_cap.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use dioxus::prelude::*;
use serde::{Deserialize, Serialize};

use crate::{gateway::PRIORITY_FEE_CAP, hooks::use_persistent::use_persistent};

const KEY: &str = "priority_fee_cap";

#[derive(Clone, Debug, Default, Deserialize, Serialize, PartialEq)]
pub struct PriorityFeeCap(pub u64);

pub fn use_priority_fee_cap() -> Signal<PriorityFeeCap> {
let priority_fee_cap = use_context::<Signal<PriorityFeeCap>>();
let mut priority_fee_cap_persistent = use_persistent(KEY, || PriorityFeeCap(PRIORITY_FEE_CAP));
use_effect(move || priority_fee_cap_persistent.set(priority_fee_cap.read().clone()));
priority_fee_cap
}

pub fn use_priority_fee_cap_provider() {
let priority_fee_cap = use_persistent(KEY, || PriorityFeeCap(PRIORITY_FEE_CAP));
use_context_provider(|| Signal::new(priority_fee_cap.get()));
}
10 changes: 6 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ mod utils;

use crate::{
hooks::{
use_appearance_provider, use_explorer_provider, use_is_onboarded_provider,
use_power_level_provider, use_priority_fee_provider, use_priority_fee_strategy_provider,
use_fee_url_provider, use_rpc_url_provider, use_show_backup_warning_provider,
use_appearance_provider, use_explorer_provider, use_fee_url_provider,
use_is_onboarded_provider, use_power_level_provider, use_priority_fee_cap_provider,
use_priority_fee_provider, use_priority_fee_strategy_provider, use_rpc_url_provider,
use_show_backup_warning_provider,
},
route::Route,
};
Expand All @@ -29,8 +30,9 @@ pub fn App() -> Element {
use_explorer_provider();
use_power_level_provider();
use_is_onboarded_provider();
use_priority_fee_strategy_provider();
use_priority_fee_provider();
use_priority_fee_cap_provider();
use_priority_fee_strategy_provider();
use_show_backup_warning_provider();
use_fee_url_provider();
use_rpc_url_provider();
Expand Down

0 comments on commit 03dae5e

Please sign in to comment.