Add referral fee support (Legacy Swap) + live E2E scripts, docs, and config#1
Add referral fee support (Legacy Swap) + live E2E scripts, docs, and config#1MonteCrypto999 wants to merge 14 commits intoSendo-labs:1.xfrom
Conversation
…ample; refine fee receiver ATA checks; ignore local-e2e
There was a problem hiding this comment.
Pull Request Overview
This PR adds optional referral fee support for Jupiter Legacy Swap, enabling platform fees (0-10%) to be collected on swaps. It introduces environment-based configuration for fee collection modes (sol_only or smart), automatic ATA derivation for fee accounts, and comprehensive testing/documentation.
Key Changes:
- Referral fee infrastructure with
platformFeeBps(quote) andfeeAccount(swap) parameters - Two fee collection modes with automatic mint selection and ATA derivation
- Live integration tests, E2E mainnet scripts, and comprehensive documentation
Reviewed Changes
Copilot reviewed 10 out of 13 changed files in this pull request and generated 4 comments.
Show a summary per file
| File | Description |
|---|---|
tsup.config.ts |
Added E2E script to build entry points |
src/types.ts |
Added referral config types (FeeMode, ReferralConfig, FeeMintSelection) |
src/service.ts |
Core referral logic: config loading, mint selection, ATA derivation, and integration into quote/swap flows |
src/scripts/e2e-mainnet.ts |
E2E mainnet script with simulation and optional signing/sending |
src/index.ts |
Exported JupiterService for direct usage |
src/__tests__/referral.test.ts |
Basic referral config validation tests |
src/__tests__/integration.test.ts |
Live integration tests against Jupiter API |
package.json |
Added dependencies (dotenv, tsx) and E2E scripts |
env.example |
Environment variable documentation for referral config |
README.md |
Comprehensive documentation on features, setup, and usage |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| let resolveHandle: any = false | ||
| let rejectHandle: any = false |
There was a problem hiding this comment.
Initialize resolveHandle and rejectHandle as null instead of false. Using false is misleading since these will be assigned function references, not boolean values.
| let resolveHandle: any = false | |
| let rejectHandle: any = false | |
| let resolveHandle: any = null | |
| let rejectHandle: any = null |
| let resolveHandle: any = false | ||
| let rejectHandle: any = false |
There was a problem hiding this comment.
Initialize resolveHandle and rejectHandle as null instead of false. Using false is misleading since these will be assigned function references, not boolean values.
| ## Features | ||
|
|
||
| - **Token Swaps**: Execute swaps via Jupiter aggregator | ||
| - **Referral Fees**: Collect platform fees on swaps (0-10%) |
There was a problem hiding this comment.
The documentation states fees can be 0-10%, but there is no validation in the code to enforce this upper limit. Consider adding validation in loadReferralConfig to ensure feeBps does not exceed 1000 (10%), or update the documentation to reflect that any value is accepted.
| - **Referral Fees**: Collect platform fees on swaps (0-10%) | |
| - **Referral Fees**: Collect platform fees on swaps (configurable percentage) |
| - The swap will proceed **without fees** | ||
| - A warning will be logged |
There was a problem hiding this comment.
The warning states that swaps will proceed without fees if the fee account doesn't exist, but the actual behavior in the code (lines 364-369 in service.ts) logs warnings and proceeds. However, Jupiter may reject the transaction if the fee account is invalid. Consider clarifying that the transaction may fail during execution rather than just proceeding silently without fees.
| - The swap will proceed **without fees** | |
| - A warning will be logged | |
| - A warning will be logged | |
| - The swap may fail (Jupiter may reject the transaction if the fee account is invalid or missing) |
|
@MonteCrypto999 can you check commit suggestion please ? I'll merge after :) |
PR: Optional Referral Fees on Jupiter Legacy Swap
This PR adds optional referral fees on Jupiter Legacy Swap via
platformFeeBpsandfeeAccount,with clean env-based config, tests, and runnable E2E examples (simulation/signing).
🧩 Features
Referral Fees
Adds
platformFeeBpsto/quotewhenREFERRAL_FEE_BPS > 0.Derives
feeAccountas the ATA for the chosen mint fromREFERRAL_FEE_RECEIVER.Modes:
sol_only: collect fees only if SOL/WSOL is in the pair.smart: prefer SOL if available, else input mint.Respects ExactIn / ExactOut rules and skips fees if constraints aren’t met.
🧠 Code Changes
src/service.tsplatformFeeBpsin quote andfeeAccountin swap.src/__tests__/integration.test.tssrc/scripts/e2e-mainnet.ts.env(can simulate and sign/send).local-e2e/(gitignored)Docs & Config
README.mdandenv.example: updated with clear config and E2E instructions..gitignore: ignoreslocal-e2eand env files.⚙️ How to Use
1. Setup Environment
Copy
.env.example→.envand set:2. Run E2E Example
npm run build && node dist/scripts/e2e-mainnet.js3. Local Simulator (ignored, for ops/dev)
Populate
local-e2e/.env.localas above, then run:🧩 Notes & Edge Cases
Error “Invalid token account / 0x1789 (6025)”
Usually a route/account issue.
Ensure the fee receiver’s ATA exists for the selected mint (e.g., WSOL in
sol_onlymode).If absent, create the ATA or let Jupiter create it idempotently.
Some routes may still fail in simulation—try higher slippage or another route.
No Referral Program needed for Legacy Swap.
(Still required for Trigger API.)
🔗 References
✅ Checklist
REFERRAL_FEE_BPS > 0and validfeeAccountfeeAccountderived fromREFERRAL_FEE_RECEIVERATA for selected mintREADMEand.env.exampleupdated