Skip to main content

Structs

This section provides an in-depth exploration of key data structures integral to the Invariant protocol's storage mechanism. These structs are specifically crafted to facilitate the sharing of the state of the exchange within the CLAMM model. These data structures play a pivotal role in maintaining and organizing information related to the exchange, ensuring efficient and organized handling of data.

State

#[account(zero_copy(unsafe))]
#[repr(packed)]
#[derive(PartialEq, Default, Debug, InitSpace)]
pub struct State {
pub admin: Pubkey,
pub nonce: u8,
pub authority: Pubkey,
pub bump: u8,
}
NameTypeDescription
adminPubkeyAddress of the admin account.
nonceu8Authority address bump.
authorityPubkeyAddress of the authority account.
bumpu8State address bump

FeeTier

Struct containing information about the fee and tick spacing

#[account(zero_copy(unsafe))]
#[repr(packed)]
#[derive(PartialEq, Default, Debug, InitSpace)]
pub struct FeeTier {
pub fee: FixedPoint,
pub tick_spacing: u16,
pub bump: u8,
}
NameTypeDescription
feeFixedPointPercentage of the fee collected upon every swap in the pool.
tick_spacingu16The spacing between usable ticks.
bumpu8Address bump

Pool

#[account(zero_copy(unsafe))]
#[derive(PartialEq, Default, Debug, InitSpace)]
pub struct Pool {
pub token_x: Pubkey,
pub token_y: Pubkey,
pub token_x_reserve: Pubkey,
pub token_y_reserve: Pubkey,
pub position_iterator: u128,
pub tick_spacing: u16,
pub fee: FixedPoint,
pub protocol_fee: FixedPoint,
pub liquidity: Liquidity,
pub sqrt_price: Price,
pub current_tick_index: i32,
pub tickmap: Pubkey,
pub fee_growth_global_x: FeeGrowth,
pub fee_growth_global_y: FeeGrowth,
pub fee_protocol_token_x: u64,
pub fee_protocol_token_y: u64,
pub seconds_per_liquidity_global: SecondsPerLiquidity,
pub start_timestamp: u64,
pub last_timestamp: u64,
pub fee_receiver: Pubkey,
pub oracle_address: Pubkey,
pub oracle_initialized: bool,
pub bump: u8,
}
NameTypeDescription
token_xPubkeyToken X address
token_yPubkeyToken Y address
token_y_reservePubkeyPools associated token account for the token X
token_y_reservePubkeyPools associated token account for the token Y
position_iteratoru128Iterator determining the id of the position relative to all positions that were created for the pool
tick_spacingu16The spacing between usable ticks.
feeFixedPointPercentage of the fee collected upon every swap in the pool.
protocol_feeFixedPointPercentage of the collected fees that will be deducted by the protocol
liquidityLiquidityAmount of virtual liquidity on pool. The difference between virtual and actual liquidity reflect the increased capital efficiency in Invariant.
sqrt_pricePriceSquare root of current price.
current_tick_indexi32The nearest tick below the current price.
tickmapPubkeyAddress of the tickmap associated with the pool
fee_growth_global_xFeeGrowthAmount of fees accumulated in x token in per one integer unit of Liquidity since pool initialization.
fee_growth_global_yFeeGrowthAmount of fees accumulated in y token in per one integer unit of Liquidity since pool initialization.
fee_protocol_token_xu64Amount of protocol tokens accumulated in x token that are available to claim.
fee_protocol_token_yu64Amount of protocol tokens accumulated in y token that are available to claim.
seconds_per_liquidity_globalSecondsPerLiquidityThe amount of time the pool existed divided by pools liquidity
start_timestampu64Time of pool initialization.
last_timestampu64Last update of pool.
fee_receiverPubkeyAddress of account allowed to claim protocol fee. By default it's admin but can be changed for specific pool.
oracle_addressPubkeyAddress of the oracle (set to Pubkey::default() if not initialized)
oracle_initializedboolFlag specifying if oracle was initialized
bumpu8Address bump

PositionList

#[account(zero_copy(unsafe))]
#[repr(packed)]
#[derive(PartialEq, Default, Debug, InitSpace)]
pub struct PositionList {
pub head: u32,
pub bump: u8,
}
NameTypesDescription
headu32Head of the position list
bumpu8Address bump

