Docs · Reference
Program: accounts
Every public account of the mesa program with its seeds, size and byte layout, the four note states, the constants and packed codes a reader needs, and how batch times, drand rounds and heartbeat deadlines are computed.
The mesa program keeps seven kinds of public account and four kinds of note. This page gives each account's address, size and fields in byte order, each note's fields, and the arithmetic that ties them to the clock.
Encoding and units
Every account starts with an 8-byte ASCII discriminator and is written field by field with no padding. Integers are little-endian. A 32-byte address or mint is stored as its raw bytes. Offsets in the tables below are in bytes from the start of the account data.
| Constant | Value | Used for |
|---|---|---|
PRICE_SCALE | 1_000_000_000_000 (10^12) | Prices: quote atoms per base atom, times PRICE_SCALE |
WAD | 1_000_000_000_000_000_000 (10^18) | Share prices, the borrow index, fill fractions, Pool.borrows |
BPS_DENOM | 10_000 | Every field ending in _bps |
FUNDING_BIAS | 2^62 | The value a perp market's funding index starts at |
MAX_BATCH_TARGETS | 8 | Target records in one Batch |
MAX_POOL_COLLATERAL | 4 | Collateral slots in one Pool |
Amounts are integers in the mint's smallest unit, called atoms here. Times are Unix seconds.
A price of 150.00 USDC per SOL, with 6 and 9 decimals, is 150 × 10^6 / 10^9 × 10^12 = 150_000_000_000.
Addresses
Every account is a program-derived address of the mesa program. The program id is listed under Addresses.
| Account | Seeds | Size in bytes | Discriminator |
|---|---|---|---|
| Authority | "authority" | no data | none |
Config | "config" | 246 | MESACFG1 |
Market | "market", id as u16 LE | 120 | MESAMKT1 |
Vault | "vault", id as u16 LE | 126 | MESAVLT1 |
Pool | "pool", id as u16 LE | 393 | MESAPOL1 |
PerpMarket | "perp", id as u16 LE | 148 | MESAPRP1 |
Batch | "batch", n as u64 LE | 891 | MESABCH1 |
Action | "action", n as u64 LE, index as u32 LE | 259 | MESAACT1 |
Ids are assigned in order of creation from the counters in Config. Markets, pools and perp markets each count from 0. Vaults share one counter: a spot market's vault and a perp market's vault each take the next vault id.
The authority and its token accounts
The authority owns no data. It is the owner of every Mesa note in the Privacy Rings pool and the token authority of Mesa's public funds. For each mint Mesa holds, the funds sit in the authority's associated token account for that mint under the SPL Token program. There is one such account per mint, shared by every market, vault and pool that uses the mint. The reserves, cash and fee counters in the accounts below say how much of it belongs to each.
Config
One account. It holds the clock, the fee split and the external programs every instruction reads.
| Field | Type | Offset | Meaning |
|---|---|---|---|
| discriminator | [u8; 8] | 0 | MESACFG1 |
admin | [u8; 32] | 8 | The address that may change Config and create markets, pools and perp markets |
authority_bump | u8 | 40 | Bump of the authority address |
paused | u8 | 41 | 0 or 1. While 1, deposit and place of an order, a supply, a borrow, a perp open or a vault deposit are refused. Everything else runs. |
protocol_share_bps | u16 | 42 | The protocol's part of each spot and perp fee, in bps of the fee |
max_actions_per_market | u16 | 44 | The most actions one target can have opened in one batch |
market_count | u16 | 46 | Spot markets created. The next market id. |
vault_count | u16 | 48 | Vaults created. The next vault id. |
pool_count | u16 | 50 | Pools created. The next pool id. |
perp_count | u16 | 52 | Perp markets created. The next perp market id. |
fee_recipient | [u8; 32] | 54 | The wallet whose associated token accounts receive sweep_fees |
pyth_receiver | [u8; 32] | 86 | The program that must own every oracle account. See Oracle accounts. |
batch_len | u64 | 118 | Length of a batch, in seconds |
open_window | u64 | 126 | Seconds after a batch closes during which its actions can be opened |
place_margin | u64 | 134 | Seconds before the close at which a batch stops taking actions. Less than batch_len. |
heartbeat | u64 | 142 | Length of a heartbeat epoch, in seconds |
max_oracle_age | u64 | 150 | The most seconds an oracle price's publish time may differ from the clock |
genesis_ts | i64 | 158 | The time batch 0 and heartbeat epoch 0 begin |
drand_genesis | u64 | 166 | Genesis time of the drand beacon |
drand_period | u64 | 174 | Seconds between drand rounds |
drand_chain_hash | [u8; 32] | 182 | Chain hash of the drand beacon the batches are sealed to |
vk_set_id | [u8; 32] | 214 | Identifier of the verifying-key set |
Market
One per spot market.
| Field | Type | Offset | Meaning |
|---|---|---|---|
| discriminator | [u8; 8] | 0 | MESAMKT1 |
id | u16 | 8 | Market id |
vault_id | u16 | 10 | Id of the market's liquidity vault |
fee_bps | u16 | 12 | Fee taken from the output of each fill |
paused | u8 | 14 | 0 or 1. While 1, orders for the market open as Invalid and are reclaimed. |
bump | u8 | 15 | Bump of this address |
base_mint | [u8; 32] | 16 | Mint of the base asset |
quote_mint | [u8; 32] | 48 | Mint of the quote asset |
last_clearing_price | u64 | 80 | Clearing price of the last batch cleared for this market, at PRICE_SCALE. At creation, the price of the vault's initial reserves. |
cumulative_base_volume | u128 | 88 | Sum of matched_base over every cleared batch, in base atoms |
cumulative_quote_volume | u128 | 104 | Sum of matched_quote over every cleared batch, in quote atoms |
Vault
One per spot market and one per perp market. kind says which.
| Field | Type | Offset | Meaning |
|---|---|---|---|
| discriminator | [u8; 8] | 0 | MESAVLT1 |
id | u16 | 8 | Vault id |
market_id | u16 | 10 | Id of the spot market (kind 0) or of the perp market (kind 1) the vault backs |
kind | u8 | 12 | 0 spot liquidity, 1 perps |
bump | u8 | 13 | Bump of this address |
base_mint | [u8; 32] | 14 | Base mint. All zero for a perps vault. |
quote_mint | [u8; 32] | 46 | Quote mint |
base_reserve | u64 | 78 | Base atoms that belong to the vault's shares. 0 for a perps vault. |
quote_reserve | u64 | 86 | Quote atoms that belong to the vault's shares |
total_shares | u64 | 94 | Shares outstanding |
admin_seed_shares | u64 | 102 | Shares minted for the reserves the vault was created with, less any the admin has withdrawn |
protocol_fees_base | u64 | 110 | Base atoms of protocol fee owed, outside base_reserve, until sweep_fees pays them out |
protocol_fees_quote | u64 | 118 | Quote atoms of protocol fee owed, outside quote_reserve, until sweep_fees pays them out |
A spot vault is created with total_shares = isqrt(initial_base · initial_quote). A perps vault is created with one share per quote atom. A spot vault's own price is quote_reserve · PRICE_SCALE / base_reserve.
Pool
One per lending pool.
| Field | Type | Offset | Meaning |
|---|---|---|---|
| discriminator | [u8; 8] | 0 | MESAPOL1 |
id | u16 | 8 | Pool id |
bump | u8 | 10 | Bump of this address |
collateral_count | u8 | 11 | Collateral slots in use, 1 to 4 |
decimals | u8 | 12 | Decimals of the pool's mint |
mint | [u8; 32] | 13 | The asset supplied and borrowed |
cash | u64 | 45 | Atoms the pool holds that are not lent out. Includes reserves. |
borrows | u128 | 53 | Total debt with interest, in atoms times WAD |
borrow_index | u128 | 69 | Cumulative borrow index. Starts at WAD and grows with interest. Never above u64::MAX. |
supply_shares | u64 | 85 | Shares outstanding |
admin_seed_shares | u64 | 93 | Shares minted for the cash the pool was created with, less any the admin has withdrawn |
reserves | u64 | 101 | The protocol's part of accrued interest, in atoms, until sweep_fees pays it out |
base_rate_bps | u16 | 109 | Annual borrow rate at zero utilization |
slope1_bps | u16 | 111 | Annual rate added between zero utilization and the kink |
slope2_bps | u16 | 113 | Annual rate added between the kink and full utilization |
kink_bps | u16 | 115 | The utilization at which the curve turns |
collateral_factor_bps | u16 | 117 | The most debt a new borrow may take, as a part of its collateral's value |
liq_threshold_bps | u16 | 119 | The part of its collateral's value a debt must stay within to renew |
liq_bonus_bps | u16 | 121 | The liquidator's bonus |
reserve_factor_bps | u16 | 123 | The part of accrued interest added to reserves |
last_accrual | i64 | 125 | Time interest was last accrued |
collateral_mints | [[u8; 32]; 4] | 133 | Mint of each collateral slot |
collateral_oracles | [[u8; 32]; 4] | 261 | Pyth feed id of each collateral slot |
collateral_decimals | [u8; 4] | 389 | Decimals of each collateral mint |
Only the first collateral_count slots are read. An instruction names a collateral by its slot index.
A pool accrues interest when it clears a batch. With borrowed = borrows / WAD:
utilization = borrowed / (cash + borrowed)
rate = base_rate + slope1 · utilization / kink utilization ≤ kink
rate = base_rate + slope1 + slope2 · (utilization − kink) / (1 − kink) utilization > kink
factor = rate · elapsed_seconds / 31_536_000
borrows = borrows + borrows · factor
borrow_index = borrow_index + borrow_index · factor
reserves = reserves + interest · reserve_factor_bps / 10_000
share price = (cash + borrowed − reserves) · WAD / supply_sharesThe share price is WAD while supply_shares is 0. A borrow of principal taken at index entry owes ceil(principal · borrow_index / entry).
PerpMarket
One per perp market.
| Field | Type | Offset | Meaning |
|---|---|---|---|
| discriminator | [u8; 8] | 0 | MESAPRP1 |
id | u16 | 8 | Perp market id |
vault_id | u16 | 10 | Id of the perps vault that is the market's counterparty |
max_leverage | u16 | 12 | The most size a new position may take, as a multiple of its collateral: collateral · max_leverage ≥ size · price |
maint_margin_bps | u16 | 14 | The margin a position must keep, in bps of its size at the oracle price |
liq_bonus_bps | u16 | 16 | The liquidator's bonus |
open_fee_bps | u16 | 18 | Fee on the position's size at its entry price |
close_fee_bps | u16 | 20 | Fee on the position's size at its closing price |
funding_rate_bps_per_hour | u16 | 22 | The hourly funding rate at full imbalance. See The funding index. |
bump | u8 | 24 | Bump of this address |
paused | u8 | 25 | 0 or 1. While 1, place of a perp open is refused. Closes, renewals and clears run. |
base_decimals | u8 | 26 | Decimals of the base asset |
quote_decimals | u8 | 27 | Decimals of the quote mint |
base_oracle | [u8; 32] | 28 | Pyth feed id of the base asset's price |
quote_mint | [u8; 32] | 60 | The mint of collateral, fees and settlement |
open_interest_long | u64 | 92 | Sum of long sizes, in base atoms |
open_interest_short | u64 | 100 | Sum of short sizes, in base atoms |
funding_index | u128 | 108 | Cumulative funding per base atom at PRICE_SCALE, offset by FUNDING_BIAS. Never above u64::MAX. |
max_oi | u64 | 124 | The most open interest each side may reach, in base atoms |
last_funding_ts | i64 | 132 | Time funding was last accrued |
last_oracle_price | u64 | 140 | The oracle price read at the last clear, at PRICE_SCALE |
Batch
One per batch that has at least one action. The first place in batch n creates it.
| Field | Type | Offset | Meaning |
|---|---|---|---|
| discriminator | [u8; 8] | 0 | MESABCH1 |
n | u64 | 8 | Batch number |
close_ts | i64 | 16 | Time the batch closes |
round | u64 | 24 | The drand round the batch is sealed to |
state | u8 | 32 | See Batch states |
bump | u8 | 33 | Bump of this address |
target_count | u8 | 34 | Target records in use, 0 to 8 |
action_count | u32 | 35 | Actions placed. The index the next action gets. |
opened_count | u32 | 39 | Actions recorded as Opened |
invalid_count | u32 | 43 | Actions recorded as Invalid |
settled_count | u32 | 47 | Opened actions that are finished: settled, or refused and then closed |
targets | [BatchTargetRecord; 8] | 51 | One record per market, pool, perp market or vault the batch touched, 105 bytes each |
n, close_ts and round follow from Batch time.
Target record
Record i starts at offset 51 + 105 · i. Only the first target_count records are in use. A record is added when the first action for its target opens, or when clear runs for a target that has none. Offsets below are from the start of the record.
| Field | Type | Offset | Meaning |
|---|---|---|---|
target_kind | u8 | 0 | 1 spot market, 2 pool, 3 perp market, 4 vault |
cleared | u8 | 1 | 0 until clear has run for this target, then 1 |
target_id | u16 | 2 | Id of the market, pool, perp market or vault |
opened_count | u32 | 4 | Actions opened for this target. clear requires exactly this many action accounts. |
clearing_price | u64 | 8 | Depends on the target kind, below |
matched_base | u64 | 16 | Depends on the target kind, below |
matched_quote | u64 | 24 | Depends on the target kind, below |
vault_base_delta | i64 | 32 | Spot market: change of the vault's base_reserve in this clear, signed |
vault_quote_delta | i64 | 40 | Spot market: change of the vault's quote_reserve in this clear, signed |
partial_fill_wad | u128 | 48 | Spot market: the fraction, at WAD, filled of each order whose limit equals the clearing price on partial_fill_side |
partial_fill_side | u8 | 64 | Spot market: 0 buys are rationed, 1 sells are rationed, 255 neither. 255 for every other target kind. |
share_price_wad | u128 | 65 | Pool: the share price after accrual, at WAD |
borrow_index_wad | u128 | 81 | Depends on the target kind, below |
pool_net_cash | i64 | 97 | Pool: supplies, less redeems paid, less borrows paid, in atoms |
The fields that depend on the target kind:
| Field | Spot market | Pool | Perp market | Vault |
|---|---|---|---|---|
clearing_price | The clearing price, at PRICE_SCALE | 0 | The oracle price read at the clear, at PRICE_SCALE | 0 |
matched_base | Base atoms traded | 0 | 0 | The vault's base_reserve before this batch's deposits and withdrawals |
matched_quote | Quote atoms traded | 0 | 0 | The vault's quote_reserve before this batch's deposits and withdrawals |
borrow_index_wad | 0 | The pool's borrow_index after accrual | The market's funding_index after accrual | The vault's total_shares before this batch's deposits and withdrawals |
settle computes every payout from these records and the action's opened fields:
- A spot order fills whole when its limit is strictly better than
clearing_priceor is 0, fillspartial_fill_wadof its amount when its limit equals the price and its side ispartial_fill_side, fills whole at the price on the other side, and fills nothing when its limit is worse. A buy receivesfilled · PRICE_SCALE / clearing_pricebase atoms and a sell receivesfilled · clearing_price / PRICE_SCALEquote atoms, each lessfee_bpsof that output. - A supply mints
amount · WAD / share_price_wadshares. A redeem paysshares · share_price_wad / WAD. - A perp close settles at
clearing_priceand the funding index inborrow_index_wad. - A vault deposit or withdrawal is priced against the reserves and total shares the record holds.
For a spot market, matched_base is the larger of the base the buys received before fees and the base the sells gave, and matched_quote is the larger of the quote the buys gave and the quote the sells received before fees. A market cleared with no orders records the vault's own price, zero volume, and a partial_fill_wad of WAD.
Batch states
| Value | Name | The batch is in it when |
|---|---|---|
0 | OPEN | It has been created and no action has been opened |
1 | OPENING | At least one action has been recorded as Opened or Invalid |
2 | CLEARING | At least one target has cleared |
3 | SETTLED | Every target record is cleared and settled_count equals opened_count |
The API reports its own states, open, sealed, opening, cleared and settled, read from these and the clock. sealed is an OPEN batch past close_ts. See Batches.
Action
One per action, created by place at index action_count of its batch.
| Field | Type | Offset | Meaning |
|---|---|---|---|
| discriminator | [u8; 8] | 0 | MESAACT1 |
batch | u64 | 8 | The batch number n |
index | u32 | 16 | Position of the action in its batch, from 0 |
state | u8 | 20 | See Action states |
bump | u8 | 21 | Bump of this address |
commitment | [u8; 32] | 22 | Hash of the action's fields, fixed at place |
rent_payer | [u8; 32] | 54 | The address that paid for the account and receives its lamports when it closes |
fields | 173 bytes | 86 | The opened fields. All zero while the action is Sealed. |
Opened fields
open writes these after checking that they hash to commitment.
| Field | Type | Offset | Meaning |
|---|---|---|---|
code | u32 | 86 | Kind, side and target id, packed. See Action code. |
batch | u64 | 90 | The batch the action was made for. An action whose value differs from the account's batch is Invalid. |
asset | [u8; 32] | 98 | Mint of the action note |
amount | u64 | 130 | By kind, below |
amount_b | u64 | 138 | By kind, below |
limit | u64 | 146 | By kind, below |
param | u64 | 154 | By kind, below |
recipient_owner_hash | [u8; 32] | 162 | Owner hash of the receive holder the action settles to |
recipient_view | [u8; 33] | 194 | Viewing public key of the receive holder: a tag byte, then 32 key bytes |
recipient_auth | [u8; 32] | 227 | The spend authorization every note settled to the recipient carries |
Fields by kind
| Kind | asset | amount | amount_b | limit | param |
|---|---|---|---|---|---|
1 order, buy | The market's quote mint | Quote atoms locked, the most the order spends | 0 | Limit price at PRICE_SCALE, 0 for a market order | 0 |
1 order, sell | The market's base mint | Base atoms locked | 0 | Limit price at PRICE_SCALE, 0 for a market order | 0 |
2 supply | The pool's mint | Atoms supplied | 0 | Unused | Unused |
3 redeem | The pool's mint | Shares redeemed | 0 | Unused | Unused |
4 borrow | The pool's mint | 0 | Principal borrowed, in atoms | Unused | The pool's borrow index when the action was placed |
5 perp open | The market's quote mint | 0 | Size, in base atoms | Unused | The oracle price when the action was placed |
6 perp close | The market's quote mint | The position's collateral, in quote atoms | The position's size, in base atoms | The position's entry funding index | The position's entry price |
7 vault deposit, spot | The vault's quote mint | Quote atoms locked | Base atoms locked | Unused | Unused |
7 vault deposit, perps | The vault's quote mint | Quote atoms locked | 0 | Unused | Unused |
8 vault withdraw | The vault's quote mint | Shares withdrawn | 0 | Unused | Unused |
An action whose fields do not fit its row, or that names a target that does not exist, is recorded as Invalid.
Action states
| Value | Name | Meaning |
|---|---|---|
0 | SEALED | Placed. The account holds the commitment and no fields. |
1 | OPENED | The fields are posted, hash to the commitment and can be executed |
2 | INVALID | The fields hash to the commitment and cannot be executed |
3 | REFUSED | Opened, and its target could not take it at the clear: a borrow or a redeem beyond the pool's cash, a perp open beyond max_oi, a perp close whose gain the perps vault cannot pay, or a vault withdrawal its reserves cannot pay |
4 | SETTLED | Paid out |
5 | RECLAIMED | Returned to its owner |
settle and reclaim close the account and send its lamports to rent_payer, and so does close_position for a refused action. An action that has settled or been reclaimed therefore has no account, and an account read from the chain is in one of states 0 to 3.
The commitment
The commitment is the hash of the action note's state. open recomputes it from the posted fields with Poseidon over twelve field elements:
Poseidon(
discriminator, // 0x90f169db4a88cbb0, the first 8 bytes of sha256("account:Action")
code, batch,
hash_bytes(asset),
amount, amount_b, limit, param,
recipient_owner_hash,
recipient_view[0], // the tag byte
hash_bytes(recipient_view[1..33]),
recipient_auth
)Each integer enters as a 32-byte big-endian value. recipient_owner_hash and recipient_auth enter as stored.
Packed codes
Action code
Action.code and Position.code pack three values into a u32:
code = kind | (side << 8) | (target_id << 16)
kind = code & 0xff
side = (code >> 8) & 0xff
target_id = (code >> 16) & 0xffff| Kind | Value | Target |
|---|---|---|
| Order | 1 | Spot market id |
| Supply | 2 | Pool id |
| Redeem | 3 | Pool id |
| Borrow | 4 | Pool id |
| Perp open | 5 | Perp market id |
| Perp close | 6 | Perp market id |
| Vault deposit | 7 | Vault id |
| Vault withdraw | 8 | Vault id |
| Side | Value | Meaning |
|---|---|---|
SIDE_BUY | 0 | Buy, or long |
SIDE_SELL | 1 | Sell, or short |
SIDE_NONE | 255 | No side |
An order and a perp carry side 0 or 1. A borrow carries 0. A supply and a redeem carry 0 or 255.
Target kinds
| Name | Value |
|---|---|
TARGET_SPOT_MARKET | 1 |
TARGET_POOL | 2 |
TARGET_PERP_MARKET | 3 |
TARGET_VAULT | 4 |
Share target
Share.target packs the kind of target and its id, in the opposite order to an action code:
target = (target_kind << 16) | target_idtarget_kind is 2 for a pool and 4 for a vault.
Note states
A Mesa note is an output in the Privacy Rings pool owned by the authority. The note's own asset and amount carry its value, and its data is one of four states. A state's fields are sealed with the note and are read by decrypting it. The tables give each state's fields in the order they are hashed and serialized. Field is a 32-byte field element.
Balance
The note's asset and amount are the balance.
| Field | Type | Meaning |
|---|---|---|
auth | Field | Spend authorization: Poseidon(spend key, nonce), with a fresh nonce per note |
Share
A claim on a pool or a vault. The note holds 0 of the pool's mint or the vault's quote mint.
| Field | Type | Meaning |
|---|---|---|
auth | Field | Spend authorization |
target | u32 | The pool or vault. See Share target. |
shares | u64 | Number of shares |
Action
A batch action. The note holds what the action locked: the amount for an order, a supply, a vault deposit or a perp close, and 0 for a redeem, a vault withdrawal, a borrow or a perp open. A spot vault deposit has two action notes under one state, one holding the base leg and one the quote leg.
| Field | Type | Meaning |
|---|---|---|
code | u32 | Kind, side and target id |
batch | u64 | The batch the action is placed into |
asset | [u8; 32] | Mint of the action note |
amount | u64 | As in Fields by kind |
amount_b | u64 | As in Fields by kind |
limit | u64 | As in Fields by kind |
param | u64 | As in Fields by kind |
recipient | UtxoOwner | The receive holder everything the action settles into is paid to |
recipient_view_tag | u8 | Byte 0 of the recipient's 33-byte viewing public key |
recipient_view | [u8; 32] | Bytes 1 to 32 of that key |
recipient_auth | Field | Spend authorization of every note settled to the recipient |
The Action account stores the same state in its opened form: recipient as its owner hash, and the two viewing-key fields as one 33-byte value.
Position
A borrow or a perp. The note's asset and amount are the collateral: one of the pool's collateral mints for a borrow, the market's quote mint for a perp.
| Field | Type | Meaning |
|---|---|---|
code | u32 | Kind 4 with side 0 and the pool id for a borrow. Kind 5 with the side and the perp market id for a perp. |
debt | u64 | Borrow: principal in atoms of the pool's mint, as of the index in entry. Perp: size in base atoms. |
entry | u64 | Borrow: the pool's borrow_index when the borrow was placed. Perp: the oracle price when the open was placed, at PRICE_SCALE. |
funding | u64 | Perp: the market's funding_index when the open was placed. Borrow: 0. |
deadline | u64 | The time the position must be renewed by. See Heartbeat deadlines. |
activation | Field | The commitment of the action that opened the position |
auth | Field | Spend authorization |
home | UtxoOwner | The account holder a liquidation's remainder is paid to |
home_view_tag | u8 | Byte 0 of the home holder's 33-byte viewing public key |
home_view | [u8; 32] | Bytes 1 to 32 of that key |
Batch time
place computes the batch from the clock. With now the cluster's Unix time:
n = floor((now − genesis_ts) / batch_len)
close_ts = genesis_ts + (n + 1) · batch_lenBatch n covers [genesis_ts + n · batch_len, close_ts). The same numbers give the Batch address for any time, before the account exists.
| Instruction | Runs when |
|---|---|
place | now ≤ close_ts − place_margin |
open | close_ts ≤ now < close_ts + open_window |
clear | now ≥ close_ts, and either opened_count + invalid_count = action_count or now ≥ close_ts + open_window |
settle | The action's target record has cleared = 1 |
reclaim | now ≥ close_ts + open_window, for an action that is Sealed or Invalid |
The drand round
A batch is sealed to the first drand round published at or after its close:
round = ceil((close_ts − drand_genesis) / drand_period) + 1The round is 1 when close_ts is at or before drand_genesis. It is computed once, when the Batch account is created, and stored in round.
Heartbeat deadlines
Heartbeat epoch e covers [genesis_ts + e · heartbeat, genesis_ts + (e + 1) · heartbeat). Every instruction that writes a position sets its deadline to the end of the epoch after the current one:
e = floor((now − genesis_ts) / heartbeat)
deadline = genesis_ts + (e + 2) · heartbeatplace uses this value for a new borrow or perp, and renew and repay use it for the position they write. renew requires the new deadline to be later than the old one. liquidate requires now to be later than the position's deadline.
The funding index
A perp market's funding_index starts at FUNDING_BIAS, so it can move both ways and stay positive. Each clear of the market moves it by
delta = oracle_price · funding_rate_bps_per_hour · elapsed_seconds · |oi_long − oi_short|
/ (10_000 · 3_600 · (oi_long + oi_short))It rises by delta when longs are the larger side and falls when shorts are. A position that entered at funding owes, in quote atoms,
long: size · (funding_index − funding) / PRICE_SCALE
short: size · (funding − funding_index) / PRICE_SCALEA negative result is an amount the position receives. Its profit is size · (price − entry) / PRICE_SCALE for a long and size · (entry − price) / PRICE_SCALE for a short.
Oracle accounts
An oracle account passed to the program is a Pyth PriceUpdateV2 account. The program accepts it when all of these hold:
- its owner is
Config.pyth_receiver; - it is at least 133 bytes and begins with the
PriceUpdateV2discriminator,22 f1 23 63 9d 7e f4 cdin hex; - byte 40, the verification level, is 1 for full verification;
- the feed id at bytes 41 to 72 equals the feed id the pool or perp market names;
- the price is above 0;
- the publish time is within
max_oracle_ageseconds of the clock, before or after.
The program reads the price as an i64 at byte 73, the exponent as an i32 at byte 89 and the publish time as an i64 at byte 93, and scales the price to PRICE_SCALE:
scaled = price · 10^(12 + exponent + quote_decimals − base_decimals)For a perp market the decimals are its base_decimals and quote_decimals. For a pool's collateral, the base is the collateral with its collateral_decimals and the quote is the pool's mint with its decimals.