Skip to content

Commit

Permalink
examples: allow to load accounts from file (#290)
Browse files Browse the repository at this point in the history
Co-authored-by: Arrowana <[email protected]>
  • Loading branch information
fanatid and Arrowana authored Mar 4, 2024
1 parent 8ee6932 commit 656ce85
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions examples/rust/src/bin/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use {
std::{
collections::HashMap,
env, fmt,
fs::File,
sync::{Arc, Mutex},
time::Duration,
},
Expand Down Expand Up @@ -106,6 +107,10 @@ struct ActionSubscribe {
#[clap(long)]
accounts_account: Vec<String>,

/// Path to a JSON array of account addresses
#[clap(long)]
accounts_account_path: Option<String>,

/// Filter by Owner Pubkey
#[clap(long)]
accounts_owner: Vec<String>,
Expand Down Expand Up @@ -199,14 +204,23 @@ struct ActionSubscribe {
}

impl Action {
fn get_subscribe_request(
async fn get_subscribe_request(
&self,
commitment: Option<CommitmentLevel>,
) -> anyhow::Result<Option<(SubscribeRequest, usize)>> {
Ok(match self {
Self::Subscribe(args) => {
let mut accounts: AccountFilterMap = HashMap::new();
if args.accounts {
let mut accounts_account = args.accounts_account.clone();
if let Some(path) = args.accounts_account_path.clone() {
let accounts = tokio::task::block_in_place(move || {
let file = File::open(path)?;
Ok::<Vec<String>, anyhow::Error>(serde_json::from_reader(file)?)
})?;
accounts_account.extend(accounts);
}

let mut filters = vec![];
for filter in args.accounts_memcmp.iter() {
match filter.split_once(',') {
Expand Down Expand Up @@ -241,7 +255,7 @@ impl Action {
accounts.insert(
"client".to_owned(),
SubscribeRequestFilterAccounts {
account: args.accounts_account.clone(),
account: accounts_account,
owner: args.accounts_owner.clone(),
filters,
},
Expand Down Expand Up @@ -463,6 +477,7 @@ async fn main() -> anyhow::Result<()> {
let (request, resub) = args
.action
.get_subscribe_request(commitment)
.await
.map_err(backoff::Error::Permanent)?
.expect("expect subscribe action");

Expand Down

0 comments on commit 656ce85

Please sign in to comment.