feat(crowdfund): implement affiliate links and referral tracking (#349)#397
Open
Ebenezer199914 wants to merge 1 commit into
Open
Conversation
…deProtocol#349) Add complete affiliate/referral tracking system to the crowdfund contract, enabling campaign organizers to register affiliate partners, track contributions made through referral links, and accrue commissions automatically. ## Changes ### contracts/crowdfund/src/errors.rs - InvalidAffiliateCode (28): empty or >32-byte affiliate code - AffiliateNotFound (29): code not registered - AffiliateAlreadyExists (30): duplicate registration attempt - InvalidCommissionBps (31): commission rate outside 1–10_000 range - UnauthorizedAffiliate (32): reserved for future ACL extension ### contracts/crowdfund/src/lib.rs Data types: - AffiliateInfo: stores wallet, commission_bps, total_referred_amount, total_commission_earned, referral_count - ReferralRecord: per-contributor first-touch record (code, amount, timestamp) DataKey variants: - Affiliate(String): keyed by code, stores AffiliateInfo - AffiliateList: ordered Vec<String> of all registered codes - ContributorReferral(Address): first-touch referral for each contributor - AffiliateCommission(String): accumulated commission per code Events: - AffiliateRegisteredEvent: emitted on register_affiliate - AffiliateContributionEvent: emitted on contribute_with_referral - AffiliateCommissionEvent: emitted when non-zero commission accrues Public methods: - register_affiliate(code, wallet, commission_bps): organizer-only registration - contribute_with_referral(contributor, amount, code): full contribute flow with referral tracking, first-touch dedup, commission accrual, and matching - get_affiliate(code) -> AffiliateInfo: query affiliate stats - get_affiliate_commission(code) -> i128: total accrued commission - get_affiliate_codes() -> Vec<String>: enumerate all codes - get_contributor_referral(contributor) -> Option<ReferralRecord>: query referral ### contracts/crowdfund/src/test.rs 25 new tests covering: - Affiliate registration (success, duplicate, invalid code, commission bounds) - Contributions with referral (raised amount, referral record, commission math) - Referral count deduplication (first-touch, multiple unique contributors) - Commission accumulation across multiple contributions - Compatibility with sponsor matching pool - Error cases (unknown code, zero amount, after deadline) - Query helpers (empty list, no referral, zero commission) - Integration with batch refund - Organizer auth guard Test results: 82 passed, 0 failed (57 existing + 25 new)
|
@Ebenezer199914 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! 🚀 |
codebestia
approved these changes
Jul 2, 2026
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.
Summary
Closes #349
Implements a complete affiliate links and referral tracking system for the crowdfund contract. Campaign organizers can register named affiliate partners, track contributions made through referral codes, and automatically accrue commission per contribution.
Changes
contracts/crowdfund/src/errors.rsFive new error variants:
InvalidAffiliateCodeAffiliateNotFoundAffiliateAlreadyExistsInvalidCommissionBpsUnauthorizedAffiliatecontracts/crowdfund/src/lib.rsData types
AffiliateInfo— storeswallet,commission_bps,total_referred_amount,total_commission_earned,referral_countReferralRecord— per-contributor first-touch snapshot:affiliate_code,amount,timestampNew
DataKeyvariantsAffiliate(String)—AffiliateInfokeyed by codeAffiliateList—Vec<String>of all registered codesContributorReferral(Address)— first-touch referral per contributorAffiliateCommission(String)— accumulated commission per codeEvents
AffiliateRegisteredEvent— emitted onregister_affiliateAffiliateContributionEvent— emitted on everycontribute_with_referralAffiliateCommissionEvent— emitted when commission > 0New public methods
register_affiliate(code, wallet, commission_bps)contribute_with_referral(contributor, amount, code)get_affiliate(code)get_affiliate_commission(code)get_affiliate_codes()get_contributor_referral(contributor)Design decisions
contribute_with_referralis fully compatible with the existing matching pool, stretch goals, and batch refund flows.contracts/crowdfund/src/test.rs25 new tests across 5 categories:
Testing
Result: 82 passed, 0 failed (57 pre-existing + 25 new)
Checklist
DataKeyvariants are non-overlappingcommission_bpsvalidated on registration (1–10 000)