1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
// SPDX-FileCopyrightText: 2025 Semiotic AI, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//! Batch balance fetching utilities
//!
//! This module provides utilities for efficiently fetching multiple token balances
//! in a single batch operation. When used with Alloy's `CallBatchLayer`, the
//! parallel balance queries are automatically batched into a single Multicall3
//! RPC request.
//!
//! # Performance
//!
//! The batch fetcher uses `futures::join_all` to execute all balance queries
//! in parallel. When the provider is configured with `CallBatchLayer`:
//!
//! ```rust,ignore
//! use alloy_provider::{layers::CallBatchLayer, ProviderBuilder};
//! use std::time::Duration;
//!
//! let provider = ProviderBuilder::new()
//! .layer(CallBatchLayer::new().wait(Duration::from_millis(10)))
//! .connect_http(url);
//! ```
//!
//! All parallel `eth_call` requests are automatically batched into a single
//! Multicall3 contract call, reducing RPC overhead and rate limit consumption.
//!
//! # Example
//!
//! ```rust,ignore
//! use semioscan::retrieval::balance::batch_fetch_balances;
//! use alloy_primitives::Address;
//!
//! // Define balance queries as (token_address, holder_address) pairs
//! let queries = vec![
//! (usdc_address, alice),
//! (usdc_address, bob),
//! (weth_address, alice),
//! ];
//!
//! let results = batch_fetch_balances(&provider, &queries).await;
//!
//! for result in results {
//! match result {
//! Ok((token, holder, balance)) => {
//! println!("{holder} holds {balance} of token {token}");
//! }
//! Err((token, holder, e)) => {
//! eprintln!("Failed to fetch balance for {holder} on {token}: {e}");
//! }
//! }
//! }
//! ```
use LazyToken;
use Network;
use ;
use Provider;
use TransactionTrait;
use join_all;
use ;
/// Query for a token balance: (token_address, holder_address)
pub type BalanceQuery = ;
/// Result of a successful balance fetch: (token_address, holder_address, balance)
pub type BalanceResult = ;
/// Error from a failed balance fetch: (token_address, holder_address, error_message)
pub type BalanceError = ;
/// Batch fetch token balances for multiple (token, holder) pairs.
///
/// This function executes all balance queries in parallel using `futures::join_all`.
/// When combined with Alloy's `CallBatchLayer`, the parallel `eth_call` requests
/// are automatically batched into a single Multicall3 RPC request.
///
/// # Arguments
///
/// * `provider` - The Alloy provider to use for RPC calls
/// * `queries` - Slice of (token_address, holder_address) pairs to query
///
/// # Returns
///
/// A vector of results, one for each query. Each result is either:
/// - `Ok((token, holder, balance))` on success
/// - `Err((token, holder, error_message))` on failure
///
/// # Performance
///
/// For N queries:
/// - Without `CallBatchLayer`: N separate RPC calls
/// - With `CallBatchLayer`: 1 batched RPC call (Multicall3)
///
/// # Example
///
/// ```rust,ignore
/// let queries = vec![
/// (usdc, alice),
/// (usdc, bob),
/// (weth, alice),
/// ];
///
/// let results = batch_fetch_balances(&provider, &queries).await;
/// ```
pub async
/// Batch fetch ETH balances for multiple addresses.
///
/// This function executes all ETH balance queries in parallel.
/// When combined with Alloy's `CallBatchLayer`, the parallel requests
/// are automatically batched.
///
/// # Arguments
///
/// * `provider` - The Alloy provider to use for RPC calls
/// * `addresses` - Slice of addresses to query ETH balances for
///
/// # Returns
///
/// A vector of results, one for each address. Each result is either:
/// - `Ok((address, balance))` on success
/// - `Err((address, error_message))` on failure
pub async