Position

#[account(zero_copy(unsafe))]
#[repr(packed)]
#[derive(PartialEq, Default, Debug, InitSpace)]
pub struct Position {
pub owner: Pubkey,
pub pool: Pubkey,
pub id: u128,
pub liquidity: Liquidity,
pub lower_tick_index: i32,
pub upper_tick_index: i32,
pub fee_growth_inside_x: FeeGrowth,
pub fee_growth_inside_y: FeeGrowth,
pub seconds_per_liquidity_inside: SecondsPerLiquidity,
pub last_slot: u64,
pub tokens_owed_x: FixedPoint,
pub tokens_owed_y: FixedPoint,
pub bump: u8,
}
NameTypeDescription
ownerPubkeyPosition owner's account key.
poolPubkeyPool account's key.
idu128Id relative to all positions created for the pool.
liquidityLiquidityAmount of virtual liquidity that the position represents.
lower_tick_indexi32Lower tick index of the Position.
upper_tick_indexi32Upper tick index of the Position.
fee_growth_inside_xFeeGrowthAmount of fees accumulated in x token per one integer unit of Liquidity in-range. It is used to determine the shares of collected fees.
fee_growth_inside_yFeeGrowthAmount of fees accumulated in y token per one integer unit of Liquidity in-range. It is used to determine the shares of collected fees.
last_slotu64Last update of position expressed in block number.
tokens_owed_xFixedPointThe quantity of x tokens collected in fees that is available for claiming.
tokens_owed_yFixedPointThe quantity of y tokens collected in fees that is available for claiming.
seconds_per_liquidity_insideSecondsPerLiquidityAmount of time that the price was within liquidity range of the position divided by positions liquidity
bumpu8Address bump

Tickmap

#[account(zero_copy(unsafe))]
#[repr(packed)]
pub struct Tickmap {
pub bitmap: [u8; 11091]
}
NameTypeDescription
bitmap[u8;11091]Array containing active bytes containing states of active ticks

Tick

#[account(zero_copy(unsafe))]
#[repr(packed)]
#[derive(PartialEq, Default, Debug, InitSpace)]
pub struct Tick {
pub pool: Pubkey,
pub index: i32,
pub sign: bool, // true means positive
pub liquidity_change: Liquidity,
pub liquidity_gross: Liquidity,
pub sqrt_price: Price,
pub fee_growth_outside_x: FeeGrowth,
pub fee_growth_outside_y: FeeGrowth,
pub seconds_per_liquidity_outside: SecondsPerLiquidity,
pub seconds_outside: u64,
pub bump: u8,
}
NameTypeDescription
poolPubkeyAddress of the pool the tick belongs to.
indexi32Index of tick.
signboolDetermine if the liquidity will be added or subtracted on cross.
liquidity_changeLiquidityAmount of virtual liquidity to adjust while. crossing.
liquidity_grossLiquidityAmount of virtual liquidity to be added on the tick, excluding liquidity taken on that tick. It is used to impose the maximum liquidity that can be place on a single tick.
sqrt_pricePriceSquare root of tick price.
fee_growth_outside_xFeeGrowthAmount of Fees accumulated in x token outside-range.
fee_growth_outside_yFeeGrowthAmount of Fees accumulated in y token outside-range.
seconds_per_liquidity_outsideSecondsPerLiquidityAmount of time that the price was past the index divided by pools liquidity.
seconds_outsideu64Seconds outside-range.
bumpu8Tick address bump

Oracle & Record

#[account(zero_copy(unsafe))]
#[repr(packed)]
pub struct Oracle {
pub data: [Record; 256],
pub head: u16,
pub amount: u16,
pub size: u16,
}
NameTypeDescription
data[Record; 256]Recorded price data with timestamps
headu16Last updated record
amountu16Amount of records
sizeu16Max amount of records (256)
#[zero_copy(unsafe)]
#[repr(packed)]
pub struct Record {
pub timestamp: u64,
pub price: Price,
}
NameTypeDescription
timestampu64Timestamp of the price
pricePriceSquare root of price at given timestamp