Skip to main content

Storage

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.

Contract State

#[ink::storage_item]
#[derive(Debug)]
pub struct InvariantConfig {
pub admin: AccountId,
pub protocol_fee: Percentage,
}
NameTypeDescription
adminAccountIdAccount address of protocol admin. Admin is able to change fee, claim protocol fee or set the fee receiver, but cannot interfere with user positions or deposits and cannot close the pool.
protocol_feePercentagePercentage of the fee collected upon every swap in the pool that goes to the protocol, rest goes to LP.

FeeTier

#[derive(scale::Decode, scale::Encode)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
)]
pub struct FeeTier {
pub fee: Percentage,
pub tick_spacing: u16,
}
NameTypeDescription
feePercentagePercentage of the fee collected upon every swap in the pool.
tick_spacingu16The spacing between usable ticks.

PoolKey

#[derive(scale::Decode, scale::Encode, Debug, Copy, Clone, PartialEq)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
)]
pub struct PoolKey {
pub token_x: AccountId,
pub token_y: AccountId,
pub fee_tier: FeeTier,
}
NameTypeDescription
token_xAccountIdThe address of x token.
token_yAccountIdThe address of y token.
fee_tierFeeTierFeeTier associated with pool.

Pool

#[derive(scale::Decode, scale::Encode)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
)]
pub struct Pool {
pub liquidity: Liquidity,
pub sqrt_price: SqrtPrice,
pub current_tick_index: i32,
pub fee_growth_global_x: FeeGrowth,
pub fee_growth_global_y: FeeGrowth,
pub fee_protocol_token_x: TokenAmount,
pub fee_protocol_token_y: TokenAmount,
pub start_timestamp: u64,
pub last_timestamp: u64,
pub fee_receiver: AccountId,
pub seconds_per_liquidity_global: SecondsPerLiquidity,
}
NameTypeDescription
liquidityLiquidityAmount of virtual liquidity on pool. The difference between virtual and actual liquidity reflect the increased capital efficiency in Invariant.
sqrt_priceSqrtPriceSquare root of current price.
current_tick_indexi32The nearest tick below the current price.
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_xTokenAmountAmount of protocol tokens accumulated in x token that are available to claim.
fee_protocol_token_yTokenAmountAmount of protocol tokens accumulated in y token that are available to claim.
start_timestampu64Time of pool initialization.
last_timestampu64Last update of pool.
fee_receiverAccountIdAddress of entity enabling to claim protocol fee. By default it's admin but can be change for specific pool.
seconds_per_liquidity_globalSecondsPerLiquidityThe amount of time the pool existed divided by pools liquidity

Position

#[derive(scale::Decode, scale::Encode)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
)]
pub struct Position {
pub pool_key: PoolKey,
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 last_block_number: u64,
pub tokens_owed_x: TokenAmount,
pub tokens_owed_y: TokenAmount,
pub seconds_per_liquidity_inside: SecondsPerLiquidity,
pub created_at: u64,
}
NameTypeDescription
pool_keyPoolKeyPool key indicating on which Pool the position has been opened.
liquidityLiquidityAmount of immutable 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_block_numberu64Last update of position expressed in block number.
tokens_owed_xTokenAmountThe quantity of x tokens collected in fees that is available for claiming.
tokens_owed_yTokenAmountThe 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
created_atu64Creation timestamp.

Tick

#[derive(scale::Decode, scale::Encode)]
#[cfg_attr(
feature = "std",
derive(scale_info::TypeInfo, ink::storage::traits::StorageLayout)
)]
pub struct Tick {
pub index: i32,
pub sign: bool,
pub liquidity_change: Liquidity,
pub liquidity_gross: Liquidity,
pub sqrt_price: SqrtPrice,
pub fee_growth_outside_x: FeeGrowth,
pub fee_growth_outside_y: FeeGrowth,
pub seconds_outside: u64,
pub seconds_per_liquidity_outside: SecondsPerLiquidity,
}
NameTypeDescription
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_priceSqrtPriceSquare 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_outsideu64Seconds outside-range.
seconds_per_liquidity_outsideSecondsPerLiquidityAmount of time that the price was past the index divided by pools liquidity.