Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

ZapRouter

Git Source

Inherits: FlashLoanBase, Ownable2Step, Pausable, ReentrancyGuard

Title: ZapRouter

Efficient router for acquiring plDXY-BULL tokens using flash mints.

Flash mints plDXY-BEAR → swaps to USDC via Curve → mints pairs → keeps plDXY-BULL. For plDXY-BEAR, users should swap directly on Curve instead.

State Variables

MAX_SLIPPAGE_BPS

Maximum allowed slippage in basis points (1% = 100 bps).

uint256 public constant MAX_SLIPPAGE_BPS = 100

SAFETY_BUFFER_BPS

Safety buffer for flash loan repayment calculations (0.5% = 50 bps).

uint256 public constant SAFETY_BUFFER_BPS = 50

USDC_INDEX

USDC index in the Curve USDC/plDXY-BEAR pool.

uint256 public constant USDC_INDEX = 0

PLDXY_BEAR_INDEX

plDXY-BEAR index in the Curve USDC/plDXY-BEAR pool.

uint256 public constant PLDXY_BEAR_INDEX = 1

SPLITTER

SyntheticSplitter contract for minting/burning pairs.

ISyntheticSplitter public immutable SPLITTER

PLDXY_BEAR

plDXY-BEAR token (flash minted for swaps).

IERC20 public immutable PLDXY_BEAR

PLDXY_BULL

plDXY-BULL token (output of zap operations).

IERC20 public immutable PLDXY_BULL

USDC

USDC stablecoin.

IERC20 public immutable USDC

CURVE_POOL

Curve pool for USDC/plDXY-BEAR swaps.

ICurvePool public immutable CURVE_POOL

CAP

Protocol CAP price (8 decimals, oracle format).

uint256 public immutable CAP

CAP_PRICE

CAP price scaled for Curve comparison (6 decimals).

uint256 public immutable CAP_PRICE

ACTION_MINT

Flash loan action: mint plDXY-BULL.

uint256 private constant ACTION_MINT = 0

ACTION_BURN

Flash loan action: burn plDXY-BULL.

uint256 private constant ACTION_BURN = 1

Functions

constructor

Deploys ZapRouter with required protocol dependencies.

constructor(
    address _splitter,
    address _plDxyBear,
    address _plDxyBull,
    address _usdc,
    address _curvePool
) Ownable(msg.sender);

Parameters

NameTypeDescription
_splitteraddressSyntheticSplitter contract address.
_plDxyBearaddressplDXY-BEAR token address.
_plDxyBulladdressplDXY-BULL token address.
_usdcaddressUSDC token address.
_curvePooladdressCurve USDC/plDXY-BEAR pool address.

zapMint

Buy plDXY-BULL using USDC with flash mint efficiency.

For plDXY-BEAR, swap directly on Curve instead.

function zapMint(
    uint256 usdcAmount,
    uint256 minAmountOut,
    uint256 maxSlippageBps,
    uint256 deadline
) external nonReentrant whenNotPaused;

Parameters

NameTypeDescription
usdcAmountuint256The amount of USDC the user is sending.
minAmountOutuint256Minimum amount of plDXY-BULL tokens to receive.
maxSlippageBpsuint256Maximum slippage tolerance in basis points (e.g., 100 = 1%). Capped at MAX_SLIPPAGE_BPS (1%) to limit MEV extraction.
deadlineuint256Unix timestamp after which the transaction reverts.

zapBurn

Sell plDXY-BULL tokens for USDC using flash mint efficiency.

function zapBurn(
    uint256 bullAmount,
    uint256 minUsdcOut,
    uint256 deadline
) external nonReentrant whenNotPaused;

Parameters

NameTypeDescription
bullAmountuint256Amount of plDXY-BULL to sell.
minUsdcOutuint256Minimum USDC to receive (slippage protection).
deadlineuint256Unix timestamp after which the transaction reverts.

zapBurnWithPermit

Sell plDXY-BULL tokens for USDC with a permit signature (gasless approval).

function zapBurnWithPermit(
    uint256 bullAmount,
    uint256 minUsdcOut,
    uint256 deadline,
    uint8 v,
    bytes32 r,
    bytes32 s
) external nonReentrant whenNotPaused;

Parameters

NameTypeDescription
bullAmountuint256Amount of plDXY-BULL to sell.
minUsdcOutuint256Minimum USDC to receive (slippage protection).
deadlineuint256Unix timestamp after which the transaction reverts.
vuint8Signature recovery byte.
rbytes32Signature r component.
sbytes32Signature s component.

_zapBurnCore

