@mysten/deepbook-v3 1.1.5 → 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 +23 -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 +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 +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.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 +17 -12
- 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/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/constants.d.mts +4 -0
- package/dist/utils/constants.d.mts.map +1 -1
- package/dist/utils/constants.mjs +12 -5
- package/dist/utils/constants.mjs.map +1 -1
- package/dist/utils/conversion.mjs +26 -0
- package/dist/utils/conversion.mjs.map +1 -0
- package/package.json +6 -5
- 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 +13 -12
- 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/constants.ts +12 -5
- package/src/utils/conversion.ts +33 -0
|
@@ -0,0 +1,104 @@
|
|
|
1
|
+
import { Account } from "../contracts/deepbook/account.mjs";
|
|
2
|
+
import { OrderDeepPrice } from "../contracts/deepbook/deep_price.mjs";
|
|
3
|
+
import "../types/bcs.mjs";
|
|
4
|
+
import { DEEP_SCALAR, FLOAT_SCALAR } from "../utils/config.mjs";
|
|
5
|
+
import { bcs } from "@mysten/sui/bcs";
|
|
6
|
+
import { Transaction } from "@mysten/sui/transactions";
|
|
7
|
+
|
|
8
|
+
//#region src/queries/accountQueries.ts
|
|
9
|
+
var AccountQueries = class {
|
|
10
|
+
#ctx;
|
|
11
|
+
constructor(ctx) {
|
|
12
|
+
this.#ctx = ctx;
|
|
13
|
+
}
|
|
14
|
+
async account(poolKey, managerKey) {
|
|
15
|
+
const tx = new Transaction();
|
|
16
|
+
const pool = this.#ctx.config.getPool(poolKey);
|
|
17
|
+
const baseScalar = this.#ctx.config.getCoin(pool.baseCoin).scalar;
|
|
18
|
+
const quoteScalar = this.#ctx.config.getCoin(pool.quoteCoin).scalar;
|
|
19
|
+
tx.add(this.#ctx.deepBook.account(poolKey, managerKey));
|
|
20
|
+
const accountInformation = (await this.#ctx.client.core.simulateTransaction({
|
|
21
|
+
transaction: tx,
|
|
22
|
+
include: {
|
|
23
|
+
commandResults: true,
|
|
24
|
+
effects: true
|
|
25
|
+
}
|
|
26
|
+
})).commandResults[0].returnValues[0].bcs;
|
|
27
|
+
const accountInfo = Account.parse(new Uint8Array(accountInformation));
|
|
28
|
+
return {
|
|
29
|
+
epoch: accountInfo.epoch,
|
|
30
|
+
open_orders: accountInfo.open_orders,
|
|
31
|
+
taker_volume: Number(accountInfo.taker_volume) / baseScalar,
|
|
32
|
+
maker_volume: Number(accountInfo.maker_volume) / baseScalar,
|
|
33
|
+
active_stake: Number(accountInfo.active_stake) / DEEP_SCALAR,
|
|
34
|
+
inactive_stake: Number(accountInfo.inactive_stake) / DEEP_SCALAR,
|
|
35
|
+
created_proposal: accountInfo.created_proposal,
|
|
36
|
+
voted_proposal: accountInfo.voted_proposal,
|
|
37
|
+
unclaimed_rebates: {
|
|
38
|
+
base: Number(accountInfo.unclaimed_rebates.base) / baseScalar,
|
|
39
|
+
quote: Number(accountInfo.unclaimed_rebates.quote) / quoteScalar,
|
|
40
|
+
deep: Number(accountInfo.unclaimed_rebates.deep) / DEEP_SCALAR
|
|
41
|
+
},
|
|
42
|
+
settled_balances: {
|
|
43
|
+
base: Number(accountInfo.settled_balances.base) / baseScalar,
|
|
44
|
+
quote: Number(accountInfo.settled_balances.quote) / quoteScalar,
|
|
45
|
+
deep: Number(accountInfo.settled_balances.deep) / DEEP_SCALAR
|
|
46
|
+
},
|
|
47
|
+
owed_balances: {
|
|
48
|
+
base: Number(accountInfo.owed_balances.base) / baseScalar,
|
|
49
|
+
quote: Number(accountInfo.owed_balances.quote) / quoteScalar,
|
|
50
|
+
deep: Number(accountInfo.owed_balances.deep) / DEEP_SCALAR
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
async lockedBalance(poolKey, balanceManagerKey) {
|
|
55
|
+
const tx = new Transaction();
|
|
56
|
+
const pool = this.#ctx.config.getPool(poolKey);
|
|
57
|
+
const baseScalar = this.#ctx.config.getCoin(pool.baseCoin).scalar;
|
|
58
|
+
const quoteScalar = this.#ctx.config.getCoin(pool.quoteCoin).scalar;
|
|
59
|
+
tx.add(this.#ctx.deepBook.lockedBalance(poolKey, balanceManagerKey));
|
|
60
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
61
|
+
transaction: tx,
|
|
62
|
+
include: {
|
|
63
|
+
commandResults: true,
|
|
64
|
+
effects: true
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
const baseLocked = Number(bcs.U64.parse(res.commandResults[0].returnValues[0].bcs));
|
|
68
|
+
const quoteLocked = Number(bcs.U64.parse(res.commandResults[0].returnValues[1].bcs));
|
|
69
|
+
const deepLocked = Number(bcs.U64.parse(res.commandResults[0].returnValues[2].bcs));
|
|
70
|
+
return {
|
|
71
|
+
base: Number((baseLocked / baseScalar).toFixed(9)),
|
|
72
|
+
quote: Number((quoteLocked / quoteScalar).toFixed(9)),
|
|
73
|
+
deep: Number((deepLocked / DEEP_SCALAR).toFixed(9))
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
async getPoolDeepPrice(poolKey) {
|
|
77
|
+
const tx = new Transaction();
|
|
78
|
+
const pool = this.#ctx.config.getPool(poolKey);
|
|
79
|
+
tx.add(this.#ctx.deepBook.getPoolDeepPrice(poolKey));
|
|
80
|
+
const baseCoin = this.#ctx.config.getCoin(pool.baseCoin);
|
|
81
|
+
const quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);
|
|
82
|
+
const deepCoin = this.#ctx.config.getCoin("DEEP");
|
|
83
|
+
const poolDeepPriceBytes = (await this.#ctx.client.core.simulateTransaction({
|
|
84
|
+
transaction: tx,
|
|
85
|
+
include: {
|
|
86
|
+
commandResults: true,
|
|
87
|
+
effects: true
|
|
88
|
+
}
|
|
89
|
+
})).commandResults[0].returnValues[0].bcs;
|
|
90
|
+
const poolDeepPrice = OrderDeepPrice.parse(new Uint8Array(poolDeepPriceBytes));
|
|
91
|
+
if (poolDeepPrice.asset_is_base) return {
|
|
92
|
+
asset_is_base: poolDeepPrice.asset_is_base,
|
|
93
|
+
deep_per_base: Number(poolDeepPrice.deep_per_asset) / FLOAT_SCALAR * baseCoin.scalar / deepCoin.scalar
|
|
94
|
+
};
|
|
95
|
+
else return {
|
|
96
|
+
asset_is_base: poolDeepPrice.asset_is_base,
|
|
97
|
+
deep_per_quote: Number(poolDeepPrice.deep_per_asset) / FLOAT_SCALAR * quoteCoin.scalar / deepCoin.scalar
|
|
98
|
+
};
|
|
99
|
+
}
|
|
100
|
+
};
|
|
101
|
+
|
|
102
|
+
//#endregion
|
|
103
|
+
export { AccountQueries };
|
|
104
|
+
//# sourceMappingURL=accountQueries.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"accountQueries.mjs","names":["#ctx"],"sources":["../../src/queries/accountQueries.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { bcs } from '@mysten/sui/bcs';\nimport { Transaction } from '@mysten/sui/transactions';\n\nimport { Account, OrderDeepPrice } from '../types/bcs.js';\nimport type { AccountInfo, LockedBalances, PoolDeepPrice } from '../types/index.js';\nimport { DEEP_SCALAR, FLOAT_SCALAR } from '../utils/config.js';\nimport type { QueryContext } from './context.js';\n\nexport class AccountQueries {\n\t#ctx: QueryContext;\n\n\tconstructor(ctx: QueryContext) {\n\t\tthis.#ctx = ctx;\n\t}\n\n\tasync account(poolKey: string, managerKey: string): Promise<AccountInfo> {\n\t\tconst tx = new Transaction();\n\t\tconst pool = this.#ctx.config.getPool(poolKey);\n\t\tconst baseScalar = this.#ctx.config.getCoin(pool.baseCoin).scalar;\n\t\tconst quoteScalar = this.#ctx.config.getCoin(pool.quoteCoin).scalar;\n\n\t\ttx.add(this.#ctx.deepBook.account(poolKey, managerKey));\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst accountInformation = res.commandResults![0].returnValues[0].bcs;\n\t\tconst accountInfo = Account.parse(new Uint8Array(accountInformation));\n\n\t\treturn {\n\t\t\tepoch: accountInfo.epoch,\n\t\t\topen_orders: accountInfo.open_orders,\n\t\t\ttaker_volume: Number(accountInfo.taker_volume) / baseScalar,\n\t\t\tmaker_volume: Number(accountInfo.maker_volume) / baseScalar,\n\t\t\tactive_stake: Number(accountInfo.active_stake) / DEEP_SCALAR,\n\t\t\tinactive_stake: Number(accountInfo.inactive_stake) / DEEP_SCALAR,\n\t\t\tcreated_proposal: accountInfo.created_proposal,\n\t\t\tvoted_proposal: accountInfo.voted_proposal,\n\t\t\tunclaimed_rebates: {\n\t\t\t\tbase: Number(accountInfo.unclaimed_rebates.base) / baseScalar,\n\t\t\t\tquote: Number(accountInfo.unclaimed_rebates.quote) / quoteScalar,\n\t\t\t\tdeep: Number(accountInfo.unclaimed_rebates.deep) / DEEP_SCALAR,\n\t\t\t},\n\t\t\tsettled_balances: {\n\t\t\t\tbase: Number(accountInfo.settled_balances.base) / baseScalar,\n\t\t\t\tquote: Number(accountInfo.settled_balances.quote) / quoteScalar,\n\t\t\t\tdeep: Number(accountInfo.settled_balances.deep) / DEEP_SCALAR,\n\t\t\t},\n\t\t\towed_balances: {\n\t\t\t\tbase: Number(accountInfo.owed_balances.base) / baseScalar,\n\t\t\t\tquote: Number(accountInfo.owed_balances.quote) / quoteScalar,\n\t\t\t\tdeep: Number(accountInfo.owed_balances.deep) / DEEP_SCALAR,\n\t\t\t},\n\t\t};\n\t}\n\n\tasync lockedBalance(poolKey: string, balanceManagerKey: string): Promise<LockedBalances> {\n\t\tconst tx = new Transaction();\n\t\tconst pool = this.#ctx.config.getPool(poolKey);\n\t\tconst baseScalar = this.#ctx.config.getCoin(pool.baseCoin).scalar;\n\t\tconst quoteScalar = this.#ctx.config.getCoin(pool.quoteCoin).scalar;\n\n\t\ttx.add(this.#ctx.deepBook.lockedBalance(poolKey, balanceManagerKey));\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst baseLocked = Number(bcs.U64.parse(res.commandResults![0].returnValues[0].bcs));\n\t\tconst quoteLocked = Number(bcs.U64.parse(res.commandResults![0].returnValues[1].bcs));\n\t\tconst deepLocked = Number(bcs.U64.parse(res.commandResults![0].returnValues[2].bcs));\n\n\t\treturn {\n\t\t\tbase: Number((baseLocked / baseScalar).toFixed(9)),\n\t\t\tquote: Number((quoteLocked / quoteScalar).toFixed(9)),\n\t\t\tdeep: Number((deepLocked / DEEP_SCALAR).toFixed(9)),\n\t\t};\n\t}\n\n\tasync getPoolDeepPrice(poolKey: string): Promise<PoolDeepPrice> {\n\t\tconst tx = new Transaction();\n\t\tconst pool = this.#ctx.config.getPool(poolKey);\n\t\ttx.add(this.#ctx.deepBook.getPoolDeepPrice(poolKey));\n\n\t\tconst baseCoin = this.#ctx.config.getCoin(pool.baseCoin);\n\t\tconst quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);\n\t\tconst deepCoin = this.#ctx.config.getCoin('DEEP');\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst poolDeepPriceBytes = res.commandResults![0].returnValues[0].bcs;\n\t\tconst poolDeepPrice = OrderDeepPrice.parse(new Uint8Array(poolDeepPriceBytes));\n\n\t\tif (poolDeepPrice.asset_is_base) {\n\t\t\treturn {\n\t\t\t\tasset_is_base: poolDeepPrice.asset_is_base,\n\t\t\t\tdeep_per_base:\n\t\t\t\t\t((Number(poolDeepPrice.deep_per_asset) / FLOAT_SCALAR) * baseCoin.scalar) /\n\t\t\t\t\tdeepCoin.scalar,\n\t\t\t};\n\t\t} else {\n\t\t\treturn {\n\t\t\t\tasset_is_base: poolDeepPrice.asset_is_base,\n\t\t\t\tdeep_per_quote:\n\t\t\t\t\t((Number(poolDeepPrice.deep_per_asset) / FLOAT_SCALAR) * quoteCoin.scalar) /\n\t\t\t\t\tdeepCoin.scalar,\n\t\t\t};\n\t\t}\n\t}\n}\n"],"mappings":";;;;;;;;AAWA,IAAa,iBAAb,MAA4B;CAC3B;CAEA,YAAY,KAAmB;AAC9B,QAAKA,MAAO;;CAGb,MAAM,QAAQ,SAAiB,YAA0C;EACxE,MAAM,KAAK,IAAI,aAAa;EAC5B,MAAM,OAAO,MAAKA,IAAK,OAAO,QAAQ,QAAQ;EAC9C,MAAM,aAAa,MAAKA,IAAK,OAAO,QAAQ,KAAK,SAAS,CAAC;EAC3D,MAAM,cAAc,MAAKA,IAAK,OAAO,QAAQ,KAAK,UAAU,CAAC;AAE7D,KAAG,IAAI,MAAKA,IAAK,SAAS,QAAQ,SAAS,WAAW,CAAC;EAMvD,MAAM,sBALM,MAAM,MAAKA,IAAK,OAAO,KAAK,oBAAoB;GAC3D,aAAa;GACb,SAAS;IAAE,gBAAgB;IAAM,SAAS;IAAM;GAChD,CAAC,EAE6B,eAAgB,GAAG,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2
|
+
import { Transaction } from "@mysten/sui/transactions";
|
|
3
|
+
import { normalizeSuiAddress } from "@mysten/sui/utils";
|
|
4
|
+
|
|
5
|
+
//#region src/queries/balanceManagerQueries.ts
|
|
6
|
+
var BalanceManagerQueries = class {
|
|
7
|
+
#ctx;
|
|
8
|
+
constructor(ctx) {
|
|
9
|
+
this.#ctx = ctx;
|
|
10
|
+
}
|
|
11
|
+
async checkManagerBalance(managerKey, coinKey) {
|
|
12
|
+
const tx = new Transaction();
|
|
13
|
+
const coin = this.#ctx.config.getCoin(coinKey);
|
|
14
|
+
tx.add(this.#ctx.balanceManager.checkManagerBalance(managerKey, coinKey));
|
|
15
|
+
const bytes = (await this.#ctx.client.core.simulateTransaction({
|
|
16
|
+
transaction: tx,
|
|
17
|
+
include: {
|
|
18
|
+
commandResults: true,
|
|
19
|
+
effects: true
|
|
20
|
+
}
|
|
21
|
+
})).commandResults[0].returnValues[0].bcs;
|
|
22
|
+
const parsed_balance = bcs.U64.parse(bytes);
|
|
23
|
+
const adjusted_balance = Number(parsed_balance) / coin.scalar;
|
|
24
|
+
return {
|
|
25
|
+
coinType: coin.type,
|
|
26
|
+
balance: Number(adjusted_balance.toFixed(9))
|
|
27
|
+
};
|
|
28
|
+
}
|
|
29
|
+
async checkManagerBalanceWithAddress(managerAddress, coinKey) {
|
|
30
|
+
const tx = new Transaction();
|
|
31
|
+
const coin = this.#ctx.config.getCoin(coinKey);
|
|
32
|
+
tx.moveCall({
|
|
33
|
+
target: `${this.#ctx.config.DEEPBOOK_PACKAGE_ID}::balance_manager::balance`,
|
|
34
|
+
arguments: [tx.object(managerAddress)],
|
|
35
|
+
typeArguments: [coin.type]
|
|
36
|
+
});
|
|
37
|
+
const bytes = (await this.#ctx.client.core.simulateTransaction({
|
|
38
|
+
transaction: tx,
|
|
39
|
+
include: {
|
|
40
|
+
commandResults: true,
|
|
41
|
+
effects: true
|
|
42
|
+
}
|
|
43
|
+
})).commandResults[0].returnValues[0].bcs;
|
|
44
|
+
const parsed_balance = bcs.U64.parse(bytes);
|
|
45
|
+
const adjusted_balance = Number(parsed_balance) / coin.scalar;
|
|
46
|
+
return {
|
|
47
|
+
coinType: coin.type,
|
|
48
|
+
balance: Number(adjusted_balance.toFixed(9))
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
async checkManagerBalancesWithAddress(managerAddresses, coinKeys) {
|
|
52
|
+
if (managerAddresses.length === 0 || coinKeys.length === 0) return {};
|
|
53
|
+
const tx = new Transaction();
|
|
54
|
+
const coins = coinKeys.map((coinKey) => this.#ctx.config.getCoin(coinKey));
|
|
55
|
+
for (const managerAddress of managerAddresses) for (const coin of coins) tx.moveCall({
|
|
56
|
+
target: `${this.#ctx.config.DEEPBOOK_PACKAGE_ID}::balance_manager::balance`,
|
|
57
|
+
arguments: [tx.object(managerAddress)],
|
|
58
|
+
typeArguments: [coin.type]
|
|
59
|
+
});
|
|
60
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
61
|
+
transaction: tx,
|
|
62
|
+
include: {
|
|
63
|
+
commandResults: true,
|
|
64
|
+
effects: true
|
|
65
|
+
}
|
|
66
|
+
});
|
|
67
|
+
if (res.FailedTransaction) throw new Error(`Transaction failed: ${res.FailedTransaction.status.error?.message || "Unknown error"}`);
|
|
68
|
+
if (!res.commandResults) throw new Error("Failed to get manager balances: No command results");
|
|
69
|
+
const results = {};
|
|
70
|
+
for (let m = 0; m < managerAddresses.length; m++) {
|
|
71
|
+
const managerAddress = managerAddresses[m];
|
|
72
|
+
const managerBalances = {};
|
|
73
|
+
for (let c = 0; c < coins.length; c++) {
|
|
74
|
+
const coin = coins[c];
|
|
75
|
+
const commandResult = res.commandResults[m * coins.length + c];
|
|
76
|
+
if (!commandResult || !commandResult.returnValues) throw new Error(`Failed to get balance for ${coin.type}: No return values`);
|
|
77
|
+
const bytes = commandResult.returnValues[0].bcs;
|
|
78
|
+
const parsed_balance = bcs.U64.parse(bytes);
|
|
79
|
+
managerBalances[coin.type] = Number((Number(parsed_balance) / coin.scalar).toFixed(9));
|
|
80
|
+
}
|
|
81
|
+
results[managerAddress] = managerBalances;
|
|
82
|
+
}
|
|
83
|
+
return results;
|
|
84
|
+
}
|
|
85
|
+
async getBalanceManagerIds(owner) {
|
|
86
|
+
const tx = new Transaction();
|
|
87
|
+
tx.add(this.#ctx.deepBook.getBalanceManagerIds(owner));
|
|
88
|
+
const bytes = (await this.#ctx.client.core.simulateTransaction({
|
|
89
|
+
transaction: tx,
|
|
90
|
+
include: {
|
|
91
|
+
commandResults: true,
|
|
92
|
+
effects: true
|
|
93
|
+
}
|
|
94
|
+
})).commandResults[0].returnValues[0].bcs;
|
|
95
|
+
return bcs.vector(bcs.Address).parse(bytes).map((id) => normalizeSuiAddress(id));
|
|
96
|
+
}
|
|
97
|
+
async accountExists(poolKey, managerKey) {
|
|
98
|
+
const tx = new Transaction();
|
|
99
|
+
tx.add(this.#ctx.deepBook.accountExists(poolKey, managerKey));
|
|
100
|
+
const bytes = (await this.#ctx.client.core.simulateTransaction({
|
|
101
|
+
transaction: tx,
|
|
102
|
+
include: {
|
|
103
|
+
commandResults: true,
|
|
104
|
+
effects: true
|
|
105
|
+
}
|
|
106
|
+
})).commandResults[0].returnValues[0].bcs;
|
|
107
|
+
return bcs.bool().parse(bytes);
|
|
108
|
+
}
|
|
109
|
+
};
|
|
110
|
+
|
|
111
|
+
//#endregion
|
|
112
|
+
export { BalanceManagerQueries };
|
|
113
|
+
//# sourceMappingURL=balanceManagerQueries.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"balanceManagerQueries.mjs","names":["#ctx"],"sources":["../../src/queries/balanceManagerQueries.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { bcs } from '@mysten/sui/bcs';\nimport { Transaction } from '@mysten/sui/transactions';\nimport { normalizeSuiAddress } from '@mysten/sui/utils';\n\nimport type { ManagerBalance } from '../types/index.js';\nimport type { QueryContext } from './context.js';\n\nexport class BalanceManagerQueries {\n\t#ctx: QueryContext;\n\n\tconstructor(ctx: QueryContext) {\n\t\tthis.#ctx = ctx;\n\t}\n\n\tasync checkManagerBalance(managerKey: string, coinKey: string): Promise<ManagerBalance> {\n\t\tconst tx = new Transaction();\n\t\tconst coin = this.#ctx.config.getCoin(coinKey);\n\n\t\ttx.add(this.#ctx.balanceManager.checkManagerBalance(managerKey, coinKey));\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\tconst parsed_balance = bcs.U64.parse(bytes);\n\t\tconst balanceNumber = Number(parsed_balance);\n\t\tconst adjusted_balance = balanceNumber / coin.scalar;\n\n\t\treturn {\n\t\t\tcoinType: coin.type,\n\t\t\tbalance: Number(adjusted_balance.toFixed(9)),\n\t\t};\n\t}\n\n\tasync checkManagerBalanceWithAddress(\n\t\tmanagerAddress: string,\n\t\tcoinKey: string,\n\t): Promise<ManagerBalance> {\n\t\tconst tx = new Transaction();\n\t\tconst coin = this.#ctx.config.getCoin(coinKey);\n\n\t\ttx.moveCall({\n\t\t\ttarget: `${this.#ctx.config.DEEPBOOK_PACKAGE_ID}::balance_manager::balance`,\n\t\t\targuments: [tx.object(managerAddress)],\n\t\t\ttypeArguments: [coin.type],\n\t\t});\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\tconst parsed_balance = bcs.U64.parse(bytes);\n\t\tconst balanceNumber = Number(parsed_balance);\n\t\tconst adjusted_balance = balanceNumber / coin.scalar;\n\n\t\treturn {\n\t\t\tcoinType: coin.type,\n\t\t\tbalance: Number(adjusted_balance.toFixed(9)),\n\t\t};\n\t}\n\n\tasync checkManagerBalancesWithAddress(\n\t\tmanagerAddresses: string[],\n\t\tcoinKeys: string[],\n\t): Promise<Record<string, Record<string, number>>> {\n\t\tif (managerAddresses.length === 0 || coinKeys.length === 0) {\n\t\t\treturn {};\n\t\t}\n\n\t\tconst tx = new Transaction();\n\t\tconst coins = coinKeys.map((coinKey) => this.#ctx.config.getCoin(coinKey));\n\n\t\tfor (const managerAddress of managerAddresses) {\n\t\t\tfor (const coin of coins) {\n\t\t\t\ttx.moveCall({\n\t\t\t\t\ttarget: `${this.#ctx.config.DEEPBOOK_PACKAGE_ID}::balance_manager::balance`,\n\t\t\t\t\targuments: [tx.object(managerAddress)],\n\t\t\t\t\ttypeArguments: [coin.type],\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tif (res.FailedTransaction) {\n\t\t\tthrow new Error(\n\t\t\t\t`Transaction failed: ${res.FailedTransaction.status.error?.message || 'Unknown error'}`,\n\t\t\t);\n\t\t}\n\n\t\tif (!res.commandResults) {\n\t\t\tthrow new Error('Failed to get manager balances: No command results');\n\t\t}\n\n\t\tconst results: Record<string, Record<string, number>> = {};\n\n\t\tfor (let m = 0; m < managerAddresses.length; m++) {\n\t\t\tconst managerAddress = managerAddresses[m];\n\t\t\tconst managerBalances: Record<string, number> = {};\n\n\t\t\tfor (let c = 0; c < coins.length; c++) {\n\t\t\t\tconst coin = coins[c];\n\t\t\t\tconst commandResult = res.commandResults[m * coins.length + c];\n\n\t\t\t\tif (!commandResult || !commandResult.returnValues) {\n\t\t\t\t\tthrow new Error(`Failed to get balance for ${coin.type}: No return values`);\n\t\t\t\t}\n\n\t\t\t\tconst bytes = commandResult.returnValues[0].bcs;\n\t\t\t\tconst parsed_balance = bcs.U64.parse(bytes);\n\t\t\t\tmanagerBalances[coin.type] = Number((Number(parsed_balance) / coin.scalar).toFixed(9));\n\t\t\t}\n\n\t\t\tresults[managerAddress] = managerBalances;\n\t\t}\n\n\t\treturn results;\n\t}\n\n\tasync getBalanceManagerIds(owner: string): Promise<string[]> {\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.deepBook.getBalanceManagerIds(owner));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\tconst vecOfAddresses = bcs.vector(bcs.Address).parse(bytes);\n\n\t\treturn vecOfAddresses.map((id: string) => normalizeSuiAddress(id));\n\t}\n\n\tasync accountExists(poolKey: string, managerKey: string): Promise<boolean> {\n\t\tconst tx = new Transaction();\n\t\ttx.add(this.#ctx.deepBook.accountExists(poolKey, managerKey));\n\n\t\tconst res = await this.#ctx.client.core.simulateTransaction({\n\t\t\ttransaction: tx,\n\t\t\tinclude: { commandResults: true, effects: true },\n\t\t});\n\n\t\tconst bytes = res.commandResults![0].returnValues[0].bcs;\n\t\treturn bcs.bool().parse(bytes);\n\t}\n}\n"],"mappings":";;;;;AAUA,IAAa,wBAAb,MAAmC;CAClC;CAEA,YAAY,KAAmB;AAC9B,QAAKA,MAAO;;CAGb,MAAM,oBAAoB,YAAoB,SAA0C;EACvF,MAAM,KAAK,IAAI,aAAa;EAC5B,MAAM,OAAO,MAAKA,IAAK,OAAO,QAAQ,QAAQ;AAE9C,KAAG,IAAI,MAAKA,IAAK,eAAe,oBAAoB,YAAY,QAAQ,CAAC;EAMzE,MAAM,SALM,MAAM,MAAKA,IAAK,OAAO,KAAK,oBAAoB;GAC3D,aAAa;GACb,SAAS;IAAE,gBAAgB;IAAM,SAAS;IAAM;GAChD,CAAC,EAEgB,eAAgB,GAAG,aAAa,GAAG;EACrD,MAAM,iBAAiB,IAAI,IAAI,MAAM,MAAM;EAE3C,MAAM,mBADgB,OAAO,eAAe,GACH,KAAK;AAE9C,SAAO;GACN,UAAU,KAAK;GACf,SAAS,O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SAAS;IAAE,gBAAgB;IAAM,SAAS;IAAM;GAChD,CAAC,EAEgB,eAAgB,GAAG,aAAa,GAAG;AACrD,SAAO,IAAI,MAAM,CAAC,MAAM,MAAM"}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
//#region src/queries/context.ts
|
|
2
|
+
function formatTokenAmount(rawAmount, scalar, decimals) {
|
|
3
|
+
const scalarBigInt = BigInt(scalar);
|
|
4
|
+
const integerPart = rawAmount / scalarBigInt;
|
|
5
|
+
const fractionalPart = rawAmount % scalarBigInt;
|
|
6
|
+
if (fractionalPart === 0n) return integerPart.toString();
|
|
7
|
+
const scalarDigits = scalar.toString().length - 1;
|
|
8
|
+
const trimmed = fractionalPart.toString().padStart(scalarDigits, "0").slice(0, decimals).replace(/0+$/, "");
|
|
9
|
+
if (!trimmed) return integerPart.toString();
|
|
10
|
+
return `${integerPart}.${trimmed}`;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
//#endregion
|
|
14
|
+
export { formatTokenAmount };
|
|
15
|
+
//# sourceMappingURL=context.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"context.mjs","names":[],"sources":["../../src/queries/context.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { DeepBookCompatibleClient } from '../client.js';\nimport type { BalanceManagerContract } from '../transactions/balanceManager.js';\nimport type { DeepBookContract } from '../transactions/deepbook.js';\nimport type { MarginManagerContract } from '../transactions/marginManager.js';\nimport type { MarginPoolContract } from '../transactions/marginPool.js';\nimport type { MarginRegistryContract } from '../transactions/marginRegistry.js';\nimport type { MarginTPSLContract } from '../transactions/marginTPSL.js';\nimport type { DeepBookConfig } from '../utils/config.js';\n\nexport interface QueryContext {\n\tclient: DeepBookCompatibleClient;\n\tconfig: DeepBookConfig;\n\taddress: string;\n\tbalanceManager: BalanceManagerContract;\n\tdeepBook: DeepBookContract;\n\tmarginManager: MarginManagerContract;\n\tmarginPool: MarginPoolContract;\n\tmarginRegistry: MarginRegistryContract;\n\tmarginTPSL: MarginTPSLContract;\n}\n\nexport function formatTokenAmount(rawAmount: bigint, scalar: number, decimals: number): string {\n\tconst scalarBigInt = BigInt(scalar);\n\tconst integerPart = rawAmount / scalarBigInt;\n\tconst fractionalPart = rawAmount % scalarBigInt;\n\n\tif (fractionalPart === 0n) {\n\t\treturn integerPart.toString();\n\t}\n\n\tconst scalarDigits = scalar.toString().length - 1;\n\tconst fractionalStr = fractionalPart.toString().padStart(scalarDigits, '0');\n\tconst truncated = fractionalStr.slice(0, decimals);\n\tconst trimmed = truncated.replace(/0+$/, '');\n\n\tif (!trimmed) {\n\t\treturn integerPart.toString();\n\t}\n\n\treturn `${integerPart}.${trimmed}`;\n}\n"],"mappings":";AAwBA,SAAgB,kBAAkB,WAAmB,QAAgB,UAA0B;CAC9F,MAAM,eAAe,OAAO,OAAO;CACnC,MAAM,cAAc,YAAY;CAChC,MAAM,iBAAiB,YAAY;AAEnC,KAAI,mBAAmB,GACtB,QAAO,YAAY,UAAU;CAG9B,MAAM,eAAe,OAAO,UAAU,CAAC,SAAS;CAGhD,MAAM,UAFgB,eAAe,UAAU,CAAC,SAAS,cAAc,IAAI,CAC3C,MAAM,GAAG,SAAS,CACxB,QAAQ,OAAO,GAAG;AAE5C,KAAI,CAAC,QACJ,QAAO,YAAY,UAAU;AAG9B,QAAO,GAAG,YAAY,GAAG"}
|
|
@@ -0,0 +1,365 @@
|
|
|
1
|
+
import { FLOAT_SCALAR } from "../utils/config.mjs";
|
|
2
|
+
import { formatTokenAmount } from "./context.mjs";
|
|
3
|
+
import { bcs } from "@mysten/sui/bcs";
|
|
4
|
+
import { Transaction } from "@mysten/sui/transactions";
|
|
5
|
+
import { normalizeSuiAddress } from "@mysten/sui/utils";
|
|
6
|
+
|
|
7
|
+
//#region src/queries/marginManagerQueries.ts
|
|
8
|
+
var MarginManagerQueries = class {
|
|
9
|
+
#ctx;
|
|
10
|
+
constructor(ctx) {
|
|
11
|
+
this.#ctx = ctx;
|
|
12
|
+
}
|
|
13
|
+
async getMarginManagerOwner(marginManagerKey) {
|
|
14
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
15
|
+
const tx = new Transaction();
|
|
16
|
+
tx.add(this.#ctx.marginManager.ownerByPoolKey(manager.poolKey, manager.address));
|
|
17
|
+
const bytes = (await this.#ctx.client.core.simulateTransaction({
|
|
18
|
+
transaction: tx,
|
|
19
|
+
include: {
|
|
20
|
+
commandResults: true,
|
|
21
|
+
effects: true
|
|
22
|
+
}
|
|
23
|
+
})).commandResults[0].returnValues[0].bcs;
|
|
24
|
+
return normalizeSuiAddress(bcs.Address.parse(bytes));
|
|
25
|
+
}
|
|
26
|
+
async getMarginManagerDeepbookPool(marginManagerKey) {
|
|
27
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
28
|
+
const tx = new Transaction();
|
|
29
|
+
tx.add(this.#ctx.marginManager.deepbookPool(manager.poolKey, manager.address));
|
|
30
|
+
const bytes = (await this.#ctx.client.core.simulateTransaction({
|
|
31
|
+
transaction: tx,
|
|
32
|
+
include: {
|
|
33
|
+
commandResults: true,
|
|
34
|
+
effects: true
|
|
35
|
+
}
|
|
36
|
+
})).commandResults[0].returnValues[0].bcs;
|
|
37
|
+
return normalizeSuiAddress(bcs.Address.parse(bytes));
|
|
38
|
+
}
|
|
39
|
+
async getMarginManagerMarginPoolId(marginManagerKey) {
|
|
40
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
41
|
+
const tx = new Transaction();
|
|
42
|
+
tx.add(this.#ctx.marginManager.marginPoolId(manager.poolKey, manager.address));
|
|
43
|
+
const bytes = (await this.#ctx.client.core.simulateTransaction({
|
|
44
|
+
transaction: tx,
|
|
45
|
+
include: {
|
|
46
|
+
commandResults: true,
|
|
47
|
+
effects: true
|
|
48
|
+
}
|
|
49
|
+
})).commandResults[0].returnValues[0].bcs;
|
|
50
|
+
const option = bcs.option(bcs.Address).parse(bytes);
|
|
51
|
+
return option ? normalizeSuiAddress(option) : null;
|
|
52
|
+
}
|
|
53
|
+
async getMarginManagerBorrowedShares(marginManagerKey) {
|
|
54
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
55
|
+
const tx = new Transaction();
|
|
56
|
+
tx.add(this.#ctx.marginManager.borrowedShares(manager.poolKey, manager.address));
|
|
57
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
58
|
+
transaction: tx,
|
|
59
|
+
include: {
|
|
60
|
+
commandResults: true,
|
|
61
|
+
effects: true
|
|
62
|
+
}
|
|
63
|
+
});
|
|
64
|
+
const baseBytes = res.commandResults[0].returnValues[0].bcs;
|
|
65
|
+
const quoteBytes = res.commandResults[0].returnValues[1].bcs;
|
|
66
|
+
return {
|
|
67
|
+
baseShares: bcs.U64.parse(baseBytes).toString(),
|
|
68
|
+
quoteShares: bcs.U64.parse(quoteBytes).toString()
|
|
69
|
+
};
|
|
70
|
+
}
|
|
71
|
+
async getMarginManagerBorrowedBaseShares(marginManagerKey) {
|
|
72
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
73
|
+
const tx = new Transaction();
|
|
74
|
+
tx.add(this.#ctx.marginManager.borrowedBaseShares(manager.poolKey, manager.address));
|
|
75
|
+
const bytes = (await this.#ctx.client.core.simulateTransaction({
|
|
76
|
+
transaction: tx,
|
|
77
|
+
include: {
|
|
78
|
+
commandResults: true,
|
|
79
|
+
effects: true
|
|
80
|
+
}
|
|
81
|
+
})).commandResults[0].returnValues[0].bcs;
|
|
82
|
+
return bcs.U64.parse(bytes).toString();
|
|
83
|
+
}
|
|
84
|
+
async getMarginManagerBorrowedQuoteShares(marginManagerKey) {
|
|
85
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
86
|
+
const tx = new Transaction();
|
|
87
|
+
tx.add(this.#ctx.marginManager.borrowedQuoteShares(manager.poolKey, manager.address));
|
|
88
|
+
const bytes = (await this.#ctx.client.core.simulateTransaction({
|
|
89
|
+
transaction: tx,
|
|
90
|
+
include: {
|
|
91
|
+
commandResults: true,
|
|
92
|
+
effects: true
|
|
93
|
+
}
|
|
94
|
+
})).commandResults[0].returnValues[0].bcs;
|
|
95
|
+
return bcs.U64.parse(bytes).toString();
|
|
96
|
+
}
|
|
97
|
+
async getMarginManagerHasBaseDebt(marginManagerKey) {
|
|
98
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
99
|
+
const tx = new Transaction();
|
|
100
|
+
tx.add(this.#ctx.marginManager.hasBaseDebt(manager.poolKey, manager.address));
|
|
101
|
+
const bytes = (await this.#ctx.client.core.simulateTransaction({
|
|
102
|
+
transaction: tx,
|
|
103
|
+
include: {
|
|
104
|
+
commandResults: true,
|
|
105
|
+
effects: true
|
|
106
|
+
}
|
|
107
|
+
})).commandResults[0].returnValues[0].bcs;
|
|
108
|
+
return bcs.bool().parse(bytes);
|
|
109
|
+
}
|
|
110
|
+
async getMarginManagerBalanceManagerId(marginManagerAddress) {
|
|
111
|
+
const res = await this.#ctx.client.core.getObject({
|
|
112
|
+
objectId: marginManagerAddress,
|
|
113
|
+
include: { content: true }
|
|
114
|
+
});
|
|
115
|
+
if (!res.object?.content) throw new Error(`Margin manager not found: ${marginManagerAddress}`);
|
|
116
|
+
return normalizeSuiAddress(bcs.struct("MarginManagerBalanceManagerId", {
|
|
117
|
+
id: bcs.Address,
|
|
118
|
+
owner: bcs.Address,
|
|
119
|
+
deepbook_pool: bcs.Address,
|
|
120
|
+
margin_pool_id: bcs.option(bcs.Address),
|
|
121
|
+
balance_manager_id: bcs.Address
|
|
122
|
+
}).parse(res.object.content).balance_manager_id);
|
|
123
|
+
}
|
|
124
|
+
async getMarginManagerAssets(marginManagerKey, decimals = 6) {
|
|
125
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
126
|
+
const tx = new Transaction();
|
|
127
|
+
tx.add(this.#ctx.marginManager.calculateAssets(manager.poolKey, manager.address));
|
|
128
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
129
|
+
transaction: tx,
|
|
130
|
+
include: {
|
|
131
|
+
commandResults: true,
|
|
132
|
+
effects: true
|
|
133
|
+
}
|
|
134
|
+
});
|
|
135
|
+
const baseBytes = res.commandResults[0].returnValues[0].bcs;
|
|
136
|
+
const quoteBytes = res.commandResults[0].returnValues[1].bcs;
|
|
137
|
+
const pool = this.#ctx.config.getPool(manager.poolKey);
|
|
138
|
+
const baseCoin = this.#ctx.config.getCoin(pool.baseCoin);
|
|
139
|
+
const quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);
|
|
140
|
+
return {
|
|
141
|
+
baseAsset: formatTokenAmount(BigInt(bcs.U64.parse(baseBytes)), baseCoin.scalar, decimals),
|
|
142
|
+
quoteAsset: formatTokenAmount(BigInt(bcs.U64.parse(quoteBytes)), quoteCoin.scalar, decimals)
|
|
143
|
+
};
|
|
144
|
+
}
|
|
145
|
+
async getMarginManagerDebts(marginManagerKey, decimals = 6) {
|
|
146
|
+
const hasBaseDebt = await this.getMarginManagerHasBaseDebt(marginManagerKey);
|
|
147
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
148
|
+
const pool = this.#ctx.config.getPool(manager.poolKey);
|
|
149
|
+
const debtCoinKey = hasBaseDebt ? pool.baseCoin : pool.quoteCoin;
|
|
150
|
+
const tx = new Transaction();
|
|
151
|
+
tx.add(this.#ctx.marginManager.calculateDebts(manager.poolKey, debtCoinKey, manager.address));
|
|
152
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
153
|
+
transaction: tx,
|
|
154
|
+
include: {
|
|
155
|
+
commandResults: true,
|
|
156
|
+
effects: true
|
|
157
|
+
}
|
|
158
|
+
});
|
|
159
|
+
if (res.FailedTransaction) throw new Error(`Transaction failed: ${res.FailedTransaction.status.error?.message || "Unknown error"}`);
|
|
160
|
+
if (!res.commandResults || !res.commandResults[0] || !res.commandResults[0].returnValues) throw new Error(`Failed to get margin manager debts: Unknown error`);
|
|
161
|
+
const baseBytes = res.commandResults[0].returnValues[0].bcs;
|
|
162
|
+
const quoteBytes = res.commandResults[0].returnValues[1].bcs;
|
|
163
|
+
const debtCoin = this.#ctx.config.getCoin(debtCoinKey);
|
|
164
|
+
return {
|
|
165
|
+
baseDebt: formatTokenAmount(BigInt(bcs.U64.parse(baseBytes)), debtCoin.scalar, decimals),
|
|
166
|
+
quoteDebt: formatTokenAmount(BigInt(bcs.U64.parse(quoteBytes)), debtCoin.scalar, decimals)
|
|
167
|
+
};
|
|
168
|
+
}
|
|
169
|
+
async getMarginManagerState(marginManagerKey, decimals = 6) {
|
|
170
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
171
|
+
const tx = new Transaction();
|
|
172
|
+
tx.add(this.#ctx.marginManager.managerState(manager.poolKey, manager.address));
|
|
173
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
174
|
+
transaction: tx,
|
|
175
|
+
include: {
|
|
176
|
+
commandResults: true,
|
|
177
|
+
effects: true
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
if (res.FailedTransaction) throw new Error(`Transaction failed: ${res.FailedTransaction.status.error?.message || "Unknown error"}`);
|
|
181
|
+
if (!res.commandResults || !res.commandResults[0] || !res.commandResults[0].returnValues) throw new Error(`Failed to get margin manager state: Unknown error`);
|
|
182
|
+
const pool = this.#ctx.config.getPool(manager.poolKey);
|
|
183
|
+
const baseCoin = this.#ctx.config.getCoin(pool.baseCoin);
|
|
184
|
+
const quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);
|
|
185
|
+
const managerId = normalizeSuiAddress(bcs.Address.parse(res.commandResults[0].returnValues[0].bcs));
|
|
186
|
+
const deepbookPoolId = normalizeSuiAddress(bcs.Address.parse(res.commandResults[0].returnValues[1].bcs));
|
|
187
|
+
const riskRatio = Number(bcs.U64.parse(res.commandResults[0].returnValues[2].bcs)) / FLOAT_SCALAR;
|
|
188
|
+
const baseAsset = formatTokenAmount(BigInt(bcs.U64.parse(res.commandResults[0].returnValues[3].bcs)), baseCoin.scalar, decimals);
|
|
189
|
+
const quoteAsset = formatTokenAmount(BigInt(bcs.U64.parse(res.commandResults[0].returnValues[4].bcs)), quoteCoin.scalar, decimals);
|
|
190
|
+
const baseDebt = formatTokenAmount(BigInt(bcs.U64.parse(res.commandResults[0].returnValues[5].bcs)), baseCoin.scalar, decimals);
|
|
191
|
+
const quoteDebt = formatTokenAmount(BigInt(bcs.U64.parse(res.commandResults[0].returnValues[6].bcs)), quoteCoin.scalar, decimals);
|
|
192
|
+
const basePythPrice = bcs.U64.parse(res.commandResults[0].returnValues[7].bcs);
|
|
193
|
+
const basePythDecimals = Number(bcs.u8().parse(new Uint8Array(res.commandResults[0].returnValues[8].bcs)));
|
|
194
|
+
const quotePythPrice = bcs.U64.parse(res.commandResults[0].returnValues[9].bcs);
|
|
195
|
+
const quotePythDecimals = Number(bcs.u8().parse(new Uint8Array(res.commandResults[0].returnValues[10].bcs)));
|
|
196
|
+
const currentPrice = BigInt(bcs.U64.parse(res.commandResults[0].returnValues[11].bcs));
|
|
197
|
+
const lowestTriggerAbovePrice = BigInt(bcs.U64.parse(res.commandResults[0].returnValues[12].bcs));
|
|
198
|
+
const highestTriggerBelowPrice = BigInt(bcs.U64.parse(res.commandResults[0].returnValues[13].bcs));
|
|
199
|
+
return {
|
|
200
|
+
managerId,
|
|
201
|
+
deepbookPoolId,
|
|
202
|
+
riskRatio,
|
|
203
|
+
baseAsset,
|
|
204
|
+
quoteAsset,
|
|
205
|
+
baseDebt,
|
|
206
|
+
quoteDebt,
|
|
207
|
+
basePythPrice: basePythPrice.toString(),
|
|
208
|
+
basePythDecimals,
|
|
209
|
+
quotePythPrice: quotePythPrice.toString(),
|
|
210
|
+
quotePythDecimals,
|
|
211
|
+
currentPrice,
|
|
212
|
+
lowestTriggerAbovePrice,
|
|
213
|
+
highestTriggerBelowPrice
|
|
214
|
+
};
|
|
215
|
+
}
|
|
216
|
+
async getMarginManagerStates(marginManagers, decimals = 6) {
|
|
217
|
+
const entries = Object.entries(marginManagers);
|
|
218
|
+
if (entries.length === 0) return {};
|
|
219
|
+
const tx = new Transaction();
|
|
220
|
+
for (const [managerId, poolKey] of entries) tx.add(this.#ctx.marginManager.managerState(poolKey, managerId));
|
|
221
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
222
|
+
transaction: tx,
|
|
223
|
+
include: {
|
|
224
|
+
commandResults: true,
|
|
225
|
+
effects: true
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
if (res.FailedTransaction) throw new Error(`Transaction failed: ${res.FailedTransaction.status.error?.message || "Unknown error"}`);
|
|
229
|
+
if (!res.commandResults) throw new Error(`Failed to get margin manager states: Unknown error`);
|
|
230
|
+
const results = {};
|
|
231
|
+
for (let i = 0; i < entries.length; i++) {
|
|
232
|
+
const commandResult = res.commandResults[i];
|
|
233
|
+
if (!commandResult || !commandResult.returnValues) throw new Error(`Failed to get margin manager state for index ${i}: No return values`);
|
|
234
|
+
const [, poolKey] = entries[i];
|
|
235
|
+
const pool = this.#ctx.config.getPool(poolKey);
|
|
236
|
+
const baseCoin = this.#ctx.config.getCoin(pool.baseCoin);
|
|
237
|
+
const quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);
|
|
238
|
+
const managerId = normalizeSuiAddress(bcs.Address.parse(commandResult.returnValues[0].bcs));
|
|
239
|
+
const deepbookPoolId = normalizeSuiAddress(bcs.Address.parse(commandResult.returnValues[1].bcs));
|
|
240
|
+
const riskRatio = Number(bcs.U64.parse(commandResult.returnValues[2].bcs)) / FLOAT_SCALAR;
|
|
241
|
+
const baseAsset = formatTokenAmount(BigInt(bcs.U64.parse(commandResult.returnValues[3].bcs)), baseCoin.scalar, decimals);
|
|
242
|
+
const quoteAsset = formatTokenAmount(BigInt(bcs.U64.parse(commandResult.returnValues[4].bcs)), quoteCoin.scalar, decimals);
|
|
243
|
+
const baseDebt = formatTokenAmount(BigInt(bcs.U64.parse(commandResult.returnValues[5].bcs)), baseCoin.scalar, decimals);
|
|
244
|
+
const quoteDebt = formatTokenAmount(BigInt(bcs.U64.parse(commandResult.returnValues[6].bcs)), quoteCoin.scalar, decimals);
|
|
245
|
+
const basePythPrice = bcs.U64.parse(commandResult.returnValues[7].bcs);
|
|
246
|
+
const basePythDecimals = Number(bcs.u8().parse(new Uint8Array(commandResult.returnValues[8].bcs)));
|
|
247
|
+
const quotePythPrice = bcs.U64.parse(commandResult.returnValues[9].bcs);
|
|
248
|
+
const quotePythDecimals = Number(bcs.u8().parse(new Uint8Array(commandResult.returnValues[10].bcs)));
|
|
249
|
+
const currentPrice = BigInt(bcs.U64.parse(commandResult.returnValues[11].bcs));
|
|
250
|
+
const lowestTriggerAbovePrice = BigInt(bcs.U64.parse(commandResult.returnValues[12].bcs));
|
|
251
|
+
const highestTriggerBelowPrice = BigInt(bcs.U64.parse(commandResult.returnValues[13].bcs));
|
|
252
|
+
results[managerId] = {
|
|
253
|
+
managerId,
|
|
254
|
+
deepbookPoolId,
|
|
255
|
+
riskRatio,
|
|
256
|
+
baseAsset,
|
|
257
|
+
quoteAsset,
|
|
258
|
+
baseDebt,
|
|
259
|
+
quoteDebt,
|
|
260
|
+
basePythPrice: basePythPrice.toString(),
|
|
261
|
+
basePythDecimals,
|
|
262
|
+
quotePythPrice: quotePythPrice.toString(),
|
|
263
|
+
quotePythDecimals,
|
|
264
|
+
currentPrice,
|
|
265
|
+
lowestTriggerAbovePrice,
|
|
266
|
+
highestTriggerBelowPrice
|
|
267
|
+
};
|
|
268
|
+
}
|
|
269
|
+
return results;
|
|
270
|
+
}
|
|
271
|
+
async getMarginManagerBaseBalance(marginManagerKey, decimals = 9) {
|
|
272
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
273
|
+
const tx = new Transaction();
|
|
274
|
+
tx.add(this.#ctx.marginManager.baseBalance(manager.poolKey, manager.address));
|
|
275
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
276
|
+
transaction: tx,
|
|
277
|
+
include: {
|
|
278
|
+
commandResults: true,
|
|
279
|
+
effects: true
|
|
280
|
+
}
|
|
281
|
+
});
|
|
282
|
+
if (res.FailedTransaction) throw new Error(`Transaction failed: ${res.FailedTransaction.status.error?.message || "Unknown error"}`);
|
|
283
|
+
if (!res.commandResults || !res.commandResults[0] || !res.commandResults[0].returnValues) throw new Error(`Failed to get margin manager base balance: Unknown error`);
|
|
284
|
+
const bytes = res.commandResults[0].returnValues[0].bcs;
|
|
285
|
+
const pool = this.#ctx.config.getPool(manager.poolKey);
|
|
286
|
+
const baseCoin = this.#ctx.config.getCoin(pool.baseCoin);
|
|
287
|
+
return formatTokenAmount(BigInt(bcs.U64.parse(bytes)), baseCoin.scalar, decimals);
|
|
288
|
+
}
|
|
289
|
+
async getMarginManagerQuoteBalance(marginManagerKey, decimals = 9) {
|
|
290
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
291
|
+
const tx = new Transaction();
|
|
292
|
+
tx.add(this.#ctx.marginManager.quoteBalance(manager.poolKey, manager.address));
|
|
293
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
294
|
+
transaction: tx,
|
|
295
|
+
include: {
|
|
296
|
+
commandResults: true,
|
|
297
|
+
effects: true
|
|
298
|
+
}
|
|
299
|
+
});
|
|
300
|
+
if (res.FailedTransaction) throw new Error(`Transaction failed: ${res.FailedTransaction.status.error?.message || "Unknown error"}`);
|
|
301
|
+
if (!res.commandResults || !res.commandResults[0] || !res.commandResults[0].returnValues) throw new Error(`Failed to get margin manager quote balance: Unknown error`);
|
|
302
|
+
const bytes = res.commandResults[0].returnValues[0].bcs;
|
|
303
|
+
const pool = this.#ctx.config.getPool(manager.poolKey);
|
|
304
|
+
const quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);
|
|
305
|
+
return formatTokenAmount(BigInt(bcs.U64.parse(bytes)), quoteCoin.scalar, decimals);
|
|
306
|
+
}
|
|
307
|
+
async getMarginManagerDeepBalance(marginManagerKey, decimals = 6) {
|
|
308
|
+
const manager = this.#ctx.config.getMarginManager(marginManagerKey);
|
|
309
|
+
const tx = new Transaction();
|
|
310
|
+
tx.add(this.#ctx.marginManager.deepBalance(manager.poolKey, manager.address));
|
|
311
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
312
|
+
transaction: tx,
|
|
313
|
+
include: {
|
|
314
|
+
commandResults: true,
|
|
315
|
+
effects: true
|
|
316
|
+
}
|
|
317
|
+
});
|
|
318
|
+
if (res.FailedTransaction) throw new Error(`Transaction failed: ${res.FailedTransaction.status.error?.message || "Unknown error"}`);
|
|
319
|
+
if (!res.commandResults || !res.commandResults[0] || !res.commandResults[0].returnValues) throw new Error(`Failed to get margin manager DEEP balance: Unknown error`);
|
|
320
|
+
const bytes = res.commandResults[0].returnValues[0].bcs;
|
|
321
|
+
const deepCoin = this.#ctx.config.getCoin("DEEP");
|
|
322
|
+
return formatTokenAmount(BigInt(bcs.U64.parse(bytes)), deepCoin.scalar, decimals);
|
|
323
|
+
}
|
|
324
|
+
async getMarginManagerBalances(marginManagers, decimals = 9) {
|
|
325
|
+
const entries = Object.entries(marginManagers);
|
|
326
|
+
if (entries.length === 0) return {};
|
|
327
|
+
const tx = new Transaction();
|
|
328
|
+
for (const [managerId, poolKey] of entries) {
|
|
329
|
+
tx.add(this.#ctx.marginManager.baseBalance(poolKey, managerId));
|
|
330
|
+
tx.add(this.#ctx.marginManager.quoteBalance(poolKey, managerId));
|
|
331
|
+
tx.add(this.#ctx.marginManager.deepBalance(poolKey, managerId));
|
|
332
|
+
}
|
|
333
|
+
const res = await this.#ctx.client.core.simulateTransaction({
|
|
334
|
+
transaction: tx,
|
|
335
|
+
include: {
|
|
336
|
+
commandResults: true,
|
|
337
|
+
effects: true
|
|
338
|
+
}
|
|
339
|
+
});
|
|
340
|
+
if (res.FailedTransaction) throw new Error(`Transaction failed: ${res.FailedTransaction.status.error?.message || "Unknown error"}`);
|
|
341
|
+
if (!res.commandResults) throw new Error("Failed to get margin manager balances: No command results");
|
|
342
|
+
const results = {};
|
|
343
|
+
const deepCoin = this.#ctx.config.getCoin("DEEP");
|
|
344
|
+
for (let i = 0; i < entries.length; i++) {
|
|
345
|
+
const [managerId, poolKey] = entries[i];
|
|
346
|
+
const pool = this.#ctx.config.getPool(poolKey);
|
|
347
|
+
const baseCoin = this.#ctx.config.getCoin(pool.baseCoin);
|
|
348
|
+
const quoteCoin = this.#ctx.config.getCoin(pool.quoteCoin);
|
|
349
|
+
const baseResult = res.commandResults[i * 3];
|
|
350
|
+
const quoteResult = res.commandResults[i * 3 + 1];
|
|
351
|
+
const deepResult = res.commandResults[i * 3 + 2];
|
|
352
|
+
if (!baseResult?.returnValues || !quoteResult?.returnValues || !deepResult?.returnValues) throw new Error(`Failed to get balances for margin manager ${managerId}: No return values`);
|
|
353
|
+
results[managerId] = {
|
|
354
|
+
base: formatTokenAmount(BigInt(bcs.U64.parse(baseResult.returnValues[0].bcs)), baseCoin.scalar, decimals),
|
|
355
|
+
quote: formatTokenAmount(BigInt(bcs.U64.parse(quoteResult.returnValues[0].bcs)), quoteCoin.scalar, decimals),
|
|
356
|
+
deep: formatTokenAmount(BigInt(bcs.U64.parse(deepResult.returnValues[0].bcs)), deepCoin.scalar, decimals)
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
return results;
|
|
360
|
+
}
|
|
361
|
+
};
|
|
362
|
+
|
|
363
|
+
//#endregion
|
|
364
|
+
export { MarginManagerQueries };
|
|
365
|
+
//# sourceMappingURL=marginManagerQueries.mjs.map
|