Pull Request: Add calculate_total_earned_by_creator Helper (#59, #12) - #76
Merged
elizabetheonoja-art merged 2 commits intoMar 25, 2026
Conversation
|
@JerryIdoko Great news! 🎉 Based on an automated assessment of this PR, the linked Wave issue(s) no longer count against your application limits. You can now already apply to more issues while waiting for a review of this PR. Keep up the great work! 🚀 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
📝 Description
This PR implements a much-needed read-only helper function to aggregate real-time earnings for creators. Previously, the frontend had to perform multiple RPC calls to iterate through individual fan streams, which was non-scalable and prone to "stale data" lag.
The new calculate_total_earned_by_creator function performs this aggregation on-chain, utilizing the current ledger timestamp to provide an up-to-the-second "Unclaimed Balance" for any given creator address.
🎯 Key Changes
Aggregation Logic: Iterates through the CreatorSubscriptionMap to sum all "streamed but not yet withdrawn" funds.
Timestamp-Aware Calculation: Uses env.ledger().timestamp() to calculate flow rates dynamically, ensuring the value displayed to the creator is exactly what they would receive if they clicked "Withdraw" at that moment.
Gas-Efficient Read: Optimized as a getter to minimize resource consumption during RPC calls.
Precision: Maintained 7-decimal precision (stroops) to ensure no fractional XLM is lost during the calculation.
💻 Logic Highlight: Real-Time Accrual
The core math follows a linear flow model:
Rust
pub fn calculate_total_earned_by_creator(e: Env, creator: Address) -> i128 {
let current_time = e.ledger().timestamp();
let mut total_unclaimed: i128 = 0;
}
✅ Acceptance Criteria Checklist
[x] Real-Time Accuracy: Verified that the value updates correctly as the ledger timestamp progresses in tests.
[x] Unclaimed Only: The function correctly ignores funds that have already been moved to the creator's wallet.
[x] Frontend Ready: Tested via Soroban-CLI to ensure the return format is easily consumable by the dashboard.
[x] Scalability: Verified that the loop performs efficiently even when a creator has a high volume of active subscribers.
🚀 How to Verify
Run the Test Suite:
Bash
cargo test test_unclaimed_balance_aggregation
Simulate via CLI:
Bash
soroban contract invoke --id [CONTRACT_ID] --source [CREATOR_KEY] --fn calculate_total_earned_by_creator -- --creator [CREATOR_ADDRESS]
🔗 Linked Issues
Closes #59
Closes #12