Skip to content

Commit

Permalink
mine fee strategy
Browse files Browse the repository at this point in the history
  • Loading branch information
miralandlabs committed Aug 11, 2024
1 parent d306191 commit 964949a
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 31 deletions.
4 changes: 2 additions & 2 deletions src/components/mine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ pub fn PriorityFeeStrategyConfig() -> Element {
priority_fee_strategy.set(s);
}
},
option { initial_selected: priority_fee_strategy.read().eq(&PriorityFeeStrategy::Dynamic), value: "{PriorityFeeStrategy::Dynamic}", "{PriorityFeeStrategy::Dynamic}" }
option { initial_selected: priority_fee_strategy.read().eq(&PriorityFeeStrategy::Estimate), value: "{PriorityFeeStrategy::Estimate}", "{PriorityFeeStrategy::Estimate}" }
option { initial_selected: priority_fee_strategy.read().eq(&PriorityFeeStrategy::Static), value: "{PriorityFeeStrategy::Static}", "{PriorityFeeStrategy::Static}" }
}
}
Expand Down Expand Up @@ -298,7 +298,7 @@ pub fn PriorityFeeConfig() -> Element {
div {
class: "flex flex-row flex-shrink h-min gap-1 shrink mb-auto",
input {
disabled: priority_fee_strategy.read().eq(&PriorityFeeStrategy::Dynamic),
disabled: priority_fee_strategy.read().eq(&PriorityFeeStrategy::Estimate),
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,
Expand Down
7 changes: 4 additions & 3 deletions src/components/priority_fee_strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,18 @@ use serde::{Deserialize, Serialize};
#[derive(Clone, Copy, Debug, Default, Deserialize, Serialize, PartialEq)]
pub enum PriorityFeeStrategy {
#[default]
Estimate,
Static,
Dynamic,
// Static(/* priority fee: */ u64),
}

impl FromStr for PriorityFeeStrategy {
type Err = io::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"Estimate priority fee" => Ok(PriorityFeeStrategy::Estimate),
"Static priority fee" => Ok(PriorityFeeStrategy::Static),
"Dynamic priority fee" => Ok(PriorityFeeStrategy::Dynamic),
_ => Err(io::Error::new(
io::ErrorKind::InvalidData,
"Unknown priority fee strategy",
Expand All @@ -27,8 +28,8 @@ impl FromStr for PriorityFeeStrategy {
impl fmt::Display for PriorityFeeStrategy {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
PriorityFeeStrategy::Estimate => write!(f, "Estimate priority fee"),
PriorityFeeStrategy::Static => write!(f, "Static priority fee"),
PriorityFeeStrategy::Dynamic => write!(f, "Dynamic priority fee"),
}
}
}
16 changes: 6 additions & 10 deletions src/gateway/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ mod pubkey;
// MI
use crate::{
components::PriorityFeeStrategy,
hooks::{use_miner_toolbar_state, MinerStatusMessage, MinerToolbarState, UpdateMinerToolbarState},
hooks::{MinerStatusMessage, MinerToolbarState, UpdateMinerToolbarState},
};
use async_std::future::{timeout, Future};
use cached::proc_macro::cached;
Expand Down Expand Up @@ -72,8 +72,8 @@ const CONFIRM_DELAY: u64 = 500;
const GATEWAY_DELAY: u64 = 0; //300;

const TIP_AMOUNT: u64 = 100_000;
const DEFAULT_CU_LIMIT: u32 = 200_000;
const DEFAULT_CU_PRICE: u64 = 10_000;
pub const DEFAULT_CU_LIMIT: u32 = 200_000;
pub const DEFAULT_CU_PRICE: u64 = 10_000;

pub enum ComputeBudget {
DynamicLimitEstimatePrice,
Expand Down Expand Up @@ -194,10 +194,6 @@ impl Gateway {
mut toolbar_state: Option<&mut Signal<MinerToolbarState>>,
) -> GatewayResult<Signature> {
let signer = signer();
// log::info!("starting use priority fee..."); // MI
// let priority_fee = use_priority_fee();
// log::info!("starting use priority fee strategy..."); // MI
// let priority_fee_strategy = use_priority_fee_strategy();

const CUS: u32 = 1_400_000;
// Set compute budget
Expand All @@ -208,7 +204,7 @@ impl Gateway {
let fee = pfee::get_recent_priority_fee_estimate().await.unwrap();
final_ixs.push(ComputeBudgetInstruction::set_compute_unit_limit(CUS));
final_ixs.push(ComputeBudgetInstruction::set_compute_unit_price(fee));
(CUS, PriorityFeeStrategy::Dynamic, fee)
(CUS, PriorityFeeStrategy::Estimate, fee)
}
ComputeBudget::DynamicLimitStaticPrice(fee) => {
// TODO simulate
Expand All @@ -220,7 +216,7 @@ impl Gateway {
let fee = pfee::get_recent_priority_fee_estimate().await.unwrap();
final_ixs.push(ComputeBudgetInstruction::set_compute_unit_limit(cus));
final_ixs.push(ComputeBudgetInstruction::set_compute_unit_price(fee));
(cus, PriorityFeeStrategy::Dynamic, fee)
(cus, PriorityFeeStrategy::Estimate, fee)
}
ComputeBudget::FixedLimitStaticPrice(cus, fee) => {
final_ixs.push(ComputeBudgetInstruction::set_compute_unit_limit(cus));
Expand Down Expand Up @@ -284,7 +280,7 @@ impl Gateway {
// Sign tx with a new blockhash (after approximately ~45 sec)
if attempts % 10 == 0 {
// Reset the compute unit price
let fee = if strategy.eq(&PriorityFeeStrategy::Dynamic) {
let fee = if strategy.eq(&PriorityFeeStrategy::Estimate) {
if let Ok(fee) = pfee::get_recent_priority_fee_estimate().await {
fee
} else {
Expand Down
4 changes: 2 additions & 2 deletions src/hooks/use_priority_fee_strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ const KEY: &str = "priority_fee_strategy";

pub fn use_priority_fee_strategy() -> Signal<PriorityFeeStrategy> {
let priority_fee_strategy = use_context::<Signal<PriorityFeeStrategy>>();
let mut priority_fee_strategy_persistent = use_persistent(KEY, || PriorityFeeStrategy::Dynamic);
let mut priority_fee_strategy_persistent = use_persistent(KEY, || PriorityFeeStrategy::Estimate);
use_effect(move || priority_fee_strategy_persistent.set(*priority_fee_strategy.read()));
priority_fee_strategy
}

pub fn use_priority_fee_strategy_provider() {
let priority_fee_strategy = use_persistent(KEY, || PriorityFeeStrategy::Dynamic).get();
let priority_fee_strategy = use_persistent(KEY, || PriorityFeeStrategy::Estimate).get();
use_context_provider(|| Signal::new(priority_fee_strategy));
}
51 changes: 37 additions & 14 deletions src/miner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,21 @@ use ore_api::{
use rand::Rng;
use serde_wasm_bindgen::to_value;
use solana_client_wasm::solana_sdk::{
keccak::Hash as KeccakHash, compute_budget::ComputeBudgetInstruction, pubkey::Pubkey,
compute_budget::ComputeBudgetInstruction, keccak::Hash as KeccakHash, pubkey::Pubkey,
signature::Signature, signer::Signer,
};
use web_sys::{window, Worker};
pub use web_worker::*;

use crate::{
components::PriorityFeeStrategy,
gateway::{self, signer, Gateway, GatewayResult, CU_LIMIT_MINE},
gateway::{
self, signer, ComputeBudget, Gateway, GatewayResult, CU_LIMIT_MINE, DEFAULT_CU_LIMIT,
PRIORITY_FEE_CAP,
},
hooks::{
MinerStatus, MinerStatusMessage, MinerToolbarState, PowerLevel,
PriorityFee, ReadMinerToolbarState, UpdateMinerToolbarState,
MinerStatus, MinerStatusMessage, MinerToolbarState, PowerLevel, PriorityFee,
ReadMinerToolbarState, UpdateMinerToolbarState,
},
utils,
};
Expand Down Expand Up @@ -134,27 +137,40 @@ impl Miner {
}
}

// let priority_fee = self.priority_fee.read().0;
let priority_fee = if self
.priority_fee_strategy
.read()
.eq(&PriorityFeeStrategy::Dynamic)
.eq(&PriorityFeeStrategy::Estimate)
{
gateway::get_recent_priority_fee_estimate().await.unwrap()
if let Ok(priority_fee) = gateway::get_recent_priority_fee_estimate().await {
priority_fee
} else {
PRIORITY_FEE_CAP
}
} else {
self.priority_fee.read().0
};
log::info!(
"current priority fee strategy: {}",
self.priority_fee_strategy
);
log::info!("current priority fee: {}", priority_fee);
self.priority_fee.clone().set(PriorityFee(priority_fee)); // set signal
log::info!("set priority fee signal: {}", priority_fee);

// Update toolbar state
toolbar_state.set_display_hash(KeccakHash::new_from_array(best_hash));
toolbar_state.set_status_message(MinerStatusMessage::Submitting(0, priority_fee));

// Submit solution
log::info!("submit solution..."); // MI
match submit_solution(&gateway, best_solution, priority_fee, toolbar_state).await {
match submit_solution(
&gateway,
best_solution,
*self.priority_fee_strategy.read(),
priority_fee,
toolbar_state,
)
.await
{
// Start mining again
Ok(sig) => {
log::info!("Sig: {}", sig); // MI
Expand Down Expand Up @@ -192,12 +208,11 @@ impl Miner {
pub async fn submit_solution(
gateway: &Rc<Gateway>,
solution: Solution,
priority_fee_strategy: PriorityFeeStrategy,
priority_fee: u64,
toolbar_state: &mut Signal<MinerToolbarState>,
) -> GatewayResult<Signature> {
let signer = signer();
// let priority_fee = use_priority_fee();
// let priority_fee_strategy = use_priority_fee_strategy();

// Build ixs
toolbar_state.set_status_message(MinerStatusMessage::Submitting(0, priority_fee));
Expand All @@ -224,8 +239,16 @@ pub async fn submit_solution(
ixs.push(ix);

// Send and configm
log::info!("starting send-and-confirm..."); // MI
gateway.send_and_confirm(&ixs, gateway::CB, false, Some(toolbar_state)).await
log::info!("starting send and confirm..."); // MI
let cb = match priority_fee_strategy {
PriorityFeeStrategy::Estimate => ComputeBudget::FixedLimitEstimatePrice(DEFAULT_CU_LIMIT),
PriorityFeeStrategy::Static => {
ComputeBudget::FixedLimitStaticPrice(DEFAULT_CU_LIMIT, priority_fee)
}
};
gateway
.send_and_confirm(&ixs, cb, false, Some(toolbar_state))
.await
}

async fn needs_reset(gateway: &Rc<Gateway>) -> bool {
Expand Down

0 comments on commit 964949a

Please sign in to comment.