MarginTrading.sol

This contract allows users to manage margin accounts, provide collateral, borrow, and repay tokens.

How to Interact with a Margin Account directly

Below, you will find the functions needed to interact with a margin account. In most cases, you will need to know the margin account ID, which is a unique number assigned to each created margin account. To understand how to obtain this number, please refer to this page.

How To Deposit Collateral (UDSC, WBTC, ETH)

To borrow funds from the liquidity pools, users must provide collateral tokens (USDC, WBTC, or ETH). We use Chainlink price feeds to calculate the value of your collateral in USDC.

    function provideERC20(uint marginAccountID, address token, uint amount) external nonReentrant onlyApprovedOrOwner(marginAccountID) {
        marginAccount.provideERC20(marginAccountID, msg.sender, token, amount);

        emit ProvideERC20(marginAccountID, msg.sender, token, amount);
    }

How to Deposit American-style options (Hegic Option Position)

    function provideERC721(uint marginAccountID, address token, uint collateralTokenID) external nonReentrant onlyApprovedOrOwner(marginAccountID) {
        require(modularSwapRouter.checkValidityERC721(token, BASE_TOKEN, collateralTokenID), "token id is not valid");
        marginAccount.provideERC721(marginAccountID, msg.sender, token, collateralTokenID);

        emit ProvideERC721(marginAccountID, msg.sender, token, collateralTokenID);
    }

How To Withdraw Collateral (USDC, WBTC, ETH)

When withdrawing collateral from a margin account, the following criteria must be met:

  • portfolioRatio > yellowCoeff

The yellowCoeff may change, so to get the actual number, please check beginning of this contract

How to Withdraw American-style option (Hegic Option Position)

How to Borrow (USDC, WBTC, ETH)

How to Repay

How to swap tokens

When a trader makes a swap, it is routed through Uniswap v3.

How to exercise American-style option (Hegic Option Position)

How to get the current margin account value in USDC

How to get the current debt across all liquidity pools in USDC (borrowed amount + interest to be paid)

How to get the current margin ratio

Last updated