Skip to main content

ChromaticAccount.sol

ChromaticAccount

This contract manages user accounts and positions.

owner

address owner

isInitialized

bool isInitialized

NotRouter

error NotRouter()

Throws an error indicating that the caller is not the chromatic router contract.

NotOwner

error NotOwner()

Throws an error indicating that the caller is not the owner of this account contract.

AlreadyInitialized

error AlreadyInitialized()

Throws an error indicating that the account is already initialized, and calling the initialization function again is not allowed.

NotEnoughBalance

error NotEnoughBalance()

Throws an error indicating that the account does not have sufficient balance to perform a particular operation, such as withdrawing an amount of tokens.

NotExistPosition

error NotExistPosition()

Throws an error indicating that the caller is not the owner of this account contractthat the caller is not the owner of this account contract.

onlyRouter

modifier onlyRouter()

Modifier that allows only the router to call a function. Throws an NotRouter error if the caller is not the chromatic router contract.

onlyOwner

modifier onlyOwner()

Modifier that allows only the owner to call a function. Throws an NotOwner error if the caller is not the owner of this account contract.

initialize

function initialize(address _owner, address _router, address _marketFactory) external

Initializes the account with the specified owner, router, and market factory addresses.

Throws an AlreadyInitialized error if the account has already been initialized.

  • Parameters:

    NameTypeDescription
    _owneraddressThe address of the account owner.
    _routeraddressThe address of the router contract.
    _marketFactoryaddressThe address of the market factory contract.

balance

function balance(address token) public view returns (uint256)

Returns the balance of the specified token for the account.

  • Parameters:

    NameTypeDescription
    tokenaddressThe address of the token.
  • Return Values:

    NameTypeDescription
    [0]uint256The balance of the token.

withdraw

function withdraw(address token, uint256 amount) external

Withdraws the specified amount of tokens from the account.

This function can only be called by owner. Throws a NotEnoughBalance error if the account does not have enough balance of the specified token.

  • Parameters:

    NameTypeDescription
    tokenaddressThe address of the token to withdraw.
    amountuint256The amount of tokens to withdraw.

addPositionId

function addPositionId(address market, uint256 positionId) internal

removePositionId

function removePositionId(address market, uint256 positionId) internal

hasPositionId

function hasPositionId(address market, uint256 id) public view returns (bool)

Checks if the specified market has the specified position ID.

  • Parameters:

    NameTypeDescription
    marketaddress
    iduint256
  • Return Values:

    NameTypeDescription
    [0]boolA boolean indicating whether the market has the position ID.

getPositionIds

function getPositionIds(address market) external view returns (uint256[])

Retrieves an array of position IDs owned by this account for the specified market.

  • Parameters:

    NameTypeDescription
    marketaddress
  • Return Values:

    NameTypeDescription
    [0]uint256[]An array of position IDs.

openPosition

function openPosition(address marketAddress, int256 qty, uint256 takerMargin, uint256 makerMargin, uint256 maxAllowableTradingFee) external returns (struct OpenPositionInfo position)

Opens a new position in the specified market.

This function can only be called by the chromatic router contract.

  • Parameters:

    NameTypeDescription
    marketAddressaddressThe address of the market.
    qtyint256The quantity of the position.
    takerMarginuint256The margin required for the taker.
    makerMarginuint256The margin required for the maker.
    maxAllowableTradingFeeuint256The maximum allowable trading fee.
  • Return Values:

    NameTypeDescription
    positionstruct OpenPositionInfoopenPositionInfo The opened position information.

closePosition

function closePosition(address marketAddress, uint256 positionId) external

Closes the specified position in the specified market.

This function can only be called by the chromatic router contract. Throws a NotExistPosition error if the position does not exist.

  • Parameters:

    NameTypeDescription
    marketAddressaddressThe address of the market.
    positionIduint256The ID of the position to close.

claimPosition

function claimPosition(address marketAddress, uint256 positionId) external

Claims the specified position in the specified market.

This function can only be called by the chromatic router contract. Throws a NotExistPosition error if the position does not exist.

  • Parameters:

    NameTypeDescription
    marketAddressaddressThe address of the market.
    positionIduint256The ID of the position to claim.

openPositionCallback

function openPositionCallback(address settlementToken, address vault, uint256 marginRequired, bytes) external

Callback function called after opening a position.

Transfers the required margin from the account to the specified vault. Throws a NotEnoughBalance error if the account does not have enough balance of the settlement token.

  • Parameters:

    NameTypeDescription
    settlementTokenaddressThe address of the settlement token used in the position.
    vaultaddressThe address of the vault contract.
    marginRequireduint256The amount of margin required for the position.
    bytes

claimPositionCallback

function claimPositionCallback(struct Position position, struct ClaimPositionInfo claimInfo, bytes) external

Callback function called after claiming a position.

  • Parameters:

    NameTypeDescription
    positionstruct PositionThe claimed position.
    claimInfostruct ClaimPositionInfoThe pnl related information of the claim
    bytes