Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

account fetcher mango feeds refactoring take iii #17

Draft
wants to merge 10 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ solana-logger = "~1.16.7"
yellowstone-grpc-client = "1.9.0"
yellowstone-grpc-proto = "1.9.0"

fixed = { git = "https:/blockworks-foundation/fixed.git", branch = "v1.11.0-borsh0_10-mango" }

jsonrpc-core = "18.0.0"
jsonrpc-core-client = { version = "18.0.0", features = ["ws", "http"] }

Expand Down Expand Up @@ -48,6 +50,8 @@ tokio = { version = "1", features = ["full"] }
tokio-tungstenite = "0.17"
rustls = "0.20.8"

async-once-cell = { version = "0.4.2", features = ["unpin"] }

warp = "0.3"


Expand Down
3 changes: 3 additions & 0 deletions connector/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ solana-account-decoder = { workspace = true }
solana-sdk = { workspace = true }
solana-logger = { workspace = true }

async-once-cell = { workspace = true }
fixed = { workspace = true }

tokio = { workspace = true }
rustls = { workspace = true }

Expand Down
90 changes: 90 additions & 0 deletions connector/examples/demo_account_fetcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
use mango_feeds_connector::account_fetcher::AccountFetcherFeeds;
use mango_feeds_connector::account_fetchers::RpcAccountFetcher;
use mango_feeds_connector::feeds_chain_data_fetcher::FeedsAccountFetcher;
use mango_feeds_connector::{account_fetcher, chain_data};
use solana_client::nonblocking::rpc_client::{RpcClient as RpcClientAsync, RpcClient};
use solana_client::rpc_config::{RpcAccountInfoConfig, RpcProgramAccountsConfig};
use solana_sdk::account::{AccountSharedData, ReadableAccount};
use solana_sdk::clock::Slot;
use solana_sdk::pubkey::Pubkey;
use solana_sdk::stake;
use std::str::FromStr;
use std::sync::atomic::{AtomicBool, AtomicU32, AtomicU64, Ordering};
use std::sync::{Arc, RwLock};

#[tokio::main]
async fn main() {
// let rpc_url: String = "https://api.mainnet-beta.solana.com/".to_string();
let rpc_url: String = "https://api.devnet.solana.com/".to_string();

program_accounts_rpc(&rpc_url).await;

program_account_fetcher(rpc_url.clone()).await;

account_fetcher(rpc_url.clone()).await;
}

/// get program accounts for Swap program and print the discriminators
/// 4636 accounts on devnet
async fn program_accounts_rpc(rpc_url: &String) {
let rpc_client = RpcClientAsync::new(rpc_url.clone());

let program_accounts_config = RpcProgramAccountsConfig {
account_config: RpcAccountInfoConfig {
encoding: Some(solana_account_decoder::UiAccountEncoding::Base64),
..RpcAccountInfoConfig::default()
},
..RpcProgramAccountsConfig::default()
};

let program = Pubkey::from_str("SwaPpA9LAaLfeLi3a68M4DjnLqgtticKg6CnyNwgAC8").unwrap();

let accounts = rpc_client
.get_program_accounts_with_config(&program, program_accounts_config)
.await
.unwrap();
println!("num_of_accounts(unfiltered): {}", accounts.len());
for (_pk, acc) in accounts {
let discriminator = &acc.data()[..8];
let mut di = [0; 8];
di.clone_from_slice(discriminator);
println!("discriminator {:02X?}", di);
}
}

async fn account_fetcher(rpc_url: String) {
let client = RpcClientAsync::new(rpc_url);

let account_fetcher = Arc::new(RpcAccountFetcher { rpc: client });

let account_key = Pubkey::from_str("2KgowxogBrGqRcgXQEmqFvC3PGtCu66qERNJevYW8Ajh").unwrap();
let (acc, slot) = account_fetcher
.feeds_fetch_raw_account(&account_key)
.await
.expect("account must exist");
println!("price: {:?} by slot {}", acc.lamports(), slot);
}

/// 318 accounts on devnet by discriminator
async fn program_account_fetcher(rpc_url: String) {
let client = RpcClientAsync::new(rpc_url);

let account_fetcher = Arc::new(RpcAccountFetcher { rpc: client });

let program_key = Pubkey::from_str("SwaPpA9LAaLfeLi3a68M4DjnLqgtticKg6CnyNwgAC8").unwrap();

// discriminator [01, 01, FC, 06, DD, F6, E1, D7]
let discriminator = [0x01, 0x01, 0xFC, 0x06, 0xDD, 0xF6, 0xE1, 0xD7];

let (acc, slot) = account_fetcher
.feeds_fetch_program_accounts(&program_key, discriminator)
.await
.expect("program account must exist");

println!(
"program has {} accounts for discriminator {:02X?}",
acc.len(),
discriminator
);
println!("slot was {}", slot);
}
19 changes: 19 additions & 0 deletions connector/src/account_fetcher.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
use anyhow::{anyhow, Context};
use solana_sdk::account::AccountSharedData;
use solana_sdk::clock::Slot;
use solana_sdk::pubkey::Pubkey;
use std::any;

#[async_trait::async_trait]
pub trait AccountFetcherFeeds: Sync + Send {
async fn feeds_fetch_raw_account(
&self,
address: &Pubkey,
) -> anyhow::Result<(AccountSharedData, Slot)>;

async fn feeds_fetch_program_accounts(
&self,
program: &Pubkey,
discriminator: [u8; 8],
) -> anyhow::Result<(Vec<(Pubkey, AccountSharedData)>, Slot)>;
}
Loading
Loading