Close Position
Overview
This documentation will guide you through the process of closing a position when trading on the Chromatic Protocol.
Prerequisites
Before closing a position, ensure that you have completed the following steps:
- Set up the Chromatic Protocol SDK by following the installation instructions.
- Create an account using the SDK, as account creation is required before trading. Refer to the "Create an Account" section for more information.
- Open a position on the Chromatic Protocol. Refer to the "Open Position" section for more information.
Closing a Position
Closing a position in the Chromatic Protocol involves two stages: "Close Position" and "Claim Position". The "Close Position" stage allows you to close your open position, while the "Claim Position" stage allows you to claim the funds associated with the closed position.
To close a position, use the closePosition
method provided by the ChromaticRouter
instance. This method requires the market address and the position ID of the position you want to close.
- ethers-v5
- ethers-v6
- viem
import { Client } from "@chromatic-protocol/sdk-ethers-v5"
const client: Client = ... // Replace with your instantiated Client object
const marketAddress: string = ... // Replace with the address of the market containing the position
const positionId: number = ... // Replace with the ID of the position you want to close
await client.router().closePosition(marketAddress, positionId)
import { Client } from "@chromatic-protocol/sdk-ethers-v6"
const client: Client = ... // Replace with your instantiated Client object
const marketAddress: string = ... // Replace with the address of the market containing the position
const positionId: number = ... // Replace with the ID of the position you want to close
await client.router().closePosition(marketAddress, positionId)
import { Client } from "@chromatic-protocol/sdk-viem"
const client: Client = ... // Replace with your instantiated Client object
const marketAddress: string = ... // Replace with the address of the market containing the position
const positionId: number = ... // Replace with the ID of the position you want to close
await client.router().closePosition(marketAddress, positionId)
Replace the placeholders (client
, marketAddress
, positionId
) with your actual values.
Claiming a Position
After closing a position using the "Close Position" stage, you need to claim the funds associated with the closed position using the claimPosition
method provided by the ChromaticRouter
instance. This method also requires the market address and the position ID.
await client.router().claimPosition(marketAddress, positionId)
Make sure to use the same marketAddress
and positionId
that you used to close the position.
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.