feat(swap-service): add affiliate address tracking alongside referral codes#19
Open
feat(swap-service): add affiliate address tracking alongside referral codes#19
Conversation
… codes - Add affiliateAddress field to Swap model (nullable, indexed) - Update CreateSwapDto to accept affiliateAddress - Calculate and store sellAmountUsd at swap creation time - Add calculateAffiliateFees method querying by affiliateAddress - Add GET /swaps/affiliate-fees/:affiliateAddress endpoint - Existing referralCode system unchanged
…n verification, and full lint cleanup - Swap service: affiliate address tracking, fee asset resolution, configurable BPS - Swap service: origin field (web/api/widget) with commission rate logic - Swap service: live price calculation with distribution cutoff (5th of month) - Verification service: per-protocol sell amount extraction (THORChain, Maya, CowSwap, Relay, Portals, Chainflip, 0x, Bebop, NEAR) - Verification service: min(verified, db) anti-inflation for fee calculation - Security: null origin returns 0% commission (safe default) - Prisma schema: affiliateBps, origin, affiliateFeeAssetId, affiliateFeeAmountCryptoBaseUnit - Lint: 277 errors → 0 across all services (proper TypeScript interfaces for external API responses, removed unnecessary async, typed Prisma queries)
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
Adds B2B affiliate address tracking to swap-service with full on-chain verification for all 18 swappers, 46 chain adapters, and a comprehensive test suite. Runs alongside the existing referralCode system.
Epic: ss-aff — B2B Affiliate Tracking System (bead 4)
Changes
Prisma Schema (
apps/swap-service/prisma/schema.prisma)affiliateAddress String?field to Swap model@@index([affiliateAddress])for query performanceMigration
20260223000000_add_affiliate_address—ALTER TABLE "Swap" ADD COLUMN "affiliateAddress" TEXT+ indexSwap Service (
apps/swap-service/src/swaps/swaps.service.ts)createSwap()now accepts and storesaffiliateAddresssellAmountUsdcalculated and snapshot at creation time usinggetAssetPriceUsd()calculateAffiliateFees()method — queries byaffiliateAddressaffiliateBps,affiliateAddress,integratorFeeRecipient,sellAmountCryptoBaseUnit)verifiedBpsfallback toswap.affiliateBpswhenaffiliateVerificationDetails.affiliateBpsis undefinedSwap Controller (
apps/swap-service/src/swaps/swaps.controller.ts)GET /swaps/affiliate-fees/:affiliateAddressendpointDELETE /swaps/test-cleanupendpoint (marks oldtest-*swaps as FAILED for test automation)Affiliate Verification (
apps/swap-service/src/verification/swap-verification.service.ts)Chain Adapters (
apps/swap-service/src/lib/chain-adapter-init.service.ts)Environment Fixes
public.blastapi.io→rpc.starknet.lava.builddaemon.thorchain.shapeshift.com(dead) →thornode.ninerealms.comdaemon.mayachain.shapeshift.com(dead) →mayanode.mayachain.infoShared Types (
packages/shared-types/src/index.ts)affiliateAddress?: stringtoCreateSwapDtoTest Suite (
tests/)test-all-swappers.mjs— Comprehensive test script for all 18 swappersTESTING.md— LLM-friendly guide for running and updating testsComprehensive Test Results
Test Methodology
The test script creates fake pending swaps using real confirmed on-chain tx hashes, then waits for the 5-second polling cron to resolve them. It validates:
POST /swapscheckTradeStatus(per-swapper implementation)PENDING → SUCCESSResults: ✅ 14/17 SUCCESS, 3 expected PENDING, 0 failures
Affiliate Verification Types
hasAffiliate=falseaffiliateBpsfrom enriched swap metadata. ReportshasAffiliate=true, bps=60✅Fee Calculation
web→ 10bps,api→ 10bps,null→ 0 (open bug)0xc770eefad204b5180df6a14ee197d99d808ee52dAPI Endpoints for Checking Results
Design Decisions
referralCode(user-to-user) andaffiliateAddress(B2B) — both can be set on the same swapsellAmountUsdcaptured at creation time for consistent fee calculationsdb:pushdrops tables not in schema, so each service gets its own DBKnown Issues
origin=nullswaps get 0 commissionorigin=null→commissionRate=0web?parseTx()requires sender address not available in testRunning the Tests
See
tests/TESTING.mdfor the full guide including how to update tx hashes, troubleshooting, and environment setup.Related PRs
swap-trackingbranch (public-api endpoints, widget fix, web app attribution, dashboard)How to test: