Skip to main content

Swapper

GitHub: link

Abstract contract for swapping between xPYTs/NYTs and their underlying asset by swapping via an external DEX and minting/burning xPYT/NYT.

Swapper supports two-hop swaps where one of the swaps is an 0x swap between two regular tokens, which enables swapping any supported token into any xPYT/NYT. Two-hop swaps are done by chaining two calls together via Multicall and setting the recipient of the first swap to the Swapper.

Errors​

Error_SameToken​

error Error_SameToken()

Error_PastDeadline​

error Error_PastDeadline()

Error_ZeroExSwapFailed​

error Error_ZeroExSwapFailed()

Error_InsufficientOutput​

error Error_InsufficientOutput()

Error_ProtocolFeeRecipientIsZero​

error Error_ProtocolFeeRecipientIsZero()

Events​

SetProtocolFee​

event SetProtocolFee(struct Swapper.ProtocolFeeInfo protocolFeeInfo_)

Structs​

SwapArgs​

struct SwapArgs {
contract Gate gate;
address vault;
contract ERC20 underlying;
contract ERC20 nyt;
contract ERC20 pyt;
contract IxPYT xPYT;
uint256 tokenAmountIn;
uint256 minAmountOut;
address recipient;
bool useSwapperBalance;
bool usePYT;
uint256 deadline;
bytes extraArgs;
}

ProtocolFeeInfo​

struct ProtocolFeeInfo {
uint8 fee;
address recipient;
}

Immutable parameters​

zeroExProxy​

address zeroExProxy

The 0x proxy contract used for 0x swaps

weth​

contract WETH weth

The Wrapped Ethereum contract

State variables​

protocolFeeInfo​

struct Swapper.ProtocolFeeInfo protocolFeeInfo

The protocol fee and the fee recipient address.

State-mutating functions​

swapUnderlyingToNyt​

function swapUnderlyingToNyt(struct Swapper.SwapArgs args) external virtual returns (uint256 tokenAmountOut)

Swaps the underlying asset of an NYT into the NYT

NameTypeDescription
argsstruct Swapper.SwapArgsThe input arguments (see SwapArgs definition)
NameTypeDescription
tokenAmountOutuint256The amount of token output

swapUnderlyingToXpyt​

function swapUnderlyingToXpyt(struct Swapper.SwapArgs args) external virtual returns (uint256 tokenAmountOut)

Swaps the underlying asset of an xPYT into the xPYT

NameTypeDescription
argsstruct Swapper.SwapArgsThe input arguments (see SwapArgs definition)
NameTypeDescription
tokenAmountOutuint256The amount of token output

swapNytToUnderlying​

function swapNytToUnderlying(struct Swapper.SwapArgs args) external virtual returns (uint256 tokenAmountOut)

Swaps an NYT to its underlying asset

NameTypeDescription
argsstruct Swapper.SwapArgsThe input arguments (see SwapArgs definition)
NameTypeDescription
tokenAmountOutuint256The amount of token output

swapXpytToUnderlying​

function swapXpytToUnderlying(struct Swapper.SwapArgs args) external virtual returns (uint256 tokenAmountOut)

Swaps an xPYT to its underlying asset

NameTypeDescription
argsstruct Swapper.SwapArgsThe input arguments (see SwapArgs definition)
NameTypeDescription
tokenAmountOutuint256The amount of token output

doZeroExSwap​

function doZeroExSwap(contract ERC20 tokenIn, uint256 tokenAmountIn, contract ERC20 tokenOut, uint256 minAmountOut, address recipient, bool useSwapperBalance, bool requireApproval, uint256 deadline, bytes swapData) external virtual returns (uint256 tokenAmountOut)

Swaps between two regular tokens using 0x.

Used in conjuction with the 0x API https://www.0x.org/docs/api

NameTypeDescription
tokenIncontract ERC20The input token
tokenAmountInuint256The amount of token input
tokenOutcontract ERC20The output token
minAmountOutuint256The minimum acceptable token output amount, used for slippage checking.
recipientaddressThe recipient of the token output
useSwapperBalanceboolSet to true to use the Swapper's token balance as token input
requireApprovalboolSet to true to approve tokenIn to zeroExProxy
deadlineuint256The Unix timestamp (in seconds) after which the call will be reverted
swapDatabytesThe call data to zeroExProxy to execute the swap, obtained from the https://api.0x.org/swap/v1/quote endpoint
NameTypeDescription
tokenAmountOutuint256The amount of token output

wrapEthInput​

function wrapEthInput() external payable

Wraps the user's ETH input into WETH

Should be used as part of a multicall to convert the user's ETH input into WETH so that it can be swapped into xPYT/NYT.

Owner functions​

ownerSetProtocolFee​

function ownerSetProtocolFee(struct Swapper.ProtocolFeeInfo protocolFeeInfo_) external virtual

Updates the protocol fee and/or the protocol fee recipient. Only callable by the owner.

NameTypeDescription
protocolFeeInfo_struct Swapper.ProtocolFeeInfoThe new protocol fee info