LoopingHook

Looping Hook contract, which uses the same implementation as MarginTradingHook contract.

View Functions

swapHelper

Get the swap helper contract that this contract swaps between quote and base assets.

function swapHelper() external view returns (address swapHelper);

lastOrderId

Last running order (take profit or stop loss) id.

function lastOrderId() external view returns (uint256 orderId);

getBaseAssetAndQuoteAsset

Get a unique base and quote assets from a pair of tokens.

function getBaseAssetAndQuoteAsset(address _tokenA, address _tokenB) external view returns (address baseAsset, address quoteAsset);

getMarginPos

Get a looping position.

function getMarginPos(uint256 _initPosId) external view returns (MarginPos memory pos);
struct MarginPos {
    address collPool; // lending pool to deposit holdToken
    address borrPool; // lending pool to borrow borrowToken
    address baseAsset; // base asset of position
    address quoteAsset; // quote asset of position
    bool isLongBaseAsset; // long base asset or not
}

External Functions

openPos

Open a new looping position by swapping borrPool's underlying token into collPool's underlying token via swap data _data.

The fucntion perform multicall to InitCore:

  1. borrow tokens

  2. callback (perform swap from borrow token to collateral token)

  3. deposit collateral tokens to lending pool

  4. collateralize inTokens

The swap callback is routed back to this contract's coreCallback which performs a swap using _data on swapHelper contract with slippage control amtOut.

SwapInfo memory swapInfo = SwapInfo(_param.initPosId, SwapType.OpenExactIn, borrToken, collToken, _param.minAmtOut, _param.data);
multicallData[1] = abi.encodeWithSelector(IInitCore(CORE).callback.selector, address(this), 0, abi.encode(swapInfo));

struct SwapInfo {
    uint initPosId; // nft id
    SwapType swapType; // swap type
    address tokenIn; // token to swap
    address tokenOut; // token to receive from swap
    uint amtOut; // token amount out info for the swap
    bytes data; // swap data
}

enum SwapType {
    OpenExactIn,
    CloseExactIn,
    CloseExactOut
}

NOTE: _tokenIn must be either _borrPool's underlying token or _collPool's underlying token.

function openPos(
    uint16 _mode,
    address _viewer,
    address _tokenIn,
    uint256 _amtIn,
    address _borrPool,
    uint256 _borrAmt,
    address _collPool,
    bytes calldata _data,
    uint256 _minAmtOut
) external payable returns (uint256 posId, uint256 initPosId, uint256 amtOut);

Returns:

increasePos

Increase an existing position's size by taking token from position owner and/or borrow more tokens.

_collPool and borrPool are fixed from the existing position.

function increasePos(
    uint256 _posId,
    address _tokenIn,
    uint256 _amtIn,
    uint256 _borrAmt,
    bytes calldata _data,
    uint256 _minAmtOut
) external payable returns (uint256 amtOut);

Returns:

addCollateral

Decrease position's leverage by adding more collateral token.

function addCollateral(uint256 _posId, uint256 _amtIn) external payable;

Parameters:

removeCollateral

Increase position's leverage by removing collateral token.

function removeCollateral(uint _posId, uint256 _shares, bool _returnNative) external;

Parameters:

repayDebt

Decrease position's leverage by repaying borrow token.

function repayDebt(uint256 _posId, uint256 _repayShares) external returns (uint256 repayAmt);

Parameters:

Returns:

reducePos

Reduce an existing position's size by withdrawing collateral and repaying borrow token. The withdrawn collateral token can be swapped to repay borrow token using _data. The user can specify which token to receive, if there is still left after swap.

function reducePos(
    uint256 _posId,
    uint256 _collAmt,
    uint256 _repayShares,
    address _tokenOut,
    uint256 _minAmtOut,
    bool _returnNative,
    bytes calldata _data
) external returns (uint256 amtOut);

Parameters:

Returns:

Last updated