JAMM DEX features an innovative referral reward system that automatically distributes fee shares via smart contracts, providing continuous incentives for referrers. This system not only encourages users to promote the platform but also helps reduce the actual trading costs for traders.
Referrer Registration Mechanism
Referrer Mapping
The JAMMFactory contract maintains a referrer mapping:
// from JAMMFactory.solmapping(address=>address)public referrer;
This mapping records the referrer address for each user (identified by tx.origin).
Setting a Referrer
Users can set their referrer by calling the setReferrer function of the JAMMFactory contract:
// from JAMMFactory.solfunctionsetReferrer(address_referrer)externaloverride{require(referrer[tx.origin]==address(0),"JAMMFactory: REFERER_EXISTS"); referrer[tx.origin]= _referrer;emitReferrer(tx.origin, _referrer);}
Key Features:
Each user can set a referrer only once.
Uses tx.origin instead of msg.sender to correctly identify the true user initiating the transaction, even if called through a contract.
Once set, the referrer cannot be changed, ensuring the stability of the referral relationship.
Referrer Event
The Referrer event is emitted when a referral relationship is established:
This event records the establishment of referral relationships, facilitating tracking by front-end applications and analytics tools.
Fee Sharing Mechanism
Dual Fee System
JAMM DEX's referral system implements a unique dual fee mechanism, handled in the _collectFee function of JAMMPair.sol, which determines fee distribution based on the presence of a referrer:
Fee Distribution Explained
Case 1: No Referrer
Protocol Fee: Collects (amountIn * fee) / 50000 from the input token.
Referrer Fee: 0.
Case 2: With Referrer
Protocol Fee: Collects (amountIn * fee) / 100000 from the input token.
Referrer Fee: Collects (amountIn * fee) / 100000 from the input token.
The fee is split evenly between the protocol and the referrer.
Two Ways to Set a Referrer
Method 1: Pre-set
A user has already set a referrer via the setReferrer function. In this case, the _collectFee function will use the registered referrer associated with tx.origin.
Method 2: Set During Transaction
A user can set a referrer via the _referrer parameter during a transaction. If tx.origin has not yet set a referrer, this parameter will be used to set it.
Referrer Earnings Calculation
Earnings Formula
Referrer earnings are calculated using the following formula:
// from JAMMFactory.sol (referrer mapping)
address referrer = IJAMMFactory(factory).referrer(tx.origin);
// from JAMMFactory.sol (setReferrer function)
require(referrer[tx.origin] == address(0), "JAMMFactory: REFERER_EXISTS");
// from JAMMPair.sol
function _safeTransferFee(address token, address to, uint value) private {
_safeTransfer(token, to, value);
if (value > 0) {
emit Fee(tx.origin, to, token, value);
}
}
const factory = new ethers.Contract(factoryAddress, factoryABI, provider);
const currentReferrer = await factory.referrer(userAddress);
if (currentReferrer === ethers.constants.AddressZero) {
console.log("User has not set a referrer.");
} else {
console.log("Current referrer:", currentReferrer);
}
// Method 1: Direct call to Factory contract
const tx = await factory.setReferrer(referrerAddress);
await tx.wait();
// Method 2: Set during a swap (via Router)
const tx = await router.swapExactTokensForTokens(
amountIn,
amountOutMin,
path,
fees,
to,
referrerAddress, // Referrer address
deadline
);
const pairContract = new ethers.Contract(pairAddress, pairABI, provider);
// Listen for fees for a specific referrer
const filter = pairContract.filters.Fee(null, referrerAddress);
pairContract.on(filter, (sender, referrer, token, amount, event) => {
console.log(`Referrer ${referrer} received ${ethers.utils.formatEther(amount)} of ${token}`);
});