Mango Markets
Searchโ€ฆ
Mango Client API
โ€‹API Referenceโ€‹
The source code for the v3 Mango Client API is hosted on github.
To access the API you will first need to setup a Connection using the @solana/web3 sdk. The easiest way for now is to connect to the `mainnet` version of the Mango Program. You can easily access the relevant connection details exported as a json constant IDS.

Mango Groups

A mango group is a basket of cross-margined tokens. Each version of Mango Markets uses a different Mango Group containing different tokens. The current v3 group is `mainnet.1`.
1
// get v3 Mango Group
2
import { IDS, MangoClient, Config, I80F48 } from '@blockworks-foundation/mango-client-v3';
3
import { Connection, PublicKey } from `@solana/web.js`
4
โ€‹
5
const cluster = 'mainnet';
6
const group = 'mainnet.1';
7
โ€‹
8
const config = new Config(IDS);
9
const groupConfig = config.getGroup(cluster, group);
10
if (!groupConfig) {
11
throw new Error("unable to get mango group config");
12
}
13
const mangoGroupKey = groupConfig.publicKey;
14
โ€‹
15
const clusterData = IDS.groups.find((g) => {
16
return g.name == group && g.cluster == cluster;
17
});
18
const mangoProgramIdPk = new PublicKey(clusterData.mangoProgramId);
19
โ€‹
20
const clusterUrl = IDS.cluster_urls[cluster];
21
const connection = new Connection(clusterUrl, 'singleGossip');
22
const client = new MangoClient(connection, mangoProgramIdPk);
23
const mangoGroup = await client.getMangoGroup(mangoGroupKey);
Copied!

Oracles

Each group uses a set of oracles that provide up to date price feeds for it's tokens. For the mainnet.1 group Pyth and Switchboard oracles are used.
1
// fetch current oracle price of a single token
2
mangoGroup.getPrice(tokenIndex: number, mangoCache: MangoCache): I80F48
3
// then use I80F48 .add/.sub/.mul/.div functions to perform calculations
4
// or convert to human-friendly number with .toNumber() or .toFixed(2)
Copied!

Lending

Each group also manages it's own lending pool for each of the traded tokens. Users can deposit tokens into different mango groups, but each token can only be assigned to one group at a time. The cross-margin capabilities of mango are currently to individual groups, meaning that deposited funds are only counted as collateral inside a single group. Interest for deposits and borrows is calculated on group level as well.
1
// look up token index by mint public key
2
mangoGroup.getTokenIndex(token: PublicKey): number
3
โ€‹
4
// fetch total deposits/borrows, deposit and borrow interest rates, as well as percent utilization of each token in the group
5
const rootBanks = await mangoGroup.loadRootBanks(connection);
6
const tokensInfo = groupConfig.tokens.map((token) => {
7
const rootBank = rootBanks.find((bank) => {
8
if (!bank) {
9
return false;
10
}
11
return bank.publicKey.toBase58() == token.rootKey.toBase58();
12
});
13
โ€‹
14
if (!rootBank) {
15
throw new Error("rootBanks is undefined");
16
}
17
return {
18
name: token.symbol,
19
totalDeposits: totalDeposits.toFixed(
20
tokenPrecision[token.symbol] || 2
21
),
22
totalBorrows: totalBorrows.toFixed(
23
tokenPrecision[token.symbol] || 2
24
),
25
depositRate: rootBank
26
.getDepositRate(mangoGroup)
27
.mul(I80F48.fromNumber(100)),
28
borrowRate: rootBank
29
.getBorrowRate(mangoGroup)
30
.mul(I80F48.fromNumber(100)),
31
utilization: totalDeposits.gt(I80F48.fromNumber(0))
32
? totalBorrows.div(totalDeposits)
33
: I80F48.fromNumber(0),
34
};
35
});
Copied!

Mango Accounts

Each user can have multiple accounts associated with a single group. They function as a balance sheet and keep track of the deposits, borrows and open positions on the serum dex order book. A Mango Markets margin account is fully cross-margined and can be liquidated once the collateral ratio drops below maintenance margin level. When interacting with the margin account you will often need to provide the serum dex program-id in order to access the open orders on it's order book.
1
import { IDS, MangoClient } from '@blockworks-foundation/mango-client-v3';
2
import { Connection, PublicKey } from `@solana/web.js`
3
โ€‹
4
const cluster = 'mainnet';
5
const group = 'mainnet.1';
6
const myMangoAccountAddress = 'YOUR MANGO ACCOUNT ADDRESS GOES HERE';
7
โ€‹
8
const clusterData = IDS_v3.groups.find((g) => {
9
return g.name == group && g.cluster == cluster;
10
});
11
const mangoProgramIdPk = new PublicKey(clusterData.mangoProgramId);
12
const serumProgramIdPk = new PublicKey(clusterData.serumProgramId);
13
โ€‹
14
const clusterUrl = IDS_v3.cluster_urls[cluster];
15
const connection = new Connection(clusterUrl, 'singleGossip');
16
โ€‹
17
const myMangoAccountPubKey = new PublicKey(myMangoAccountAddress);
18
const client = new MangoClient(connection, mangoProgramIdPk);
19
const myMangoAccount = await client.getMangoAccount(myMangoAccountPubKey, serumProgramIdPk);
Copied!
Last modified 14d ago