Purpose
Provide you with a brief introduction to some of the important technical aspects of Mango v3 program. This doc is at an intermediate level so if you want a more basic intro to solana programming, check out the links section below. This doc will only help familiarize you with concepts so reading the code is a bit easier. There is no substitute for actually reading the code: https://github.com/blockworks-foundation/mango-v3.
Table of Contents
State Accounts
There are a few major Solana Accounts that you will encounter frequently.
MangoGroup
- Holds broad market info about tokens, Serum DEX markets, Perp markets, oracles, insurance fund, fees vaults
- This account is passed in as read-only in most instructions so transactions may be parallelized
MangoAccount
- A user's MangoAccount keeping track of their deposits, borrows and
PerpAccount
for each perp market.
MangoCache
- Solana txs are limited to 1200 bytes in tx data. As a result, only ~35 accounts may be passed in any tx. This limitation would prevent us from cross margining as many tokens. To get around this, the MangoCache contains important bits of information aggregated across several different accounts into one account.
- MangoCache holds PriceCache, RootBankCache, PerpMarketCache
- The Cache is always used for consistency instead of the raw value even if the raw value is available in a transaction
PerpMarket
- Contains PerpMarket related info (e.g. lot sizes, liquidity mining information)
- Funding is kept track through an O(1) mechanism on the PerpMarket
long_funding
variable goes up when the book price is above the index price.
- The
long_settled_funding
on each MangoAccount
keeps track of the last time the user settled funding. At the start, the long_settled_funding
will equal long_funding
on the cache. The difference between the long_funding
and the long_settled_funding
is the funding per contract the MangoAccount owes.