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

Retry getting block date and price #9

Merged
merged 2 commits into from
Jan 31, 2024
Merged
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
24 changes: 22 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ use {
path::PathBuf,
process::exit,
str::FromStr,
thread::sleep,
time::Duration,
},
sys::{
app_version,
Expand Down Expand Up @@ -121,10 +123,29 @@ async fn get_block_date_and_price(
let block_date = rpc_client_utils::get_block_date(rpc_client, slot).await?;
Ok((
block_date,
token.get_historical_price(rpc_client, block_date).await?,
retry_get_historical_price(rpc_client, block_date, token).await?,
))
}

async fn retry_get_historical_price(
rpc_client: &RpcClient,
block_date: NaiveDate,
token: MaybeToken,
) -> Result<Decimal, Box<dyn std::error::Error>> {
const NUM_RETRIES: usize = 20;
for _ in 1..NUM_RETRIES {
let price = token.get_historical_price(rpc_client, block_date).await;
if price.is_ok() {
println!("Got price");
return price;
}
println!("Retry get_historical_price");
//empirically observed cool down period is ~14s
sleep(Duration::from_secs(5));
}
token.get_historical_price(rpc_client, block_date).await
}

fn add_exchange_deposit_address_to_db(
db: &mut Db,
exchange: Exchange,
Expand Down Expand Up @@ -3440,7 +3461,6 @@ async fn process_account_sync(
let slot = inflation_reward.effective_slot;
let (when, price) =
get_block_date_and_price(rpc_client, slot, account.token).await?;

let lot = Lot {
lot_number: db.next_lot_number(),
acquisition: LotAcquistion::new(
Expand Down
Loading