Skip to content

feat(crowdfund): implement affiliate links and referral tracking (#349)#397

Open
Ebenezer199914 wants to merge 1 commit into
ShadeProtocol:mainfrom
Ebenezer199914:feat/affiliate-referral-tracking-349
Open

feat(crowdfund): implement affiliate links and referral tracking (#349)#397
Ebenezer199914 wants to merge 1 commit into
ShadeProtocol:mainfrom
Ebenezer199914:feat/affiliate-referral-tracking-349

Conversation

@Ebenezer199914

Copy link
Copy Markdown
Contributor

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.rs

Five new error variants:

Variant Code When
InvalidAffiliateCode 28 Empty or >32-byte code
AffiliateNotFound 29 Code not registered
AffiliateAlreadyExists 30 Duplicate registration
InvalidCommissionBps 31 Rate outside 1–10 000 bps
UnauthorizedAffiliate 32 Reserved for future ACL

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 snapshot: affiliate_code, amount, timestamp

New DataKey variants

  • Affiliate(String)AffiliateInfo keyed by code
  • AffiliateListVec<String> of all registered codes
  • ContributorReferral(Address) — first-touch referral per contributor
  • AffiliateCommission(String) — accumulated commission per code

Events

  • AffiliateRegisteredEvent — emitted on register_affiliate
  • AffiliateContributionEvent — emitted on every contribute_with_referral
  • AffiliateCommissionEvent — emitted when commission > 0

New public methods

Method Auth Description
register_affiliate(code, wallet, commission_bps) organizer Register a new affiliate code
contribute_with_referral(contributor, amount, code) contributor Full contribute flow + referral tracking
get_affiliate(code) Fetch affiliate stats
get_affiliate_commission(code) Total accrued commission
get_affiliate_codes() Enumerate all codes
get_contributor_referral(contributor) Query contributor's referral record

Design decisions

  • Commission is calculated on the raw contribution before sponsor matching, preventing inflated payouts.
  • Referral count uses first-touch attribution — repeated contributions from the same address do not re-increment the counter.
  • contribute_with_referral is fully compatible with the existing matching pool, stretch goals, and batch refund flows.

contracts/crowdfund/src/test.rs

25 new tests across 5 categories:

  1. Registration — success, duplicate, empty code, commission edge cases (0 bps, >10 000, exact 10 000)
  2. Contributions with referral — raised amount, referral record persisted, commission math, multi-contributor dedup, accumulation
  3. Matching pool compatibility — commission calculated on raw amount only
  4. Error cases — unknown code, zero amount, after-deadline contribution
  5. Integration — referred contributors included in batch refund; organizer auth guard

Testing

cargo test -p crowdfund

Result: 82 passed, 0 failed (57 pre-existing + 25 new)

Pre-existing compile errors in ticketing and some shade test files are unrelated to this PR and were present before these changes.


Checklist

  • New error codes do not conflict with existing ones (28–32)
  • All new DataKey variants are non-overlapping
  • commission_bps validated on registration (1–10 000)
  • Affiliate code length validated (1–32 bytes)
  • First-touch attribution prevents referral count inflation
  • Commission calculated pre-match to avoid inflated payouts
  • All existing tests continue to pass
  • 25 new tests cover happy paths and all error cases

…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)
@drips-wave

drips-wave Bot commented Jun 30, 2026

Copy link
Copy Markdown

@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! 🚀

Learn more about application limits

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Implement Affiliate Links and Referral Tracking for Campaigns

2 participants