Skip to content

Commit 1cfd4d6

Browse files
committed
use smart bus strategy
1 parent a2d137c commit 1cfd4d6

File tree

1 file changed

+27
-9
lines changed

1 file changed

+27
-9
lines changed

src/mine.rs

Lines changed: 27 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,13 @@ use drillx::{
77
};
88
use ore_api::{
99
consts::{BUS_ADDRESSES, BUS_COUNT, EPOCH_DURATION},
10-
state::{Config, Proof},
10+
state::{Bus, Config, Proof},
1111
};
12+
use ore_utils::AccountDeserialize;
1213
use rand::Rng;
1314
use solana_program::pubkey::Pubkey;
1415
use solana_rpc_client::spinner;
15-
use solana_sdk::signer::Signer;
16+
use solana_sdk::{account, signer::Signer};
1617

1718
use crate::{
1819
args::MineArgs,
@@ -59,7 +60,7 @@ impl Miner {
5960
ixs.push(ore_api::instruction::mine(
6061
signer.pubkey(),
6162
signer.pubkey(),
62-
find_bus(),
63+
self.find_bus().await,
6364
solution,
6465
));
6566
self.send_and_confirm(&ixs, ComputeBudget::Fixed(compute_budget), false)
@@ -194,14 +195,31 @@ impl Miner {
194195
.saturating_sub(clock.unix_timestamp)
195196
.max(0) as u64
196197
}
198+
199+
async fn find_bus(&self) -> Pubkey {
200+
// Fetch the bus with the largest balance
201+
if let Ok(accounts) = self.rpc_client.get_multiple_accounts(&BUS_ADDRESSES).await {
202+
let mut top_bus_balance: u64 = 0;
203+
let mut top_bus = BUS_ADDRESSES[0];
204+
for account in accounts {
205+
if let Some(account) = account {
206+
if let Ok(bus) = Bus::try_from_bytes(&account.data) {
207+
if bus.rewards.gt(&top_bus_balance) {
208+
top_bus_balance = bus.rewards;
209+
top_bus = BUS_ADDRESSES[bus.id as usize];
210+
}
211+
}
212+
}
213+
}
214+
return top_bus;
215+
}
216+
217+
// Otherwise return a random bus
218+
let i = rand::thread_rng().gen_range(0..BUS_COUNT);
219+
BUS_ADDRESSES[i]
220+
}
197221
}
198222

199223
fn calculate_multiplier(balance: u64, top_balance: u64) -> f64 {
200224
1.0 + (balance as f64 / top_balance as f64).min(1.0f64)
201225
}
202-
203-
// TODO Pick a better strategy (avoid draining bus)
204-
fn find_bus() -> Pubkey {
205-
let i = rand::thread_rng().gen_range(0..BUS_COUNT);
206-
BUS_ADDRESSES[i]
207-
}

0 commit comments

Comments
 (0)