Contract Interactions
- Swapping ERC20 → ETH:
swapTokensToETH
- Swapping ETH → ERC20:
swapETHToTokens
- Swapping ERC20 → ERC20:
swap
- 1.Approve the
Router
contract for the token and amount you would like to swap - 2.Call
Router.swap
with parameters:- 1.
_path
:[tokenIn, tokenOut]
- 2.
_amountIn
: amount oftokenIn
to swap - 3.
_minOut
: minimum expected output amount - 4.
_receiver
: address of the receiver oftokenOut
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 oftokenIn
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
- Contract to use:
PositionRouter
-https://arbiscan.io/address/0xe510571cac76279dadf6c4b6eace5370f86e3dc2#code - Using an ERC20 to increase your exposure to an asset:
createIncreasePosition
- Using ETH to increase your exposure to an asset:
createIncreasePositionETH
- Cancelling a request to increase your position:
cancelIncreasePosition
Steps:
- 1.Approve the
Router
contract for the token and amount you would deposit as collateral for the position. - 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.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.
PositionRouter.createIncreasePosition
has the same input parameters but additionally has anexecutionFee
value that can be set toPositionRouter.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.Call
PositionManager.increasePosition
with parameters:- 1.
_path
:[tokenIn, collateralToken]
or[tokenIn]
. - 2.
_indexToken
: the address of the token you want to long or short. - 3.
_amountIn
: the amount oftokenIn
you want to deposit as collateral. - 4.
_minOut
: the min amount ofcollateralToken
to swap for. - 5.
_sizeDelta
: the USD value of the change in position size. - 6.
_isLong
: whether to long or short. - 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 thecollateralToken
if needed.- For longs, the
collateralToken
must be the same as theindexToken
. - 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.
- Contract to use:
PositionRouter
-https://arbiscan.io/address/0xe510571cac76279dadf6c4b6eace5370f86e3dc2#code - Decreasing your exposure to an asset:
createDecreasePosition
- Cancelling a request to decrease your position:
cancelDecreasePosition
Steps:
- 1.
PositionManager.decreasePosition
can be called by partner contracts and will close or decrease the position in one transaction. - 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.
PositionRouter.createDecreasePosition
has the same input parameters but additionally has anexecutionFee
value that can be set toPositionRouter.minExecutionFee
. If the position cannot be opened at the specified "_price
" value then the request will be cancelled - 4.Call `Router.decreasePosition with parameters:
- 1.
_collateralToken
: the collateral token used - 2.
_indexToken
: the index token of the position - 3.
_collateralDelta
: the amount of collateral in USD value to withdraw - 4.
_sizeDelta
: the USD value of the change in position size - 5.
_isLong
: whether the position is a long or short - 6.
_receiver
: the address to receive the withdrawn tokens - 7.
_price
: the USD value of the min (for shorts) or max (for longs) index price accepted when decreasing the position - 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
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 ofcollateralTokens
._indexTokens
: an array ofindexTokens
._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 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 buy price would be getAum(true) / mlpSupply
- The sell price would be getAum(false) / mlpSupply
Last modified 1yr ago