From fe68c3b09bf4c0b4a7fde9ab0cba77dda00e128b Mon Sep 17 00:00:00 2001 From: mjzk Date: Tue, 23 Jul 2024 20:47:22 +0800 Subject: [PATCH] refactor(fetch,types): add Account type and renaming parse mod to types --- src/fetch.rs | 43 ++++++++++++++++++++++++++------------ src/lib.rs | 2 +- src/{parse.rs => types.rs} | 13 ++++++++++++ 3 files changed, 44 insertions(+), 14 deletions(-) rename src/{parse.rs => types.rs} (92%) diff --git a/src/fetch.rs b/src/fetch.rs index ed573da..3a95064 100644 --- a/src/fetch.rs +++ b/src/fetch.rs @@ -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 = @@ -49,6 +51,13 @@ impl SolFetcher { Ok(CurrentEpoch(epoch_info)) } + #[allow(unused)] + pub(crate) fn fetch_account_sync(&self, pubkey: &str) -> eyre::Result { + 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 { const MAX_RETRIES: u32 = 5; @@ -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), @@ -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(()) + } } diff --git a/src/lib.rs b/src/lib.rs index 6b68cb6..f43667c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -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; diff --git a/src/parse.rs b/src/types.rs similarity index 92% rename from src/parse.rs rename to src/types.rs index 67541b1..c7496ea 100644 --- a/src/parse.rs +++ b/src/types.rs @@ -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, + data_len: usize, +} + #[cfg(test)] mod unit_tests { use crate::fetch::{SolFetcher, SOL_RPC_URL};