forked from blockworks-foundation/mango-client-v3
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.ts
221 lines (199 loc) · 5.19 KB
/
example.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
import * as os from 'os';
import * as fs from 'fs';
import { MangoClient } from './client';
import { Account, Commitment, Connection } from '@solana/web3.js';
import configFile from './ids.json';
import { Config, getMarketByBaseSymbolAndKind, GroupConfig } from './config';
import { Market } from '@project-serum/serum';
import { ZERO_BN } from './utils';
function readKeypair() {
return JSON.parse(
process.env.KEYPAIR ||
fs.readFileSync(os.homedir() + '/.config/solana/devnet.json', 'utf-8'),
);
}
async function examplePerp() {
// setup client
const config = new Config(configFile);
const groupConfig = config.getGroup(
'devnet',
'mango_test_v2.2',
) as GroupConfig;
const connection = new Connection(
'https://api.devnet.solana.com',
'processed' as Commitment,
);
const client = new MangoClient(connection, groupConfig.mangoProgramId);
// load group & market
const perpMarketConfig = getMarketByBaseSymbolAndKind(
groupConfig,
'BTC',
'perp',
);
const mangoGroup = await client.getMangoGroup(groupConfig.publicKey);
const perpMarket = await mangoGroup.loadPerpMarket(
connection,
perpMarketConfig.marketIndex,
perpMarketConfig.baseDecimals,
perpMarketConfig.quoteDecimals,
);
// Fetch orderbooks
const bids = await perpMarket.loadBids(connection);
const asks = await perpMarket.loadAsks(connection);
// L2 orderbook data
for (const [price, size] of bids.getL2(20)) {
console.log(price, size);
}
// L3 orderbook data
for (const order of asks) {
console.log(
order.owner.toBase58(),
order.orderId.toString('hex'),
order.price,
order.size,
order.side, // 'buy' or 'sell'
);
}
// Place order
const owner = new Account(readKeypair());
const mangoAccount = (
await client.getMangoAccountsForOwner(mangoGroup, owner.publicKey)
)[0];
await client.placePerpOrder(
mangoGroup,
mangoAccount,
mangoGroup.mangoCache,
perpMarket,
owner,
'buy', // or 'sell'
39000,
0.0001,
'limit',
); // or 'ioc' or 'postOnly'
// retrieve open orders for account
const openOrders = await perpMarket.loadOrdersForAccount(
connection,
mangoAccount,
);
// cancel orders
for (const order of openOrders) {
await client.cancelPerpOrder(
mangoGroup,
mangoAccount,
owner,
perpMarket,
order,
);
}
// Retrieve fills
for (const fill of await perpMarket.loadFills(connection)) {
console.log(
fill.maker.toBase58(),
fill.taker.toBase58(),
fill.price,
fill.quantity,
);
}
}
async function exampleSpot() {
// setup client
const config = new Config(configFile);
const groupConfig = config.getGroup(
'devnet',
'mango_test_v2.2',
) as GroupConfig;
const connection = new Connection(
'https://api.devnet.solana.com',
'processed' as Commitment,
);
const client = new MangoClient(connection, groupConfig.mangoProgramId);
// load group & market
const spotMarketConfig = getMarketByBaseSymbolAndKind(
groupConfig,
'BTC',
'spot',
);
const mangoGroup = await client.getMangoGroup(groupConfig.publicKey);
const spotMarket = await Market.load(
connection,
spotMarketConfig.publicKey,
undefined,
groupConfig.serumProgramId,
);
// Fetch orderbooks
const bids = await spotMarket.loadBids(connection);
const asks = await spotMarket.loadAsks(connection);
// L2 orderbook data
for (const [price, size] of bids.getL2(20)) {
console.log(price, size);
}
// L3 orderbook data
for (const order of asks) {
console.log(
order.openOrdersAddress.toBase58(),
order.orderId.toString('hex'),
order.price,
order.size,
order.side, // 'buy' or 'sell'
);
}
// Place order
const owner = new Account(readKeypair());
const mangoAccount = (
await client.getMangoAccountsForOwner(mangoGroup, owner.publicKey)
)[0];
await client.placeSpotOrder(
mangoGroup,
mangoAccount,
mangoGroup.mangoCache,
spotMarket,
owner,
'buy', // or 'sell'
41000,
0.0001,
'limit',
); // or 'ioc' or 'postOnly'
// retrieve open orders for account
const openOrders = await spotMarket.loadOrdersForOwner(
connection,
mangoAccount.publicKey,
);
// cancel orders
for (const order of openOrders) {
await client.cancelSpotOrder(
mangoGroup,
mangoAccount,
owner,
spotMarket,
order,
);
}
// Retrieve fills
for (const fill of await spotMarket.loadFills(connection)) {
console.log(
fill.openOrders.toBase58(),
fill.eventFlags.maker ? 'maker' : 'taker',
fill.size * (fill.side === 'buy' ? 1 : -1),
spotMarket.quoteSplSizeToNumber(
fill.side === 'buy'
? fill.nativeQuantityPaid
: fill.nativeQuantityReleased,
),
);
}
// Settle funds
for (const openOrders of await mangoAccount.loadOpenOrders(
connection,
groupConfig.serumProgramId,
)) {
if (!openOrders) continue;
if (
openOrders.baseTokenFree.gt(ZERO_BN) ||
openOrders.quoteTokenFree.gt(ZERO_BN)
) {
await client.settleFunds(mangoGroup, mangoAccount, owner, spotMarket);
}
}
}
examplePerp();
exampleSpot();