Skip to content

Commit

Permalink
remove helium-program-library dependency (#16)
Browse files Browse the repository at this point in the history
  • Loading branch information
lthiery authored Jul 18, 2023
1 parent 96fa68a commit 0041b07
Show file tree
Hide file tree
Showing 13 changed files with 1,161 additions and 1,444 deletions.
2,463 changes: 1,083 additions & 1,380 deletions Cargo.lock

Large diffs are not rendered by default.

13 changes: 4 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,17 @@ csv = "*"
clap = { version = "4", features = ["derive"]}
futures = "0"
helium-crypto = {version = "0.6.3", features = ["solana"] }
helium-anchor-gen = { git = "https://github.com/lthiery/helium-anchor-gen.git" }
reqwest = { version = "0", default-features = false, features = ["rustls-tls", "json"]}
helium-sub-daos = { git = "https://github.com/helium/helium-program-library.git", features = [ "no-entrypoint" ] }
fanout = { git = "https://github.com/helium/helium-program-library.git", features = [ "no-entrypoint" ] }
voter-stake-registry = { git = "https://github.com/helium/helium-program-library.git", features = [ "no-entrypoint" ] }
serde = { version = "*", features = ["derive"] }
serde_json = "*"
json = "*"
lazy_static = "1"
rand = "0.8"
serde_bytes = "0"
solana-account-decoder = "1"
solana-sdk = "1.14.10"
solana-program = "1.14.10"
spl-associated-token-account = { version = "*", git = "https://github.com/ChewingGlass/solana-program-library", features = [ "no-entrypoint" ] }
spl-token-2022 = { version = "*", git = "https://github.com/ChewingGlass/solana-program-library" }
tokio ={ version = "1", features = ["fs", "macros"] }
spl-associated-token-account = { version = "1" }
spl-token-2022 = { version = "0" }
tokio ={ version = "1", features = ["fs", "macros", "rt-multi-thread"] }
thiserror = "1"
helium-api = "3"
mime_guess = "2"
Expand Down
31 changes: 9 additions & 22 deletions src/cli/accounts.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,6 @@ impl HeliumBalances {
}
}

use solana_account_decoder::parse_account_data::{ParsableAccount, PARSABLE_PROGRAM_IDS};
use spl_token_2022::extension::StateWithExtensions;
use std::sync::Arc;

#[derive(Debug)]
Expand All @@ -74,16 +72,15 @@ struct SplAccount {
pub token: u64,
}

use spl_token_2022::extension::StateWithExtensions;

