Skip to content
5 changes: 5 additions & 0 deletions .changeset/easy-maps-repair.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@orca-so/whirlpools-rust": patch
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also need a changeset for the TS sdk

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

---

Fix fetch_positions_for_owner() to perform batch calls
1 change: 1 addition & 0 deletions rust-sdk/whirlpool/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ mod pool;
mod position;
mod swap;
mod token;
mod utils;

#[cfg(test)]
mod e2e;
Expand Down
20 changes: 10 additions & 10 deletions rust-sdk/whirlpool/src/position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use solana_client::{nonblocking::rpc_client::RpcClient, rpc_request::TokenAccoun
use solana_sdk::account::Account;
use solana_sdk::pubkey::Pubkey;

use crate::utils::batch_get_multiple_accounts;
use crate::{get_token_accounts_for_owner, ParsedTokenAccount};

/// Represents a single Position account.
Expand Down Expand Up @@ -166,16 +167,15 @@ pub async fn fetch_positions_for_owner(
.map(|x| get_position_bundle_address(&x.mint).map(|x| x.0))
.collect::<Result<Vec<Pubkey>, _>>()?;

let position_infos = rpc.get_multiple_accounts(&position_addresses).await?;
let position_infos = batch_get_multiple_accounts(rpc, &position_addresses, None).await?;

let positions: Vec<Option<Position>> = position_infos
.iter()
.map(|x| x.as_ref().and_then(|x| Position::from_bytes(&x.data).ok()))
.collect();

let position_bundle_infos = rpc
.get_multiple_accounts(&position_bundle_addresses)
.await?;
let position_bundle_infos =
batch_get_multiple_accounts(rpc, &position_bundle_addresses, None).await?;

let position_bundles: Vec<Option<PositionBundle>> = position_bundle_infos
.iter()
Expand All @@ -191,12 +191,12 @@ pub async fn fetch_positions_for_owner(
.flat_map(get_position_in_bundle_addresses)
.collect();

let bundled_positions_infos: Vec<Account> = rpc
.get_multiple_accounts(&bundled_positions_addresses)
.await?
.into_iter()
.flatten()
.collect();
let bundled_positions_infos: Vec<Account> =
batch_get_multiple_accounts(rpc, &bundled_positions_addresses, None)
.await?
.into_iter()
.flatten()
.collect();

let mut bundled_positions_map: HashMap<Pubkey, Vec<(Pubkey, Position)>> = HashMap::new();
for i in 0..bundled_positions_addresses.len() {
Expand Down
20 changes: 20 additions & 0 deletions rust-sdk/whirlpool/src/utils.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use solana_client::nonblocking::rpc_client::RpcClient;
use solana_sdk::account::Account;
use solana_sdk::pubkey::Pubkey;

const DEFAULT_CHUNK_SIZE: usize = 100;

pub(crate) async fn batch_get_multiple_accounts(
rpc_client: &RpcClient,
pubkeys: &[Pubkey],
chunk_size: Option<usize>,
) -> Result<Vec<Option<Account>>, Box<dyn std::error::Error>> {
let mut results = vec![];

for chunk in pubkeys.chunks(chunk_size.unwrap_or(DEFAULT_CHUNK_SIZE)) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Clamp the chunk_size to (0, MAX_CHUNK_SIZE)?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An iterator over a slice in (non-overlapping) chunks (chunk_size elements at a time), starting at the beginning of the slice.
When the slice len is not evenly divided by the chunk size, the last slice of the iteration will be the remainder.

Clamp is necessary here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Anything less than 1 and more than 100 is invalid

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

let accounts = rpc_client.get_multiple_accounts(chunk).await?;
results.extend(accounts);
}

Ok(results)
}