Mango Markets
Searchโ€ฆ
Mango Client API (javascript)

Installation

1
yarn add @blockworks-foundation/mango-client
Copied!

Usage Example

This example assumes that you have a wallet that is already setup with devnet tokens. The private key should be stored in ~/.config/solana/devnet.json. Visit https://v3.mango.markets/ and connect with the wallet to fund your margin account so that you can place orders. You can find the full source code in example.ts.
1
// Fetch orderbooks
2
const bids = await perpMarket.loadBids(connection);
3
const asks = await perpMarket.loadAsks(connection);
4
โ€‹
5
// L2 orderbook data
6
for (const [price, size] of bids.getL2(20)) {
7
console.log(price, size);
8
}
9
โ€‹
10
// L3 orderbook data
11
for (const order of asks) {
12
console.log(
13
order.owner.toBase58(),
14
order.orderId.toString('hex'),
15
order.price,
16
order.size,
17
order.side, // 'buy' or 'sell'
18
);
19
}
20
โ€‹
21
// Place order
22
await client.placePerpOrder(
23
mangoGroup,
24
mangoAccount,
25
mangoGroup.mangoCache,
26
perpMarket,
27
owner,
28
'buy', // or 'sell'
29
39000,
30
0.0001,
31
'limit', // or 'ioc' or 'postOnly'
32
);
33
โ€‹
34
// retrieve open orders for account
35
const openOrders = await perpMarket.loadOrdersForAccount(
36
connection,
37
mangoAccount,
38
);
39
โ€‹
40
// cancel orders
41
for (const order of openOrders) {
42
await client.cancelPerpOrder(
43
mangoGroup,
44
mangoAccount,
45
owner,
46
perpMarket,
47
order,
48
);
49
}
50
โ€‹
51
// Retrieve fills
52
for (const fill of await perpMarket.loadFills(connection)) {
53
console.log(
54
fill.owner.toBase58(),
55
fill.maker ? 'maker' : 'taker',
56
fill.baseChange.toNumber(),
57
fill.quoteChange.toNumber(),
58
fill.longFunding.toFixed(3),
59
fill.shortFunding.toFixed(3),
60
);
61
}
Copied!
Last modified 3mo ago
Copy link