function _zapBurnCore(
    uint256 bullAmount,
    uint256 minUsdcOut,
    uint256 deadline
) internal;

onFlashLoan

ERC-3156 flash loan callback. Routes to mint or burn handler.

function onFlashLoan(
    address initiator,
    address,
    uint256 amount,
    uint256 fee,
    bytes calldata data
) external override returns (bytes32);

Parameters

NameTypeDescription
initiatoraddressAddress that initiated the flash loan (must be this contract).
<none>address
amountuint256Amount of plDXY-BEAR borrowed.
feeuint256Flash loan fee (always 0 for SyntheticToken).
databytesEncoded operation parameters.

Returns

NameTypeDescription
<none>bytes32CALLBACK_SUCCESS on successful execution.

onMorphoFlashLoan

Morpho flash loan callback - not used by ZapRouter.

Always reverts as ZapRouter only uses ERC-3156 flash mints.

function onMorphoFlashLoan(
    uint256,
    bytes calldata
) external pure override;

_handleMint

Executes mint operation within flash loan callback.

function _handleMint(
    uint256 loanAmount,
    uint256 fee,
    bytes calldata data
) internal;

Parameters

NameTypeDescription
loanAmountuint256Amount of plDXY-BEAR borrowed.
feeuint256Flash loan fee (always 0).
databytesEncoded mint parameters (action, user, usdcAmount, minSwapOut, minAmountOut, maxSlippageBps).

_handleBurn

Executes burn operation within flash loan callback.

function _handleBurn(
    uint256 loanAmount,
    uint256 fee,
    bytes calldata data
) internal;

Parameters

NameTypeDescription
loanAmountuint256Amount of plDXY-BEAR borrowed.
feeuint256Flash loan fee (always 0).
databytesEncoded burn parameters (action, user, bullAmount, minUsdcOut).

previewZapMint

Preview the result of a zapMint operation.

function previewZapMint(
    uint256 usdcAmount
)
    external
    view
    returns (
        uint256 flashAmount,
        uint256 expectedSwapOut,
        uint256 totalUSDC,
        uint256 expectedTokensOut,
        uint256 flashFee
    );

Parameters

NameTypeDescription
usdcAmountuint256The amount of USDC the user will send.

Returns

NameTypeDescription
flashAmountuint256Amount of plDXY-BEAR to flash mint.
expectedSwapOutuint256Expected USDC from selling flash-minted plDXY-BEAR.
totalUSDCuint256Total USDC for minting pairs (user + swap).
expectedTokensOutuint256Expected plDXY-BULL tokens to receive.
flashFeeuint256Flash mint fee (if any).

previewZapBurn

Preview the result of a zapBurn operation.

function previewZapBurn(
    uint256 bullAmount
)
    external
    view
    returns (uint256 expectedUsdcFromBurn, uint256 usdcForBearBuyback, uint256 expectedUsdcOut, uint256 flashFee);

Parameters

NameTypeDescription
bullAmountuint256The amount of plDXY-BULL tokens to sell.

Returns

NameTypeDescription
expectedUsdcFromBurnuint256USDC received from burning pairs via Splitter.
usdcForBearBuybackuint256USDC needed to buy back plDXY-BEAR for flash loan repayment.
expectedUsdcOutuint256Net USDC the user will receive.
flashFeeuint256Flash mint fee (if any).

pause

Pause the router. Blocks zapMint and zapBurn.

function pause() external onlyOwner;

unpause

Unpause the router.

function unpause() external onlyOwner;

Events

ZapMint

Emitted when user acquires plDXY-BULL via zapMint.

event ZapMint(
    address indexed user, uint256 usdcIn, uint256 tokensOut, uint256 maxSlippageBps, uint256 actualSwapOut
);

ZapBurn

Emitted when user sells plDXY-BULL via zapBurn.

event ZapBurn(address indexed user, uint256 tokensIn, uint256 usdcOut);

Errors

ZapRouter__ZeroAddress

error ZapRouter__ZeroAddress();

ZapRouter__ZeroAmount

error ZapRouter__ZeroAmount();

ZapRouter__Expired

error ZapRouter__Expired();

ZapRouter__SlippageExceedsMax

error ZapRouter__SlippageExceedsMax();

ZapRouter__SplitterNotActive

error ZapRouter__SplitterNotActive();

ZapRouter__BearPriceAboveCap

error ZapRouter__BearPriceAboveCap();

ZapRouter__InsufficientOutput

error ZapRouter__InsufficientOutput();

ZapRouter__InvalidCurvePrice

error ZapRouter__InvalidCurvePrice();

ZapRouter__SolvencyBreach

error ZapRouter__SolvencyBreach();