> For the complete documentation index, see [llms.txt](https://gambit.gitbook.io/gambit/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://gambit.gitbook.io/gambit/contracts.md).

# Contracts

**BSC Mainnet Contracts**

* Vault: [0xc73A8DcAc88498FD4b4B1b2AaA37b0a2614Ff67B](https://bscscan.com/address/0xc73A8DcAc88498FD4b4B1b2AaA37b0a2614Ff67B)
* Router: [0xD46B23D042E976F8666F554E928e0Dc7478a8E1f](https://bscscan.com/address/0xD46B23D042E976F8666F554E928e0Dc7478a8E1f)
* Reader: [0x1f6bae121A9f97ca3E02CbA33F799C5dF0e9948F](https://bscscan.com/address/0x1f6bae121A9f97ca3E02CbA33F799C5dF0e9948F)

**Source code**

* Vault: <https://github.com/xvi10/gambit-contracts/blob/master/contracts/core/Vault.sol>
* Router: <https://github.com/xvi10/gambit-contracts/blob/master/contracts/core/Router.sol>
* Reader: <https://github.com/xvi10/gambit-contracts/blob/master/contracts/peripherals/Reader.sol>

**Token List**

For a list of supported tokens: <https://gambit.gitbook.io/gambit/api>.

**Swapping**

To execute a swap:

* Approve the Router contract for the token and amount you would like to swap
* Call Router.swap with parameters:
  * \_path: \[tokenIn, tokenOut]
  * \_amountIn: amount of tokenIn to swap
  * \_minOut: minimum expected output amount
  * \_receiver: address of the receiver of tokenOut
* The function will revert if the amount of tokenOut sent to the receiver is less than *\_*&#x6D;inOut

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

**Opening / Increasing a position**

To open or increase the size of an existing position:

* Approve the Router contract for the token and amount you would deposit as collateral for the position
* Call Router.increasePosition with parameters:
  * \_path: \[tokenIn, collateralToken] or \[tokenIn]
  * \_indexToken: the address of the token you want to long or short
  * \_amountIn: the amount of tokenIn you want to deposit as collateral
  * \_minOut: the min amount of collateralToken to swap for
  * \_sizeDelta: the USD value of the change in position size&#x20;
  * \_isLong: whether to long or short
  * \_price: the USD value of the min (for longs) or max (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

**Closing / Decreasing a position**

To close or decrease an existing position:

* Call Router.decreasePosition with parameters:
  * \_collateralToken: the collateral token used
  * \_indexToken: the index token of the position
  * \_collateralDelta: the amount of collateral in USD value to withdraw
  * \_sizeDelta: the USD value of the change in position size
  * \_isLong: whether the position is a long or short
  * \_receiver: the address to receive the withdrawn tokens
  * \_price: the USD value of the min (for shorts) or max (for longs) index price accepted when decreasing the position
  * If \_sizeDelta is the same size as the position, the collateral after adding profits or deducting losses will be sent to the receiver address
