Skip to content

Commit

Permalink
feat: handle new rate-limits for querying pools
Browse files Browse the repository at this point in the history
  • Loading branch information
akorchyn committed Dec 23, 2024
1 parent 5927f4b commit b9d8d79
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 25 deletions.
73 changes: 50 additions & 23 deletions src/commands/account/view_account_summary/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,32 +124,59 @@ fn get_account_inquiry(
let runtime = tokio::runtime::Builder::new_multi_thread()
.enable_all()
.build()?;
let concurrency = 10;
// Staring from Feb 2025, the rate limit is 150 requests per 30 seconds for mainnet.
// We will limit the number of requests per batch to 140 to be conservative.
let batch_size = 140;
let batch_cooldown = tokio::time::Duration::from_secs(30);
let concurrency = 10; // Process 10 requests concurrently within each batch

let delegated_stake: color_eyre::Result<
std::collections::BTreeMap<near_primitives::types::AccountId, near_token::NearToken>,
> = match validators {
Ok(validators) => Ok(runtime.block_on(
futures::stream::iter(validators)
.map(|validator_account_id| async {
let balance = get_delegated_staked_balance(
&json_rpc_client,
block_reference,
&validator_account_id,
account_id,
)
.await?;
Ok::<_, color_eyre::eyre::Report>((validator_account_id, balance))
})
.buffer_unordered(concurrency)
.filter(|balance_result| {
futures::future::ready(if let Ok((_, balance)) = balance_result {
!balance.is_zero()
} else {
true
})
})
.try_collect(),
)?),
Ok(validators) => {
let mut all_results = std::collections::BTreeMap::new();
let validators: Vec<_> = validators.into_iter().collect();

for (batch_index, validator_batch) in validators
.chunks(batch_size)
.map(|x| x.to_vec())
.enumerate()
{
if batch_index > 0 {
// Wait 30 seconds before starting next batch
tracing::info!(
"Waiting for 30 seconds before fetching next batch of stake information"
);
runtime.block_on(async { tokio::time::sleep(batch_cooldown).await });
}

let batch_results = runtime.block_on(
futures::stream::iter(validator_batch)
.map(|validator_account_id| async {
let balance = get_delegated_staked_balance(
&json_rpc_client,
block_reference,
&validator_account_id,
account_id,
)
.await?;
Ok::<_, color_eyre::eyre::Report>((validator_account_id, balance))
})
.buffer_unordered(concurrency)
.filter(|balance_result| {
futures::future::ready(if let Ok((_, balance)) = balance_result {
!balance.is_zero()
} else {
true
})
})
.try_collect::<std::collections::BTreeMap<_, _>>(),
)?;

all_results.extend(batch_results);
}
Ok(all_results)
}
Err(err) => Err(err),
};

Expand Down
13 changes: 13 additions & 0 deletions src/config/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,11 +87,24 @@ impl From<NetworkConfigV1> for NetworkConfigV2 {
}
}

pub fn update_config_v2_to_v2_1(config_v2: ConfigV2) -> ConfigVersion {
let mut config_v2_1 = config_v2;
for (name, network_config) in config_v2_1.network_connection.iter_mut() {
if name == "testnet" && network_config.fastnear_url.is_none() {
network_config.fastnear_url = Some("https://test.api.fastnear.com/".parse().unwrap());
}
}
ConfigVersion::V2_1(config_v2_1)
}

#[derive(serde::Serialize, serde::Deserialize)]
#[serde(tag = "version")]
pub enum ConfigVersion {
#[serde(rename = "1")]
V1(ConfigV1),
#[serde(rename = "2")]
V2(ConfigV2),
// Adds fastnear_url to the testnet config if it's not present
#[serde(rename = "2.1")]
V2_1(ConfigV2),
}
7 changes: 5 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ impl Config {
}

pub fn into_latest_version(self) -> migrations::ConfigVersion {
migrations::ConfigVersion::V2(self)
migrations::ConfigVersion::V2_1(self)
}

pub fn get_config_toml() -> color_eyre::eyre::Result<Self> {
Expand Down Expand Up @@ -193,7 +193,10 @@ impl From<migrations::ConfigVersion> for Config {
migrations::ConfigVersion::V2(config_v1.into())
}
migrations::ConfigVersion::V2(config_v2) => {
break config_v2;
migrations::update_config_v2_to_v2_1(config_v2)
}
migrations::ConfigVersion::V2_1(config_v2_1) => {
break config_v2_1;
}
};
}
Expand Down

0 comments on commit b9d8d79

Please sign in to comment.