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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
// SPDX-FileCopyrightText: 2025 Semiotic AI, Inc.
//
// SPDX-License-Identifier: Apache-2.0
//! Token discovery by scanning Transfer events
//!
//! This module provides utilities for discovering which tokens have been transferred
//! to a specific address (typically a router contract) by scanning blockchain Transfer events.
//!
//! # Use Cases
//!
//! - **Token inventory**: Discover which tokens a contract has received
//! - **Balance checking**: Identify tokens that may have non-zero balances
//! - **Historical analysis**: Track token flows over time
//!
//! # Example: Discover tokens transferred to a router
//!
//! ```rust,ignore
//! use semioscan::extract_transferred_to_tokens;
//! use alloy_chains::NamedChain;
//! use alloy_primitives::Address;
//! use alloy_provider::ProviderBuilder;
//!
//! let provider = ProviderBuilder::new().connect_http(rpc_url.parse()?);
//! let router = Address::from_str("0x1234...")?;
//!
//! // Scan blocks 1000-2000 for Transfer events to this router
//! let tokens = extract_transferred_to_tokens(
//! &provider,
//! NamedChain::Arbitrum,
//! router,
//! 1000,
//! 2000,
//! ).await?;
//!
//! println!("Found {} unique tokens", tokens.len());
//! ```
//!
//! # Typical Workflow
//!
//! 1. Scan router for tokens: [`extract_transferred_to_tokens()`]
//! 2. Check balances for each discovered token
//! 3. Liquidate tokens with non-zero balances above threshold
//!
//! # Performance
//!
//! - Automatically chunks large block ranges to avoid RPC limits
//! - Rate-limited by default for chains like Base and Sonic (250ms delay between chunks)
//! - Returns deduplicated token addresses in deterministic order
//! - Handles 100k+ block ranges efficiently with progress logging
//!
//! Configure behavior via [`SemioscanConfig`](crate::SemioscanConfig).
//!
//! # Advanced Usage
//!
//! For custom scanning patterns beyond "transfers to a specific address", use
//! [`EventScanner`](crate::events::scanner::EventScanner) and
//! [`TransferFilterBuilder`](crate::events::filter::TransferFilterBuilder):
//!
//! ```rust,ignore
//! use semioscan::{EventScanner, TransferFilterBuilder, SemioscanConfigBuilder};
//! use alloy_chains::NamedChain;
//! use alloy_primitives::address;
//! use std::time::Duration;
//!
//! // Custom config for premium RPC endpoints
//! let config = SemioscanConfigBuilder::new()
//! .minimal() // No rate limiting for dedicated endpoints
//! .max_block_range(10_000)
//! .build();
//!
//! let scanner = EventScanner::new(&provider, config);
//!
//! // Filter by both sender AND recipient
//! let filter = TransferFilterBuilder::new()
//! .with_sender(sender_address)
//! .with_recipient(recipient_address)
//! .build();
//!
//! let logs = scanner.scan(NamedChain::Arbitrum, filter, 1000, 2000).await?;
//! ```
use NamedChain;
use ;
use Provider;
use SolEvent;
use ;
use crateSemioscanConfig;
use crateEventProcessingError;
use crateTransfer;
use crateTransferFilterBuilder;
use crateEventScanner;
use crateTokenSet;
/// Extract tokens transferred to a router contract using default configuration
///
/// Scans Transfer events over the specified block range to find all unique tokens
/// that have been transferred to the router address. Uses default rate limiting
/// and block range settings optimized for common RPC providers.
///
/// # Arguments
///
/// * `provider` - RPC provider for blockchain queries
/// * `chain` - The blockchain to scan
/// * `router` - Address to find transfers to (typically a router contract)
/// * `start_block` - First block in range (inclusive)
/// * `end_block` - Last block in range (inclusive)
///
/// # Returns
///
/// A [`TokenSet`] of unique token addresses that have been transferred to the router.
/// Using [`TokenSet`] ensures:
/// - Automatic deduplication
/// - Deterministic ordering
/// - Clear semantic meaning (this is a set of tokens, not arbitrary addresses)
///
/// # Example
///
/// ```rust,ignore
/// use semioscan::extract_transferred_to_tokens;
/// use alloy_chains::NamedChain;
/// use alloy_primitives::address;
/// use alloy_provider::ProviderBuilder;
///
/// let provider = ProviderBuilder::new().connect_http(rpc_url.parse()?);
/// let router = address!("0x1234567890abcdef1234567890abcdef12345678");
///
/// let tokens = extract_transferred_to_tokens(
/// &provider,
/// NamedChain::Base,
/// router,
/// 1_000_000,
/// 1_010_000,
/// ).await?;
///
/// for token in tokens.iter() {
/// println!("Token: {}", token);
/// }
/// ```
pub async
/// Extract tokens transferred to a router contract with custom configuration
///
/// Like [`extract_transferred_to_tokens`], but allows customizing RPC behavior
/// through a [`SemioscanConfig`](crate::SemioscanConfig). Use this when you need
/// to control rate limiting or block range sizes.
///
/// # Arguments
///
/// * `provider` - RPC provider for blockchain queries
/// * `chain` - The blockchain to scan
/// * `router` - Address to find transfers to (typically a router contract)
/// * `start_block` - First block in range (inclusive)
/// * `end_block` - Last block in range (inclusive)
/// * `config` - Custom configuration for RPC behavior
///
/// # Returns
///
/// A [`TokenSet`] of unique token addresses that have been transferred to the router.
///
/// # Example
///
/// ```rust,ignore
/// use semioscan::{extract_transferred_to_tokens_with_config, SemioscanConfigBuilder};
/// use alloy_chains::NamedChain;
/// use alloy_primitives::address;
/// use alloy_provider::ProviderBuilder;
/// use std::time::Duration;
///
/// let provider = ProviderBuilder::new().connect_http(rpc_url.parse()?);
/// let router = address!("0x1234567890abcdef1234567890abcdef12345678");
///
/// // Custom config with slower rate limiting
/// let config = SemioscanConfigBuilder::new()
/// .max_block_range(1000)
/// .rate_limit_delay(Duration::from_millis(500))
/// .build();
///
/// let tokens = extract_transferred_to_tokens_with_config(
/// &provider,
/// NamedChain::Polygon,
/// router,
/// 40_000_000,
/// 40_100_000,
/// &config,
/// ).await?;
///
/// println!("Found {} tokens", tokens.len());
/// ```
pub async