Skip to main content

Close Position

Overview

Closing a position in the Chromatic Protocol consists of two stages: "Close Position" and "Claim Position." The first stage involves initiating the closure of a position, while the second stage allows users to claim the position and receive the associated settlement tokens.

ChromaticRouter Interface

The IChromaticRouter interface serves as the primary interface for interacting with the ChromaticRouter contract. It provides access to the following functions for closing and claiming positions:

interface IChromaticRouter {
/**
* @notice Closes a position in a ChromaticMarket contract.
* @param market The address of the ChromaticMarket contract.
* @param positionId The ID of the position to close.
*/
function closePosition(address market, uint256 positionId) external;

/**
* @notice Claims a position from a ChromaticMarket contract.
* @param market The address of the ChromaticMarket contract.
* @param positionId The ID of the position to claim.
*/
function claimPosition(address market, uint256 positionId) external;
}

Closing a Position

The first stage, "Close Position," involves initiating the closure of a position within a specific ChromaticMarket contract. To close a position, users need to call the closePosition function of the ChromaticRouter contract, providing the address of the target ChromaticMarket contract and the ID of the position to be closed.

Upon closing a position, the current oracle version is stored in the position's closeVersion field. This information is saved to track the version at which the position was closed.

Claiming a Position

The second stage, "Claim Position," allows users to claim the settlement tokens associated with a closed position from the ChromaticMarket contract. To claim a position, users need to call the claimPosition function of the ChromaticRouter contract, providing the address of the target ChromaticMarket contract and the ID of the position to be claimed.

The claimPosition function can only be called after the next oracle version updates the price feed, which determines the exit price for the closed position.

Profit and Loss Calculation

Upon claiming a position, the Chromatic Protocol calculates the profit and loss (PnL) based on the difference between the entry price and the exit price. If the PnL is positive, indicating a profit, the user receives the profit amount added from the maker margin. Conversely, if the PnL is negative, indicating a loss, the taker margin balance is used to cover the loss.

Usage

To close a position, users should follow these steps:

  1. Call the closePosition function of the ChromaticRouter contract, providing the address of the target ChromaticMarket contract and the ID of the position to be closed.

To claim a closed position, users should follow these steps:

  1. Wait for the next oracle version to update the price feed.
  2. Call the claimPosition function of the ChromaticRouter contract, providing the address of the target ChromaticMarket contract and the ID of the position to be claimed.
  3. Check the account balance or perform further actions with the claimed settlement tokens.

Please note that the claimPosition function can only be called after the position has been closed at the closeVersion and the subsequent oracle version updates the price feed.