Skip to content

Commit

Permalink
pass toolbar state to gateway
Browse files Browse the repository at this point in the history
  • Loading branch information
miralandlabs committed Aug 11, 2024
1 parent 37779af commit d306191
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 15 deletions.
36 changes: 23 additions & 13 deletions src/gateway/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ mod pubkey;
// MI
use crate::{
components::PriorityFeeStrategy,
hooks::{use_miner_toolbar_state, MinerStatusMessage, UpdateMinerToolbarState},
hooks::{use_miner_toolbar_state, MinerStatusMessage, MinerToolbarState, UpdateMinerToolbarState},
};
use async_std::future::{timeout, Future};
use cached::proc_macro::cached;
use dioxus::prelude::*;
pub use error::*;
use gloo_storage::{LocalStorage, Storage};
use ore_api::{
Expand Down Expand Up @@ -72,7 +73,7 @@ const GATEWAY_DELAY: u64 = 0; //300;

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

pub enum ComputeBudget {
DynamicLimitEstimatePrice,
Expand Down Expand Up @@ -190,6 +191,7 @@ impl Gateway {
ixs: &[Instruction],
compute_budget: ComputeBudget,
skip_confirm: bool,
mut toolbar_state: Option<&mut Signal<MinerToolbarState>>,
) -> GatewayResult<Signature> {
let signer = signer();
// log::info!("starting use priority fee..."); // MI
Expand Down Expand Up @@ -272,22 +274,30 @@ impl Gateway {
let mut tx = Transaction::new_with_payer(final_ixs.as_slice(), Some(&signer.pubkey()));

// Submit tx
// let mut toolbar_state = use_miner_toolbar_state();
let mut attempts = 0;
loop {
log::info!("Attempt: {:?}", attempts);
// toolbar_state.set_status_message(MinerStatusMessage::Submitting(attempts as u64, fee));
if toolbar_state.is_some() {
toolbar_state.as_mut().unwrap().set_status_message(MinerStatusMessage::Submitting(attempts as u64, fee));
}

// 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) {
pfee::get_recent_priority_fee_estimate().await.unwrap()
if let Ok(fee) = pfee::get_recent_priority_fee_estimate().await {
fee
} else {
log::info!("failed to get fee estimate, use last known priority fee setting instead."); // MI
fee
}
} else {
fee
};

// toolbar_state
// .set_status_message(MinerStatusMessage::Submitting(attempts as u64, fee));
if toolbar_state.is_some() {
toolbar_state.as_mut().unwrap().set_status_message(MinerStatusMessage::Submitting(attempts as u64, fee));
}

final_ixs.remove(1);
final_ixs.insert(1, ComputeBudgetInstruction::set_compute_unit_price(fee));
Expand Down Expand Up @@ -381,7 +391,7 @@ impl Gateway {
// Sign and send transaction.
let ix = ore_api::instruction::open(signer.pubkey(), signer.pubkey(), signer.pubkey());

match self.send_and_confirm(&[ix], CB, false).await {
match self.send_and_confirm(&[ix], CB, false, None).await {
Ok(_) => Ok(()),
Err(_) => Err(GatewayError::FailedOpen),
}
Expand Down Expand Up @@ -410,7 +420,7 @@ impl Gateway {
}
let ix = ore_api::instruction::claim(signer.pubkey(), beneficiary, amount);
ixs.push(ix);
self.send_and_confirm(&ixs, CB, false).await
self.send_and_confirm(&ixs, CB, false, None).await
}

// MI
Expand All @@ -422,7 +432,7 @@ impl Gateway {
let cu_price_ix = ComputeBudgetInstruction::set_compute_unit_price(priority_fee);
let ix = ore_api::instruction::stake(signer.pubkey(), sender, amount);

self.send_and_confirm(&[cu_limit_ix, cu_price_ix, ix], CB, false)
self.send_and_confirm(&[cu_limit_ix, cu_price_ix, ix], CB, false, None)
.await
}

Expand Down Expand Up @@ -460,7 +470,7 @@ impl Gateway {
amount,
));

self.send_and_confirm(&ixs, CB, false).await
self.send_and_confirm(&ixs, CB, false, None).await
}

pub async fn transfer_ore(
Expand Down Expand Up @@ -493,7 +503,7 @@ impl Gateway {
)
.unwrap();

self.send_and_confirm(&[cu_limit_ix, cu_price_ix, memo_ix, transfer_ix], CB, false)
self.send_and_confirm(&[cu_limit_ix, cu_price_ix, memo_ix, transfer_ix], CB, false, None)
.await
}

Expand Down Expand Up @@ -541,7 +551,7 @@ impl Gateway {
);

match self
.send_and_confirm(&[cu_limit_ix, cu_price_ix, ix], CB, false)
.send_and_confirm(&[cu_limit_ix, cu_price_ix, ix], CB, false, None)
.await
{
Ok(_) => {}
Expand Down
6 changes: 4 additions & 2 deletions src/miner/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ impl Miner {

// Submit solution
log::info!("submit solution..."); // MI
match submit_solution(&gateway, best_solution, priority_fee).await {
match submit_solution(&gateway, best_solution, priority_fee, toolbar_state).await {
// Start mining again
Ok(sig) => {
log::info!("Sig: {}", sig); // MI
Expand Down Expand Up @@ -193,12 +193,14 @@ pub async fn submit_solution(
gateway: &Rc<Gateway>,
solution: Solution,
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));
let cu_limit_ix = ComputeBudgetInstruction::set_compute_unit_limit(CU_LIMIT_MINE);
let cu_price_ix = ComputeBudgetInstruction::set_compute_unit_price(priority_fee);
let auth_ix = ore_api::instruction::auth(utils::proof_pubkey(signer.pubkey())); // MI
Expand All @@ -223,7 +225,7 @@ pub async fn submit_solution(

// Send and configm
log::info!("starting send-and-confirm..."); // MI
gateway.send_and_confirm(&ixs, gateway::CB, false).await
gateway.send_and_confirm(&ixs, gateway::CB, false, Some(toolbar_state)).await
}

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

0 comments on commit d306191

Please sign in to comment.