DefaultAdapter

Git Source

Inherits: IRouterAdapter

Functions

receive

Enable this contract to receive Ether when withdrawing from WETH.

Consider implementing rescue functions to withdraw Ether from this contract.

receive() external payable;

adapterSwap

Performs a tokenIn -> tokenOut swap, according to the provided params. If tokenIn is ETH_ADDRESS, this method should be invoked with msg.value = amountIn. If tokenIn is ERC20, the tokens should be already transferred to this contract (using msg.value = 0). If tokenOut is ETH_ADDRESS, native ETH will be sent to the recipient (be aware of potential reentrancy). If tokenOut is ERC20, the tokens will be transferred to the recipient.

Contracts implementing {IRouterAdapter} interface are required to enforce the above restrictions. On top of that, they must ensure that exactly amountOut worth of tokenOut is transferred to the recipient. Swap deadline and slippage is checked outside of this contract.

function adapterSwap(
    address recipient,
    address tokenIn,
    uint256 amountIn,
    address tokenOut,
    bytes memory rawParams
)
    external
    payable
    returns (uint256 amountOut);

Parameters

NameTypeDescription
recipientaddressAddress to receive the swapped token
tokenInaddressToken to sell (use ETH_ADDRESS to start from native ETH)
amountInuint256Amount of tokens to sell
tokenOutaddressToken to buy (use ETH_ADDRESS to end with native ETH)
rawParamsbytesAdditional swap parameters

Returns

NameTypeDescription
amountOutuint256Amount of bought tokens

_adapterSwap

Internal logic for doing a tokenIn -> tokenOut swap. Note: tokenIn is assumed to have already been transferred to this contract.

function _adapterSwap(
    address recipient,
    address tokenIn,
    uint256 amountIn,
    address tokenOut,
    bytes memory rawParams
)
    internal
    virtual
    returns (uint256 amountOut);

_checkParams

Checks the params and decodes them into a struct.

function _checkParams(
    address tokenIn,
    address tokenOut,
    bytes memory rawParams
)
    internal
    pure
    returns (DefaultParams memory params);

_wrapReceivedETH

Wraps native ETH into WETH, if requested. Returns the address of the token this contract ends up with.

function _wrapReceivedETH(
    address tokenIn,
    uint256 amountIn,
    address tokenOut,
    DefaultParams memory params
)
    internal
    returns (address wrappedTokenIn);

_deriveTokenSwapTo

Derives the address of token to be received after an action defined in params.

function _deriveTokenSwapTo(
    address tokenIn,
    address tokenOut,
    DefaultParams memory params
)
    internal
    view
    returns (address tokenSwapTo);

_performPoolAction

Performs an action defined in params and returns the amount of tokenSwapTo received.

function _performPoolAction(
    address tokenIn,
    uint256 amountIn,
    address tokenSwapTo,
    DefaultParams memory params
)
    internal
    returns (uint256 amountOut);

_swap

Performs a swap through the given pool. Note: The pool should be already approved for spending tokenIn.

function _swap(address pool, DefaultParams memory params, uint256 amountIn, address tokenOut) internal;

_addLiquidity

Adds liquidity in a form of a single token to the given pool. Note: The pool should be already approved for spending tokenIn.

function _addLiquidity(address pool, DefaultParams memory params, uint256 amountIn, address tokenOut) internal;

_removeLiquidity

Removes liquidity in a form of a single token from the given pool. Note: The pool should be already approved for spending tokenIn.

function _removeLiquidity(address pool, DefaultParams memory params, uint256 amountIn, address tokenOut) internal;

_getPoolLPToken

Returns the LP token address of the given pool.

function _getPoolLPToken(address pool) internal view returns (address lpToken);

_getPoolNumTokens

Returns the number of tokens in the given pool.

function _getPoolNumTokens(address pool) internal view returns (uint256 numTokens);

_getPoolTokens

Returns the tokens in the given pool.

function _getPoolTokens(address pool) internal view returns (address[] memory tokens);

_getPoolSwapQuote

Returns the quote for a swap through the given pool. Note: will return 0 on invalid swaps.

function _getPoolSwapQuote(
    address pool,
    uint8 tokenIndexFrom,
    uint8 tokenIndexTo,
    uint256 amountIn
)
    internal
    view
    returns (uint256 amountOut);

_wrapETH

Wraps ETH into WETH.

function _wrapETH(address weth, uint256 amount) internal;

_unwrapETH

Unwraps WETH into ETH.

function _unwrapETH(address weth, uint256 amount) internal;

_deriveWethAddress

Derives WETH address from swap parameters.

function _deriveWethAddress(
    address token,
    DefaultParams memory params,
    bool isTokenFromWeth
)
    internal
    view
    returns (address weth);