JAMMRouter Contract
Overview
JAMMRouter is the user interface layer contract of JAMM DEX, providing user-friendly trading and liquidity management functions. It encapsulates complex underlying logic, supports multi-hop swaps, automatic path calculation, deadline protection, and is deeply integrated with WJU, enabling users to trade directly using JU tokens.
Contract Basic Information
contract JAMMRouter is IJAMMRouter02 {
address public immutable override factory;
address public immutable override WJU;
constructor(address _factory, address _WJU) {
factory = _factory;
WJU = _WJU;
}
}Immutable Variables:
factory: JAMMFactory contract addressWJU: WJU wrapped token contract address
Modifiers
Deadline Protection
All trading functions use this modifier to ensure transactions execute within the specified time.
ETH Reception
Router only accepts ETH from WJU contract, used for ETH transfers during WJU unwrapping.
Liquidity Management
Internal Liquidity Calculation
Calculation Logic:
Check if trading pair exists, create if not
Get current reserves
If first addition, use desired amounts
Otherwise calculate optimal ratio, ensuring minimum amount requirements
Add Liquidity
Add JU Liquidity
JU Processing Flow:
Calculate optimal amounts
Transfer tokens to trading pair
Wrap JU to WJU and transfer to trading pair
Mint LP tokens
Refund excess JU
Remove Liquidity
Remove JU Liquidity
Permit Support
Remove Liquidity with Permit
Permit Advantages:
No need to pre-call approve
Complete authorization and operation in one transaction
Reduce gas consumption and transaction steps
Token Swapping
Internal Swap Logic
Multi-hop Swap Logic:
Validate relationship between path and fee array lengths
Iterate through swap path
Calculate output amount for each hop
Determine recipient address for next hop
Call trading pair's swap function
Exact Input Swap
Exact Output Swap
JU Token Swapping
Buy Tokens with JU
Sell Tokens for JU
Fee-on-Transfer Token Support
Internal Swap Logic (Fee-on-Transfer Support)
Fee-on-Transfer Token Handling:
Don't pre-calculate amounts, base on actual received amounts
Recalculate input amount for each hop
Suitable for tokens that charge fees on transfer
Fee-on-Transfer Swap Functions
Query Functions
Price Query
Output Amount Calculation
Input Amount Calculation
Multi-hop Amount Calculation
Usage Examples
Token Swapping
JU Token Swapping
Add Liquidity
Price Query
Last updated