@haneullabs/deepbook-v3 1.0.11 → 1.2.1
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 +74 -0
- package/dist/client.d.mts +68 -787
- package/dist/client.d.mts.map +1 -1
- package/dist/client.mjs +258 -2154
- package/dist/client.mjs.map +1 -1
- package/dist/contracts/deepbook/account.d.mts +0 -1
- package/dist/contracts/deepbook/account.d.mts.map +1 -1
- package/dist/contracts/deepbook/balances.d.mts +4 -5
- package/dist/contracts/deepbook/balances.d.mts.map +1 -1
- package/dist/contracts/deepbook/deep_price.d.mts +3 -4
- package/dist/contracts/deepbook/deep_price.d.mts.map +1 -1
- package/dist/contracts/deepbook/deps/haneul/vec_set.d.mts +0 -1
- package/dist/contracts/deepbook/deps/haneul/vec_set.d.mts.map +1 -1
- package/dist/contracts/deepbook/order.d.mts +12 -13
- package/dist/contracts/deepbook/order.d.mts.map +1 -1
- package/dist/contracts/utils/index.d.mts.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/PriceServiceConnection.d.mts.map +1 -1
- package/dist/pyth/pyth.d.mts.map +1 -1
- 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 +12 -12
- 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 +12 -12
- 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 +0 -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 +0 -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 -8
- 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 -31
- package/dist/transactions/marginManager.d.mts.map +1 -1
- package/dist/transactions/marginManager.mjs +17 -27
- package/dist/transactions/marginManager.mjs.map +1 -1
- package/dist/transactions/marginPool.d.mts +18 -18
- 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/marginRegistry.d.mts.map +1 -1
- package/dist/transactions/marginTPSL.d.mts +10 -10
- 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 +5 -5
- 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 +191 -54
- package/dist/types/index.d.mts.map +1 -1
- package/dist/types/index.mjs.map +1 -1
- package/dist/utils/config.d.mts +10 -2
- package/dist/utils/config.d.mts.map +1 -1
- package/dist/utils/config.mjs +22 -6
- package/dist/utils/config.mjs.map +1 -1
- package/dist/utils/constants.d.mts +15 -7
- package/dist/utils/constants.d.mts.map +1 -1
- package/dist/utils/constants.mjs +54 -30
- package/dist/utils/constants.mjs.map +1 -1
- package/dist/utils/conversion.mjs +26 -0
- package/dist/utils/conversion.mjs.map +1 -0
- package/dist/utils/errors.d.mts.map +1 -1
- package/dist/utils/validation.d.mts.map +1 -1
- package/package.json +6 -5
- package/src/client.ts +429 -2688
- 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 +13 -40
- package/src/transactions/marginPool.ts +3 -2
- package/src/transactions/marginTPSL.ts +12 -7
- package/src/transactions/poolProxy.ts +12 -11
- package/src/types/index.ts +200 -53
- package/src/utils/config.ts +31 -8
- package/src/utils/constants.ts +60 -37
- package/src/utils/conversion.ts +33 -0
|
@@ -0,0 +1,575 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { bcs } from '@haneullabs/haneul/bcs';
|
|
5
|
+
import { Transaction } from '@haneullabs/haneul/transactions';
|
|
6
|
+
import { normalizeHaneulAddress } from '@haneullabs/haneul/utils';
|
|
7
|
+
|
|
8
|
+
import type {
|
|
9
|
+
BorrowedShares,
|
|
10
|
+
MarginManagerAssets,
|
|
11
|
+
MarginManagerBalancesResult,
|
|
12
|
+
MarginManagerDebts,
|
|
13
|
+
MarginManagerState,
|
|
14
|
+
} from '../types/index.js';
|
|
15
|
+
import { FLOAT_SCALAR } from '../utils/config.js';
|
|
16
|
+
import { formatTokenAmount } from './context.js';
|
|
17
|
+
import type { QueryContext } from './context.js';
|
|
18
|
+
|
|
19
|
+
export class MarginManagerQueries {
|
|
20
|
+
#ctx: QueryContext;
|
|
21
|
+
|
|
22
|
+
constructor(ctx: QueryContext) {
|
|
23
|
+
this.#ctx = ctx;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
async getMarginManagerOwner(marginManagerKey: string): Promise<string> {
|
|
27
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
28
|
+
const tx = new Transaction();
|
|
29
|
+
tx.add(this.#ctx.marginManager.ownerByPoolKey(manager.poolKey, manager.address));
|
|
30
|
+
|
|
31
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
32
|
+
transaction: tx,
|
|
33
|
+
include: { commandResults: true, effects: true },
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
37
|
+
return normalizeHaneulAddress(bcs.Address.parse(bytes));
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
async getMarginManagerDeepbookPool(marginManagerKey: string): Promise<string> {
|
|
41
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
42
|
+
const tx = new Transaction();
|
|
43
|
+
tx.add(this.#ctx.marginManager.deepbookPool(manager.poolKey, manager.address));
|
|
44
|
+
|
|
45
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
46
|
+
transaction: tx,
|
|
47
|
+
include: { commandResults: true, effects: true },
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
51
|
+
return normalizeHaneulAddress(bcs.Address.parse(bytes));
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
async getMarginManagerMarginPoolId(marginManagerKey: string): Promise<string | null> {
|
|
55
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
56
|
+
const tx = new Transaction();
|
|
57
|
+
tx.add(this.#ctx.marginManager.marginPoolId(manager.poolKey, manager.address));
|
|
58
|
+
|
|
59
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
60
|
+
transaction: tx,
|
|
61
|
+
include: { commandResults: true, effects: true },
|
|
62
|
+
});
|
|
63
|
+
|
|
64
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
65
|
+
const option = bcs.option(bcs.Address).parse(bytes);
|
|
66
|
+
return option ? normalizeHaneulAddress(option) : null;
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
async getMarginManagerBorrowedShares(marginManagerKey: string): Promise<BorrowedShares> {
|
|
70
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
71
|
+
const tx = new Transaction();
|
|
72
|
+
tx.add(this.#ctx.marginManager.borrowedShares(manager.poolKey, manager.address));
|
|
73
|
+
|
|
74
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
75
|
+
transaction: tx,
|
|
76
|
+
include: { commandResults: true, effects: true },
|
|
77
|
+
});
|
|
78
|
+
|
|
79
|
+
const baseBytes = res.commandResults![0].returnValues[0].bcs;
|
|
80
|
+
const quoteBytes = res.commandResults![0].returnValues[1].bcs;
|
|
81
|
+
const baseShares = bcs.U64.parse(baseBytes).toString();
|
|
82
|
+
const quoteShares = bcs.U64.parse(quoteBytes).toString();
|
|
83
|
+
|
|
84
|
+
return { baseShares, quoteShares };
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
async getMarginManagerBorrowedBaseShares(marginManagerKey: string): Promise<string> {
|
|
88
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
89
|
+
const tx = new Transaction();
|
|
90
|
+
tx.add(this.#ctx.marginManager.borrowedBaseShares(manager.poolKey, manager.address));
|
|
91
|
+
|
|
92
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
93
|
+
transaction: tx,
|
|
94
|
+
include: { commandResults: true, effects: true },
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
98
|
+
return bcs.U64.parse(bytes).toString();
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
async getMarginManagerBorrowedQuoteShares(marginManagerKey: string): Promise<string> {
|
|
102
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
103
|
+
const tx = new Transaction();
|
|
104
|
+
tx.add(this.#ctx.marginManager.borrowedQuoteShares(manager.poolKey, manager.address));
|
|
105
|
+
|
|
106
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
107
|
+
transaction: tx,
|
|
108
|
+
include: { commandResults: true, effects: true },
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
112
|
+
return bcs.U64.parse(bytes).toString();
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
async getMarginManagerHasBaseDebt(marginManagerKey: string): Promise<boolean> {
|
|
116
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
117
|
+
const tx = new Transaction();
|
|
118
|
+
tx.add(this.#ctx.marginManager.hasBaseDebt(manager.poolKey, manager.address));
|
|
119
|
+
|
|
120
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
121
|
+
transaction: tx,
|
|
122
|
+
include: { commandResults: true, effects: true },
|
|
123
|
+
});
|
|
124
|
+
|
|
125
|
+
const bytes = res.commandResults![0].returnValues[0].bcs;
|
|
126
|
+
return bcs.bool().parse(bytes);
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
async getMarginManagerBalanceManagerId(marginManagerAddress: string): Promise<string> {
|
|
130
|
+
const res = await this.#ctx.client.core.getObject({
|
|
131
|
+
objectId: marginManagerAddress,
|
|
132
|
+
include: { content: true },
|
|
133
|
+
});
|
|
134
|
+
|
|
135
|
+
if (!res.object?.content) {
|
|
136
|
+
throw new Error(`Margin manager not found: ${marginManagerAddress}`);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
const MarginManagerBalanceManagerId = bcs.struct('MarginManagerBalanceManagerId', {
|
|
140
|
+
id: bcs.Address,
|
|
141
|
+
owner: bcs.Address,
|
|
142
|
+
deepbook_pool: bcs.Address,
|
|
143
|
+
margin_pool_id: bcs.option(bcs.Address),
|
|
144
|
+
balance_manager_id: bcs.Address,
|
|
145
|
+
});
|
|
146
|
+
|
|
147
|
+
const parsed = MarginManagerBalanceManagerId.parse(res.object.content);
|
|
148
|
+
return normalizeHaneulAddress(parsed.balance_manager_id);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
async getMarginManagerAssets(
|
|
152
|
+
marginManagerKey: string,
|
|
153
|
+
decimals: number = 6,
|
|
154
|
+
): Promise<MarginManagerAssets> {
|
|
155
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
156
|
+
const tx = new Transaction();
|
|
157
|
+
tx.add(this.#ctx.marginManager.calculateAssets(manager.poolKey, manager.address));
|
|
158
|
+
|
|
159
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
160
|
+
transaction: tx,
|
|
161
|
+
include: { commandResults: true, effects: true },
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
const baseBytes = res.commandResults![0].returnValues[0].bcs;
|
|
165
|
+
const quoteBytes = res.commandResults![0].returnValues[1].bcs;
|
|
166
|
+
const pool = this.#ctx.config.getPool(manager.poolKey);
|
|
167
|
+
const baseCoin = this.#ctx.config.getCoin(pool.baseCoin);
|
|
168
|
+
const quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);
|
|
169
|
+
|
|
170
|
+
const baseAsset = formatTokenAmount(
|
|
171
|
+
BigInt(bcs.U64.parse(baseBytes)),
|
|
172
|
+
baseCoin.scalar,
|
|
173
|
+
decimals,
|
|
174
|
+
);
|
|
175
|
+
const quoteAsset = formatTokenAmount(
|
|
176
|
+
BigInt(bcs.U64.parse(quoteBytes)),
|
|
177
|
+
quoteCoin.scalar,
|
|
178
|
+
decimals,
|
|
179
|
+
);
|
|
180
|
+
|
|
181
|
+
return { baseAsset, quoteAsset };
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
async getMarginManagerDebts(
|
|
185
|
+
marginManagerKey: string,
|
|
186
|
+
decimals: number = 6,
|
|
187
|
+
): Promise<MarginManagerDebts> {
|
|
188
|
+
const hasBaseDebt = await this.getMarginManagerHasBaseDebt(marginManagerKey);
|
|
189
|
+
|
|
190
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
191
|
+
const pool = this.#ctx.config.getPool(manager.poolKey);
|
|
192
|
+
const debtCoinKey = hasBaseDebt ? pool.baseCoin : pool.quoteCoin;
|
|
193
|
+
|
|
194
|
+
const tx = new Transaction();
|
|
195
|
+
tx.add(this.#ctx.marginManager.calculateDebts(manager.poolKey, debtCoinKey, manager.address));
|
|
196
|
+
|
|
197
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
198
|
+
transaction: tx,
|
|
199
|
+
include: { commandResults: true, effects: true },
|
|
200
|
+
});
|
|
201
|
+
|
|
202
|
+
if (res.FailedTransaction) {
|
|
203
|
+
throw new Error(
|
|
204
|
+
`Transaction failed: ${res.FailedTransaction.status.error?.message || 'Unknown error'}`,
|
|
205
|
+
);
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
if (!res.commandResults || !res.commandResults[0] || !res.commandResults[0].returnValues) {
|
|
209
|
+
throw new Error(`Failed to get margin manager debts: ${'Unknown error'}`);
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
const baseBytes = res.commandResults[0].returnValues[0].bcs;
|
|
213
|
+
const quoteBytes = res.commandResults[0].returnValues[1].bcs;
|
|
214
|
+
const debtCoin = this.#ctx.config.getCoin(debtCoinKey);
|
|
215
|
+
|
|
216
|
+
const baseDebt = formatTokenAmount(BigInt(bcs.U64.parse(baseBytes)), debtCoin.scalar, decimals);
|
|
217
|
+
const quoteDebt = formatTokenAmount(
|
|
218
|
+
BigInt(bcs.U64.parse(quoteBytes)),
|
|
219
|
+
debtCoin.scalar,
|
|
220
|
+
decimals,
|
|
221
|
+
);
|
|
222
|
+
|
|
223
|
+
return { baseDebt, quoteDebt };
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
async getMarginManagerState(
|
|
227
|
+
marginManagerKey: string,
|
|
228
|
+
decimals: number = 6,
|
|
229
|
+
): Promise<MarginManagerState> {
|
|
230
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
231
|
+
const tx = new Transaction();
|
|
232
|
+
tx.add(this.#ctx.marginManager.managerState(manager.poolKey, manager.address));
|
|
233
|
+
|
|
234
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
235
|
+
transaction: tx,
|
|
236
|
+
include: { commandResults: true, effects: true },
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
if (res.FailedTransaction) {
|
|
240
|
+
throw new Error(
|
|
241
|
+
`Transaction failed: ${res.FailedTransaction.status.error?.message || 'Unknown error'}`,
|
|
242
|
+
);
|
|
243
|
+
}
|
|
244
|
+
|
|
245
|
+
if (!res.commandResults || !res.commandResults[0] || !res.commandResults[0].returnValues) {
|
|
246
|
+
throw new Error(`Failed to get margin manager state: Unknown error`);
|
|
247
|
+
}
|
|
248
|
+
|
|
249
|
+
const pool = this.#ctx.config.getPool(manager.poolKey);
|
|
250
|
+
const baseCoin = this.#ctx.config.getCoin(pool.baseCoin);
|
|
251
|
+
const quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);
|
|
252
|
+
|
|
253
|
+
const managerId = normalizeHaneulAddress(
|
|
254
|
+
bcs.Address.parse(res.commandResults[0].returnValues[0].bcs),
|
|
255
|
+
);
|
|
256
|
+
const deepbookPoolId = normalizeHaneulAddress(
|
|
257
|
+
bcs.Address.parse(res.commandResults[0].returnValues[1].bcs),
|
|
258
|
+
);
|
|
259
|
+
const riskRatio =
|
|
260
|
+
Number(bcs.U64.parse(res.commandResults[0].returnValues[2].bcs)) / FLOAT_SCALAR;
|
|
261
|
+
const baseAsset = formatTokenAmount(
|
|
262
|
+
BigInt(bcs.U64.parse(res.commandResults[0].returnValues[3].bcs)),
|
|
263
|
+
baseCoin.scalar,
|
|
264
|
+
decimals,
|
|
265
|
+
);
|
|
266
|
+
const quoteAsset = formatTokenAmount(
|
|
267
|
+
BigInt(bcs.U64.parse(res.commandResults[0].returnValues[4].bcs)),
|
|
268
|
+
quoteCoin.scalar,
|
|
269
|
+
decimals,
|
|
270
|
+
);
|
|
271
|
+
const baseDebt = formatTokenAmount(
|
|
272
|
+
BigInt(bcs.U64.parse(res.commandResults[0].returnValues[5].bcs)),
|
|
273
|
+
baseCoin.scalar,
|
|
274
|
+
decimals,
|
|
275
|
+
);
|
|
276
|
+
const quoteDebt = formatTokenAmount(
|
|
277
|
+
BigInt(bcs.U64.parse(res.commandResults[0].returnValues[6].bcs)),
|
|
278
|
+
quoteCoin.scalar,
|
|
279
|
+
decimals,
|
|
280
|
+
);
|
|
281
|
+
const basePythPrice = bcs.U64.parse(res.commandResults[0].returnValues[7].bcs);
|
|
282
|
+
const basePythDecimals = Number(
|
|
283
|
+
bcs.u8().parse(new Uint8Array(res.commandResults[0].returnValues[8].bcs)),
|
|
284
|
+
);
|
|
285
|
+
const quotePythPrice = bcs.U64.parse(res.commandResults[0].returnValues[9].bcs);
|
|
286
|
+
const quotePythDecimals = Number(
|
|
287
|
+
bcs.u8().parse(new Uint8Array(res.commandResults[0].returnValues[10].bcs)),
|
|
288
|
+
);
|
|
289
|
+
const currentPrice = BigInt(bcs.U64.parse(res.commandResults[0].returnValues[11].bcs));
|
|
290
|
+
const lowestTriggerAbovePrice = BigInt(
|
|
291
|
+
bcs.U64.parse(res.commandResults[0].returnValues[12].bcs),
|
|
292
|
+
);
|
|
293
|
+
const highestTriggerBelowPrice = BigInt(
|
|
294
|
+
bcs.U64.parse(res.commandResults[0].returnValues[13].bcs),
|
|
295
|
+
);
|
|
296
|
+
|
|
297
|
+
return {
|
|
298
|
+
managerId,
|
|
299
|
+
deepbookPoolId,
|
|
300
|
+
riskRatio,
|
|
301
|
+
baseAsset,
|
|
302
|
+
quoteAsset,
|
|
303
|
+
baseDebt,
|
|
304
|
+
quoteDebt,
|
|
305
|
+
basePythPrice: basePythPrice.toString(),
|
|
306
|
+
basePythDecimals,
|
|
307
|
+
quotePythPrice: quotePythPrice.toString(),
|
|
308
|
+
quotePythDecimals,
|
|
309
|
+
currentPrice,
|
|
310
|
+
lowestTriggerAbovePrice,
|
|
311
|
+
highestTriggerBelowPrice,
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
async getMarginManagerStates(
|
|
316
|
+
marginManagers: Record<string, string>,
|
|
317
|
+
decimals: number = 6,
|
|
318
|
+
): Promise<Record<string, MarginManagerState>> {
|
|
319
|
+
const entries = Object.entries(marginManagers);
|
|
320
|
+
if (entries.length === 0) {
|
|
321
|
+
return {};
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
const tx = new Transaction();
|
|
325
|
+
|
|
326
|
+
for (const [managerId, poolKey] of entries) {
|
|
327
|
+
tx.add(this.#ctx.marginManager.managerState(poolKey, managerId));
|
|
328
|
+
}
|
|
329
|
+
|
|
330
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
331
|
+
transaction: tx,
|
|
332
|
+
include: { commandResults: true, effects: true },
|
|
333
|
+
});
|
|
334
|
+
|
|
335
|
+
if (res.FailedTransaction) {
|
|
336
|
+
throw new Error(
|
|
337
|
+
`Transaction failed: ${res.FailedTransaction.status.error?.message || 'Unknown error'}`,
|
|
338
|
+
);
|
|
339
|
+
}
|
|
340
|
+
|
|
341
|
+
if (!res.commandResults) {
|
|
342
|
+
throw new Error(`Failed to get margin manager states: Unknown error`);
|
|
343
|
+
}
|
|
344
|
+
|
|
345
|
+
const results: Record<string, MarginManagerState> = {};
|
|
346
|
+
|
|
347
|
+
for (let i = 0; i < entries.length; i++) {
|
|
348
|
+
const commandResult = res.commandResults[i];
|
|
349
|
+
if (!commandResult || !commandResult.returnValues) {
|
|
350
|
+
throw new Error(`Failed to get margin manager state for index ${i}: No return values`);
|
|
351
|
+
}
|
|
352
|
+
|
|
353
|
+
const [, poolKey] = entries[i];
|
|
354
|
+
const pool = this.#ctx.config.getPool(poolKey);
|
|
355
|
+
const baseCoin = this.#ctx.config.getCoin(pool.baseCoin);
|
|
356
|
+
const quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);
|
|
357
|
+
|
|
358
|
+
const managerId = normalizeHaneulAddress(bcs.Address.parse(commandResult.returnValues[0].bcs));
|
|
359
|
+
const deepbookPoolId = normalizeHaneulAddress(
|
|
360
|
+
bcs.Address.parse(commandResult.returnValues[1].bcs),
|
|
361
|
+
);
|
|
362
|
+
const riskRatio = Number(bcs.U64.parse(commandResult.returnValues[2].bcs)) / FLOAT_SCALAR;
|
|
363
|
+
const baseAsset = formatTokenAmount(
|
|
364
|
+
BigInt(bcs.U64.parse(commandResult.returnValues[3].bcs)),
|
|
365
|
+
baseCoin.scalar,
|
|
366
|
+
decimals,
|
|
367
|
+
);
|
|
368
|
+
const quoteAsset = formatTokenAmount(
|
|
369
|
+
BigInt(bcs.U64.parse(commandResult.returnValues[4].bcs)),
|
|
370
|
+
quoteCoin.scalar,
|
|
371
|
+
decimals,
|
|
372
|
+
);
|
|
373
|
+
const baseDebt = formatTokenAmount(
|
|
374
|
+
BigInt(bcs.U64.parse(commandResult.returnValues[5].bcs)),
|
|
375
|
+
baseCoin.scalar,
|
|
376
|
+
decimals,
|
|
377
|
+
);
|
|
378
|
+
const quoteDebt = formatTokenAmount(
|
|
379
|
+
BigInt(bcs.U64.parse(commandResult.returnValues[6].bcs)),
|
|
380
|
+
quoteCoin.scalar,
|
|
381
|
+
decimals,
|
|
382
|
+
);
|
|
383
|
+
const basePythPrice = bcs.U64.parse(commandResult.returnValues[7].bcs);
|
|
384
|
+
const basePythDecimals = Number(
|
|
385
|
+
bcs.u8().parse(new Uint8Array(commandResult.returnValues[8].bcs)),
|
|
386
|
+
);
|
|
387
|
+
const quotePythPrice = bcs.U64.parse(commandResult.returnValues[9].bcs);
|
|
388
|
+
const quotePythDecimals = Number(
|
|
389
|
+
bcs.u8().parse(new Uint8Array(commandResult.returnValues[10].bcs)),
|
|
390
|
+
);
|
|
391
|
+
const currentPrice = BigInt(bcs.U64.parse(commandResult.returnValues[11].bcs));
|
|
392
|
+
const lowestTriggerAbovePrice = BigInt(bcs.U64.parse(commandResult.returnValues[12].bcs));
|
|
393
|
+
const highestTriggerBelowPrice = BigInt(bcs.U64.parse(commandResult.returnValues[13].bcs));
|
|
394
|
+
|
|
395
|
+
results[managerId] = {
|
|
396
|
+
managerId,
|
|
397
|
+
deepbookPoolId,
|
|
398
|
+
riskRatio,
|
|
399
|
+
baseAsset,
|
|
400
|
+
quoteAsset,
|
|
401
|
+
baseDebt,
|
|
402
|
+
quoteDebt,
|
|
403
|
+
basePythPrice: basePythPrice.toString(),
|
|
404
|
+
basePythDecimals,
|
|
405
|
+
quotePythPrice: quotePythPrice.toString(),
|
|
406
|
+
quotePythDecimals,
|
|
407
|
+
currentPrice,
|
|
408
|
+
lowestTriggerAbovePrice,
|
|
409
|
+
highestTriggerBelowPrice,
|
|
410
|
+
};
|
|
411
|
+
}
|
|
412
|
+
|
|
413
|
+
return results;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
async getMarginManagerBaseBalance(
|
|
417
|
+
marginManagerKey: string,
|
|
418
|
+
decimals: number = 9,
|
|
419
|
+
): Promise<string> {
|
|
420
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
421
|
+
const tx = new Transaction();
|
|
422
|
+
tx.add(this.#ctx.marginManager.baseBalance(manager.poolKey, manager.address));
|
|
423
|
+
|
|
424
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
425
|
+
transaction: tx,
|
|
426
|
+
include: { commandResults: true, effects: true },
|
|
427
|
+
});
|
|
428
|
+
|
|
429
|
+
if (res.FailedTransaction) {
|
|
430
|
+
throw new Error(
|
|
431
|
+
`Transaction failed: ${res.FailedTransaction.status.error?.message || 'Unknown error'}`,
|
|
432
|
+
);
|
|
433
|
+
}
|
|
434
|
+
|
|
435
|
+
if (!res.commandResults || !res.commandResults[0] || !res.commandResults[0].returnValues) {
|
|
436
|
+
throw new Error(`Failed to get margin manager base balance: Unknown error`);
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
const bytes = res.commandResults[0].returnValues[0].bcs;
|
|
440
|
+
const pool = this.#ctx.config.getPool(manager.poolKey);
|
|
441
|
+
const baseCoin = this.#ctx.config.getCoin(pool.baseCoin);
|
|
442
|
+
|
|
443
|
+
return formatTokenAmount(BigInt(bcs.U64.parse(bytes)), baseCoin.scalar, decimals);
|
|
444
|
+
}
|
|
445
|
+
|
|
446
|
+
async getMarginManagerQuoteBalance(
|
|
447
|
+
marginManagerKey: string,
|
|
448
|
+
decimals: number = 9,
|
|
449
|
+
): Promise<string> {
|
|
450
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
451
|
+
const tx = new Transaction();
|
|
452
|
+
tx.add(this.#ctx.marginManager.quoteBalance(manager.poolKey, manager.address));
|
|
453
|
+
|
|
454
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
455
|
+
transaction: tx,
|
|
456
|
+
include: { commandResults: true, effects: true },
|
|
457
|
+
});
|
|
458
|
+
|
|
459
|
+
if (res.FailedTransaction) {
|
|
460
|
+
throw new Error(
|
|
461
|
+
`Transaction failed: ${res.FailedTransaction.status.error?.message || 'Unknown error'}`,
|
|
462
|
+
);
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
if (!res.commandResults || !res.commandResults[0] || !res.commandResults[0].returnValues) {
|
|
466
|
+
throw new Error(`Failed to get margin manager quote balance: Unknown error`);
|
|
467
|
+
}
|
|
468
|
+
|
|
469
|
+
const bytes = res.commandResults[0].returnValues[0].bcs;
|
|
470
|
+
const pool = this.#ctx.config.getPool(manager.poolKey);
|
|
471
|
+
const quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);
|
|
472
|
+
|
|
473
|
+
return formatTokenAmount(BigInt(bcs.U64.parse(bytes)), quoteCoin.scalar, decimals);
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
async getMarginManagerDeepBalance(
|
|
477
|
+
marginManagerKey: string,
|
|
478
|
+
decimals: number = 6,
|
|
479
|
+
): Promise<string> {
|
|
480
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
481
|
+
const tx = new Transaction();
|
|
482
|
+
tx.add(this.#ctx.marginManager.deepBalance(manager.poolKey, manager.address));
|
|
483
|
+
|
|
484
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
485
|
+
transaction: tx,
|
|
486
|
+
include: { commandResults: true, effects: true },
|
|
487
|
+
});
|
|
488
|
+
|
|
489
|
+
if (res.FailedTransaction) {
|
|
490
|
+
throw new Error(
|
|
491
|
+
`Transaction failed: ${res.FailedTransaction.status.error?.message || 'Unknown error'}`,
|
|
492
|
+
);
|
|
493
|
+
}
|
|
494
|
+
|
|
495
|
+
if (!res.commandResults || !res.commandResults[0] || !res.commandResults[0].returnValues) {
|
|
496
|
+
throw new Error(`Failed to get margin manager DEEP balance: Unknown error`);
|
|
497
|
+
}
|
|
498
|
+
|
|
499
|
+
const bytes = res.commandResults[0].returnValues[0].bcs;
|
|
500
|
+
const deepCoin = this.#ctx.config.getCoin('DEEP');
|
|
501
|
+
|
|
502
|
+
return formatTokenAmount(BigInt(bcs.U64.parse(bytes)), deepCoin.scalar, decimals);
|
|
503
|
+
}
|
|
504
|
+
|
|
505
|
+
async getMarginManagerBalances(
|
|
506
|
+
marginManagers: Record<string, string>,
|
|
507
|
+
decimals: number = 9,
|
|
508
|
+
): Promise<Record<string, MarginManagerBalancesResult>> {
|
|
509
|
+
const entries = Object.entries(marginManagers);
|
|
510
|
+
if (entries.length === 0) {
|
|
511
|
+
return {};
|
|
512
|
+
}
|
|
513
|
+
|
|
514
|
+
const tx = new Transaction();
|
|
515
|
+
|
|
516
|
+
for (const [managerId, poolKey] of entries) {
|
|
517
|
+
tx.add(this.#ctx.marginManager.baseBalance(poolKey, managerId));
|
|
518
|
+
tx.add(this.#ctx.marginManager.quoteBalance(poolKey, managerId));
|
|
519
|
+
tx.add(this.#ctx.marginManager.deepBalance(poolKey, managerId));
|
|
520
|
+
}
|
|
521
|
+
|
|
522
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
523
|
+
transaction: tx,
|
|
524
|
+
include: { commandResults: true, effects: true },
|
|
525
|
+
});
|
|
526
|
+
|
|
527
|
+
if (res.FailedTransaction) {
|
|
528
|
+
throw new Error(
|
|
529
|
+
`Transaction failed: ${res.FailedTransaction.status.error?.message || 'Unknown error'}`,
|
|
530
|
+
);
|
|
531
|
+
}
|
|
532
|
+
|
|
533
|
+
if (!res.commandResults) {
|
|
534
|
+
throw new Error('Failed to get margin manager balances: No command results');
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
const results: Record<string, MarginManagerBalancesResult> = {};
|
|
538
|
+
const deepCoin = this.#ctx.config.getCoin('DEEP');
|
|
539
|
+
|
|
540
|
+
for (let i = 0; i < entries.length; i++) {
|
|
541
|
+
const [managerId, poolKey] = entries[i];
|
|
542
|
+
const pool = this.#ctx.config.getPool(poolKey);
|
|
543
|
+
const baseCoin = this.#ctx.config.getCoin(pool.baseCoin);
|
|
544
|
+
const quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);
|
|
545
|
+
|
|
546
|
+
const baseResult = res.commandResults[i * 3];
|
|
547
|
+
const quoteResult = res.commandResults[i * 3 + 1];
|
|
548
|
+
const deepResult = res.commandResults[i * 3 + 2];
|
|
549
|
+
|
|
550
|
+
if (!baseResult?.returnValues || !quoteResult?.returnValues || !deepResult?.returnValues) {
|
|
551
|
+
throw new Error(`Failed to get balances for margin manager ${managerId}: No return values`);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
results[managerId] = {
|
|
555
|
+
base: formatTokenAmount(
|
|
556
|
+
BigInt(bcs.U64.parse(baseResult.returnValues[0].bcs)),
|
|
557
|
+
baseCoin.scalar,
|
|
558
|
+
decimals,
|
|
559
|
+
),
|
|
560
|
+
quote: formatTokenAmount(
|
|
561
|
+
BigInt(bcs.U64.parse(quoteResult.returnValues[0].bcs)),
|
|
562
|
+
quoteCoin.scalar,
|
|
563
|
+
decimals,
|
|
564
|
+
),
|
|
565
|
+
deep: formatTokenAmount(
|
|
566
|
+
BigInt(bcs.U64.parse(deepResult.returnValues[0].bcs)),
|
|
567
|
+
deepCoin.scalar,
|
|
568
|
+
decimals,
|
|
569
|
+
),
|
|
570
|
+
};
|
|
571
|
+
}
|
|
572
|
+
|
|
573
|
+
return results;
|
|
574
|
+
}
|
|
575
|
+
}
|