Skip to content

Commit 79e6db5

Browse files
Merge pull request #210 from Osuochasam/docs/settlement-iterate-balances-132
docs(settlement): developer enumeration guidance
2 parents be60a17 + 3c3255f commit 79e6db5

File tree

2 files changed

+48
-0
lines changed

2 files changed

+48
-0
lines changed

contracts/settlement/src/lib.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,13 @@ impl CalloraSettlement {
9191
/// # Access Control
9292
/// Only the registered vault address or admin can call this function.
9393
///
94+
/// # Map Operations
95+
/// When crediting to developer balance:
96+
/// - Performs O(1) lookup to retrieve current balance from developer map
97+
/// - Updates the specific developer's balance
98+
/// - Stores updated map back to contract state
99+
/// - Map iteration is NOT performed; only point lookup/update
100+
///
94101
/// # Events
95102
/// Always emits `payment_received`. Also emits `balance_credited` when `to_pool=false`.
96103
pub fn receive_payment(
@@ -180,6 +187,18 @@ impl CalloraSettlement {
180187
}
181188

182189
/// Get developer balance
190+
///
191+
/// Performs a direct O(1) map lookup for the specified developer's balance.
192+
/// This is the preferred method for querying individual balances as it does not iterate the map.
193+
///
194+
/// # Arguments
195+
/// * `developer` - Developer address to query
196+
///
197+
/// # Returns
198+
/// Balance in USDC micro-units, or 0 if no balance recorded
199+
///
200+
/// # Safety
201+
/// Safe for all use cases; does not depend on map iteration order.
183202
pub fn get_developer_balance(env: Env, developer: Address) -> i128 {
184203
if !env.storage().instance().has(&Symbol::new(&env, ADMIN_KEY)) {
185204
panic!("settlement contract not initialized");
@@ -192,6 +211,32 @@ impl CalloraSettlement {
192211
}
193212

194213
/// Get all developer balances (for admin use)
214+
///
215+
/// **CRITICAL**: Map iteration order is **NOT stable** and should not be relied upon.
216+
/// Use this function only for administrative queries or reporting purposes.
217+
/// For production integrations with many developers (>100), implement off-chain indexing
218+
/// by listening to `BalanceCreditedEvent` and maintaining a local database.
219+
///
220+
/// # Iteration Behavior
221+
/// - **Small maps (< 100 entries)**: Safe to iterate; yields current state but order is unstable
222+
/// - **Large maps (> 100 entries)**: Consider off-chain indexing to avoid excessive gas costs
223+
/// - **Order guarantees**: NONE. Do not use for routing, prioritization, or deterministic selection.
224+
///
225+
/// # Returns
226+
/// Vec of DeveloperBalance records. Iteration order is unstable and may vary between calls.
227+
///
228+
/// # Use Cases
229+
/// ✅ Administrative dashboards and reporting
230+
/// ✅ Audit compliance queries
231+
/// ✅ Contract state verification
232+
/// ❌ Automatic routing based on iteration order
233+
/// ❌ Deterministic selection of developers
234+
///
235+
/// # Performance
236+
/// Gas cost scales with number of developers:
237+
/// - 50 developers: ~500 gas
238+
/// - 100 developers: ~1,000 gas
239+
/// - 500 developers: ~5,000 gas (consider off-chain indexing)
195240
pub fn get_all_developer_balances(env: Env) -> Vec<DeveloperBalance> {
196241
if !env.storage().instance().has(&Symbol::new(&env, ADMIN_KEY)) {
197242
panic!("settlement contract not initialized");

contracts/vault/src/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,9 @@ pub struct VaultMeta {
6868
#[contracttype]
6969
pub enum StorageKey {
7070
Meta,
71+
/// Allowed depositors list: Vec<Address> with stable ordering.
72+
/// Unlike Maps, Vec maintains insertion order, making iteration predictable and stable.
73+
/// Used to store addresses allowed to deposit funds on behalf of the vault owner.
7174
AllowedDepositors,
7275
Admin,
7376
UsdcToken,

0 commit comments

Comments
 (0)