Skip to content

feat(crowdfund): add affiliate links and referral tracking (#349)#387

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

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

Conversation

@Ebenezer199914

Copy link
Copy Markdown

Implement affiliate/referral tracking for crowdfund campaigns so that campaign organizers can register affiliates, distribute unique referral codes, and reward affiliates with an on-chain commission whenever a contributor uses their code.

Changes

contracts/crowdfund/src/errors.rs

  • AffiliateNotRegistered (28) – caller not registered as an affiliate
  • ReferralCodeAlreadyTaken (29) – code owned by a different affiliate
  • InvalidCommissionBps (30) – bps value exceeds 10 000
  • ReferralCodeNotFound (31) – no affiliate for the supplied code
  • ReferralAlreadyUsed (32) – contributor already used a referral code

contracts/crowdfund/src/lib.rs

New DataKey variants:
AffiliateCommissionBps, ReferralCodeOwner(BytesN<32>), AffiliateCode(Address), ReferralCount(BytesN<32>), ReferralEarnings(BytesN<32>), PledgeReferral(Address)

New events:
AffiliateRegisteredEvent, ReferralUsedEvent

New public functions:
set_affiliate_commission – organizer sets campaign-wide commission bps
register_affiliate – organizer maps affiliate ↔ code_hash
contribute_with_referral – contribute + validate code + pay commission
get_referral_count – unique contributors who used a code
get_referral_earnings – cumulative commission earned by code
get_referral_code – affiliate address → code hash lookup
get_contributor_referral – contributor → used code lookup
get_affiliate_commission_bps – current commission rate

Bug fix:
Added missing #[contractevent] macro to PledgeReceivedEvent (CICB bug)

contracts/crowdfund/src/test.rs

24 new tests covering:

  • set_affiliate_commission happy-path and bounds validation
  • register_affiliate idempotency, duplicate-code conflict
  • contribute_with_referral: raised amount, commission transfer, referral count, earnings accumulation, contributor recording
  • Error paths: invalid code, double-referral, past deadline, zero amt
  • Zero-commission edge case
  • Integration with matching pool
  • Campaign execution after referral contributions

All 83 tests pass.

Description

Type of Change

  • Bug fix (non-breaking change which fixes an issue)
  • New feature (non-breaking change which adds functionality)
  • Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • Documentation update
  • Code refactoring
  • Test improvements
  • Other (please describe):

Related Issues

Fixes #

Changes Made

Testing

  • I have added tests that prove my fix is effective or that my feature works
  • All existing tests pass locally
  • I have tested this manually

Checklist

  • My code follows the project's style guidelines
  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have made corresponding changes to the documentation
  • My changes generate no new warnings
  • I have added tests that prove my fix is effective or that my feature works
  • New and existing unit tests pass locally with my changes
  • Any dependent changes have been merged and published

Screenshots (if applicable)

Additional Notes

…ocol#349)

Implement affiliate/referral tracking for crowdfund campaigns so that
campaign organizers can register affiliates, distribute unique referral
codes, and reward affiliates with an on-chain commission whenever a
contributor uses their code.

## Changes

### contracts/crowdfund/src/errors.rs
- AffiliateNotRegistered (28) – caller not registered as an affiliate
- ReferralCodeAlreadyTaken (29) – code owned by a different affiliate
- InvalidCommissionBps (30) – bps value exceeds 10 000
- ReferralCodeNotFound (31) – no affiliate for the supplied code
- ReferralAlreadyUsed (32) – contributor already used a referral code

### contracts/crowdfund/src/lib.rs
New DataKey variants:
  AffiliateCommissionBps, ReferralCodeOwner(BytesN<32>),
  AffiliateCode(Address), ReferralCount(BytesN<32>),
  ReferralEarnings(BytesN<32>), PledgeReferral(Address)

New events:
  AffiliateRegisteredEvent, ReferralUsedEvent

New public functions:
  set_affiliate_commission – organizer sets campaign-wide commission bps
  register_affiliate       – organizer maps affiliate ↔ code_hash
  contribute_with_referral – contribute + validate code + pay commission
  get_referral_count       – unique contributors who used a code
  get_referral_earnings    – cumulative commission earned by code
  get_referral_code        – affiliate address → code hash lookup
  get_contributor_referral – contributor → used code lookup
  get_affiliate_commission_bps – current commission rate

Bug fix:
  Added missing #[contractevent] macro to PledgeReceivedEvent (CICB bug)

### contracts/crowdfund/src/test.rs
24 new tests covering:
  - set_affiliate_commission happy-path and bounds validation
  - register_affiliate idempotency, duplicate-code conflict
  - contribute_with_referral: raised amount, commission transfer,
    referral count, earnings accumulation, contributor recording
  - Error paths: invalid code, double-referral, past deadline, zero amt
  - Zero-commission edge case
  - Integration with matching pool
  - Campaign execution after referral contributions

All 83 tests pass.
@drips-wave

drips-wave Bot commented Jun 30, 2026

Copy link
Copy Markdown

Hey @Ebenezer199914! 👋 It looks like this PR isn't linked to any issue.

If this PR is for one of the issues assigned to you as part of a Wave, please link it to ensure your contribution is tracked properly. You can do this by adding a keyword to the PR description (e.g., Closes #123), or by clicking a button below:

Issue Title
#349 Implement Affiliate Links and Referral Tracking for Campaigns Link to this issue

ℹ️ Learn more about linking PRs to issues

@Ebenezer199914 Ebenezer199914 marked this pull request as draft June 30, 2026 00:48
@Ebenezer199914 Ebenezer199914 marked this pull request as ready for review June 30, 2026 00:57
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.

1 participant