Skip to content

Commit

Permalink
pr feedback
Browse files Browse the repository at this point in the history
  • Loading branch information
bryzettler committed Jan 14, 2025
1 parent 2527773 commit 0dd0e45
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 242 deletions.
12 changes: 6 additions & 6 deletions helium-lib/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static USER_AGENT: &str = concat!(env!("CARGO_PKG_NAME"), "/", env!("CARGO_PKG_V
pub struct SolanaClient {
pub inner: Arc<SolanaRpcClient>,
pub base_url: String,
pub wallet: Option<Arc<Keypair>>,
pub keypair: Option<Arc<Keypair>>,
}

impl Default for SolanaClient {
Expand All @@ -44,7 +44,7 @@ impl Default for SolanaClient {
}

impl SolanaClient {
pub fn new(url: &str, wallet: Option<Arc<Keypair>>) -> Result<Self, Error> {
pub fn new(url: &str, keypair: Option<Arc<Keypair>>) -> Result<Self, Error> {
let client = Arc::new(
solana_client::nonblocking::rpc_client::RpcClient::new_with_commitment(
url.to_string(),
Expand All @@ -55,7 +55,7 @@ impl SolanaClient {
Ok(Self {
inner: client,
base_url: url.to_string(),
wallet,
keypair,
})
}

Expand All @@ -67,10 +67,10 @@ impl SolanaClient {
}

pub fn pubkey(&self) -> Result<Pubkey, Error> {
self.wallet
self.keypair
.as_ref()
.map(|wallet| wallet.pubkey())
.ok_or_else(|| Error::WalletUnconfigured)
.map(|keypair| keypair.pubkey())
.ok_or_else(|| Error::KeypairUnconfigured)
}
}

Expand Down
86 changes: 30 additions & 56 deletions helium-lib/src/dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,51 +184,31 @@ impl SubDao {

pub fn rewardable_entity_config_key(&self) -> Pubkey {
let sub_dao = self.key();
match self {
Self::Iot => {
Pubkey::find_program_address(
&[
b"rewardable_entity_config",
&sub_dao.as_ref(),
"IOT".as_bytes(),
],
&helium_entity_manager::ID,
)
.0
}
Self::Mobile => {
Pubkey::find_program_address(
&[
b"rewardable_entity_config",
&sub_dao.as_ref(),
"MOBILE".as_bytes(),
],
&helium_entity_manager::ID,
)
.0
}
}
let suffix = match self {
Self::Iot => b"IOT".as_ref(),
Self::Mobile => b"MOBILE".as_ref(),
};

Pubkey::find_program_address(
&[b"rewardable_entity_config", &sub_dao.as_ref(), suffix],
&helium_entity_manager::id(),
)
.0
}

pub fn info_key<E: AsEntityKey>(&self, entity_key: &E) -> Pubkey {
let config_key = self.rewardable_entity_config_key();
let hash = Sha256::digest(&entity_key.as_entity_key());
match self {
Self::Iot => {
Pubkey::find_program_address(
&[b"iot_info", &config_key.as_ref(), &hash],
&helium_entity_manager::ID,
)
.0
}
Self::Mobile => {
Pubkey::find_program_address(
&[b"mobile_info", &config_key.as_ref(), &hash],
&helium_entity_manager::ID,
)
.0
}
}
let prefix = match self {
Self::Iot => "iot_info",
Self::Mobile => "mobile_info",
};

Pubkey::find_program_address(
&[prefix.as_bytes(), &config_key.as_ref(), &hash],
&helium_entity_manager::id(),
)
.0
}

pub fn lazy_distributor_key(&self) -> Pubkey {
Expand All @@ -253,22 +233,16 @@ impl SubDao {

pub fn config_key(&self) -> Pubkey {
let sub_dao = self.key();
match self {
Self::Iot => {
Pubkey::find_program_address(
&[b"iot_config", &sub_dao.as_ref()],
&helium_entity_manager::ID,
)
.0
}
Self::Mobile => {
Pubkey::find_program_address(
&[b"mobile_config", sub_dao.as_ref()],
&helium_entity_manager::ID,
)
.0
}
}
let prefix = match self {
Self::Iot => "iot_config",
Self::Mobile => "mobile_config",
};

Pubkey::find_program_address(
&[prefix.as_bytes(), &sub_dao.as_ref()],
&helium_entity_manager::id(),
)
.0
}

pub fn epoch_info_key(&self) -> Pubkey {
Expand Down
16 changes: 2 additions & 14 deletions helium-lib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,16 +47,8 @@ pub enum Error {
Decode(#[from] DecodeError),
#[error("encode: {0}")]
Encode(#[from] EncodeError),
#[error("Wallet is not configured")]
WalletUnconfigured,
#[error("error: {0}")]
Error(String),
}

impl From<Box<dyn std::error::Error>> for Error {
fn from(err: Box<dyn std::error::Error>) -> Self {
Self::other(err.to_string())
}
#[error("Keypair is not configured")]
KeypairUnconfigured,
}

impl From<solana_client::client_error::ClientError> for Error {
Expand Down Expand Up @@ -92,10 +84,6 @@ impl Error {
_ => false,
}
}

pub fn other<S: ToString>(reason: S) -> Self {
Self::Error(reason.to_string())
}
}

#[derive(Debug, Error)]
Expand Down
1 change: 0 additions & 1 deletion helium-lib/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ pub mod priority_fee;
pub mod programs;
pub mod reward;
pub mod token;
pub mod transaction;

pub use anchor_client;
pub use anchor_client::solana_client;
Expand Down
36 changes: 28 additions & 8 deletions helium-lib/src/priority_fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use crate::{
signers::Signers,
transaction::Transaction,
},
transaction::replace_or_insert_instruction,
};
use itertools::Itertools;

Expand Down Expand Up @@ -132,14 +131,14 @@ pub async fn compute_price_instruction_for_accounts<C: AsRef<SolanaRpcClient>>(

pub async fn compute_budget_for_instructions<C: AsRef<SolanaRpcClient>, T: Signers + ?Sized>(
client: &C,
instructions: Vec<Instruction>,
instructions: &[Instruction],
signers: &T,
compute_multiplier: f32,
payer: Option<&Pubkey>,
blockhash: Option<solana_program::hash::Hash>,
) -> Result<solana_sdk::instruction::Instruction, crate::error::Error> {
// Check for existing compute unit limit instruction and replace it if found
let mut updated_instructions = instructions.clone();
let mut updated_instructions = instructions.to_vec();
for ix in &mut updated_instructions {
if ix.program_id == solana_sdk::compute_budget::id()
&& ix.data.first()
Expand Down Expand Up @@ -174,18 +173,18 @@ pub async fn compute_budget_for_instructions<C: AsRef<SolanaRpcClient>, T: Signe

pub async fn auto_compute_limit_and_price<C: AsRef<SolanaRpcClient>, T: Signers + ?Sized>(
client: &C,
instructions: Vec<Instruction>,
instructions: &[Instruction],
signers: &T,
compute_multiplier: f32,
payer: Option<&Pubkey>,
blockhash: Option<solana_program::hash::Hash>,
) -> Result<Vec<Instruction>, Error> {
let mut updated_instructions = instructions.clone();
let mut updated_instructions = instructions.to_vec();

// Compute budget instruction
let compute_budget_ix = compute_budget_for_instructions(
client,
instructions.clone(),
&updated_instructions,
signers,
compute_multiplier,
payer,
Expand All @@ -204,7 +203,28 @@ pub async fn auto_compute_limit_and_price<C: AsRef<SolanaRpcClient>, T: Signers
let compute_price_ix =
compute_price_instruction_for_accounts(client, &accounts, MIN_PRIORITY_FEE).await?;

replace_or_insert_instruction(&mut updated_instructions, compute_budget_ix, 0);
replace_or_insert_instruction(&mut updated_instructions, compute_price_ix, 1);
insert_or_replace_compute_instructions(
&mut updated_instructions,
compute_budget_ix,
compute_price_ix,
);

Ok(updated_instructions)
}

fn insert_or_replace_compute_instructions(
instructions: &mut Vec<Instruction>,
budget_ix: Instruction,
price_ix: Instruction,
) {
if let Some(pos) = instructions
.iter()
.position(|ix| ix.program_id == solana_sdk::compute_budget::id())
{
instructions[pos] = budget_ix;
instructions[pos + 1] = price_ix;
} else {
instructions.insert(0, budget_ix);
instructions.insert(1, price_ix);
}
}
Loading

0 comments on commit 0dd0e45

Please sign in to comment.