The contract interfaces with Aave as the margin account provider, Balancer for flashloans and Uniswap V3 for swaps.
- Deposit funds into margin account, which is the Aave account linked the the user's wallet
- Open long & short positions
- Close long & short positions
- Withdraw funds from the margin account
depositIntoMarginAccount (uint256 amount)
Deposits USDC into margin account. The amount is supplied as collateral on Aave V3.
@dev Make sure to approve the transfer of USDC from user to this contract.
@param amount
The amount of USDC to be transferred.
withdrawFromMarginAccount(uint256 amount)
Withdraws USDC into margin account. The amount is withdrawn from Aave V3 supplies.
@dev Aave will allow the withdraw only if new HF > 1 else tx will revert.
@dev Make sure to appove the transfer aUSDC from user to this contract.
@param amount
The amount of USDC to be withdrawn.
requestOpen(uint256 amount, address tokenAddress, uint8 direction)
Opens long or short position by:
- Initiating a flashloan from Balancer
- Swapping to the asset if long or USDC if short
- Supplying to Aave
- Borrowing against collateral (borrow USDC if long, asset if short)
- Repaying flashloan with borrowed amount
@dev Make sure to do the necessary checks to calculate if the position can be opened else Aave will revert the tx.
@dev Make sure to approve credit delegation of vTokens of USDC (if long) or the requested asset (if short) from user to this contract.
@paramamount
Size of position denoted in USDC (if long) or denoted in requested asset (if short).
@paramtokenAddress
Contract address of a supported asset.
@paramdirection
0 = long, 1 = short.
requestClose(uint256 repay, address tokenAddress, uint8 direction, uint256 profit)
Closes long or short position by:
- Initiating a flashloan from Balancer
- Swapping to USDC if long or the asset if short
- Supplying to Aave
- Withdrawing collateral from opening the position (withdraw asset if long, USDC if short)
- Repaying flashloan with the withdrawn amount
- Booking profit if any
@dev Make sure to do the necessary checks to calculate if the position can be closed else Aave will revert the tx.
@dev If closing long, make sure to approve the transfer of close size of tokens and aTokens of the requested asset from user to this contract.
@dev If closing short, make sure to approve the transfer of close size of USDC and aUSDC from user to this contract address.
@paramrepay
If closing long, size of position minus profit (denoted in requested asset). If closing short, size of borrow (denoted in USDC).
@paramtokenAddress
Contract address of the asset.
@paramdirection
2 = closing long, 3 = closing short.
@paramprofit
Amount of profit booked. If closing long, denoted in requested asset. If closing short, denoted in USDC. 0 if profit <= 0.