@mysten/deepbook-v3 0.0.0-experimental-20240730220552
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +8 -0
- package/LICENSE +201 -0
- package/README.md +1 -0
- package/dist/cjs/client.d.ts +177 -0
- package/dist/cjs/client.js +387 -0
- package/dist/cjs/client.js.map +7 -0
- package/dist/cjs/index.d.ts +7 -0
- package/dist/cjs/index.js +37 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/package.json +4 -0
- package/dist/cjs/transactions/balanceManager.d.ts +80 -0
- package/dist/cjs/transactions/balanceManager.js +185 -0
- package/dist/cjs/transactions/balanceManager.js.map +7 -0
- package/dist/cjs/transactions/deepbook.d.ts +183 -0
- package/dist/cjs/transactions/deepbook.js +545 -0
- package/dist/cjs/transactions/deepbook.js.map +7 -0
- package/dist/cjs/transactions/deepbookAdmin.d.ts +43 -0
- package/dist/cjs/transactions/deepbookAdmin.js +159 -0
- package/dist/cjs/transactions/deepbookAdmin.js.map +7 -0
- package/dist/cjs/transactions/flashLoans.d.ts +56 -0
- package/dist/cjs/transactions/flashLoans.js +122 -0
- package/dist/cjs/transactions/flashLoans.js.map +7 -0
- package/dist/cjs/transactions/governance.d.ts +42 -0
- package/dist/cjs/transactions/governance.js +131 -0
- package/dist/cjs/transactions/governance.js.map +7 -0
- package/dist/cjs/types/index.d.ts +80 -0
- package/dist/cjs/types/index.js +38 -0
- package/dist/cjs/types/index.js.map +7 -0
- package/dist/cjs/utils/config.d.ts +38 -0
- package/dist/cjs/utils/config.js +103 -0
- package/dist/cjs/utils/config.js.map +7 -0
- package/dist/cjs/utils/constants.d.ts +22 -0
- package/dist/cjs/utils/constants.js +117 -0
- package/dist/cjs/utils/constants.js.map +7 -0
- package/dist/esm/client.d.ts +177 -0
- package/dist/esm/client.js +367 -0
- package/dist/esm/client.js.map +7 -0
- package/dist/esm/index.d.ts +7 -0
- package/dist/esm/index.js +17 -0
- package/dist/esm/index.js.map +7 -0
- package/dist/esm/package.json +4 -0
- package/dist/esm/transactions/balanceManager.d.ts +80 -0
- package/dist/esm/transactions/balanceManager.js +165 -0
- package/dist/esm/transactions/balanceManager.js.map +7 -0
- package/dist/esm/transactions/deepbook.d.ts +183 -0
- package/dist/esm/transactions/deepbook.js +525 -0
- package/dist/esm/transactions/deepbook.js.map +7 -0
- package/dist/esm/transactions/deepbookAdmin.d.ts +43 -0
- package/dist/esm/transactions/deepbookAdmin.js +139 -0
- package/dist/esm/transactions/deepbookAdmin.js.map +7 -0
- package/dist/esm/transactions/flashLoans.d.ts +56 -0
- package/dist/esm/transactions/flashLoans.js +102 -0
- package/dist/esm/transactions/flashLoans.js.map +7 -0
- package/dist/esm/transactions/governance.d.ts +42 -0
- package/dist/esm/transactions/governance.js +111 -0
- package/dist/esm/transactions/governance.js.map +7 -0
- package/dist/esm/types/index.d.ts +80 -0
- package/dist/esm/types/index.js +18 -0
- package/dist/esm/types/index.js.map +7 -0
- package/dist/esm/utils/config.d.ts +38 -0
- package/dist/esm/utils/config.js +90 -0
- package/dist/esm/utils/config.js.map +7 -0
- package/dist/esm/utils/constants.d.ts +22 -0
- package/dist/esm/utils/constants.js +97 -0
- package/dist/esm/utils/constants.js.map +7 -0
- package/dist/tsconfig.esm.tsbuildinfo +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +52 -0
- package/src/client.ts +429 -0
- package/src/index.ts +10 -0
- package/src/transactions/balanceManager.ts +182 -0
- package/src/transactions/deepbook.ts +587 -0
- package/src/transactions/deepbookAdmin.ts +140 -0
- package/src/transactions/flashLoans.ts +121 -0
- package/src/transactions/governance.ts +119 -0
- package/src/types/index.ts +98 -0
- package/src/utils/config.ts +103 -0
- package/src/utils/constants.ts +106 -0
package/src/client.ts
ADDED
|
@@ -0,0 +1,429 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { bcs } from '@mysten/sui/bcs';
|
|
4
|
+
import type { SuiClient } from '@mysten/sui/client';
|
|
5
|
+
import { Transaction } from '@mysten/sui/transactions';
|
|
6
|
+
import { normalizeSuiAddress } from '@mysten/sui/utils';
|
|
7
|
+
|
|
8
|
+
import { BalanceManagerContract } from './transactions/balanceManager.js';
|
|
9
|
+
import { DeepBookContract } from './transactions/deepbook.js';
|
|
10
|
+
import { DeepBookAdminContract } from './transactions/deepbookAdmin.js';
|
|
11
|
+
import { FlashLoanContract } from './transactions/flashLoans.js';
|
|
12
|
+
import { GovernanceContract } from './transactions/governance.js';
|
|
13
|
+
import type { BalanceManager, Environment } from './types/index.js';
|
|
14
|
+
import { DEEP_SCALAR, DeepBookConfig, FLOAT_SCALAR } from './utils/config.js';
|
|
15
|
+
import type { CoinMap, PoolMap } from './utils/constants.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* DeepBookClient class for managing DeepBook operations.
|
|
19
|
+
*/
|
|
20
|
+
export class DeepBookClient {
|
|
21
|
+
client: SuiClient;
|
|
22
|
+
#config: DeepBookConfig;
|
|
23
|
+
#address: string;
|
|
24
|
+
balanceManager: BalanceManagerContract;
|
|
25
|
+
deepBook: DeepBookContract;
|
|
26
|
+
deepBookAdmin: DeepBookAdminContract;
|
|
27
|
+
flashLoans: FlashLoanContract;
|
|
28
|
+
governance: GovernanceContract;
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* @param {SuiClient} client SuiClient instance
|
|
32
|
+
* @param {string} address Address of the client
|
|
33
|
+
* @param {Environment} env Environment configuration
|
|
34
|
+
* @param {Object.<string, BalanceManager>} [balanceManagers] Optional initial BalanceManager map
|
|
35
|
+
* @param {CoinMap} [coins] Optional initial CoinMap
|
|
36
|
+
* @param {PoolMap} [pools] Optional initial PoolMap
|
|
37
|
+
* @param {string} [adminCap] Optional admin capability
|
|
38
|
+
*/
|
|
39
|
+
constructor({
|
|
40
|
+
client,
|
|
41
|
+
address,
|
|
42
|
+
env,
|
|
43
|
+
balanceManagers,
|
|
44
|
+
coins,
|
|
45
|
+
pools,
|
|
46
|
+
adminCap,
|
|
47
|
+
}: {
|
|
48
|
+
client: SuiClient;
|
|
49
|
+
address: string;
|
|
50
|
+
env: Environment;
|
|
51
|
+
balanceManagers?: { [key: string]: BalanceManager };
|
|
52
|
+
coins?: CoinMap;
|
|
53
|
+
pools?: PoolMap;
|
|
54
|
+
adminCap?: string;
|
|
55
|
+
}) {
|
|
56
|
+
this.client = client;
|
|
57
|
+
this.#address = normalizeSuiAddress(address);
|
|
58
|
+
this.#config = new DeepBookConfig({
|
|
59
|
+
address: this.#address,
|
|
60
|
+
env,
|
|
61
|
+
balanceManagers,
|
|
62
|
+
coins,
|
|
63
|
+
pools,
|
|
64
|
+
adminCap,
|
|
65
|
+
});
|
|
66
|
+
this.balanceManager = new BalanceManagerContract(this.#config);
|
|
67
|
+
this.deepBook = new DeepBookContract(this.#config);
|
|
68
|
+
this.deepBookAdmin = new DeepBookAdminContract(this.#config);
|
|
69
|
+
this.flashLoans = new FlashLoanContract(this.#config);
|
|
70
|
+
this.governance = new GovernanceContract(this.#config);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* @description Check the balance of a balance manager for a specific coin
|
|
75
|
+
* @param {string} managerKey Key of the balance manager
|
|
76
|
+
* @param {string} coinKey Key of the coin
|
|
77
|
+
* @returns {Promise<{ coinType: string, balance: number }>} An object with coin type and balance
|
|
78
|
+
*/
|
|
79
|
+
async checkManagerBalance(managerKey: string, coinKey: string) {
|
|
80
|
+
const tx = new Transaction();
|
|
81
|
+
const coin = this.#config.getCoin(coinKey);
|
|
82
|
+
|
|
83
|
+
tx.add(this.balanceManager.checkManagerBalance(managerKey, coinKey));
|
|
84
|
+
const res = await this.client.devInspectTransactionBlock({
|
|
85
|
+
sender: this.#address,
|
|
86
|
+
transactionBlock: tx,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
const bytes = res.results![0].returnValues![0][0];
|
|
90
|
+
const parsed_balance = bcs.U64.parse(new Uint8Array(bytes));
|
|
91
|
+
const balanceNumber = Number(parsed_balance);
|
|
92
|
+
const adjusted_balance = balanceNumber / coin.scalar;
|
|
93
|
+
|
|
94
|
+
return {
|
|
95
|
+
coinType: coin.type,
|
|
96
|
+
balance: adjusted_balance,
|
|
97
|
+
};
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* @description Check if a pool is whitelisted
|
|
102
|
+
* @param {string} poolKey Key of the pool
|
|
103
|
+
* @returns {Promise<boolean>} Boolean indicating if the pool is whitelisted
|
|
104
|
+
*/
|
|
105
|
+
async whitelisted(poolKey: string) {
|
|
106
|
+
const tx = new Transaction();
|
|
107
|
+
|
|
108
|
+
tx.add(this.deepBook.whitelisted(poolKey));
|
|
109
|
+
const res = await this.client.devInspectTransactionBlock({
|
|
110
|
+
sender: normalizeSuiAddress(this.#address),
|
|
111
|
+
transactionBlock: tx,
|
|
112
|
+
});
|
|
113
|
+
|
|
114
|
+
const bytes = res.results![0].returnValues![0][0];
|
|
115
|
+
const whitelisted = bcs.Bool.parse(new Uint8Array(bytes));
|
|
116
|
+
|
|
117
|
+
return whitelisted;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* @description Get the quote quantity out for a given base quantity
|
|
122
|
+
* @param {string} poolKey Key of the pool
|
|
123
|
+
* @param {number} baseQuantity Base quantity to convert
|
|
124
|
+
* @returns {Promise<{ baseQuantity: number, baseOut: number, quoteOut: number, deepRequired: number }>}
|
|
125
|
+
* An object with base quantity, base out, quote out, and deep required for the dry run
|
|
126
|
+
*/
|
|
127
|
+
async getQuoteQuantityOut(poolKey: string, baseQuantity: number) {
|
|
128
|
+
const tx = new Transaction();
|
|
129
|
+
const pool = this.#config.getPool(poolKey);
|
|
130
|
+
const baseScalar = this.#config.getCoin(pool.baseCoin).scalar;
|
|
131
|
+
const quoteScalar = this.#config.getCoin(pool.quoteCoin).scalar;
|
|
132
|
+
|
|
133
|
+
tx.add(this.deepBook.getQuoteQuantityOut(poolKey, baseQuantity));
|
|
134
|
+
const res = await this.client.devInspectTransactionBlock({
|
|
135
|
+
sender: normalizeSuiAddress(this.#address),
|
|
136
|
+
transactionBlock: tx,
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const baseOut = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![0][0])));
|
|
140
|
+
const quoteOut = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![1][0])));
|
|
141
|
+
const deepRequired = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![2][0])));
|
|
142
|
+
|
|
143
|
+
return {
|
|
144
|
+
baseQuantity,
|
|
145
|
+
baseOut: baseOut / baseScalar,
|
|
146
|
+
quoteOut: quoteOut / quoteScalar,
|
|
147
|
+
deepRequired: deepRequired / DEEP_SCALAR,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
/**
|
|
152
|
+
* @description Get the base quantity out for a given quote quantity
|
|
153
|
+
* @param {string} poolKey Key of the pool
|
|
154
|
+
* @param {number} quoteQuantity Quote quantity to convert
|
|
155
|
+
* @returns {Promise<{ quoteQuantity: number, baseOut: number, quoteOut: number, deepRequired: number }>}
|
|
156
|
+
* An object with quote quantity, base out, quote out, and deep required for the dry run
|
|
157
|
+
*/
|
|
158
|
+
async getBaseQuantityOut(poolKey: string, quoteQuantity: number) {
|
|
159
|
+
const tx = new Transaction();
|
|
160
|
+
const pool = this.#config.getPool(poolKey);
|
|
161
|
+
const baseScalar = this.#config.getCoin(pool.baseCoin).scalar;
|
|
162
|
+
const quoteScalar = this.#config.getCoin(pool.quoteCoin).scalar;
|
|
163
|
+
|
|
164
|
+
tx.add(this.deepBook.getBaseQuantityOut(poolKey, quoteQuantity));
|
|
165
|
+
const res = await this.client.devInspectTransactionBlock({
|
|
166
|
+
sender: normalizeSuiAddress(this.#address),
|
|
167
|
+
transactionBlock: tx,
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
const baseOut = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![0][0])));
|
|
171
|
+
const quoteOut = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![1][0])));
|
|
172
|
+
const deepRequired = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![2][0])));
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
quoteQuantity: quoteQuantity,
|
|
176
|
+
baseOut: baseOut / baseScalar,
|
|
177
|
+
quoteOut: quoteOut / quoteScalar,
|
|
178
|
+
deepRequired: deepRequired / DEEP_SCALAR,
|
|
179
|
+
};
|
|
180
|
+
}
|
|
181
|
+
|
|
182
|
+
/**
|
|
183
|
+
* @description Get the output quantities for given base and quote quantities. Only one quantity can be non-zero
|
|
184
|
+
* @param {string} poolKey Key of the pool
|
|
185
|
+
* @param {number} baseQuantity Base quantity to convert
|
|
186
|
+
* @param {number} quoteQuantity Quote quantity to convert
|
|
187
|
+
* @returns {Promise<{ baseQuantity: number, quoteQuantity: number, baseOut: number, quoteOut: number, deepRequired: number }>}
|
|
188
|
+
* An object with base quantity, quote quantity, base out, quote out, and deep required for the dry run
|
|
189
|
+
*/
|
|
190
|
+
async getQuantityOut(poolKey: string, baseQuantity: number, quoteQuantity: number) {
|
|
191
|
+
const tx = new Transaction();
|
|
192
|
+
const pool = this.#config.getPool(poolKey);
|
|
193
|
+
const baseScalar = this.#config.getCoin(pool.baseCoin).scalar;
|
|
194
|
+
const quoteScalar = this.#config.getCoin(pool.quoteCoin).scalar;
|
|
195
|
+
|
|
196
|
+
tx.add(this.deepBook.getQuantityOut(poolKey, baseQuantity, quoteQuantity));
|
|
197
|
+
const res = await this.client.devInspectTransactionBlock({
|
|
198
|
+
sender: normalizeSuiAddress(this.#address),
|
|
199
|
+
transactionBlock: tx,
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const baseOut = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![0][0])));
|
|
203
|
+
const quoteOut = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![1][0])));
|
|
204
|
+
const deepRequired = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![2][0])));
|
|
205
|
+
|
|
206
|
+
return {
|
|
207
|
+
baseQuantity,
|
|
208
|
+
quoteQuantity,
|
|
209
|
+
baseOut: baseOut / baseScalar,
|
|
210
|
+
quoteOut: quoteOut / quoteScalar,
|
|
211
|
+
deepRequired: deepRequired / DEEP_SCALAR,
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
/**
|
|
216
|
+
* @description Get open orders for a balance manager in a pool
|
|
217
|
+
* @param {string} poolKey Key of the pool
|
|
218
|
+
* @param {string} managerKey Key of the balance manager
|
|
219
|
+
* @returns {Promise<Array>} An array of open order IDs
|
|
220
|
+
*/
|
|
221
|
+
async accountOpenOrders(poolKey: string, managerKey: string) {
|
|
222
|
+
const tx = new Transaction();
|
|
223
|
+
|
|
224
|
+
tx.add(this.deepBook.accountOpenOrders(poolKey, managerKey));
|
|
225
|
+
const res = await this.client.devInspectTransactionBlock({
|
|
226
|
+
sender: normalizeSuiAddress(this.#address),
|
|
227
|
+
transactionBlock: tx,
|
|
228
|
+
});
|
|
229
|
+
|
|
230
|
+
const order_ids = res.results![0].returnValues![0][0];
|
|
231
|
+
const VecSet = bcs.struct('VecSet', {
|
|
232
|
+
constants: bcs.vector(bcs.U128),
|
|
233
|
+
});
|
|
234
|
+
|
|
235
|
+
return VecSet.parse(new Uint8Array(order_ids)).constants;
|
|
236
|
+
}
|
|
237
|
+
|
|
238
|
+
/**
|
|
239
|
+
* @description Get the order information for a specific order in a pool
|
|
240
|
+
* @param {string} poolKey Key of the pool
|
|
241
|
+
* @param {string} orderId Order ID
|
|
242
|
+
* @returns {Promise<Object>} A promise that resolves to an object containing the order information
|
|
243
|
+
*/
|
|
244
|
+
async getOrder(poolKey: string, orderId: string) {
|
|
245
|
+
const tx = new Transaction();
|
|
246
|
+
|
|
247
|
+
tx.add(this.deepBook.getOrder(poolKey, orderId));
|
|
248
|
+
const res = await this.client.devInspectTransactionBlock({
|
|
249
|
+
sender: normalizeSuiAddress(this.#address),
|
|
250
|
+
transactionBlock: tx,
|
|
251
|
+
});
|
|
252
|
+
|
|
253
|
+
const ID = bcs.struct('ID', {
|
|
254
|
+
bytes: bcs.Address,
|
|
255
|
+
});
|
|
256
|
+
const OrderDeepPrice = bcs.struct('OrderDeepPrice', {
|
|
257
|
+
asset_is_base: bcs.bool(),
|
|
258
|
+
deep_per_asset: bcs.u64(),
|
|
259
|
+
});
|
|
260
|
+
const Order = bcs.struct('Order', {
|
|
261
|
+
balance_manager_id: ID,
|
|
262
|
+
order_id: bcs.u128(),
|
|
263
|
+
client_order_id: bcs.u64(),
|
|
264
|
+
quantity: bcs.u64(),
|
|
265
|
+
filled_quantity: bcs.u64(),
|
|
266
|
+
fee_is_deep: bcs.bool(),
|
|
267
|
+
order_deep_price: OrderDeepPrice,
|
|
268
|
+
epoch: bcs.u64(),
|
|
269
|
+
status: bcs.u8(),
|
|
270
|
+
expire_timestamp: bcs.u64(),
|
|
271
|
+
});
|
|
272
|
+
|
|
273
|
+
const orderInformation = res.results![0].returnValues![0][0];
|
|
274
|
+
return Order.parse(new Uint8Array(orderInformation));
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
/**
|
|
278
|
+
* @description Get level 2 order book specifying range of price
|
|
279
|
+
* @param {string} poolKey Key of the pool
|
|
280
|
+
* @param {number} priceLow Lower bound of the price range
|
|
281
|
+
* @param {number} priceHigh Upper bound of the price range
|
|
282
|
+
* @param {boolean} isBid Whether to get bid or ask orders
|
|
283
|
+
* @returns {Promise<{ prices: Array<number>, quantities: Array<number> }>}
|
|
284
|
+
* An object with arrays of prices and quantities
|
|
285
|
+
*/
|
|
286
|
+
async getLevel2Range(poolKey: string, priceLow: number, priceHigh: number, isBid: boolean) {
|
|
287
|
+
const tx = new Transaction();
|
|
288
|
+
const pool = this.#config.getPool(poolKey);
|
|
289
|
+
const baseCoin = this.#config.getCoin(pool.baseCoin);
|
|
290
|
+
const quoteCoin = this.#config.getCoin(pool.quoteCoin);
|
|
291
|
+
|
|
292
|
+
tx.add(this.deepBook.getLevel2Range(poolKey, priceLow, priceHigh, isBid));
|
|
293
|
+
const res = await this.client.devInspectTransactionBlock({
|
|
294
|
+
sender: normalizeSuiAddress(this.#address),
|
|
295
|
+
transactionBlock: tx,
|
|
296
|
+
});
|
|
297
|
+
|
|
298
|
+
const prices = res.results![0].returnValues![0][0];
|
|
299
|
+
const parsed_prices = bcs.vector(bcs.u64()).parse(new Uint8Array(prices));
|
|
300
|
+
const quantities = res.results![0].returnValues![1][0];
|
|
301
|
+
const parsed_quantities = bcs.vector(bcs.u64()).parse(new Uint8Array(quantities));
|
|
302
|
+
|
|
303
|
+
return {
|
|
304
|
+
prices: parsed_prices.map(
|
|
305
|
+
(price) => (Number(price) / FLOAT_SCALAR / quoteCoin.scalar) * baseCoin.scalar,
|
|
306
|
+
),
|
|
307
|
+
quantities: parsed_quantities.map((price) => Number(price) / baseCoin.scalar),
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* @description Get level 2 order book ticks from mid-price for a pool
|
|
313
|
+
* @param {string} poolKey Key of the pool
|
|
314
|
+
* @param {number} ticks Number of ticks from mid-price
|
|
315
|
+
* @returns {Promise<{ bid_prices: Array<number>, bid_quantities: Array<number>, ask_prices: Array<number>, ask_quantities: Array<number> }>}
|
|
316
|
+
* An object with arrays of prices and quantities
|
|
317
|
+
*/
|
|
318
|
+
async getLevel2TicksFromMid(poolKey: string, ticks: number) {
|
|
319
|
+
const tx = new Transaction();
|
|
320
|
+
const pool = this.#config.getPool(poolKey);
|
|
321
|
+
const baseCoin = this.#config.getCoin(pool.baseCoin);
|
|
322
|
+
const quoteCoin = this.#config.getCoin(pool.quoteCoin);
|
|
323
|
+
|
|
324
|
+
tx.add(this.deepBook.getLevel2TicksFromMid(poolKey, ticks));
|
|
325
|
+
const res = await this.client.devInspectTransactionBlock({
|
|
326
|
+
sender: normalizeSuiAddress(this.#address),
|
|
327
|
+
transactionBlock: tx,
|
|
328
|
+
});
|
|
329
|
+
|
|
330
|
+
const bid_prices = res.results![0].returnValues![0][0];
|
|
331
|
+
const bid_parsed_prices = bcs.vector(bcs.u64()).parse(new Uint8Array(bid_prices));
|
|
332
|
+
const bid_quantities = res.results![0].returnValues![1][0];
|
|
333
|
+
const bid_parsed_quantities = bcs.vector(bcs.u64()).parse(new Uint8Array(bid_quantities));
|
|
334
|
+
|
|
335
|
+
const ask_prices = res.results![0].returnValues![2][0];
|
|
336
|
+
const ask_parsed_prices = bcs.vector(bcs.u64()).parse(new Uint8Array(ask_prices));
|
|
337
|
+
const ask_quantities = res.results![0].returnValues![3][0];
|
|
338
|
+
const ask_parsed_quantities = bcs.vector(bcs.u64()).parse(new Uint8Array(ask_quantities));
|
|
339
|
+
|
|
340
|
+
return {
|
|
341
|
+
bid_prices: bid_parsed_prices.map(
|
|
342
|
+
(price) => (Number(price) / FLOAT_SCALAR / quoteCoin.scalar) * baseCoin.scalar,
|
|
343
|
+
),
|
|
344
|
+
bid_quantities: bid_parsed_quantities.map((quantity) => Number(quantity) / baseCoin.scalar),
|
|
345
|
+
ask_prices: ask_parsed_prices.map(
|
|
346
|
+
(price) => (Number(price) / FLOAT_SCALAR / quoteCoin.scalar) * baseCoin.scalar,
|
|
347
|
+
),
|
|
348
|
+
ask_quantities: ask_parsed_quantities.map((quantity) => Number(quantity) / baseCoin.scalar),
|
|
349
|
+
};
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
/**
|
|
353
|
+
* @description Get the vault balances for a pool
|
|
354
|
+
* @param {string} poolKey Key of the pool
|
|
355
|
+
* @returns {Promise<{ base: number, quote: number, deep: number }>}
|
|
356
|
+
* An object with base, quote, and deep balances in the vault
|
|
357
|
+
*/
|
|
358
|
+
async vaultBalances(poolKey: string) {
|
|
359
|
+
const tx = new Transaction();
|
|
360
|
+
const pool = this.#config.getPool(poolKey);
|
|
361
|
+
const baseScalar = this.#config.getCoin(pool.baseCoin).scalar;
|
|
362
|
+
const quoteScalar = this.#config.getCoin(pool.quoteCoin).scalar;
|
|
363
|
+
|
|
364
|
+
tx.add(this.deepBook.vaultBalances(poolKey));
|
|
365
|
+
const res = await this.client.devInspectTransactionBlock({
|
|
366
|
+
sender: normalizeSuiAddress(this.#address),
|
|
367
|
+
transactionBlock: tx,
|
|
368
|
+
});
|
|
369
|
+
|
|
370
|
+
const baseInVault = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![0][0])));
|
|
371
|
+
const quoteInVault = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![1][0])));
|
|
372
|
+
const deepInVault = Number(bcs.U64.parse(new Uint8Array(res.results![0].returnValues![2][0])));
|
|
373
|
+
|
|
374
|
+
return {
|
|
375
|
+
base: baseInVault / baseScalar,
|
|
376
|
+
quote: quoteInVault / quoteScalar,
|
|
377
|
+
deep: deepInVault / DEEP_SCALAR,
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
|
|
381
|
+
/**
|
|
382
|
+
* @description Get the pool ID by asset types
|
|
383
|
+
* @param {string} baseType Type of the base asset
|
|
384
|
+
* @param {string} quoteType Type of the quote asset
|
|
385
|
+
* @returns {Promise<string>} The address of the pool
|
|
386
|
+
*/
|
|
387
|
+
async getPoolIdByAssets(baseType: string, quoteType: string) {
|
|
388
|
+
const tx = new Transaction();
|
|
389
|
+
tx.add(this.deepBook.getPoolIdByAssets(baseType, quoteType));
|
|
390
|
+
|
|
391
|
+
const res = await this.client.devInspectTransactionBlock({
|
|
392
|
+
sender: normalizeSuiAddress(this.#address),
|
|
393
|
+
transactionBlock: tx,
|
|
394
|
+
});
|
|
395
|
+
|
|
396
|
+
const ID = bcs.struct('ID', {
|
|
397
|
+
bytes: bcs.Address,
|
|
398
|
+
});
|
|
399
|
+
const address = ID.parse(new Uint8Array(res.results![0].returnValues![0][0]))['bytes'];
|
|
400
|
+
|
|
401
|
+
return address;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
/**
|
|
405
|
+
* @description Get the mid price for a pool
|
|
406
|
+
* @param {string} poolKey Key of the pool
|
|
407
|
+
* @returns {Promise<number>} The mid price
|
|
408
|
+
*/
|
|
409
|
+
async midPrice(poolKey: string) {
|
|
410
|
+
const tx = new Transaction();
|
|
411
|
+
const pool = this.#config.getPool(poolKey);
|
|
412
|
+
tx.add(this.deepBook.midPrice(poolKey));
|
|
413
|
+
|
|
414
|
+
const baseCoin = this.#config.getCoin(pool.baseCoin);
|
|
415
|
+
const quoteCoin = this.#config.getCoin(pool.quoteCoin);
|
|
416
|
+
|
|
417
|
+
const res = await this.client.devInspectTransactionBlock({
|
|
418
|
+
sender: normalizeSuiAddress(this.#address),
|
|
419
|
+
transactionBlock: tx,
|
|
420
|
+
});
|
|
421
|
+
|
|
422
|
+
const bytes = res.results![0].returnValues![0][0];
|
|
423
|
+
const parsed_mid_price = Number(bcs.U64.parse(new Uint8Array(bytes)));
|
|
424
|
+
const adjusted_mid_price =
|
|
425
|
+
(parsed_mid_price * baseCoin.scalar) / quoteCoin.scalar / FLOAT_SCALAR;
|
|
426
|
+
|
|
427
|
+
return adjusted_mid_price;
|
|
428
|
+
}
|
|
429
|
+
}
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
export { DeepBookClient } from './client.js';
|
|
5
|
+
export { BalanceManagerContract } from './transactions/balanceManager.js';
|
|
6
|
+
export { DeepBookContract } from './transactions/deepbook.js';
|
|
7
|
+
export { DeepBookAdminContract } from './transactions/deepbookAdmin.js';
|
|
8
|
+
export { FlashLoanContract } from './transactions/flashLoans.js';
|
|
9
|
+
export { GovernanceContract } from './transactions/governance.js';
|
|
10
|
+
export { DeepBookConfig } from './utils/config.js';
|
|
@@ -0,0 +1,182 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
import { coinWithBalance } from '@mysten/sui/transactions';
|
|
4
|
+
import type { Transaction } from '@mysten/sui/transactions';
|
|
5
|
+
|
|
6
|
+
import type { DeepBookConfig } from '../utils/config.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* BalanceManagerContract class for managing BalanceManager operations.
|
|
10
|
+
*/
|
|
11
|
+
export class BalanceManagerContract {
|
|
12
|
+
#config: DeepBookConfig;
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @param {DeepBookConfig} config Configuration for BalanceManagerContract
|
|
16
|
+
*/
|
|
17
|
+
constructor(config: DeepBookConfig) {
|
|
18
|
+
this.#config = config;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* @description Create and share a new BalanceManager
|
|
23
|
+
* @returns A function that takes a Transaction object
|
|
24
|
+
*/
|
|
25
|
+
createAndShareBalanceManager = () => (tx: Transaction) => {
|
|
26
|
+
const manager = tx.moveCall({
|
|
27
|
+
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::balance_manager::new`,
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
tx.moveCall({
|
|
31
|
+
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::balance_manager::share`,
|
|
32
|
+
arguments: [manager],
|
|
33
|
+
});
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
/**
|
|
37
|
+
* @description Deposit funds into the BalanceManager
|
|
38
|
+
* @param {string} managerKey The key of the BalanceManager
|
|
39
|
+
* @param {string} coinKey The key of the coin to deposit
|
|
40
|
+
* @param {number} amountToDeposit The amount to deposit
|
|
41
|
+
* @returns A function that takes a Transaction object
|
|
42
|
+
*/
|
|
43
|
+
depositIntoManager =
|
|
44
|
+
(managerKey: string, coinKey: string, amountToDeposit: number) => (tx: Transaction) => {
|
|
45
|
+
tx.setSenderIfNotSet(this.#config.address);
|
|
46
|
+
const managerId = this.#config.getBalanceManager(managerKey).address;
|
|
47
|
+
const coin = this.#config.getCoin(coinKey);
|
|
48
|
+
const deposit = coinWithBalance({
|
|
49
|
+
type: coin.type,
|
|
50
|
+
balance: amountToDeposit * coin.scalar,
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
tx.moveCall({
|
|
54
|
+
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::balance_manager::deposit`,
|
|
55
|
+
arguments: [tx.object(managerId), deposit],
|
|
56
|
+
typeArguments: [coin.type],
|
|
57
|
+
});
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* @description Withdraw funds from the BalanceManager
|
|
62
|
+
* @param {string} managerKey The key of the BalanceManager
|
|
63
|
+
* @param {string} coinKey The key of the coin to withdraw
|
|
64
|
+
* @param {number} amountToWithdraw The amount to withdraw
|
|
65
|
+
* @param {string} recipient The recipient of the withdrawn funds
|
|
66
|
+
* @returns A function that takes a Transaction object
|
|
67
|
+
*/
|
|
68
|
+
withdrawFromManager =
|
|
69
|
+
(managerKey: string, coinKey: string, amountToWithdraw: number, recipient: string) =>
|
|
70
|
+
(tx: Transaction) => {
|
|
71
|
+
const managerId = this.#config.getBalanceManager(managerKey).address;
|
|
72
|
+
const coin = this.#config.getCoin(coinKey);
|
|
73
|
+
const coinObject = tx.moveCall({
|
|
74
|
+
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::balance_manager::withdraw`,
|
|
75
|
+
arguments: [tx.object(managerId), tx.pure.u64(amountToWithdraw * coin.scalar)],
|
|
76
|
+
typeArguments: [coin.type],
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
tx.transferObjects([coinObject], recipient);
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* @description Withdraw all funds from the BalanceManager
|
|
84
|
+
* @param {string} managerKey The key of the BalanceManager
|
|
85
|
+
* @param {string} coinKey The key of the coin to withdraw
|
|
86
|
+
* @param {string} recipient The recipient of the withdrawn funds
|
|
87
|
+
* @returns A function that takes a Transaction object
|
|
88
|
+
*/
|
|
89
|
+
withdrawAllFromManager =
|
|
90
|
+
(managerKey: string, coinKey: string, recipient: string) => (tx: Transaction) => {
|
|
91
|
+
const managerId = this.#config.getBalanceManager(managerKey).address;
|
|
92
|
+
const coin = this.#config.getCoin(coinKey);
|
|
93
|
+
const withdrawalCoin = tx.moveCall({
|
|
94
|
+
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::balance_manager::withdraw_all`,
|
|
95
|
+
arguments: [tx.object(managerId)],
|
|
96
|
+
typeArguments: [coin.type],
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
tx.transferObjects([withdrawalCoin], recipient);
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
/**
|
|
103
|
+
* @description Check the balance of the BalanceManager
|
|
104
|
+
* @param {string} managerKey The key of the BalanceManager
|
|
105
|
+
* @param {string} coinKey The key of the coin to check the balance of
|
|
106
|
+
* @returns A function that takes a Transaction object
|
|
107
|
+
*/
|
|
108
|
+
checkManagerBalance = (managerKey: string, coinKey: string) => (tx: Transaction) => {
|
|
109
|
+
const managerId = this.#config.getBalanceManager(managerKey).address;
|
|
110
|
+
const coin = this.#config.getCoin(coinKey);
|
|
111
|
+
tx.moveCall({
|
|
112
|
+
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::balance_manager::balance`,
|
|
113
|
+
arguments: [tx.object(managerId)],
|
|
114
|
+
typeArguments: [coin.type],
|
|
115
|
+
});
|
|
116
|
+
};
|
|
117
|
+
|
|
118
|
+
/**
|
|
119
|
+
* @description Generate a trade proof for the BalanceManager. Calls the appropriate function based on whether tradeCap is set.
|
|
120
|
+
* @param {string} managerKey The key of the BalanceManager
|
|
121
|
+
* @returns A function that takes a Transaction object
|
|
122
|
+
*/
|
|
123
|
+
generateProof = (managerKey: string) => (tx: Transaction) => {
|
|
124
|
+
const balanceManager = this.#config.getBalanceManager(managerKey);
|
|
125
|
+
return tx.add(
|
|
126
|
+
balanceManager.tradeCap
|
|
127
|
+
? this.generateProofAsTrader(balanceManager.address, balanceManager.tradeCap)
|
|
128
|
+
: this.generateProofAsOwner(balanceManager.address),
|
|
129
|
+
);
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* @description Generate a trade proof as the owner
|
|
134
|
+
* @param {string} managerId The ID of the BalanceManager
|
|
135
|
+
* @returns A function that takes a Transaction object
|
|
136
|
+
*/
|
|
137
|
+
generateProofAsOwner = (managerId: string) => (tx: Transaction) => {
|
|
138
|
+
return tx.moveCall({
|
|
139
|
+
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::balance_manager::generate_proof_as_owner`,
|
|
140
|
+
arguments: [tx.object(managerId)],
|
|
141
|
+
});
|
|
142
|
+
};
|
|
143
|
+
|
|
144
|
+
/**
|
|
145
|
+
* @description Generate a trade proof as a trader
|
|
146
|
+
* @param {string} managerId The ID of the BalanceManager
|
|
147
|
+
* @param {string} tradeCapId The ID of the tradeCap
|
|
148
|
+
* @returns A function that takes a Transaction object
|
|
149
|
+
*/
|
|
150
|
+
generateProofAsTrader = (managerId: string, tradeCapId: string) => (tx: Transaction) => {
|
|
151
|
+
return tx.moveCall({
|
|
152
|
+
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::balance_manager::generate_proof_as_trader`,
|
|
153
|
+
arguments: [tx.object(managerId), tx.object(tradeCapId)],
|
|
154
|
+
});
|
|
155
|
+
};
|
|
156
|
+
|
|
157
|
+
/**
|
|
158
|
+
* @description Get the owner of the BalanceManager
|
|
159
|
+
* @param {string} managerKey The key of the BalanceManager
|
|
160
|
+
* @returns A function that takes a Transaction object
|
|
161
|
+
*/
|
|
162
|
+
owner = (managerKey: string) => (tx: Transaction) => {
|
|
163
|
+
const managerId = this.#config.getBalanceManager(managerKey).address;
|
|
164
|
+
tx.moveCall({
|
|
165
|
+
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::balance_manager::owner`,
|
|
166
|
+
arguments: [tx.object(managerId)],
|
|
167
|
+
});
|
|
168
|
+
};
|
|
169
|
+
|
|
170
|
+
/**
|
|
171
|
+
* @description Get the ID of the BalanceManager
|
|
172
|
+
* @param {string} managerKey The key of the BalanceManager
|
|
173
|
+
* @returns A function that takes a Transaction object
|
|
174
|
+
*/
|
|
175
|
+
id = (managerKey: string) => (tx: Transaction) => {
|
|
176
|
+
const managerId = this.#config.getBalanceManager(managerKey).address;
|
|
177
|
+
tx.moveCall({
|
|
178
|
+
target: `${this.#config.DEEPBOOK_PACKAGE_ID}::balance_manager::id`,
|
|
179
|
+
arguments: [tx.object(managerId)],
|
|
180
|
+
});
|
|
181
|
+
};
|
|
182
|
+
}
|