use anyhow::Result;
use pyth_client::{cast, Price};
use solana_shadow::{BlockchainShadow, SyncOptions};
use std::time::Duration;
use tracing_subscriber::EnvFilter;
fn configure_logging() {
tracing_subscriber::fmt::Subscriber::builder()
.with_writer(std::io::stdout)
.with_env_filter(
EnvFilter::try_from_default_env().unwrap_or(EnvFilter::new("info")),
)
.init();
}
#[tokio::main]
async fn main() -> Result<()> {
configure_logging();
let ethusd = "JBu1AL4obBcCMqKBBxhpWCNUt136ijcuMZLFvTP7iWdB".parse()?;
let btcusd = "GVXRSBjFk6e6J3NbVPXohDJetcTjaeeuykUpbQF8UoMU".parse()?;
let solusd = "H6ARHf6YXhGYeQfUzQNGk6rDNnLBQKrenN712K4AQJEG".parse()?;
let mut shadow = BlockchainShadow::new_for_accounts(
&vec![ethusd, btcusd],
SyncOptions::default(),
)
.await?;
println!("this example will print prices of ETH and BTC every 3 seconds");
println!("starting on the 15th second it will also print SOL prices");
println!();
for i in 0.. {
let ethacc = shadow.get_account(ðusd).unwrap();
let ethprice = cast::<Price>(ðacc.data).agg.price;
let btcacc = shadow.get_account(&btcusd).unwrap();
let btcprice = cast::<Price>(&btcacc.data).agg.price;
println!("ETH/USD: {}", ethprice);
println!("BTC/USD: {}", btcprice);
if i == 5 {
shadow.add_account(&solusd).await?;
}
if i > 5 {
let solacc = shadow.get_account(&solusd).unwrap();
let solprice = cast::<Price>(&solacc.data).agg.price;
println!("SOL/USD: {}", solprice);
}
println!();
tokio::time::sleep(Duration::from_secs(3)).await;
}
shadow.worker().await?;
Ok(())
}