@mysten/deepbook-v3 1.1.5 → 1.2.0
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 +12 -0
- package/dist/client.d.mts +58 -763
- package/dist/client.d.mts.map +1 -1
- package/dist/client.mjs +253 -2127
- package/dist/client.mjs.map +1 -1
- package/dist/contracts/utils/index.mjs +1 -1
- package/dist/index.d.mts +4 -4
- package/dist/index.mjs +4 -4
- package/dist/pyth/pyth.mjs +1 -1
- package/dist/queries/accountQueries.mjs +104 -0
- package/dist/queries/accountQueries.mjs.map +1 -0
- package/dist/queries/balanceManagerQueries.mjs +113 -0
- package/dist/queries/balanceManagerQueries.mjs.map +1 -0
- package/dist/queries/context.mjs +15 -0
- package/dist/queries/context.mjs.map +1 -0
- package/dist/queries/marginManagerQueries.mjs +365 -0
- package/dist/queries/marginManagerQueries.mjs.map +1 -0
- package/dist/queries/marginPoolQueries.mjs +184 -0
- package/dist/queries/marginPoolQueries.mjs.map +1 -0
- package/dist/queries/orderQueries.mjs +165 -0
- package/dist/queries/orderQueries.mjs.map +1 -0
- package/dist/queries/poolQueries.mjs +234 -0
- package/dist/queries/poolQueries.mjs.map +1 -0
- package/dist/queries/priceFeedQueries.mjs +83 -0
- package/dist/queries/priceFeedQueries.mjs.map +1 -0
- package/dist/queries/quantityQueries.mjs +216 -0
- package/dist/queries/quantityQueries.mjs.map +1 -0
- package/dist/queries/referralQueries.mjs +96 -0
- package/dist/queries/referralQueries.mjs.map +1 -0
- package/dist/queries/registryQueries.mjs +162 -0
- package/dist/queries/registryQueries.mjs.map +1 -0
- package/dist/queries/tpslQueries.mjs +62 -0
- package/dist/queries/tpslQueries.mjs.map +1 -0
- package/dist/transactions/balanceManager.d.mts.map +1 -1
- package/dist/transactions/balanceManager.mjs +5 -4
- package/dist/transactions/balanceManager.mjs.map +1 -1
- package/dist/transactions/deepbook.d.mts +24 -24
- package/dist/transactions/deepbook.d.mts.map +1 -1
- package/dist/transactions/deepbook.mjs +48 -47
- package/dist/transactions/deepbook.mjs.map +1 -1
- package/dist/transactions/deepbookAdmin.d.mts +2 -2
- package/dist/transactions/deepbookAdmin.d.mts.map +1 -1
- package/dist/transactions/deepbookAdmin.mjs +10 -9
- package/dist/transactions/deepbookAdmin.mjs.map +1 -1
- package/dist/transactions/flashLoans.d.mts.map +1 -1
- package/dist/transactions/flashLoans.mjs +6 -4
- package/dist/transactions/flashLoans.mjs.map +1 -1
- package/dist/transactions/governance.d.mts.map +1 -1
- package/dist/transactions/governance.mjs +5 -4
- package/dist/transactions/governance.mjs.map +1 -1
- package/dist/transactions/marginAdmin.d.mts +7 -7
- package/dist/transactions/marginAdmin.d.mts.map +1 -1
- package/dist/transactions/marginAdmin.mjs +8 -7
- package/dist/transactions/marginAdmin.mjs.map +1 -1
- package/dist/transactions/marginLiquidations.d.mts +3 -3
- package/dist/transactions/marginLiquidations.d.mts.map +1 -1
- package/dist/transactions/marginLiquidations.mjs +5 -4
- package/dist/transactions/marginLiquidations.mjs.map +1 -1
- package/dist/transactions/marginMaintainer.d.mts +5 -5
- package/dist/transactions/marginMaintainer.d.mts.map +1 -1
- package/dist/transactions/marginMaintainer.mjs +15 -14
- package/dist/transactions/marginMaintainer.mjs.map +1 -1
- package/dist/transactions/marginManager.d.mts +23 -23
- package/dist/transactions/marginManager.d.mts.map +1 -1
- package/dist/transactions/marginManager.mjs +12 -11
- package/dist/transactions/marginManager.mjs.map +1 -1
- package/dist/transactions/marginPool.d.mts.map +1 -1
- package/dist/transactions/marginPool.mjs +3 -2
- package/dist/transactions/marginPool.mjs.map +1 -1
- package/dist/transactions/marginRegistry.d.mts +15 -15
- package/dist/transactions/marginTPSL.d.mts +1 -1
- package/dist/transactions/marginTPSL.d.mts.map +1 -1
- package/dist/transactions/marginTPSL.mjs +6 -8
- package/dist/transactions/marginTPSL.mjs.map +1 -1
- package/dist/transactions/poolProxy.d.mts.map +1 -1
- package/dist/transactions/poolProxy.mjs +13 -13
- package/dist/transactions/poolProxy.mjs.map +1 -1
- package/dist/types/index.d.mts +189 -52
- package/dist/types/index.d.mts.map +1 -1
- package/dist/types/index.mjs.map +1 -1
- package/dist/utils/conversion.mjs +26 -0
- package/dist/utils/conversion.mjs.map +1 -0
- package/package.json +4 -3
- package/src/client.ts +421 -2662
- package/src/index.ts +27 -0
- package/src/queries/accountQueries.ts +117 -0
- package/src/queries/balanceManagerQueries.ts +156 -0
- package/src/queries/context.ts +44 -0
- package/src/queries/index.ts +16 -0
- package/src/queries/marginManagerQueries.ts +575 -0
- package/src/queries/marginPoolQueries.ts +226 -0
- package/src/queries/orderQueries.ts +202 -0
- package/src/queries/poolQueries.ts +266 -0
- package/src/queries/priceFeedQueries.ts +141 -0
- package/src/queries/quantityQueries.ts +266 -0
- package/src/queries/referralQueries.ts +112 -0
- package/src/queries/registryQueries.ts +185 -0
- package/src/queries/tpslQueries.ts +88 -0
- package/src/transactions/balanceManager.ts +5 -4
- package/src/transactions/deepbook.ts +98 -84
- package/src/transactions/deepbookAdmin.ts +10 -9
- package/src/transactions/flashLoans.ts +5 -4
- package/src/transactions/governance.ts +5 -4
- package/src/transactions/marginAdmin.ts +8 -7
- package/src/transactions/marginLiquidations.ts +5 -4
- package/src/transactions/marginMaintainer.ts +15 -14
- package/src/transactions/marginManager.ts +12 -11
- package/src/transactions/marginPool.ts +3 -2
- package/src/transactions/marginTPSL.ts +11 -6
- package/src/transactions/poolProxy.ts +12 -11
- package/src/types/index.ts +200 -53
- package/src/utils/conversion.ts +33 -0
|
@@ -0,0 +1,226 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { bcs } from '@mysten/sui/bcs';
|
|
5
|
+
import { Transaction } from '@mysten/sui/transactions';
|
|
6
|
+
|
|
7
|
+
import { FLOAT_SCALAR } from '../utils/config.js';
|
|
8
|
+
import { formatTokenAmount } from './context.js';
|
|
9
|
+
import type { QueryContext } from './context.js';
|
|
10
|
+
|
|
11
|
+
export class MarginPoolQueries {
|
|
12
|
+
#ctx: QueryContext;
|
|
13
|
+
|
|
14
|
+
constructor(ctx: QueryContext) {
|
|
15
|
+
this.#ctx = ctx;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
async getMarginPoolId(coinKey: string): Promise<string> {
|
|
19
|
+
const tx = new Transaction();
|
|
20
|
+
tx.add(this.#ctx.marginPool.getId(coinKey));
|
|
21
|
+
|
|
22
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
23
|
+
transaction: tx,
|
|
24
|
+
include: { commandResults: true, effects: true },
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
28
|
+
return bcs.Address.parse(bytes);
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
async isDeepbookPoolAllowed(coinKey: string, deepbookPoolId: string): Promise<boolean> {
|
|
32
|
+
const tx = new Transaction();
|
|
33
|
+
tx.add(this.#ctx.marginPool.deepbookPoolAllowed(coinKey, deepbookPoolId));
|
|
34
|
+
|
|
35
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
36
|
+
transaction: tx,
|
|
37
|
+
include: { commandResults: true, effects: true },
|
|
38
|
+
});
|
|
39
|
+
|
|
40
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
41
|
+
return bcs.bool().parse(bytes);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
async getMarginPoolTotalSupply(coinKey: string, decimals: number = 6): Promise<string> {
|
|
45
|
+
const tx = new Transaction();
|
|
46
|
+
tx.add(this.#ctx.marginPool.totalSupply(coinKey));
|
|
47
|
+
|
|
48
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
49
|
+
transaction: tx,
|
|
50
|
+
include: { commandResults: true, effects: true },
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
54
|
+
const rawAmount = BigInt(bcs.U64.parse(bytes));
|
|
55
|
+
const coin = this.#ctx.config.getCoin(coinKey);
|
|
56
|
+
return formatTokenAmount(rawAmount, coin.scalar, decimals);
|
|
57
|
+
}
|
|
58
|
+
|
|
59
|
+
async getMarginPoolSupplyShares(coinKey: string, decimals: number = 6): Promise<string> {
|
|
60
|
+
const tx = new Transaction();
|
|
61
|
+
tx.add(this.#ctx.marginPool.supplyShares(coinKey));
|
|
62
|
+
|
|
63
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
64
|
+
transaction: tx,
|
|
65
|
+
include: { commandResults: true, effects: true },
|
|
66
|
+
});
|
|
67
|
+
|
|
68
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
69
|
+
const rawShares = BigInt(bcs.U64.parse(bytes));
|
|
70
|
+
const coin = this.#ctx.config.getCoin(coinKey);
|
|
71
|
+
return formatTokenAmount(rawShares, coin.scalar, decimals);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
async getMarginPoolTotalBorrow(coinKey: string, decimals: number = 6): Promise<string> {
|
|
75
|
+
const tx = new Transaction();
|
|
76
|
+
tx.add(this.#ctx.marginPool.totalBorrow(coinKey));
|
|
77
|
+
|
|
78
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
79
|
+
transaction: tx,
|
|
80
|
+
include: { commandResults: true, effects: true },
|
|
81
|
+
});
|
|
82
|
+
|
|
83
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
84
|
+
const rawAmount = BigInt(bcs.U64.parse(bytes));
|
|
85
|
+
const coin = this.#ctx.config.getCoin(coinKey);
|
|
86
|
+
return formatTokenAmount(rawAmount, coin.scalar, decimals);
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
async getMarginPoolBorrowShares(coinKey: string, decimals: number = 6): Promise<string> {
|
|
90
|
+
const tx = new Transaction();
|
|
91
|
+
tx.add(this.#ctx.marginPool.borrowShares(coinKey));
|
|
92
|
+
|
|
93
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
94
|
+
transaction: tx,
|
|
95
|
+
include: { commandResults: true, effects: true },
|
|
96
|
+
});
|
|
97
|
+
|
|
98
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
99
|
+
const rawShares = BigInt(bcs.U64.parse(bytes));
|
|
100
|
+
const coin = this.#ctx.config.getCoin(coinKey);
|
|
101
|
+
return formatTokenAmount(rawShares, coin.scalar, decimals);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
async getMarginPoolLastUpdateTimestamp(coinKey: string): Promise<number> {
|
|
105
|
+
const tx = new Transaction();
|
|
106
|
+
tx.add(this.#ctx.marginPool.lastUpdateTimestamp(coinKey));
|
|
107
|
+
|
|
108
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
109
|
+
transaction: tx,
|
|
110
|
+
include: { commandResults: true, effects: true },
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
114
|
+
return Number(bcs.U64.parse(bytes));
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
async getMarginPoolSupplyCap(coinKey: string, decimals: number = 6): Promise<string> {
|
|
118
|
+
const tx = new Transaction();
|
|
119
|
+
tx.add(this.#ctx.marginPool.supplyCap(coinKey));
|
|
120
|
+
|
|
121
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
122
|
+
transaction: tx,
|
|
123
|
+
include: { commandResults: true, effects: true },
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
127
|
+
const rawAmount = BigInt(bcs.U64.parse(bytes));
|
|
128
|
+
const coin = this.#ctx.config.getCoin(coinKey);
|
|
129
|
+
return formatTokenAmount(rawAmount, coin.scalar, decimals);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
async getMarginPoolMaxUtilizationRate(coinKey: string): Promise<number> {
|
|
133
|
+
const tx = new Transaction();
|
|
134
|
+
tx.add(this.#ctx.marginPool.maxUtilizationRate(coinKey));
|
|
135
|
+
|
|
136
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
137
|
+
transaction: tx,
|
|
138
|
+
include: { commandResults: true, effects: true },
|
|
139
|
+
});
|
|
140
|
+
|
|
141
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
142
|
+
const rawRate = Number(bcs.U64.parse(bytes));
|
|
143
|
+
return rawRate / FLOAT_SCALAR;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
async getMarginPoolProtocolSpread(coinKey: string): Promise<number> {
|
|
147
|
+
const tx = new Transaction();
|
|
148
|
+
tx.add(this.#ctx.marginPool.protocolSpread(coinKey));
|
|
149
|
+
|
|
150
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
151
|
+
transaction: tx,
|
|
152
|
+
include: { commandResults: true, effects: true },
|
|
153
|
+
});
|
|
154
|
+
|
|
155
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
156
|
+
const rawSpread = Number(bcs.U64.parse(bytes));
|
|
157
|
+
return rawSpread / FLOAT_SCALAR;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
async getMarginPoolMinBorrow(coinKey: string, decimals: number = 6): Promise<string> {
|
|
161
|
+
const tx = new Transaction();
|
|
162
|
+
tx.add(this.#ctx.marginPool.minBorrow(coinKey));
|
|
163
|
+
|
|
164
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
165
|
+
transaction: tx,
|
|
166
|
+
include: { commandResults: true, effects: true },
|
|
167
|
+
});
|
|
168
|
+
|
|
169
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
170
|
+
const rawAmount = BigInt(bcs.U64.parse(bytes));
|
|
171
|
+
const coin = this.#ctx.config.getCoin(coinKey);
|
|
172
|
+
return formatTokenAmount(rawAmount, coin.scalar, decimals);
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async getMarginPoolInterestRate(coinKey: string): Promise<number> {
|
|
176
|
+
const tx = new Transaction();
|
|
177
|
+
tx.add(this.#ctx.marginPool.interestRate(coinKey));
|
|
178
|
+
|
|
179
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
180
|
+
transaction: tx,
|
|
181
|
+
include: { commandResults: true, effects: true },
|
|
182
|
+
});
|
|
183
|
+
|
|
184
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
185
|
+
const rawRate = Number(bcs.U64.parse(bytes));
|
|
186
|
+
return rawRate / FLOAT_SCALAR;
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
async getUserSupplyShares(
|
|
190
|
+
coinKey: string,
|
|
191
|
+
supplierCapId: string,
|
|
192
|
+
decimals: number = 6,
|
|
193
|
+
): Promise<string> {
|
|
194
|
+
const tx = new Transaction();
|
|
195
|
+
tx.add(this.#ctx.marginPool.userSupplyShares(coinKey, supplierCapId));
|
|
196
|
+
|
|
197
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
198
|
+
transaction: tx,
|
|
199
|
+
include: { commandResults: true, effects: true },
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
203
|
+
const rawShares = BigInt(bcs.U64.parse(bytes));
|
|
204
|
+
const coin = this.#ctx.config.getCoin(coinKey);
|
|
205
|
+
return formatTokenAmount(rawShares, coin.scalar, decimals);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
async getUserSupplyAmount(
|
|
209
|
+
coinKey: string,
|
|
210
|
+
supplierCapId: string,
|
|
211
|
+
decimals: number = 6,
|
|
212
|
+
): Promise<string> {
|
|
213
|
+
const tx = new Transaction();
|
|
214
|
+
tx.add(this.#ctx.marginPool.userSupplyAmount(coinKey, supplierCapId));
|
|
215
|
+
|
|
216
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
217
|
+
transaction: tx,
|
|
218
|
+
include: { commandResults: true, effects: true },
|
|
219
|
+
});
|
|
220
|
+
|
|
221
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
222
|
+
const rawAmount = BigInt(bcs.U64.parse(bytes));
|
|
223
|
+
const coin = this.#ctx.config.getCoin(coinKey);
|
|
224
|
+
return formatTokenAmount(rawAmount, coin.scalar, decimals);
|
|
225
|
+
}
|
|
226
|
+
}
|
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { bcs } from '@mysten/sui/bcs';
|
|
5
|
+
import { Transaction } from '@mysten/sui/transactions';
|
|
6
|
+
|
|
7
|
+
import { Order } from '../types/bcs.js';
|
|
8
|
+
import { VecSet } from '../types/bcs.js';
|
|
9
|
+
import type { Level2Range, Level2TicksFromMid } from '../types/index.js';
|
|
10
|
+
import { DEEP_SCALAR, FLOAT_SCALAR } from '../utils/config.js';
|
|
11
|
+
import type { QueryContext } from './context.js';
|
|
12
|
+
|
|
13
|
+
export class OrderQueries {
|
|
14
|
+
#ctx: QueryContext;
|
|
15
|
+
|
|
16
|
+
constructor(ctx: QueryContext) {
|
|
17
|
+
this.#ctx = ctx;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
async accountOpenOrders(poolKey: string, managerKey: string): Promise<string[]> {
|
|
21
|
+
const tx = new Transaction();
|
|
22
|
+
|
|
23
|
+
tx.add(this.#ctx.deepBook.accountOpenOrders(poolKey, managerKey));
|
|
24
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
25
|
+
transaction: tx,
|
|
26
|
+
include: { commandResults: true, effects: true },
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
const order_ids = res.commandResults![0].returnValues[0].bcs;
|
|
30
|
+
|
|
31
|
+
return VecSet(bcs.u128()).parse(new Uint8Array(order_ids)).contents;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
async getOrder(poolKey: string, orderId: string): Promise<ReturnType<typeof Order.parse> | null> {
|
|
35
|
+
const tx = new Transaction();
|
|
36
|
+
|
|
37
|
+
tx.add(this.#ctx.deepBook.getOrder(poolKey, orderId));
|
|
38
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
39
|
+
transaction: tx,
|
|
40
|
+
include: { commandResults: true, effects: true },
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
try {
|
|
44
|
+
const orderInformation = res.commandResults![0].returnValues[0].bcs;
|
|
45
|
+
return Order.parse(new Uint8Array(orderInformation));
|
|
46
|
+
} catch {
|
|
47
|
+
return null;
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
async getOrderNormalized(poolKey: string, orderId: string) {
|
|
52
|
+
const tx = new Transaction();
|
|
53
|
+
tx.add(this.#ctx.deepBook.getOrder(poolKey, orderId));
|
|
54
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
55
|
+
transaction: tx,
|
|
56
|
+
include: { commandResults: true, effects: true },
|
|
57
|
+
});
|
|
58
|
+
|
|
59
|
+
try {
|
|
60
|
+
const orderInformation = res.commandResults![0].returnValues[0].bcs;
|
|
61
|
+
const orderInfo = Order.parse(new Uint8Array(orderInformation));
|
|
62
|
+
|
|
63
|
+
if (!orderInfo) {
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
const baseCoin = this.#ctx.config.getCoin(this.#ctx.config.getPool(poolKey).baseCoin);
|
|
67
|
+
const quoteCoin = this.#ctx.config.getCoin(this.#ctx.config.getPool(poolKey).quoteCoin);
|
|
68
|
+
|
|
69
|
+
const encodedOrderId = BigInt(orderInfo.order_id);
|
|
70
|
+
const isBid = encodedOrderId >> 127n === 0n;
|
|
71
|
+
const rawPrice = Number((encodedOrderId >> 64n) & ((1n << 63n) - 1n));
|
|
72
|
+
const normalizedPrice = (rawPrice * baseCoin.scalar) / quoteCoin.scalar / FLOAT_SCALAR;
|
|
73
|
+
|
|
74
|
+
const normalizedOrderInfo = {
|
|
75
|
+
...orderInfo,
|
|
76
|
+
quantity: String((Number(orderInfo.quantity) / baseCoin.scalar).toFixed(9)),
|
|
77
|
+
filled_quantity: String((Number(orderInfo.filled_quantity) / baseCoin.scalar).toFixed(9)),
|
|
78
|
+
order_deep_price: {
|
|
79
|
+
...orderInfo.order_deep_price,
|
|
80
|
+
deep_per_asset: String(
|
|
81
|
+
(Number(orderInfo.order_deep_price.deep_per_asset) / DEEP_SCALAR).toFixed(9),
|
|
82
|
+
),
|
|
83
|
+
},
|
|
84
|
+
isBid,
|
|
85
|
+
normalized_price: normalizedPrice.toFixed(9),
|
|
86
|
+
};
|
|
87
|
+
return normalizedOrderInfo;
|
|
88
|
+
} catch {
|
|
89
|
+
return null;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
async getOrders(
|
|
94
|
+
poolKey: string,
|
|
95
|
+
orderIds: string[],
|
|
96
|
+
): Promise<ReturnType<typeof Order.parse>[] | null> {
|
|
97
|
+
const tx = new Transaction();
|
|
98
|
+
|
|
99
|
+
tx.add(this.#ctx.deepBook.getOrders(poolKey, orderIds));
|
|
100
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
101
|
+
transaction: tx,
|
|
102
|
+
include: { commandResults: true, effects: true },
|
|
103
|
+
});
|
|
104
|
+
|
|
105
|
+
try {
|
|
106
|
+
const orderInformation = res.commandResults![0].returnValues[0].bcs;
|
|
107
|
+
return bcs.vector(Order).parse(new Uint8Array(orderInformation));
|
|
108
|
+
} catch {
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
async getLevel2Range(
|
|
114
|
+
poolKey: string,
|
|
115
|
+
priceLow: number | bigint,
|
|
116
|
+
priceHigh: number | bigint,
|
|
117
|
+
isBid: boolean,
|
|
118
|
+
): Promise<Level2Range> {
|
|
119
|
+
const tx = new Transaction();
|
|
120
|
+
const pool = this.#ctx.config.getPool(poolKey);
|
|
121
|
+
const baseCoin = this.#ctx.config.getCoin(pool.baseCoin);
|
|
122
|
+
const quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);
|
|
123
|
+
|
|
124
|
+
tx.add(this.#ctx.deepBook.getLevel2Range(poolKey, priceLow, priceHigh, isBid));
|
|
125
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
126
|
+
transaction: tx,
|
|
127
|
+
include: { commandResults: true, effects: true },
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
const prices = res.commandResults![0].returnValues[0].bcs;
|
|
131
|
+
const parsed_prices = bcs.vector(bcs.u64()).parse(new Uint8Array(prices));
|
|
132
|
+
const quantities = res.commandResults![0].returnValues[1].bcs;
|
|
133
|
+
const parsed_quantities = bcs.vector(bcs.u64()).parse(new Uint8Array(quantities));
|
|
134
|
+
|
|
135
|
+
return {
|
|
136
|
+
prices: parsed_prices.map((price) =>
|
|
137
|
+
Number(((Number(price) / FLOAT_SCALAR / quoteCoin.scalar) * baseCoin.scalar).toFixed(9)),
|
|
138
|
+
),
|
|
139
|
+
quantities: parsed_quantities.map((price) =>
|
|
140
|
+
Number((Number(price) / baseCoin.scalar).toFixed(9)),
|
|
141
|
+
),
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
async getLevel2TicksFromMid(poolKey: string, ticks: number): Promise<Level2TicksFromMid> {
|
|
146
|
+
const tx = new Transaction();
|
|
147
|
+
const pool = this.#ctx.config.getPool(poolKey);
|
|
148
|
+
const baseCoin = this.#ctx.config.getCoin(pool.baseCoin);
|
|
149
|
+
const quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);
|
|
150
|
+
|
|
151
|
+
tx.add(this.#ctx.deepBook.getLevel2TicksFromMid(poolKey, ticks));
|
|
152
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
153
|
+
transaction: tx,
|
|
154
|
+
include: { commandResults: true, effects: true },
|
|
155
|
+
});
|
|
156
|
+
|
|
157
|
+
const bid_prices = res.commandResults![0].returnValues[0].bcs;
|
|
158
|
+
const bid_parsed_prices = bcs.vector(bcs.u64()).parse(new Uint8Array(bid_prices));
|
|
159
|
+
const bid_quantities = res.commandResults![0].returnValues[1].bcs;
|
|
160
|
+
const bid_parsed_quantities = bcs.vector(bcs.u64()).parse(new Uint8Array(bid_quantities));
|
|
161
|
+
|
|
162
|
+
const ask_prices = res.commandResults![0].returnValues[2].bcs;
|
|
163
|
+
const ask_parsed_prices = bcs.vector(bcs.u64()).parse(new Uint8Array(ask_prices));
|
|
164
|
+
const ask_quantities = res.commandResults![0].returnValues[3].bcs;
|
|
165
|
+
const ask_parsed_quantities = bcs.vector(bcs.u64()).parse(new Uint8Array(ask_quantities));
|
|
166
|
+
|
|
167
|
+
return {
|
|
168
|
+
bid_prices: bid_parsed_prices.map((price) =>
|
|
169
|
+
Number(((Number(price) / FLOAT_SCALAR / quoteCoin.scalar) * baseCoin.scalar).toFixed(9)),
|
|
170
|
+
),
|
|
171
|
+
bid_quantities: bid_parsed_quantities.map((quantity) =>
|
|
172
|
+
Number((Number(quantity) / baseCoin.scalar).toFixed(9)),
|
|
173
|
+
),
|
|
174
|
+
ask_prices: ask_parsed_prices.map((price) =>
|
|
175
|
+
Number(((Number(price) / FLOAT_SCALAR / quoteCoin.scalar) * baseCoin.scalar).toFixed(9)),
|
|
176
|
+
),
|
|
177
|
+
ask_quantities: ask_parsed_quantities.map((quantity) =>
|
|
178
|
+
Number((Number(quantity) / baseCoin.scalar).toFixed(9)),
|
|
179
|
+
),
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async getAccountOrderDetails(
|
|
184
|
+
poolKey: string,
|
|
185
|
+
managerKey: string,
|
|
186
|
+
): Promise<ReturnType<typeof Order.parse>[] | []> {
|
|
187
|
+
const tx = new Transaction();
|
|
188
|
+
tx.add(this.#ctx.deepBook.getAccountOrderDetails(poolKey, managerKey));
|
|
189
|
+
|
|
190
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
191
|
+
transaction: tx,
|
|
192
|
+
include: { commandResults: true, effects: true },
|
|
193
|
+
});
|
|
194
|
+
|
|
195
|
+
try {
|
|
196
|
+
const orderInformation = res.commandResults![0].returnValues[0].bcs;
|
|
197
|
+
return bcs.vector(Order).parse(new Uint8Array(orderInformation));
|
|
198
|
+
} catch {
|
|
199
|
+
return [];
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
}
|
|
@@ -0,0 +1,266 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { bcs } from '@mysten/sui/bcs';
|
|
5
|
+
import { Transaction } from '@mysten/sui/transactions';
|
|
6
|
+
import { normalizeSuiAddress } from '@mysten/sui/utils';
|
|
7
|
+
|
|
8
|
+
import type {
|
|
9
|
+
CanPlaceLimitOrderParams,
|
|
10
|
+
CanPlaceMarketOrderParams,
|
|
11
|
+
PoolBookParams,
|
|
12
|
+
PoolTradeParams,
|
|
13
|
+
VaultBalances,
|
|
14
|
+
} from '../types/index.js';
|
|
15
|
+
import { DEEP_SCALAR, FLOAT_SCALAR } from '../utils/config.js';
|
|
16
|
+
import type { QueryContext } from './context.js';
|
|
17
|
+
|
|
18
|
+
export class PoolQueries {
|
|
19
|
+
#ctx: QueryContext;
|
|
20
|
+
|
|
21
|
+
constructor(ctx: QueryContext) {
|
|
22
|
+
this.#ctx = ctx;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
async whitelisted(poolKey: string): Promise<boolean> {
|
|
26
|
+
const tx = new Transaction();
|
|
27
|
+
|
|
28
|
+
tx.add(this.#ctx.deepBook.whitelisted(poolKey));
|
|
29
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
30
|
+
transaction: tx,
|
|
31
|
+
include: { commandResults: true, effects: true },
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
35
|
+
return bcs.Bool.parse(bytes);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
async vaultBalances(poolKey: string): Promise<VaultBalances> {
|
|
39
|
+
const tx = new Transaction();
|
|
40
|
+
const pool = this.#ctx.config.getPool(poolKey);
|
|
41
|
+
const baseScalar = this.#ctx.config.getCoin(pool.baseCoin).scalar;
|
|
42
|
+
const quoteScalar = this.#ctx.config.getCoin(pool.quoteCoin).scalar;
|
|
43
|
+
|
|
44
|
+
tx.add(this.#ctx.deepBook.vaultBalances(poolKey));
|
|
45
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
46
|
+
transaction: tx,
|
|
47
|
+
include: { commandResults: true, effects: true },
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const baseInVault = Number(bcs.U64.parse(res.commandResults![0].returnValues[0].bcs));
|
|
51
|
+
const quoteInVault = Number(bcs.U64.parse(res.commandResults![0].returnValues[1].bcs));
|
|
52
|
+
const deepInVault = Number(bcs.U64.parse(res.commandResults![0].returnValues[2].bcs));
|
|
53
|
+
|
|
54
|
+
return {
|
|
55
|
+
base: Number((baseInVault / baseScalar).toFixed(9)),
|
|
56
|
+
quote: Number((quoteInVault / quoteScalar).toFixed(9)),
|
|
57
|
+
deep: Number((deepInVault / DEEP_SCALAR).toFixed(9)),
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
async getPoolIdByAssets(baseType: string, quoteType: string): Promise<string> {
|
|
62
|
+
const tx = new Transaction();
|
|
63
|
+
tx.add(this.#ctx.deepBook.getPoolIdByAssets(baseType, quoteType));
|
|
64
|
+
|
|
65
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
66
|
+
transaction: tx,
|
|
67
|
+
include: { commandResults: true, effects: true },
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
return bcs.Address.parse(res.commandResults![0].returnValues[0].bcs);
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
async midPrice(poolKey: string): Promise<number> {
|
|
74
|
+
const tx = new Transaction();
|
|
75
|
+
const pool = this.#ctx.config.getPool(poolKey);
|
|
76
|
+
tx.add(this.#ctx.deepBook.midPrice(poolKey));
|
|
77
|
+
|
|
78
|
+
const baseCoin = this.#ctx.config.getCoin(pool.baseCoin);
|
|
79
|
+
const quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);
|
|
80
|
+
|
|
81
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
82
|
+
transaction: tx,
|
|
83
|
+
include: { commandResults: true, effects: true },
|
|
84
|
+
});
|
|
85
|
+
|
|
86
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
87
|
+
const parsed_mid_price = Number(bcs.U64.parse(bytes));
|
|
88
|
+
const adjusted_mid_price =
|
|
89
|
+
(parsed_mid_price * baseCoin.scalar) / quoteCoin.scalar / FLOAT_SCALAR;
|
|
90
|
+
|
|
91
|
+
return Number(adjusted_mid_price.toFixed(9));
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
async poolTradeParams(poolKey: string): Promise<PoolTradeParams> {
|
|
95
|
+
const tx = new Transaction();
|
|
96
|
+
|
|
97
|
+
tx.add(this.#ctx.deepBook.poolTradeParams(poolKey));
|
|
98
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
99
|
+
transaction: tx,
|
|
100
|
+
include: { commandResults: true, effects: true },
|
|
101
|
+
});
|
|
102
|
+
|
|
103
|
+
const takerFee = Number(bcs.U64.parse(res.commandResults![0].returnValues[0].bcs));
|
|
104
|
+
const makerFee = Number(bcs.U64.parse(res.commandResults![0].returnValues[1].bcs));
|
|
105
|
+
const stakeRequired = Number(bcs.U64.parse(res.commandResults![0].returnValues[2].bcs));
|
|
106
|
+
|
|
107
|
+
return {
|
|
108
|
+
takerFee: Number(takerFee / FLOAT_SCALAR),
|
|
109
|
+
makerFee: Number(makerFee / FLOAT_SCALAR),
|
|
110
|
+
stakeRequired: Number(stakeRequired / DEEP_SCALAR),
|
|
111
|
+
};
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
async poolBookParams(poolKey: string): Promise<PoolBookParams> {
|
|
115
|
+
const tx = new Transaction();
|
|
116
|
+
const pool = this.#ctx.config.getPool(poolKey);
|
|
117
|
+
const baseScalar = this.#ctx.config.getCoin(pool.baseCoin).scalar;
|
|
118
|
+
const quoteScalar = this.#ctx.config.getCoin(pool.quoteCoin).scalar;
|
|
119
|
+
|
|
120
|
+
tx.add(this.#ctx.deepBook.poolBookParams(poolKey));
|
|
121
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
122
|
+
transaction: tx,
|
|
123
|
+
include: { commandResults: true, effects: true },
|
|
124
|
+
});
|
|
125
|
+
|
|
126
|
+
const tickSize = Number(bcs.U64.parse(res.commandResults![0].returnValues[0].bcs));
|
|
127
|
+
const lotSize = Number(bcs.U64.parse(res.commandResults![0].returnValues[1].bcs));
|
|
128
|
+
const minSize = Number(bcs.U64.parse(res.commandResults![0].returnValues[2].bcs));
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
tickSize: Number((tickSize * baseScalar) / quoteScalar / FLOAT_SCALAR),
|
|
132
|
+
lotSize: Number(lotSize / baseScalar),
|
|
133
|
+
minSize: Number(minSize / baseScalar),
|
|
134
|
+
};
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
async stablePool(poolKey: string): Promise<boolean> {
|
|
138
|
+
const tx = new Transaction();
|
|
139
|
+
tx.add(this.#ctx.deepBook.stablePool(poolKey));
|
|
140
|
+
|
|
141
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
142
|
+
transaction: tx,
|
|
143
|
+
include: { commandResults: true, effects: true },
|
|
144
|
+
});
|
|
145
|
+
|
|
146
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
147
|
+
return bcs.bool().parse(bytes);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
async registeredPool(poolKey: string): Promise<boolean> {
|
|
151
|
+
const tx = new Transaction();
|
|
152
|
+
tx.add(this.#ctx.deepBook.registeredPool(poolKey));
|
|
153
|
+
|
|
154
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
155
|
+
transaction: tx,
|
|
156
|
+
include: { commandResults: true, effects: true },
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
160
|
+
return bcs.bool().parse(bytes);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
async poolTradeParamsNext(poolKey: string): Promise<PoolTradeParams> {
|
|
164
|
+
const tx = new Transaction();
|
|
165
|
+
tx.add(this.#ctx.deepBook.poolTradeParamsNext(poolKey));
|
|
166
|
+
|
|
167
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
168
|
+
transaction: tx,
|
|
169
|
+
include: { commandResults: true, effects: true },
|
|
170
|
+
});
|
|
171
|
+
|
|
172
|
+
const takerFee = Number(bcs.U64.parse(res.commandResults![0].returnValues[0].bcs));
|
|
173
|
+
const makerFee = Number(bcs.U64.parse(res.commandResults![0].returnValues[1].bcs));
|
|
174
|
+
const stakeRequired = Number(bcs.U64.parse(res.commandResults![0].returnValues[2].bcs));
|
|
175
|
+
|
|
176
|
+
return {
|
|
177
|
+
takerFee: takerFee / FLOAT_SCALAR,
|
|
178
|
+
makerFee: makerFee / FLOAT_SCALAR,
|
|
179
|
+
stakeRequired: stakeRequired / DEEP_SCALAR,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
async quorum(poolKey: string): Promise<number> {
|
|
184
|
+
const tx = new Transaction();
|
|
185
|
+
tx.add(this.#ctx.deepBook.quorum(poolKey));
|
|
186
|
+
|
|
187
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
188
|
+
transaction: tx,
|
|
189
|
+
include: { commandResults: true, effects: true },
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
193
|
+
const quorum = Number(bcs.U64.parse(bytes));
|
|
194
|
+
return quorum / DEEP_SCALAR;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
async poolId(poolKey: string): Promise<string> {
|
|
198
|
+
const tx = new Transaction();
|
|
199
|
+
tx.add(this.#ctx.deepBook.poolId(poolKey));
|
|
200
|
+
|
|
201
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
202
|
+
transaction: tx,
|
|
203
|
+
include: { commandResults: true, effects: true },
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
207
|
+
return normalizeSuiAddress(bcs.Address.parse(bytes));
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
async canPlaceLimitOrder(params: CanPlaceLimitOrderParams): Promise<boolean> {
|
|
211
|
+
const tx = new Transaction();
|
|
212
|
+
tx.add(this.#ctx.deepBook.canPlaceLimitOrder(params));
|
|
213
|
+
|
|
214
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
215
|
+
transaction: tx,
|
|
216
|
+
include: { commandResults: true, effects: true },
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
220
|
+
return bcs.bool().parse(bytes);
|
|
221
|
+
}
|
|
222
|
+
|
|
223
|
+
async canPlaceMarketOrder(params: CanPlaceMarketOrderParams): Promise<boolean> {
|
|
224
|
+
const tx = new Transaction();
|
|
225
|
+
tx.add(this.#ctx.deepBook.canPlaceMarketOrder(params));
|
|
226
|
+
|
|
227
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
228
|
+
transaction: tx,
|
|
229
|
+
include: { commandResults: true, effects: true },
|
|
230
|
+
});
|
|
231
|
+
|
|
232
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
233
|
+
return bcs.bool().parse(bytes);
|
|
234
|
+
}
|
|
235
|
+
|
|
236
|
+
async checkMarketOrderParams(poolKey: string, quantity: number | bigint): Promise<boolean> {
|
|
237
|
+
const tx = new Transaction();
|
|
238
|
+
tx.add(this.#ctx.deepBook.checkMarketOrderParams(poolKey, quantity));
|
|
239
|
+
|
|
240
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
241
|
+
transaction: tx,
|
|
242
|
+
include: { commandResults: true, effects: true },
|
|
243
|
+
});
|
|
244
|
+
|
|
245
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
246
|
+
return bcs.bool().parse(bytes);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
async checkLimitOrderParams(
|
|
250
|
+
poolKey: string,
|
|
251
|
+
price: number | bigint,
|
|
252
|
+
quantity: number | bigint,
|
|
253
|
+
expireTimestamp: number,
|
|
254
|
+
): Promise<boolean> {
|
|
255
|
+
const tx = new Transaction();
|
|
256
|
+
tx.add(this.#ctx.deepBook.checkLimitOrderParams(poolKey, price, quantity, expireTimestamp));
|
|
257
|
+
|
|
258
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
259
|
+
transaction: tx,
|
|
260
|
+
include: { commandResults: true, effects: true },
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
264
|
+
return bcs.bool().parse(bytes);
|
|
265
|
+
}
|
|
266
|
+
}
|