async fn get_account(
rpc_client: &Arc<rpc::Client>,
mint: &Pubkey,
pubkey: &Pubkey,
) -> Result<SplAccount> {
let spl_atc = spl_associated_token_account::get_associated_token_address(pubkey, mint);
let program_id = Pubkey::from_str("TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA").unwrap();

let account = rpc_client.get_account(&spl_atc).await;

if let Err(rpc::Error::AccountNotFound) = account {
return Ok(SplAccount {
mint: *mint,
Expand All @@ -92,21 +89,11 @@ async fn get_account(
});
};
let account = account?;

let program_name = PARSABLE_PROGRAM_IDS
.get(&program_id)
.ok_or(Error::SolanaProgramIdNotParsable(program_id.to_string()))?;

match program_name {
ParsableAccount::SplToken | ParsableAccount::SplToken2022 => {
let account_data =
StateWithExtensions::<spl_token_2022::state::Account>::unpack(&account.data)?;
Ok(SplAccount {
mint: *mint,
lamports: account.lamports,
token: account_data.base.amount,
})
}
_ => Err(Error::UnexpectedProgramName),
}
let account_data =
StateWithExtensions::<spl_token_2022::state::Account>::unpack(&account.data)?;
Ok(SplAccount {
mint: *mint,
lamports: account.lamports,
token: account_data.base.amount,
})
}
6 changes: 4 additions & 2 deletions src/cli/epoch_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use anchor_lang::AccountDeserialize;
/// Scrape all SubDao epoch info
pub struct EpochInfo {}

use helium_sub_daos::SubDaoEpochInfoV0;
use helium_anchor_gen::{
helium_sub_daos::{SubDaoEpochInfoV0, SubDaoEpochInfoV0Trait},
voter_stake_registry::PRECISION_FACTOR,
};
use rpc::GetProgramAccountsFilter;
use rust_decimal::Decimal;
use serde::Serialize;
use std::collections::HashMap;
use voter_stake_registry::state::PRECISION_FACTOR;

pub async fn get_epoch_summaries(rpc_client: &rpc::Client) -> Result<Vec<EpochSummary>> {
let helium_dao_id = Pubkey::from_str(HELIUM_DAO_ID)?;
Expand Down
4 changes: 2 additions & 2 deletions src/cli/locked.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::*;

use helium_anchor_gen::voter_stake_registry::{PositionV0, Registrar, VotingMintConfigV0};
use std::collections::{HashMap, HashSet};
use voter_stake_registry::state::{PositionV0, Registrar, VotingMintConfigV0};

#[derive(Debug, Clone, clap::Args)]
/// Fetches all delegated positions and total HNT, veHNT, and subDAO delegations.
Expand Down Expand Up @@ -59,7 +59,7 @@ pub async fn get_data(rpc_client: &rpc::Client) -> Result<Data> {
let mut registrar_to_mint = HashMap::new();
for (pubkey, registrar) in registrar_keys.iter().zip(registrars_raw.iter()) {
let mint = &registrar.voting_mints[0];
mint_configs.insert(mint.mint, mint.clone());
mint_configs.insert(mint.mint, *mint);
registrar_to_mint.insert(**pubkey, mint.mint);
}

Expand Down
6 changes: 4 additions & 2 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use super::*;

use anchor_lang::prelude::{AccountDeserialize, Result as AnchorResult};
use solana_sdk::pubkey::Pubkey;
use anchor_lang::{
prelude::{AccountDeserialize, Result as AnchorResult},
solana_program::pubkey::Pubkey,
};

pub mod accounts;
pub mod epoch_info;
Expand Down
47 changes: 38 additions & 9 deletions src/cli/positions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,15 @@ pub struct Positions {
verify: bool,
}

use helium_sub_daos::{
caclulate_vhnt_info, DelegatedPositionV0, PrecisePosition, SubDaoV0, VehntInfo as VehntInfoRaw,
use helium_anchor_gen::{
helium_sub_daos::{
caclulate_vhnt_info, DelegatedPositionV0, PrecisePosition, SubDaoV0,
VehntInfo as VehntInfoRaw,
},
voter_stake_registry::{
LockupKind, PositionV0, PositionV0Trait, VotingMintConfigV0, PRECISION_FACTOR,
},
};
use voter_stake_registry::state::{LockupKind, PositionV0, VotingMintConfigV0, PRECISION_FACTOR};

#[allow(unused)]
/// This function can be used when a single query is too big
Expand Down Expand Up @@ -192,13 +197,25 @@ pub async fn get_positions_of_mint(
timestamp: i64,
) -> Result<(HashMap<Pubkey, PositionV0>, HashMap<Pubkey, Position>)> {
let voting_mint_config = &positions_data.mint_configs[&Pubkey::from_str(mint)?];
let voting_mint_config = VotingMintConfigV0 {
mint: voting_mint_config.mint,
baseline_vote_weight_scaled_factor: voting_mint_config.baseline_vote_weight_scaled_factor,
max_extra_lockup_vote_weight_scaled_factor: voting_mint_config
.max_extra_lockup_vote_weight_scaled_factor,
genesis_vote_power_multiplier: voting_mint_config.genesis_vote_power_multiplier,
genesis_vote_power_multiplier_expiration_ts: voting_mint_config
.genesis_vote_power_multiplier_expiration_ts,
lockup_saturation_secs: voting_mint_config.lockup_saturation_secs,
digit_shift: voting_mint_config.digit_shift,
};

let mut positions_raw = HashMap::new();
let mut positions = HashMap::new();

for (pubkey, position) in positions_data.positions.iter() {
if let Some(position_mint) = positions_data.registrar_to_mint.get(&position.registrar) {
if position_mint.to_string().as_str() == mint {
positions_raw.insert(*pubkey, position.clone());
positions_raw.insert(*pubkey, *position);
let owner: Result<Pubkey> = match position_owners_map.get(pubkey) {
Some(owner) => Ok(*owner),
None => {
Expand All @@ -218,9 +235,9 @@ pub async fn get_positions_of_mint(
let position = Position::try_from_positionv0(
owner,
*pubkey,
position.clone(),
*position,
timestamp,
voting_mint_config,
&voting_mint_config,
)
.await?;
positions.insert(*pubkey, position);
Expand Down Expand Up @@ -253,8 +270,8 @@ pub async fn get_data(
position_owners_map: &mut PositionOwners,
) -> Result<AllPositionsData> {
let mut all_data = AllPositionsData::new();
let mut d = &mut all_data.vehnt;
let mut s = &mut all_data.stats;
let d = &mut all_data.vehnt;
let s = &mut all_data.stats;

let positions_data = locked::get_data(rpc_client).await?;
// if the map is empty, we assume it hasn't been initialized and so we initialize it
Expand Down Expand Up @@ -344,6 +361,18 @@ pub async fn get_data(
.collect::<AnchorResult<Vec<_>>>()?;

let voting_mint_config = &positions_data.mint_configs[&Pubkey::from_str(HNT_MINT)?];
let voting_mint_config = VotingMintConfigV0 {
mint: voting_mint_config.mint,
baseline_vote_weight_scaled_factor: voting_mint_config.baseline_vote_weight_scaled_factor,
max_extra_lockup_vote_weight_scaled_factor: voting_mint_config
.max_extra_lockup_vote_weight_scaled_factor,
genesis_vote_power_multiplier: voting_mint_config.genesis_vote_power_multiplier,
genesis_vote_power_multiplier_expiration_ts: voting_mint_config
.genesis_vote_power_multiplier_expiration_ts,
lockup_saturation_secs: voting_mint_config.lockup_saturation_secs,
digit_shift: voting_mint_config.digit_shift,
};

for (pubkey, delegated_position) in delegated_positions {
let delegated_position = delegated_position;
let position_v0 = vehnt_positions_raw.get(&delegated_position.position);
Expand All @@ -355,7 +384,7 @@ pub async fn get_data(
delegated_position,
&epoch_info,
position_v0,
voting_mint_config,
&voting_mint_config,
)?);
d.delegated_positions
.push(PositionLegacy::from(position.clone()));
Expand Down
14 changes: 5 additions & 9 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
use crate::rpc;
use solana_sdk::pubkey::Pubkey;
use anchor_lang::solana_program::pubkey::Pubkey;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum Error {
#[error("solana pubkey parse: {0}")]
SolanaPubkeyParse(#[from] solana_sdk::pubkey::ParsePubkeyError),
SolanaPubkeyParse(#[from] anchor_lang::solana_program::pubkey::ParsePubkeyError),
#[error("io error: {0}")]
Io(#[from] std::io::Error),
#[error("anchor lang: {0}")]
AnchorLang(Box<anchor_lang::error::Error>),
#[error("base64 decode error: {0}")]
Base64Decode(#[from] base64::DecodeError),
#[error("invalid subdao: {0}")]
InvalidSubDao(solana_sdk::pubkey::Pubkey),
InvalidSubDao(Pubkey),
#[error("reqwest error: {0}")]
Reqwest(#[from] reqwest::Error),
#[error("parse int error: {0}")]
Expand All @@ -30,12 +30,6 @@ pub enum Error {
Axum(#[from] axum::BoxError),
#[error("{0}")]
Custom(&'static str),
#[error("SolanaProgramIdNotParsable: {0}")]
SolanaProgramIdNotParsable(String),
#[error("SolanaProgramError: {0}")]
SolanaProgram(#[from] solana_program::program_error::ProgramError),
#[error("UnexpectedProgramName")]
UnexpectedProgramName,
#[error("serde json error: {0}")]
SerdeJson(#[from] serde_json::Error),
#[error("rpc error: {0}")]
Expand All @@ -44,6 +38,8 @@ pub enum Error {
MissingPosition { position: Pubkey },
#[error("No registrar for mint {0}")]
NoRegistrarForMint(&'static str),
#[error("SolanaProgramError: {0}")]
SolanaProgram(#[from] anchor_lang::prelude::ProgramError),
}

impl From<anchor_lang::error::Error> for Error {
Expand Down
4 changes: 3 additions & 1 deletion src/rpc/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub enum Error {
#[error("base64 decode error: {0}")]
B64Decode(#[from] base64::DecodeError),
#[error("solana parse pubkey error: {0}")]
SolanaParsePubkey(#[from] solana_sdk::pubkey::ParsePubkeyError),
SolanaParsePubkey(#[from] anchor_lang::solana_program::pubkey::ParsePubkeyError),
#[error("join error: {0} ")]
Join(#[from] tokio::task::JoinError),
#[error("no asset by authority for {0}")]
Expand All @@ -24,6 +24,8 @@ pub enum Error {
ParseInt(#[from] std::num::ParseIntError),
#[error("Account not found.")]
AccountNotFound,
#[error("try from slice error: {0}")]
TryFromSlice(#[from] std::array::TryFromSliceError),
}

impl Error {
Expand Down
8 changes: 4 additions & 4 deletions src/rpc/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use anchor_lang::solana_program::pubkey::Pubkey;
use base64::Engine;
use serde::{de::DeserializeOwned, Deserialize, Serialize, Serializer};
use solana_sdk::pubkey::Pubkey;
use std::str::FromStr;
use std::time::{Duration, SystemTime};

Expand Down Expand Up @@ -166,7 +166,7 @@ impl Client {
pub async fn get_owner_by_mint(&self, mint: &Pubkey) -> Result<Pubkey> {
let token_largest_accounts = self.get_token_largest_account(mint).await?;
let account_data = self.get_account_data(&token_largest_accounts).await?;
Ok(Pubkey::new(&account_data[32..64]))
Ok(Pubkey::try_from(&account_data[32..64])?)
}

pub async fn get_all_owners_by_mint(
Expand Down Expand Up @@ -195,8 +195,8 @@ impl Client {
.await?;
let these_owners = account_data
.into_iter()
.map(|v| Pubkey::new(&v[32..64]))
.collect::<Vec<Pubkey>>();
.map(|v| Pubkey::try_from(&v[32..64]).map_err(Error::TryFromSlice))
.collect::<Result<Vec<Pubkey>>>()?;
owners.extend(&these_owners);
if last_output.elapsed().as_secs() > 5 || owners.len() == position_id.len() {
last_output = Instant::now();
Expand Down
2 changes: 1 addition & 1 deletion src/server/accounts.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use super::{positions, *};
use anchor_lang::solana_program::pubkey::Pubkey;
use axum::extract::Path;
use solana_sdk::pubkey::Pubkey;
use std::str::FromStr;

use crate::cli::accounts::{self, HeliumBalances};
Expand Down
2 changes: 1 addition & 1 deletion src/server/positions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ pub use crate::cli::positions::{
AllPositionsData, LockupType, Position, PositionOwners, Positions,
};
use crate::types::SubDao;
use anchor_lang::solana_program::pubkey::Pubkey;
use axum::{
body::{self, Empty, Full},
extract::Path,
http::{header, HeaderValue},
response::{IntoResponse, Response},
};
use solana_sdk::pubkey::Pubkey;
use std::ops::DerefMut;
use std::str::FromStr;
use tokio::{fs::File, io::AsyncReadExt};
Expand Down
5 changes: 3 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use super::Result;
use solana_sdk::pubkey::Pubkey;
use anchor_lang::solana_program::pubkey::Pubkey;

pub const HELIUM_DAO_ID: &str = "hdaoVTCqhfHHo75XdAMxBKdUqvq1i5bF23sisBqVgGR";
pub const HELIUM_VSR_ID: &str = "hvsrNC3NKbcryqDs2DocYHZ9yPKEVzdSjQG6RVtK1s8";
Expand All @@ -14,7 +14,8 @@ pub const MOBILE_SUBDAO: &str = "Gm9xDCJawDEKDrrQW6haw94gABaYzQwCq4ZQU8h8bd22";
pub const TOKEN_DIVIDER: u128 = 100_000_000; // 10^8
pub const DNT_DIVIDER: u128 = 1_000_000; // 10^6

pub const ANOTHER_DIVIDER: u128 = TOKEN_DIVIDER * voter_stake_registry::state::PRECISION_FACTOR;
pub const ANOTHER_DIVIDER: u128 =
TOKEN_DIVIDER * helium_anchor_gen::voter_stake_registry::PRECISION_FACTOR;

#[derive(Debug, Default, Clone, Copy, PartialEq, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "snake_case")]
Expand Down

0 comments on commit 0041b07

Please sign in to comment.