Skip to content

Commit

Permalink
refactor(fetch,types): add Account type and
Browse files Browse the repository at this point in the history
renaming parse mod to types
  • Loading branch information
mjzk committed Jul 23, 2024
1 parent ba44190 commit fe68c3b
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 14 deletions.
43 changes: 30 additions & 13 deletions src/fetch.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use crate::parse::{parse_transaction, Transaction};
use crate::types::{parse_transaction, Transaction};
use datafusion::arrow::{array::RecordBatch, json::ReaderBuilder};
use eyre::Ok;
use log::trace;
use solana_client::{rpc_client::RpcClient, rpc_config::RpcBlockConfig};
use solana_sdk::{commitment_config::CommitmentConfig, epoch_info::EpochInfo};
use solana_sdk::{
account::Account, commitment_config::CommitmentConfig, epoch_info::EpochInfo, pubkey::Pubkey,
};
use solana_transaction_status::{UiConfirmedBlock, UiTransactionEncoding};
use std::{sync::Arc, time::Duration};
use std::{str::FromStr, sync::Arc, time::Duration};
use tokio::time::sleep;

// pub const SOL_RPC_URL_HELIUS: &str =
Expand Down Expand Up @@ -49,6 +51,13 @@ impl SolFetcher {
Ok(CurrentEpoch(epoch_info))
}

#[allow(unused)]
pub(crate) fn fetch_account_sync(&self, pubkey: &str) -> eyre::Result<Account> {
let pubkey = Pubkey::from_str(pubkey)?;
let account = self.rpc_client.get_account(&pubkey)?;
Ok(account)
}

#[inline(always)]
pub(crate) async fn fetch_sol_block(&self, slot: u64) -> eyre::Result<UiConfirmedBlock> {
const MAX_RETRIES: u32 = 5;
Expand All @@ -57,16 +66,7 @@ impl SolFetcher {
let mut retry_delay = INITIAL_RETRY_DELAY;

for attempt in 0..MAX_RETRIES {
let result = self.rpc_client.get_block_with_config(
slot,
RpcBlockConfig {
encoding: Some(UiTransactionEncoding::Binary),
transaction_details: None,
rewards: None,
commitment: None,
max_supported_transaction_version: Some(0),
},
);
let result = self.fetch_sol_block_sync(slot);

match result {
std::result::Result::Ok(block) => return Ok(block),
Expand Down Expand Up @@ -172,4 +172,21 @@ mod unit_tests {
assert_eq!(batch.num_rows(), 15);
Ok(())
}

#[test]
fn test_fetch_account_sync() -> eyre::Result<()> {
let sol_fetcher = SolFetcher::new(SOL_RPC_URL);
//REF: https://docs.solanalabs.com/runtime/programs
let some_program_ids = vec![
"11111111111111111111111111111111",
"Stake11111111111111111111111111111111111111",
"TokenkegQfeZyiNwAJbNbGKPFXCWuBvf9Ss623VQ5DA",
"ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL",
];
for pid in some_program_ids {
let account = sol_fetcher.fetch_account_sync(pid)?;
println!("account: {:#?}", account);
}
Ok(())
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pub mod api;
pub(crate) mod datetime;
pub mod fetch;
pub(crate) mod parse;
pub(crate) mod store;
pub mod stream;
pub(crate) mod types;
13 changes: 13 additions & 0 deletions src/parse.rs → src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,19 @@ pub(crate) fn parse_transaction(
})
}

#[allow(unused)]
//NOTE parts from solana_sdk::account::Account
#[derive(Debug, Serialize, Deserialize)]
struct Account {
pubkey: String,
lamports: u64,
owner: String,
executable: bool,
rent_epoch: u64,
data: Vec<u8>,
data_len: usize,
}

#[cfg(test)]
mod unit_tests {
use crate::fetch::{SolFetcher, SOL_RPC_URL};
Expand Down

0 comments on commit fe68c3b

Please sign in to comment.