Links

Contract Interactions

Performing a swap

  1. 1.
    Approve the Router contract for the token and amount you would like to swap
  2. 2.
    Call Router.swap with parameters:
    1. 1.
      _path: [tokenIn, tokenOut]
    2. 2.
      _amountIn: amount of tokenIn to swap
    3. 3.
      _minOut: minimum expected output amount
    4. 4.
      _receiver: address of the receiver of tokenOut
To get swap amounts before execution:
  • Call Reader.getMaxAmountIn with parameters:
    • _vault: address of the vault
    • _tokenIn: address of token that will be given
    • _tokenOut: address of token to be received
    • The max amount of tokenIn that can be swapped will be returned
  • Call Reader.getAmountOut with parameters:
    • _vault: address of the vault
    • _tokenIn: address of token that will be given
    • _tokenOut: address of token to be received
    • _amountIn: amount of tokenIn to swap
    • Two values will be returned, the first is the amount out after fees, and the second is the fee amount
    • The fee amount will be in terms of tokenOut

Increasing your position

Steps:
  1. 1.
    Approve the Router contract for the token and amount you would deposit as collateral for the position.
  2. 2.
    PositionManager.increasePosition can be called by partner contracts and will open the position in one transaction, partner contracts can only be added if it has measures in place to prevent front-running issues.
  3. 3.
    Alternatively, PositionRouter.createIncreasePosition can be used by any contract and will request the position to be opened, a keeper will then execute this request.
  4. 4.
    PositionRouter.createIncreasePosition has the same input parameters but additionally has an executionFee value that can be set to PositionRouter.minExecutionFee. If the position cannot be opened at the specified "_price" value then the request will be cancelled and the funds sent back to the account that made the request.
  5. 5.
    Call PositionManager.increasePosition with parameters:
    1. 1.
      _path: [tokenIn, collateralToken] or [tokenIn].
    2. 2.
      _indexToken: the address of the token you want to long or short.
    3. 3.
      _amountIn: the amount of tokenIn you want to deposit as collateral.
    4. 4.
      _minOut: the min amount of collateralToken to swap for.
    5. 5.
      _sizeDelta: the USD value of the change in position size.
    6. 6.
      _isLong: whether to long or short.
    7. 7.
      _price: the USD value of the max (for longs) or min (for shorts) index price accepted when opening the position.
  • _path allows swapping to the collateralToken if needed.
  • For longs, the collateralToken must be the same as the indexToken.
  • For shorts, the collateralToken can be any stablecoin token.
  • _minOut can be zero if no swap is required.
  • USD values for _sizeDelta and _price are multiplied by (10 ** 30), so for example to open a long position of size 1000 USD, the value 1000 * (10 ** 30) should be used.

Decreasing your position

Steps:
  1. 1.
    PositionManager.decreasePosition can be called by partner contracts and will close or decrease the position in one transaction.
  2. 2.
    Alternatively PositionRouter.createDecreasePosition can be used by any contract and will request the position to be closed / decreased, a keeper will then execute this request.
  3. 3.
    PositionRouter.createDecreasePosition has the same input parameters but additionally has an executionFee value that can be set to PositionRouter.minExecutionFee. If the position cannot be opened at the specified "_price" value then the request will be cancelled
  4. 4.
    Call `Router.decreasePosition with parameters:
    1. 1.
      _collateralToken: the collateral token used
    2. 2.
      _indexToken: the index token of the position
    3. 3.
      _collateralDelta: the amount of collateral in USD value to withdraw
    4. 4.
      _sizeDelta: the USD value of the change in position size
    5. 5.
      _isLong: whether the position is a long or short
    6. 6.
      _receiver: the address to receive the withdrawn tokens
    7. 7.
      _price: the USD value of the min (for shorts) or max (for longs) index price accepted when decreasing the position
    8. 8.
      If _sizeDelta is the same size as the position, the collateral after adding profits or deducting losses will be sent to the receiver address

Positions List

A list of position details can be retrieved by calling Reader.getPositions with the following parameters:
  • _vault: the vault contract address.
  • _account: the account of the user.
  • _collateralTokens: an array of collateralTokens.
  • _indexTokens: an array of indexTokens.
  • _isLong: an array of whether the position is a long position.
The returned positions will be in the order of the query, for example, given the following inputs:
  • _collateralTokens: [WBTC.address, WETH.address, USDC.address].
  • _indexTokens: [WBTC.address, WETH.address, WBTC.address].
  • _isLong: [true, true, false].
The position details would be returned for
  • Long BTC position, positionIndex: 0
  • Long ETH position, positionIndex: 1
  • Short BTC position, positionIndex: 2
The returned array would be a list of values ordered by the positions:
  • size
    • position size in USD
    • value at: positionIndex * 9
  • collateral
    • position collateral in USD
    • value at: positionIndex * 9 + 1
  • averagePrice
    • average entry price of the position in USD
    • value at: positionIndex * 9 + 2
  • entryFundingRate
    • a snapshot of the cumulative funding rate at the time the position was entered
    • value at: positionIndex * 9 + 3
  • hasRealisedProfit
    • 1 if the position has a positive realised profit, 0 otherwise
    • value at: positionIndex * 9 + 4
  • realisedPnl
    • the realised PnL for the position in USD
    • value at: positionIndex * 9 + 5
  • lastIncreasedTime
    • timestamp of the last time the position was increased
    • value at: positionIndex * 9 + 6
  • hasProfit
    • 1 if the position is currently in profit, 0 otherwise
    • value at: positionIndex * 9 + 7
  • delta
    • amount of current profit or loss of the position in USD
    • value at: positionIndex * 9 + 8

Buying / Selling MLP

Buying and selling MLP can be done through the RewardRouterV2 at 0xd98d8e458F7aD22DD3C1d7A8B35C74005eb52b0b.
To buy MLP, call mintAndStakeMlp
  • _token: the token to buy MLP with
  • _amount: the amount of token to use for the purchase
  • _minUsdg: the minimum acceptable USD value of the MLP purchased
  • _minMlp: the minimum acceptable MLP amount
To sell MLP, unstakeAndRedeemMlp
  • _tokenOut: the token to sell MLP for
  • _mlpAmount: the amount of MLP to sell
  • _minOut: the minimum acceptable amount of tokenOut to be received
  • _receiver: the address to send tokenOut to
The price of MLP can be retrieved from 0x2DE28AB4827112Cd3F89E5353Ca5A8D80dB7018f.
  • The buy price would be getAum(true) / mlpSupply
  • The sell price would be getAum(false) / mlpSupply
mlpSupply would be the totalSupply value of 0x752b746426b6D0c3188bb530660374f92FD9cf7c