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.

Invariant Config

struct InvariantConfig {  
admin: Address,
mut protocolFee: U256
}
NameTypeDescription
adminAddressAccount 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.
protocolFeeU256Percentage of the fee collected upon every swap in the pool that goes to the protocol, the rest goes to LP.

FeeTier

struct FeeTier {
mut fee: U256,
mut tickSpacing: U256
}
NameTypeDescription
feeU256Percentage of the fee collected upon every swap in the pool.
tickSpacingU256The spacing between usable ticks.

PoolKey

struct PoolKey {
mut tokenX: ByteVec,
mut tokenY: ByteVec,
mut feeTier: FeeTier
}
NameTypeDescription
tokenXByteVecThe contract id of x token.
tokenYByteVecThe contract id of y token.
feeTierFeeTierFeeTier associated with the pool.

Pool

struct Pool {
mut poolKey: PoolKey,
mut liquidity: U256,
mut sqrtPrice: U256,
mut currentTickIndex: I256,
mut feeGrowthGlobalX: U256,
mut feeGrowthGlobalY: U256,
mut feeProtocolTokenX: U256,
mut feeProtocolTokenY: U256,
mut startTimestamp: U256,
mut lastTimestamp: U256,
mut feeReceiver: Address,
mut reserveX: ByteVec,
mut reserveY: ByteVec
}
NameTypeDescription
poolKeyPoolKeyPoolKey associated with the pool.
liquidityU256Amount of virtual liquidity on pool. The difference between virtual and actual liquidity reflect the increased capital efficiency in Invariant.
sqrtPriceU256Square root of current price.
currentTickIndexI256The nearest tick below the current price, aligned with tick spacing.
feeGrowthGlobalXU256Amount of fees accumulated in x token in per one integer unit of Liquidity since pool initialization.
feeGrowthGlobalYU256Amount of fees accumulated in y token in per one integer unit of Liquidity since pool initialization.
feeProtocolTokenXU256Amount of protocol tokens accumulated in x token that are available to claim.
feeProtocolTokenYU256Amount of protocol tokens accumulated in y token that are available to claim.
startTimestampU256Time of pool initialization.
lastTimestampU256Last update of pool.
feeReceiverAddressAddress of entity enabling to claim protocol fee. By default it is admin but can be change for specific pool.
reserveXByteVecContractId of the Reserve hosting the first token type.
reserveYByteVecContractId of the Reserve hosting the second token type.

Position

struct Position {
mut poolKey: PoolKey,
mut liquidity: U256,
mut lowerTickIndex: I256,
mut upperTickIndex: I256,
mut feeGrowthInsideX: U256,
mut feeGrowthInsideY: U256,
mut lastBlockNumber: U256,
mut tokensOwedX: U256,
mut tokensOwedY: U256,
mut owner: Address
}
NameTypeDescription
poolKeyU256Pool key identifying on which Pool the position has been opened.
liquidityU256Amount of immutable virtual liquidity that the position represents.
lowerTickIndexI256Lower tick index of the Position.
upperTickIndexI256Upper tick index of the Position.
feeGrowthInsideXU256Amount of fees accumulated in x token per one integer unit of Liquidity in-range. It is used to determine the shares of collected fees. The feeGrowthInsideX value does not directly indicate the amount of fees accumulated; instead, it serves as a snapshot of the counter used to calculate the fees enabled to claim from position.
feeGrowthInsideYU256Amount of fees accumulated in y token per one integer unit of Liquidity in-range. It is used to determine the shares of collected fees. The feeGrowthInsideY value does not directly indicate the amount of fees accumulated; instead, it serves as a snapshot of the counter used to calculate the fees enabled to claim from position.
lastBlockNumberU256Last update of position expressed in block number.
tokensOwedXU256The quantity of x tokens collected in fees that is available for claiming. It typically equals zero because converting FeeGrowth into a TokenAmount nomination also triggers immediate claiming of the tokens.
tokensOwedYU256The quantity of y tokens collected in fees that is available for claiming. It typically equals zero because converting FeeGrowth into a TokenAmount nomination also triggers immediate claiming of the tokens.
ownerAddressThe owner of the position.

Tick

struct Tick {
mut sign: Bool,
mut index: I256,
mut liquidityChange: U256,
mut liquidityGross: U256,
mut sqrtPrice: U256,
mut feeGrowthOutsideX: U256,
mut feeGrowthOutsideY: U256,
mut secondsOutside: U256
}
NameTypeDescription
signboolDetermine if the liquidity will be added or subtracted on cross.
indexI256Index of tick.
liquidityChangeU256The amount of virtual liquidity that will be added/removed from the current liquidity in the pool at the time of crossing this tick.
liquidityGrossU256Amount 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.
sqrtPriceU256Square root of tick price with less precision than the usual current price.
feeGrowthOutsideXU256Amount of Fees accumulated in x token outside-range. This value does not indicate the amount of fee collected outside of the tick; it should be interpreted relative to other FeeGrowth values.
feeGrowthOutsideYU256Amount of Fees accumulated in y token outside-range. This value does not indicate the amount of fee collected outside of the tick; it should be interpreted relative to other FeeGrowth values.
secondsOutsideU256Seconds outside-range.

TickmapBatch

struct TickmapBatch {
mut chunks: [U256; 94]
}
NameTypeDescription
chunks[U256;94]Chunks stored in a single TickmapBatch.

Reserve

Contract Reserve(invariant: Address, mut assetsStored: U256) {...}
NameTypeDescription
invariantAddressAddress of the Invariant contract.
assetsStoredU256Count of tokens currently stored in the Reserve.