Vault

Vault

_Vault is used to store the token address and shared decimals.

The positions is the mapping of the position key to the position. The position key is the keccak256 hash of the account, collateral token, index token and long/short. The position is the struct that stores the position information.

The {isInitialized} variable is used to ensure that the initialize function can only be called once. The {router} variable is the address of the router. The {initialize} function is used to initialize the router address. The {getPositionKey} function is used to get the position key.

The {increasePosition} function is used to increase the position size. The {decreasePosition} function is used to decrease the position size._

positions

mapping(bytes32 => struct Vault.Position) positions

isInitialized

bool isInitialized

router

address router

constructor

constructor() public

onlyRouter

modifier onlyRouter()

_onlyRouter is used to ensure that the function can only be called by the router.

If the caller is not the router then revert._

initialize

function initialize(address _router) external

_constructor is used to initialize the router address.

The {isInitialized} variable is used to ensure that the initialize function can only be called once.

If the {isInitialized} variable is true then revert.

The {router} variable is the address of the router._

Position

struct Position {
  uint256 size;
  uint256 collateral;
  uint256 averagePrice;
  uint256 entryFundingRate;
  uint256 reserveAmount;
  int256 realisedPnl;
  uint256 lastIncreasedTime;
}

getPositionKey

function getPositionKey(address _account, address _collateralToken, address _indexToken, bool _isLong) public view returns (bytes32)

_getPositionKey is used to get the position key.

The position key is the keccak256 hash of the account, collateral token, index token and long/short.

For example, the position key for the long position of 1 ETH using USDC as collateral is keccak256(abi.encodePacked(account, usdc, eth, true)).

For example, the position key for the short position of 1 ETH using USDC as collateral is keccak256(abi.encodePacked(account, usdc, eth, false)).

The result of the getPositionKey function is used as the key for the positions mapping._

Parameters

NameTypeDescription

_account

address

The address of the account to get the position key for.

_collateralToken

address

The address of the collateral token to get the position key for.

_indexToken

address

The address of the index token to get the position key for.

_isLong

bool

The direction of the position to get the position key for.

Return Values

NameTypeDescription

[0]

bytes32

The position key. The key is bytes32. The key is the keccak256 hash of the account, collateral token, index token and long/short.

increasePosition

function increasePosition(address _account, address _collateralToken, address _indexToken, uint256 _sizeDelta, bool _isLong) external

_increasePosition is used to increase the position size.

The {getPositionKey} function is used to get the position key. The {positions} mapping is used to get the position. The {position} variable is the position.

The increasePosition is an external function.

The increasePosition function can only be called by the router. And if the caller is not the router then revert.

nonReentrant is used to ensure that the function is not called again until the previous call has completed. And nonReentrant is used to prevent reentrancy attacks._

Parameters

NameTypeDescription

_account

address

The address of the account to increase the position size for.

_collateralToken

address

The address of the collateral token to increase the position size for.

_indexToken

address

The address of the index token to increase the position size for.

_sizeDelta

uint256

The amount to increase the position size by.

_isLong

bool

The direction of the position to increase the position size for.

decreasePosition

function decreasePosition(address _account, address _collateralToken, address _indexToken, uint256 _collateralDelta, uint256 _sizeDelta, bool _isLong, address _receiver) external returns (uint256)

_decreasePosition is used to decrease the position size.

The {getPositionKey} function is used to get the position key. The {positions} mapping is used to get the position. The {position} variable is the position.

nonReentrant is used to ensure that the function is not called again until the previous call has completed. And nonReentrant is used to prevent reentrancy attacks.

The decreasePosition is an external function.

The decreasePosition function is nonReentrant. And if the function is called again before the previous call has completed then revert.

The decreasePosition function can only be called by the router. And if the caller is not the router then revert._

Parameters

NameTypeDescription

_account

address

The address of the account to decrease the position size for.

_collateralToken

address

The address of the collateral token to decrease the position size for.

_indexToken

address

The address of the index token to decrease the position size for.

_collateralDelta

uint256

The amount of collateral to decrease the position size by.

_sizeDelta

uint256

The amount to decrease the position size by.

_isLong

bool

The direction of the position to decrease the position size for.

_receiver

address

The address to send the tokens to.

Return Values

NameTypeDescription

[0]

uint256

The amount of tokens sent to the receiver.

Last updated