SwapRouter
Overview
The SwapRouter is a Venus periphery contract that enables users to swap tokens and interact with Venus markets in a single transaction. It supports both ERC-20 and native tokens (e.g., BNB), allowing users to supply or repay Venus markets seamlessly after a swap. This contract is designed for composability, security, and gas efficiency.
Prerequisites
Before using the SwapRouter, users should:
Approve the SwapRouter to spend their tokens (for ERC-20 operations).
Ensure the target vToken market is listed in the Venus Comptroller.
For native token operations, send the appropriate value with the transaction.
Inheritance
Ownable2StepUpgradeable— Two-step ownership transfer patternReentrancyGuardUpgradeable— Reentrancy protection
Immutable State Variables
NATIVE_TOKEN_ADDR: Address representing the native token (e.g., BNB)COMPTROLLER: Venus Comptroller contract for market validationSWAP_HELPER: SwapHelper contract for executing swapsWRAPPED_NATIVE: Wrapped native token contract (e.g., WBNB)NATIVE_VTOKEN: vToken contract for the native token (e.g., vBNB)
Architecture
Solidity API
swapAndSupply
Swaps an input token for the underlying asset of a Venus market and supplies it on behalf of the user.
Parameters:
vToken: The vToken market to supply totokenIn: The input token to swap fromamountIn: The amount of input tokens to swapminAmountOut: The minimum amount of output tokens expectedswapCallData: Encoded swap instructions for SwapHelper
Scenario Example: User wants to supply 1,000 USDC to a Venus market using DAI:
User holds 1,000 DAI.
Calls
swapAndSupplywith vUSDC, DAI, 1,000 DAI, minAmountOut = 995 USDC, and swapCallData for DAI→USDC swap.SwapRouter swaps 1,000 DAI → ~998 USDC.
Supplies ~998 USDC to vUSDC market.
Final: User has ~998 USDC supplied, 0 DAI, receives vUSDC tokens.
Events:
SwapAndSupply
Errors:
ZeroAmount: Thrown ifamountInis zero.ZeroAddress: Thrown ifvTokenortokenInis zero address.MarketNotListed: Thrown ifvTokenis not listed in Comptroller.InsufficientBalance: Thrown if user has insufficient balance for swap.SwapFailed: Thrown if swap operation fails.InsufficientAmountOut: Thrown if received amount is less thanminAmountOut.NoTokensReceived: Thrown if no tokens are received from swap.SupplyFailed: Thrown if supply to Venus market fails (returns error code).
swapNativeAndSupply
Swaps native tokens (e.g., BNB) for the underlying asset and supplies it to a Venus market.
Parameters:
vToken: The vToken market to supply tominAmountOut: The minimum amount of output tokens expectedswapCallData: Encoded swap instructions for SwapHelper
Scenario Example:
User wants to supply 2 BNB to a Venus market using native BNB:
User holds 2 BNB.
Calls
swapNativeAndSupplywith vUSDT, minAmountOut = 600 USDT, and swapCallData for BNB→USDT swap, sending 2 BNB.SwapRouter wraps 2 BNB to WBNB, swaps WBNB → ~610 USDT.
Supplies ~610 USDT to vUSDT market.
Final: User has ~610 USDT supplied, 0 BNB, receives vUSDT tokens.
Events:
SwapAndSupply
Errors:
ZeroAmount: Thrown ifmsg.valueis zero.ZeroAddress: Thrown ifvTokenis zero address.MarketNotListed: Thrown ifvTokenis not listed in Comptroller.SwapFailed: Thrown if swap operation fails.InsufficientAmountOut: Thrown if received amount is less thanminAmountOut.NoTokensReceived: Thrown if no tokens are received from swap.SupplyFailed: Thrown if supply to Venus market fails.
swapAndRepay
Swaps an input token and repays the user's debt in a Venus market.
Parameters:
vToken: The vToken market to repay debt totokenIn: The input token to swap fromamountIn: The amount of input tokens to swapminAmountOut: The minimum amount of output tokens expectedswapCallData: Encoded swap instructions for SwapHelper
Scenario Example: User has 500 USDT debt in a Venus market and wants to repay using USDC:
User holds 510 USDC.
Calls
swapAndRepaywith vUSDT, USDC, 510 USDC, minAmountOut = 500 USDT, and swapCallData for USDC→USDT swap.SwapRouter swaps 510 USDC → ~505 USDT.
Repays 500 USDT debt in vUSDT market.
Final: User has 0 USDT debt, 5 USDT returned as excess, 0 USDC.
Events:
SwapAndRepay
Errors:
ZeroAmount: Thrown ifamountInor user debt is zero.ZeroAddress: Thrown ifvTokenortokenInis zero address.MarketNotListed: Thrown ifvTokenis not listed in Comptroller.InsufficientBalance: Thrown if user has insufficient balance for swap.SwapFailed: Thrown if swap operation fails.InsufficientAmountOut: Thrown if received amount is less thanminAmountOut.NoTokensReceived: Thrown if no tokens are received from swap.RepayFailed: Thrown if repay to Venus market fails (returns error code).
swapNativeAndRepay
Swaps native tokens and repays the user's debt in a Venus market.
Parameters:
vToken: The vToken market to repay debt tominAmountOut: The minimum amount of output tokens expectedswapCallData: Encoded swap instructions for SwapHelper
Scenario Example: User has 0.5 BNB debt in a Venus market and wants to repay using native BNB:
User holds 0.5 BNB.
Calls
swapNativeAndRepaywith vDAI, minAmountOut = 1,000 DAI, and swapCallData for BNB→DAI swap, sending 0.5 BNB.SwapRouter wraps 0.5 BNB to WBNB, swaps WBNB. → ~1,010 DAI.
Repays 1,000 DAI debt in vDAI market.
Final: User has 0 DAI debt, 10 DAI returned as excess, 0 ETH.
Events:
SwapAndRepay
Errors:
ZeroAmount: Thrown ifmsg.valueor user debt is zero.ZeroAddress: Thrown ifvTokenis zero address.MarketNotListed: Thrown ifvTokenis not listed in Comptroller.SwapFailed: Thrown if swap operation fails.InsufficientAmountOut: Thrown if received amount is less thanminAmountOut.NoTokensReceived: Thrown if no tokens are received from swap.RepayFailed: Thrown if repay to Venus market fails.
swapAndRepayFull
Swaps tokens to repay the user's full debt in a Venus market.
Parameters:
vToken: The vToken market to repay full debt totokenIn: The input token to swap frommaxAmountIn: The maximum amount of input tokens to useswapCallData: Encoded swap instructions for SwapHelper
Scenario Example: User has 1,200 USDC debt in a Venus market and wants to repay full debt using DAI:
User holds 1,250 DAI.
Calls
swapAndRepayFullwith vUSDC, DAI, 1,250 DAI, and swapCallData for DAI→USDC swap.SwapRouter swaps 1,250 DAI → ~1,210 USDC.
Repays 1,200 USDC debt in vUSDC market.
Final: User has 0 USDC debt, 10 USDC returned as excess, 0 DAI.
Events:
SwapAndRepay
Errors:
ZeroAmount: Thrown ifmaxAmountInor user debt is zero.ZeroAddress: Thrown ifvTokenortokenInis zero address.MarketNotListed: Thrown ifvTokenis not listed in Comptroller.InsufficientBalance: Thrown if user has insufficient balance for swap.SwapFailed: Thrown if swap operation fails.InsufficientAmountOut: Thrown if received amount is less than debt amount.NoTokensReceived: Thrown if no tokens are received from swap.RepayFailed: Thrown if repay to Venus market fails.
swapNativeAndRepayFull
Swaps native tokens to repay the user's full debt in a Venus market.
Parameters:
vToken: The vToken market to repay full debt toswapCallData: Encoded swap instructions for SwapHelper
Scenario Example: User has 0.8 BNB debt in a Venus market and wants to repay full debt using native BNB:
User holds 0.85 BNB.
Calls
swapNativeAndRepayFullwith vUSDT and swapCallData for BNB→USDT swap, sending 0.85 BNB.SwapRouter wraps 0.85 BNB to WBNB, swaps WBNB → ~260 USDT.
Repays 250 USDT debt in vUSDT market.
Final: User has 0 USDT debt, 10 USDT returned as excess, 0 BNB.
Events:
SwapAndRepay
Errors:
ZeroAmount: Thrown ifmsg.valueor user debt is zero.ZeroAddress: Thrown ifvTokenis zero address.MarketNotListed: Thrown ifvTokenis not listed in Comptroller.SwapFailed: Thrown if swap operation fails.InsufficientAmountOut: Thrown if received amount is less than debt amount.NoTokensReceived: Thrown if no tokens are received from swap.RepayFailed: Thrown if repay to Venus market fails.
sweepToken
Allows the contract owner to recover leftover ERC-20 tokens from the contract.
Example:
sweepNative
Allows the contract owner to recover leftover native tokens from the contract.
Example:
Error Definitions
ZeroAddress(): Thrown when a zero address is provided for required parameters.ZeroAmount(): Thrown when a zero amount is provided for required parameters or user debt is zero.SupplyFailed(uint256 errorCode): Thrown when supply operation to Venus market fails. Returns the error code from the vToken.RepayFailed(uint256 errorCode): Thrown when repay operation to Venus market fails. Returns the error code from the vToken.SwapFailed(): Thrown when the swap operation fails (e.g., SwapHelper call reverts).NoTokensReceived(): Thrown when no tokens are received from the swap operation.NativeTransferFailed(): Thrown when native token transfer fails (e.g., sweepNative fails to send ETH/BNB).InsufficientBalance(): Thrown when the user has insufficient balance for the swap or supply/repay operation.MarketNotListed(address vToken): Thrown when the vToken market is not listed in the Comptroller.InsufficientAmountOut(uint256 amountOut, uint256 minAmountOut): Thrown when the swap output is less than the minimum required amount (slippage protection).UnauthorizedNativeSender(address sender): Thrown when an unauthorized sender tries to send native tokens to the contract.
Security Considerations
Access Control
Only the contract owner can sweep tokens or native currency.
All swap/supply/repay functions are open to users, but require proper approvals and market listing.
Reentrancy Protection
All external functions are protected by the
nonReentrantmodifier.
Slippage Protection
All swap functions require a
minAmountOutparameter. The transaction reverts if the swap output is below this threshold.
Market Validation
The contract checks that the vToken market is listed in the Comptroller before proceeding.
Account Safety
The contract validates user balances and market status before executing operations.
Dust Handling
Any excess tokens after repay operations are returned to the user.
Integration
For Users
Approve the SwapRouter to spend your tokens (for ERC-20 operations):
Call the desired function (
swapAndSupply,swapAndRepay, etc.) with the appropriate parameters and swapCallData.For native token operations, send the required value with your transaction.
Monitor events (
SwapAndSupply,SwapAndRepay) for transaction status.
For Developers
Swap API Integration
Cross-asset operations require signed swap data from the Venus Swap API:
Request a swap quote from the API with source and destination tokens
Receive encoded
multicallparameters with backend signaturePass the signed
_swapDatato the entry function
Audits
SwapRouter undergoes security audits before mainnet deployment. Audit reports are available in the venus-periphery repository.
Deployment
See Deployed Contracts for current addresses.
Last updated

