@omnipair/dusk-sdk 2.0.2 → 2.16.0-next.20261102
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/README.md +1011 -16
- package/dist/amounts.d.ts +9 -0
- package/dist/amounts.d.ts.map +1 -0
- package/dist/amounts.js +10 -0
- package/dist/amounts.js.map +1 -0
- package/dist/constants.d.ts +71 -14
- package/dist/constants.d.ts.map +1 -1
- package/dist/constants.js +155 -28
- package/dist/constants.js.map +1 -1
- package/dist/delegate.d.ts +94 -0
- package/dist/delegate.d.ts.map +1 -0
- package/dist/delegate.js +131 -0
- package/dist/delegate.js.map +1 -0
- package/dist/dusk.d.ts +14 -0
- package/dist/dusk.d.ts.map +1 -1
- package/dist/dusk.js +22 -0
- package/dist/dusk.js.map +1 -1
- package/dist/get.d.ts +3253 -378
- package/dist/get.d.ts.map +1 -1
- package/dist/get.js +130 -70
- package/dist/get.js.map +1 -1
- package/dist/governance-client.d.ts +169 -0
- package/dist/governance-client.d.ts.map +1 -0
- package/dist/governance-client.js +146 -0
- package/dist/governance-client.js.map +1 -0
- package/dist/governance.d.ts +313 -0
- package/dist/governance.d.ts.map +1 -0
- package/dist/governance.js +902 -0
- package/dist/governance.js.map +1 -0
- package/dist/hash.d.ts +9 -0
- package/dist/hash.d.ts.map +1 -0
- package/dist/hash.js +16 -0
- package/dist/hash.js.map +1 -0
- package/dist/hlp-liquidation-client.d.ts +106 -0
- package/dist/hlp-liquidation-client.d.ts.map +1 -0
- package/dist/hlp-liquidation-client.js +150 -0
- package/dist/hlp-liquidation-client.js.map +1 -0
- package/dist/idl_delegate.d.ts +3 -0
- package/dist/idl_delegate.d.ts.map +1 -0
- package/dist/idl_delegate.js +3 -0
- package/dist/idl_delegate.js.map +1 -0
- package/dist/idl_delegate.json +6119 -0
- package/dist/idl_v2.json +17175 -5862
- package/dist/index.d.ts +23 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +23 -0
- package/dist/index.js.map +1 -1
- package/dist/interest-accounting.d.ts +49 -0
- package/dist/interest-accounting.d.ts.map +1 -0
- package/dist/interest-accounting.js +72 -0
- package/dist/interest-accounting.js.map +1 -0
- package/dist/leverage-actions.d.ts +122 -0
- package/dist/leverage-actions.d.ts.map +1 -0
- package/dist/leverage-actions.js +228 -0
- package/dist/leverage-actions.js.map +1 -0
- package/dist/leverage-margins.d.ts +12 -0
- package/dist/leverage-margins.d.ts.map +1 -0
- package/dist/leverage-margins.js +45 -0
- package/dist/leverage-margins.js.map +1 -0
- package/dist/leverage-preview.d.ts +587 -0
- package/dist/leverage-preview.d.ts.map +1 -0
- package/dist/leverage-preview.js +89 -0
- package/dist/leverage-preview.js.map +1 -0
- package/dist/liquidation-client.d.ts +230 -0
- package/dist/liquidation-client.d.ts.map +1 -0
- package/dist/liquidation-client.js +239 -0
- package/dist/liquidation-client.js.map +1 -0
- package/dist/liquidation.d.ts +78 -0
- package/dist/liquidation.d.ts.map +1 -0
- package/dist/liquidation.js +122 -0
- package/dist/liquidation.js.map +1 -0
- package/dist/lp-naming.d.ts +43 -0
- package/dist/lp-naming.d.ts.map +1 -0
- package/dist/lp-naming.js +81 -0
- package/dist/lp-naming.js.map +1 -0
- package/dist/lp-vanity.d.ts +33 -0
- package/dist/lp-vanity.d.ts.map +1 -0
- package/dist/lp-vanity.js +94 -0
- package/dist/lp-vanity.js.map +1 -0
- package/dist/market-bootstrap.d.ts +109 -0
- package/dist/market-bootstrap.d.ts.map +1 -0
- package/dist/market-bootstrap.js +158 -0
- package/dist/market-bootstrap.js.map +1 -0
- package/dist/market-launch.d.ts +45 -0
- package/dist/market-launch.d.ts.map +1 -0
- package/dist/market-launch.js +76 -0
- package/dist/market-launch.js.map +1 -0
- package/dist/oracle.d.ts +46 -0
- package/dist/oracle.d.ts.map +1 -0
- package/dist/oracle.js +103 -0
- package/dist/oracle.js.map +1 -0
- package/dist/preview.d.ts +39 -1
- package/dist/preview.d.ts.map +1 -1
- package/dist/preview.js +12 -0
- package/dist/preview.js.map +1 -1
- package/dist/protection.d.ts +48 -0
- package/dist/protection.d.ts.map +1 -0
- package/dist/protection.js +88 -0
- package/dist/protection.js.map +1 -0
- package/dist/risk-pricing.d.ts +14 -0
- package/dist/risk-pricing.d.ts.map +1 -0
- package/dist/risk-pricing.js +25 -0
- package/dist/risk-pricing.js.map +1 -0
- package/dist/simulation.d.ts +45 -0
- package/dist/simulation.d.ts.map +1 -0
- package/dist/simulation.js +106 -0
- package/dist/simulation.js.map +1 -0
- package/dist/tranche-client.d.ts +121 -0
- package/dist/tranche-client.d.ts.map +1 -0
- package/dist/tranche-client.js +185 -0
- package/dist/tranche-client.js.map +1 -0
- package/dist/transfer-fees.d.ts +46 -0
- package/dist/transfer-fees.d.ts.map +1 -0
- package/dist/transfer-fees.js +76 -0
- package/dist/transfer-fees.js.map +1 -0
- package/dist/type-aliases.d.ts +271 -31
- package/dist/type-aliases.d.ts.map +1 -1
- package/dist/types_delegate.d.ts +6126 -0
- package/dist/types_delegate.d.ts.map +1 -0
- package/dist/types_delegate.js +2 -0
- package/dist/types_delegate.js.map +1 -0
- package/dist/types_v2.d.ts +17235 -5922
- package/dist/types_v2.d.ts.map +1 -1
- package/dist/virtual-book/index.d.ts +4 -0
- package/dist/virtual-book/index.d.ts.map +1 -0
- package/dist/virtual-book/index.js +4 -0
- package/dist/virtual-book/index.js.map +1 -0
- package/dist/virtual-book/market.d.ts +608 -0
- package/dist/virtual-book/market.d.ts.map +1 -0
- package/dist/virtual-book/market.js +59 -0
- package/dist/virtual-book/market.js.map +1 -0
- package/dist/virtual-book/projection.d.ts +44 -0
- package/dist/virtual-book/projection.d.ts.map +1 -0
- package/dist/virtual-book/projection.js +229 -0
- package/dist/virtual-book/projection.js.map +1 -0
- package/dist/virtual-book/quotes.d.ts +1241 -0
- package/dist/virtual-book/quotes.d.ts.map +1 -0
- package/dist/virtual-book/quotes.js +233 -0
- package/dist/virtual-book/quotes.js.map +1 -0
- package/dist/write.d.ts +517 -1
- package/dist/write.d.ts.map +1 -1
- package/dist/write.js +1172 -23
- package/dist/write.js.map +1 -1
- package/dist/yield-client.d.ts +51 -0
- package/dist/yield-client.d.ts.map +1 -0
- package/dist/yield-client.js +98 -0
- package/dist/yield-client.js.map +1 -0
- package/package.json +9 -2
- package/src/amounts.ts +13 -0
- package/src/constants.ts +236 -26
- package/src/delegate.ts +238 -0
- package/src/dusk.ts +28 -0
- package/src/get.ts +247 -82
- package/src/governance-client.ts +234 -0
- package/src/governance.ts +1422 -0
- package/src/hash.ts +15 -0
- package/src/hlp-liquidation-client.ts +195 -0
- package/src/idl_delegate.json +6119 -0
- package/src/idl_delegate.ts +3 -0
- package/src/idl_v2.json +17175 -5862
- package/src/index.ts +31 -0
- package/src/interest-accounting.ts +85 -0
- package/src/leverage-actions.ts +216 -0
- package/src/leverage-margins.ts +48 -0
- package/src/leverage-preview.ts +86 -0
- package/src/liquidation-client.ts +297 -0
- package/src/liquidation.ts +137 -0
- package/src/lp-naming.ts +127 -0
- package/src/lp-vanity.ts +135 -0
- package/src/market-bootstrap.ts +273 -0
- package/src/market-launch.ts +103 -0
- package/src/oracle.ts +120 -0
- package/src/preview.ts +53 -1
- package/src/protection.ts +112 -0
- package/src/risk-pricing.ts +28 -0
- package/src/simulation.ts +169 -0
- package/src/tranche-client.ts +187 -0
- package/src/transfer-fees.ts +99 -0
- package/src/type-aliases.ts +280 -31
- package/src/types_delegate.ts +6125 -0
- package/src/types_v2.ts +17174 -5861
- package/src/virtual-book/index.ts +3 -0
- package/src/virtual-book/market.ts +76 -0
- package/src/virtual-book/projection.ts +265 -0
- package/src/virtual-book/quotes.ts +308 -0
- package/src/write.ts +2073 -34
- package/src/yield-client.ts +117 -0
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
import { DuskWrite } from "../write.js";
|
|
2
|
+
import { DEFAULT_READONLY_PUBLIC_KEY } from "../address.js";
|
|
3
|
+
import type { Program } from "@coral-xyz/anchor";
|
|
4
|
+
import { PublicKey, type TransactionInstruction } from "@solana/web3.js";
|
|
5
|
+
import { address as publicKey, type AddressLike } from "../address.js";
|
|
6
|
+
import { deriveMarketAddress, MARKET_LAYOUT_VERSION } from "../constants.js";
|
|
7
|
+
import { decodePreviewMarketReturnData } from "../preview.js";
|
|
8
|
+
import type { Market } from "../type-aliases.js";
|
|
9
|
+
import type { Dusk } from "../types_v2.js";
|
|
10
|
+
import { simulatePreviewWithContext, type PreviewSimulationOptions } from "../simulation.js";
|
|
11
|
+
|
|
12
|
+
/** Accrued market and preview observed together in one simulation bank. */
|
|
13
|
+
export async function previewVirtualBookSnapshot(
|
|
14
|
+
program: Program<Dusk>,
|
|
15
|
+
marketAddress: AddressLike,
|
|
16
|
+
options: PreviewSimulationOptions = {}
|
|
17
|
+
) {
|
|
18
|
+
options.signal?.throwIfAborted();
|
|
19
|
+
const market = publicKey(marketAddress).toBase58();
|
|
20
|
+
const floor = options.minContextSlot ?? 0;
|
|
21
|
+
const instruction = await new DuskWrite(program).instruction("previewMarket", [], {
|
|
22
|
+
accounts: { market: publicKey(market) },
|
|
23
|
+
});
|
|
24
|
+
const result = await simulatePreviewWithContext(program, [instruction], {
|
|
25
|
+
...options,
|
|
26
|
+
feePayer: options.feePayer ?? DEFAULT_READONLY_PUBLIC_KEY,
|
|
27
|
+
commitment: "confirmed",
|
|
28
|
+
accounts: [market],
|
|
29
|
+
});
|
|
30
|
+
const slot = result.context.slot;
|
|
31
|
+
const data = result.value.returnData;
|
|
32
|
+
const info = result.value.accounts?.[0];
|
|
33
|
+
if (
|
|
34
|
+
!Number.isSafeInteger(slot) ||
|
|
35
|
+
slot < floor ||
|
|
36
|
+
result.value.err ||
|
|
37
|
+
!data ||
|
|
38
|
+
data.programId !== program.programId.toBase58() ||
|
|
39
|
+
data.data[1] !== "base64" ||
|
|
40
|
+
result.value.accounts?.length !== 1 ||
|
|
41
|
+
!info ||
|
|
42
|
+
info.owner !== program.programId.toBase58() ||
|
|
43
|
+
info.executable ||
|
|
44
|
+
info.data[1] !== "base64"
|
|
45
|
+
)
|
|
46
|
+
throw new Error("Market depth snapshot unavailable");
|
|
47
|
+
const accountBytes = Buffer.from(info.data[0], "base64");
|
|
48
|
+
if (accountBytes.toString("base64") !== info.data[0])
|
|
49
|
+
throw new Error("Invalid depth account encoding");
|
|
50
|
+
const preview = decodePreviewMarketReturnData(data.data);
|
|
51
|
+
const account = program.coder.accounts.decode<Market>("market", accountBytes);
|
|
52
|
+
const [address, bump] = deriveMarketAddress(
|
|
53
|
+
account.baseSide.assetMint,
|
|
54
|
+
account.quoteSide.assetMint,
|
|
55
|
+
account.paramsHash,
|
|
56
|
+
program.programId
|
|
57
|
+
);
|
|
58
|
+
if (
|
|
59
|
+
preview.slot.toString() !== String(slot) ||
|
|
60
|
+
account.version !== MARKET_LAYOUT_VERSION ||
|
|
61
|
+
!address.equals(new PublicKey(market)) ||
|
|
62
|
+
account.bump !== bump ||
|
|
63
|
+
!preview.amm.initialized ||
|
|
64
|
+
account.amm.concentratedCurveCache.mathRevision !== 1
|
|
65
|
+
)
|
|
66
|
+
throw new Error("Incompatible depth snapshot");
|
|
67
|
+
return {
|
|
68
|
+
market,
|
|
69
|
+
programId: program.programId.toBase58(),
|
|
70
|
+
slot,
|
|
71
|
+
observedAt: result.observedAt,
|
|
72
|
+
account,
|
|
73
|
+
preview,
|
|
74
|
+
};
|
|
75
|
+
}
|
|
76
|
+
export type DuskVirtualBookSnapshot = Awaited<ReturnType<typeof previewVirtualBookSnapshot>>;
|
|
@@ -0,0 +1,265 @@
|
|
|
1
|
+
import type { DuskVirtualBookSnapshot } from "./market.js";
|
|
2
|
+
|
|
3
|
+
export interface VirtualBookLevel {
|
|
4
|
+
price: number;
|
|
5
|
+
size: number;
|
|
6
|
+
total: number;
|
|
7
|
+
quoteSize: number;
|
|
8
|
+
quoteTotal: number;
|
|
9
|
+
averagePrice: number;
|
|
10
|
+
impactPercent: number;
|
|
11
|
+
feePercent: number;
|
|
12
|
+
surchargePercent: number;
|
|
13
|
+
}
|
|
14
|
+
export interface VirtualBookView {
|
|
15
|
+
market: string;
|
|
16
|
+
baseMint: string;
|
|
17
|
+
quoteMint: string;
|
|
18
|
+
mid: number;
|
|
19
|
+
step: number;
|
|
20
|
+
bids: VirtualBookLevel[];
|
|
21
|
+
asks: VirtualBookLevel[];
|
|
22
|
+
slot: number;
|
|
23
|
+
concentrated: boolean;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
const NAD = 1_000_000_000n;
|
|
27
|
+
const n = (value: { toString(): string }) => BigInt(value.toString());
|
|
28
|
+
type Layer = { liquidity: bigint; lower: bigint; upper: bigint };
|
|
29
|
+
|
|
30
|
+
/** Sampling projection of curve.rs::nested_point_at_sqrt_price (math revision 1).
|
|
31
|
+
* Inventory deltas select cumulative inputs for native swap previews. These
|
|
32
|
+
* pre-fee samples are never rendered as the user-facing book.
|
|
33
|
+
*/
|
|
34
|
+
export function virtualCurvePoint(tail: bigint, layers: readonly Layer[], sqrt: bigint) {
|
|
35
|
+
if (tail <= 0n || sqrt <= 0n) throw new Error("Invalid depth curve");
|
|
36
|
+
let base = (tail * NAD) / sqrt,
|
|
37
|
+
quote = (tail * sqrt) / NAD;
|
|
38
|
+
for (const { liquidity: l, lower, upper } of layers) {
|
|
39
|
+
if (l < 0n || lower <= 0n || upper <= lower) throw new Error("Invalid depth range");
|
|
40
|
+
const s = sqrt < lower ? lower : sqrt > upper ? upper : sqrt;
|
|
41
|
+
base += (l * NAD) / s - (l * NAD) / upper;
|
|
42
|
+
quote += (l * s) / NAD - (l * lower) / NAD;
|
|
43
|
+
}
|
|
44
|
+
return { base, quote };
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function projectDuskVirtualBookCurve(
|
|
48
|
+
snapshot: DuskVirtualBookSnapshot | null,
|
|
49
|
+
groupingBps = 10,
|
|
50
|
+
count = 12
|
|
51
|
+
): VirtualBookView | null {
|
|
52
|
+
if (!snapshot) return null;
|
|
53
|
+
if (
|
|
54
|
+
![5, 10, 25, 50, 100].includes(groupingBps) ||
|
|
55
|
+
!Number.isInteger(count) ||
|
|
56
|
+
count < 1 ||
|
|
57
|
+
count > 50
|
|
58
|
+
)
|
|
59
|
+
throw new Error("Invalid depth grouping");
|
|
60
|
+
const { account, preview } = snapshot;
|
|
61
|
+
const c = account.amm.concentratedCurveCache;
|
|
62
|
+
if (c.mathRevision !== 1) throw new Error("Unknown depth curve revision");
|
|
63
|
+
const base = n(preview.amm.ordinaryBaseReserveNad);
|
|
64
|
+
const quote = n(preview.amm.ordinaryQuoteReserveNad);
|
|
65
|
+
if (base <= 0n || quote <= 0n) return null;
|
|
66
|
+
const concentrated = n(c.concentratedLiquidity);
|
|
67
|
+
let tail = n(c.tailLiquidity);
|
|
68
|
+
if (tail <= 0n || concentrated < 0n) throw new Error("Invalid depth curve");
|
|
69
|
+
if (concentrated === 0n) {
|
|
70
|
+
// curve.rs::effective_reserves/quote_exact_in uses the live reserve
|
|
71
|
+
// product for CPMM. Conservative output rounding can grow that product
|
|
72
|
+
// without changing the cached liquidity. Reconstruct sqrt(x*y) instead
|
|
73
|
+
// of treating the original cache as the current trading invariant.
|
|
74
|
+
const product = base * quote;
|
|
75
|
+
let root = product;
|
|
76
|
+
let next = (root + 1n) / 2n;
|
|
77
|
+
while (next < root) {
|
|
78
|
+
root = next;
|
|
79
|
+
next = (root + product / root) / 2n;
|
|
80
|
+
}
|
|
81
|
+
tail = root;
|
|
82
|
+
}
|
|
83
|
+
const layers: Layer[] =
|
|
84
|
+
concentrated === 0n
|
|
85
|
+
? []
|
|
86
|
+
: c.fadeWidthBps === 0
|
|
87
|
+
? [
|
|
88
|
+
{
|
|
89
|
+
liquidity: concentrated,
|
|
90
|
+
lower: n(c.coreLowerSqrtPriceNad),
|
|
91
|
+
upper: n(c.coreUpperSqrtPriceNad),
|
|
92
|
+
},
|
|
93
|
+
]
|
|
94
|
+
: [
|
|
95
|
+
{
|
|
96
|
+
liquidity: (concentrated + 1n) / 2n,
|
|
97
|
+
lower: n(c.coreLowerSqrtPriceNad),
|
|
98
|
+
upper: n(c.coreUpperSqrtPriceNad),
|
|
99
|
+
},
|
|
100
|
+
{
|
|
101
|
+
liquidity: concentrated / 2n,
|
|
102
|
+
lower: n(c.outerLowerSqrtPriceNad),
|
|
103
|
+
upper: n(c.outerUpperSqrtPriceNad),
|
|
104
|
+
},
|
|
105
|
+
];
|
|
106
|
+
const point = (sqrt: bigint) => virtualCurvePoint(tail, layers, sqrt);
|
|
107
|
+
// Invert the current ordinary base inventory. Lending EMA / last fill are
|
|
108
|
+
// not the current AMM marginal price and cannot anchor this book.
|
|
109
|
+
let lo = 1n,
|
|
110
|
+
hi = NAD;
|
|
111
|
+
for (let i = 0; point(hi).base > base; i++) {
|
|
112
|
+
if (i > 128) throw new Error("Depth curve is outside supported domain");
|
|
113
|
+
hi *= 2n;
|
|
114
|
+
}
|
|
115
|
+
while (hi - lo > 1n) {
|
|
116
|
+
const middle = (lo + hi) / 2n;
|
|
117
|
+
if (point(middle).base > base) lo = middle;
|
|
118
|
+
else hi = middle;
|
|
119
|
+
}
|
|
120
|
+
const anchor = point(hi);
|
|
121
|
+
// A different curve implementation must fail closed, not draw plausible depth.
|
|
122
|
+
const difference = anchor.quote > quote ? anchor.quote - quote : quote - anchor.quote;
|
|
123
|
+
if (difference > quote / 100_000n + 100n)
|
|
124
|
+
throw new Error("Depth curve does not match the current reserves");
|
|
125
|
+
const mid = (Number(hi) / 1e9) ** 2;
|
|
126
|
+
const step = (mid * groupingBps) / 10_000;
|
|
127
|
+
const cashBase =
|
|
128
|
+
Number(preview.base.cashReserve.toString()) / 10 ** account.baseSide.assetDecimals;
|
|
129
|
+
const cashQuote =
|
|
130
|
+
Number(preview.quote.cashReserve.toString()) / 10 ** account.quoteSide.assetDecimals;
|
|
131
|
+
const cashBaseNad =
|
|
132
|
+
(n(preview.base.cashReserve) * NAD) / 10n ** BigInt(account.baseSide.assetDecimals);
|
|
133
|
+
const cashQuoteNad =
|
|
134
|
+
(n(preview.quote.cashReserve) * NAD) / 10n ** BigInt(account.quoteSide.assetDecimals);
|
|
135
|
+
const side = (direction: 1 | -1) => {
|
|
136
|
+
const levels: VirtualBookLevel[] = [];
|
|
137
|
+
let previousBase = 0,
|
|
138
|
+
previousQuote = 0;
|
|
139
|
+
for (let i = 1; i <= count; i++) {
|
|
140
|
+
let price = mid + direction * step * i;
|
|
141
|
+
if (!(price > 0)) break;
|
|
142
|
+
let targetSqrt = BigInt(Math.max(1, Math.floor(Math.sqrt(price) * 1e9)));
|
|
143
|
+
const exceedsCash = (s: bigint) => {
|
|
144
|
+
const p = point(s);
|
|
145
|
+
return direction === 1
|
|
146
|
+
? anchor.base - p.base > cashBaseNad
|
|
147
|
+
: anchor.quote - p.quote > cashQuoteNad;
|
|
148
|
+
};
|
|
149
|
+
const clipped = exceedsCash(targetSqrt);
|
|
150
|
+
if (clipped) {
|
|
151
|
+
// Include the final partial bucket instead of hiding executable cash
|
|
152
|
+
// whenever it is smaller than the selected price step.
|
|
153
|
+
let lower = direction === 1 ? hi : targetSqrt;
|
|
154
|
+
let upper = direction === 1 ? targetSqrt : hi;
|
|
155
|
+
while (upper - lower > 1n) {
|
|
156
|
+
const middle = (lower + upper) / 2n;
|
|
157
|
+
if (exceedsCash(middle) === (direction === 1)) upper = middle;
|
|
158
|
+
else lower = middle;
|
|
159
|
+
}
|
|
160
|
+
targetSqrt = direction === 1 ? lower : upper;
|
|
161
|
+
price = (Number(targetSqrt) / 1e9) ** 2;
|
|
162
|
+
}
|
|
163
|
+
const target = point(targetSqrt);
|
|
164
|
+
const total =
|
|
165
|
+
Number(direction === 1 ? anchor.base - target.base : target.base - anchor.base) / 1e9;
|
|
166
|
+
const quoteTotal =
|
|
167
|
+
Number(direction === 1 ? target.quote - anchor.quote : anchor.quote - target.quote) / 1e9;
|
|
168
|
+
// Never show borrowed/virtual inventory as withdrawable output cash.
|
|
169
|
+
if (
|
|
170
|
+
total <= previousBase ||
|
|
171
|
+
quoteTotal <= previousQuote ||
|
|
172
|
+
(direction === 1 && total > cashBase) ||
|
|
173
|
+
(direction === -1 && quoteTotal > cashQuote)
|
|
174
|
+
)
|
|
175
|
+
break;
|
|
176
|
+
const averagePrice = quoteTotal / total;
|
|
177
|
+
levels.push({
|
|
178
|
+
price,
|
|
179
|
+
size: total - previousBase,
|
|
180
|
+
total,
|
|
181
|
+
quoteSize: quoteTotal - previousQuote,
|
|
182
|
+
quoteTotal,
|
|
183
|
+
averagePrice,
|
|
184
|
+
impactPercent: Math.abs(averagePrice / mid - 1) * 100,
|
|
185
|
+
feePercent: 0,
|
|
186
|
+
surchargePercent: 0,
|
|
187
|
+
});
|
|
188
|
+
previousBase = total;
|
|
189
|
+
previousQuote = quoteTotal;
|
|
190
|
+
if (clipped) break;
|
|
191
|
+
}
|
|
192
|
+
return levels;
|
|
193
|
+
};
|
|
194
|
+
if (![mid, step, cashBase, cashQuote].every(Number.isFinite))
|
|
195
|
+
throw new Error("Nonfinite depth values");
|
|
196
|
+
return {
|
|
197
|
+
market: snapshot.market,
|
|
198
|
+
baseMint: account.baseSide.assetMint.toBase58(),
|
|
199
|
+
quoteMint: account.quoteSide.assetMint.toBase58(),
|
|
200
|
+
mid,
|
|
201
|
+
step,
|
|
202
|
+
bids: side(-1),
|
|
203
|
+
asks: side(1),
|
|
204
|
+
slot: snapshot.slot,
|
|
205
|
+
concentrated: concentrated > 0n,
|
|
206
|
+
};
|
|
207
|
+
}
|
|
208
|
+
|
|
209
|
+
/** Native cumulative swap quotes are alternatives against unchanged market state.
|
|
210
|
+
* Differences between adjacent cumulative quotes form the displayed price levels;
|
|
211
|
+
* this includes nonlinear fees without charging each bucket as a separate swap.
|
|
212
|
+
*/
|
|
213
|
+
export function projectDuskVirtualBook(
|
|
214
|
+
snapshot: import("./quotes.js").DuskVirtualBookQuotes | null
|
|
215
|
+
): VirtualBookView | null {
|
|
216
|
+
if (!snapshot) return null;
|
|
217
|
+
const { account, quotes, mid, groupingBps } = snapshot;
|
|
218
|
+
const side = (name: "bids" | "asks") => {
|
|
219
|
+
let previousBase = 0,
|
|
220
|
+
previousQuote = 0;
|
|
221
|
+
const rows: VirtualBookLevel[] = [];
|
|
222
|
+
for (const quote of quotes[name]) {
|
|
223
|
+
const input = n(quote.preview.exactAssetIn);
|
|
224
|
+
const output = n(quote.preview.amountOut) - quote.outputTransferFee;
|
|
225
|
+
const total = Number(name === "bids" ? input : output) / 10 ** account.baseSide.assetDecimals;
|
|
226
|
+
const quoteTotal =
|
|
227
|
+
Number(name === "bids" ? output : input) / 10 ** account.quoteSide.assetDecimals;
|
|
228
|
+
const size = total - previousBase;
|
|
229
|
+
const quoteSize = quoteTotal - previousQuote;
|
|
230
|
+
// A larger input that buys no additional output cannot add book depth.
|
|
231
|
+
if (size <= 0 || quoteSize <= 0) break;
|
|
232
|
+
const averagePrice = quoteTotal / total;
|
|
233
|
+
const row = {
|
|
234
|
+
price: quoteSize / size,
|
|
235
|
+
size,
|
|
236
|
+
total,
|
|
237
|
+
quoteSize,
|
|
238
|
+
quoteTotal,
|
|
239
|
+
averagePrice,
|
|
240
|
+
impactPercent: Math.abs(averagePrice / mid - 1) * 100,
|
|
241
|
+
feePercent: Number(quote.preview.totalFeeRateNad.toString()) / 1e7,
|
|
242
|
+
surchargePercent:
|
|
243
|
+
Number(n(quote.preview.divergenceFeeRateNad) + n(quote.preview.volatilityFeeRateNad)) /
|
|
244
|
+
1e7,
|
|
245
|
+
};
|
|
246
|
+
if (!Object.values(row).every(Number.isFinite))
|
|
247
|
+
throw new Error("Nonfinite native depth values");
|
|
248
|
+
rows.push(row);
|
|
249
|
+
previousBase = total;
|
|
250
|
+
previousQuote = quoteTotal;
|
|
251
|
+
}
|
|
252
|
+
return rows;
|
|
253
|
+
};
|
|
254
|
+
return {
|
|
255
|
+
market: snapshot.market,
|
|
256
|
+
baseMint: account.baseSide.assetMint.toBase58(),
|
|
257
|
+
quoteMint: account.quoteSide.assetMint.toBase58(),
|
|
258
|
+
mid,
|
|
259
|
+
step: (mid * groupingBps) / 10_000,
|
|
260
|
+
bids: side("bids"),
|
|
261
|
+
asks: side("asks"),
|
|
262
|
+
slot: snapshot.slot,
|
|
263
|
+
concentrated: n(account.amm.concentratedCurveCache.concentratedLiquidity) > 0n,
|
|
264
|
+
};
|
|
265
|
+
}
|
|
@@ -0,0 +1,308 @@
|
|
|
1
|
+
import { DuskWrite } from "../write.js";
|
|
2
|
+
import { oracleReadAccounts, encodeOraclePolicy } from "../oracle.js";
|
|
3
|
+
import { DEFAULT_READONLY_PUBLIC_KEY } from "../address.js";
|
|
4
|
+
import * as anchor from "@coral-xyz/anchor";
|
|
5
|
+
import type { Program } from "@coral-xyz/anchor";
|
|
6
|
+
const BN: typeof anchor.BN = Reflect.get(anchor, "BN") ?? Reflect.get(anchor, "default")?.BN;
|
|
7
|
+
import {
|
|
8
|
+
PublicKey,
|
|
9
|
+
SYSVAR_CLOCK_PUBKEY,
|
|
10
|
+
type AccountInfo,
|
|
11
|
+
type RpcResponseAndContext,
|
|
12
|
+
type SimulatedTransactionResponse,
|
|
13
|
+
type TransactionInstruction,
|
|
14
|
+
} from "@solana/web3.js";
|
|
15
|
+
import {
|
|
16
|
+
calculateEpochFee,
|
|
17
|
+
getTransferFeeConfig,
|
|
18
|
+
TOKEN_2022_PROGRAM_ID,
|
|
19
|
+
TOKEN_PROGRAM_ID,
|
|
20
|
+
unpackMint,
|
|
21
|
+
} from "@solana/spl-token";
|
|
22
|
+
import { deriveFutarchyAuthorityAddress, deriveMarketAddress, MARKET_LAYOUT_VERSION } from "../constants.js";
|
|
23
|
+
import { decodePreviewSwapReturnData, type SwapPreview } from "../preview.js";
|
|
24
|
+
import type { Market } from "../type-aliases.js";
|
|
25
|
+
import type { Dusk } from "../types_v2.js";
|
|
26
|
+
import { simulatePreviewWithContext, type PreviewSimulationOptions } from "../simulation.js";
|
|
27
|
+
import type { DuskVirtualBookSnapshot } from "./market.js";
|
|
28
|
+
import { projectDuskVirtualBookCurve } from "./projection.js";
|
|
29
|
+
|
|
30
|
+
type Side = "bids" | "asks";
|
|
31
|
+
export type VirtualBookQuoteRequest = { side: Side; amount: bigint };
|
|
32
|
+
export type VirtualBookQuote = {
|
|
33
|
+
preview: SwapPreview;
|
|
34
|
+
outputTransferFee: bigint;
|
|
35
|
+
slot: number;
|
|
36
|
+
};
|
|
37
|
+
export type DuskVirtualBookQuotes = DuskVirtualBookSnapshot & {
|
|
38
|
+
groupingBps: number;
|
|
39
|
+
firstQuoteSlot: number;
|
|
40
|
+
mid: number;
|
|
41
|
+
quotes: Record<Side, VirtualBookQuote[]>;
|
|
42
|
+
};
|
|
43
|
+
const CLOCK_OWNER = "Sysvar1111111111111111111111111111111111111";
|
|
44
|
+
const BATCH_SIZE = 4;
|
|
45
|
+
const U64_MAX = (1n << 64n) - 1n;
|
|
46
|
+
const raw = (value: { toString(): string }) => {
|
|
47
|
+
const amount = BigInt(value.toString());
|
|
48
|
+
if (amount < 0n || amount > U64_MAX) throw new Error("Invalid depth amount");
|
|
49
|
+
return amount;
|
|
50
|
+
};
|
|
51
|
+
const canonicalBytes = (data: string[]) => {
|
|
52
|
+
if (
|
|
53
|
+
data.length !== 2 ||
|
|
54
|
+
data[1] !== "base64" ||
|
|
55
|
+
Buffer.from(data[0], "base64").toString("base64") !== data[0]
|
|
56
|
+
)
|
|
57
|
+
throw new Error("Invalid native depth encoding");
|
|
58
|
+
return Buffer.from(data[0], "base64");
|
|
59
|
+
};
|
|
60
|
+
|
|
61
|
+
/** Only the requested top-level, read-only SDK previews can supply these returns. */
|
|
62
|
+
export function decodeDuskVirtualBookBatch(
|
|
63
|
+
program: Program<Dusk>,
|
|
64
|
+
snapshot: DuskVirtualBookSnapshot,
|
|
65
|
+
requests: VirtualBookQuoteRequest[],
|
|
66
|
+
result: RpcResponseAndContext<SimulatedTransactionResponse>,
|
|
67
|
+
floor: number
|
|
68
|
+
) {
|
|
69
|
+
const { slot } = result.context;
|
|
70
|
+
const { value } = result;
|
|
71
|
+
const programId = snapshot.programId;
|
|
72
|
+
if (program.programId.toBase58() !== programId) throw new Error("Depth SDK deployment mismatch");
|
|
73
|
+
if (!Number.isSafeInteger(slot) || slot < floor || value.err)
|
|
74
|
+
throw new Error("Native market depth preview unavailable");
|
|
75
|
+
const returned = value.returnData;
|
|
76
|
+
const accounts = value.accounts;
|
|
77
|
+
const oracleAccounts = oracleReadAccounts(snapshot.account.oracle.policy);
|
|
78
|
+
if (!returned || returned.programId !== programId || accounts?.length !== 5 + oracleAccounts.length)
|
|
79
|
+
throw new Error("Incomplete native market depth preview");
|
|
80
|
+
const infos: AccountInfo<Buffer>[] = accounts.map((info) => {
|
|
81
|
+
if (!info || info.executable) throw new Error("Invalid native depth account");
|
|
82
|
+
return {
|
|
83
|
+
owner: new PublicKey(info.owner),
|
|
84
|
+
executable: false,
|
|
85
|
+
lamports: info.lamports,
|
|
86
|
+
data: canonicalBytes(info.data),
|
|
87
|
+
};
|
|
88
|
+
});
|
|
89
|
+
const [marketInfo, baseInfo, quoteInfo, authorityInfo, clock] = infos;
|
|
90
|
+
if (
|
|
91
|
+
marketInfo.owner.toBase58() !== programId ||
|
|
92
|
+
authorityInfo.owner.toBase58() !== programId ||
|
|
93
|
+
clock.owner.toBase58() !== CLOCK_OWNER ||
|
|
94
|
+
clock.data.length !== 40 ||
|
|
95
|
+
clock.data.readBigUInt64LE(0) !== BigInt(slot)
|
|
96
|
+
)
|
|
97
|
+
throw new Error("Native depth account identity or clock mismatch");
|
|
98
|
+
const epoch = clock.data.readBigUInt64LE(16);
|
|
99
|
+
const market = program.coder.accounts.decode<Market>("market", marketInfo.data);
|
|
100
|
+
const [address, bump] = deriveMarketAddress(
|
|
101
|
+
market.baseSide.assetMint,
|
|
102
|
+
market.quoteSide.assetMint,
|
|
103
|
+
market.paramsHash,
|
|
104
|
+
program.programId
|
|
105
|
+
);
|
|
106
|
+
if (
|
|
107
|
+
market.version !== MARKET_LAYOUT_VERSION ||
|
|
108
|
+
market.bump !== bump ||
|
|
109
|
+
address.toBase58() !== snapshot.market ||
|
|
110
|
+
!market.baseSide.assetMint.equals(snapshot.account.baseSide.assetMint) ||
|
|
111
|
+
!market.quoteSide.assetMint.equals(snapshot.account.quoteSide.assetMint) ||
|
|
112
|
+
market.amm.concentratedCurveCache.mathRevision !== 1
|
|
113
|
+
)
|
|
114
|
+
throw new Error("Native depth market identity changed");
|
|
115
|
+
if (!Buffer.from(encodeOraclePolicy(market.oracle.policy))
|
|
116
|
+
.equals(Buffer.from(encodeOraclePolicy(snapshot.account.oracle.policy))))
|
|
117
|
+
throw new Error("Depth oracle policy changed");
|
|
118
|
+
for (const info of infos.slice(5)) {
|
|
119
|
+
if (!info.owner.equals(market.oracle.policy.provider))
|
|
120
|
+
throw new Error("Depth oracle owner changed");
|
|
121
|
+
}
|
|
122
|
+
const mint = (info: AccountInfo<Buffer>, side: Market["baseSide"]) => {
|
|
123
|
+
if (!info.owner.equals(TOKEN_PROGRAM_ID) && !info.owner.equals(TOKEN_2022_PROGRAM_ID))
|
|
124
|
+
throw new Error("Invalid depth token program");
|
|
125
|
+
const decoded = unpackMint(side.assetMint, info, info.owner);
|
|
126
|
+
if (!decoded.isInitialized || decoded.decimals !== side.assetDecimals)
|
|
127
|
+
throw new Error("Depth token precision changed");
|
|
128
|
+
return decoded;
|
|
129
|
+
};
|
|
130
|
+
const base = mint(baseInfo, market.baseSide);
|
|
131
|
+
const quote = mint(quoteInfo, market.quoteSide);
|
|
132
|
+
const prefix = `Program return: ${programId} `;
|
|
133
|
+
const returns = (value.logs ?? [])
|
|
134
|
+
.filter((line) => line.startsWith(prefix))
|
|
135
|
+
.map((line) => line.slice(prefix.length));
|
|
136
|
+
if (returns.length !== requests.length || returns.at(-1) !== returned.data[0])
|
|
137
|
+
throw new Error("Incomplete native depth return log");
|
|
138
|
+
canonicalBytes(returned.data);
|
|
139
|
+
const quotes = returns.map((encoded, index) => {
|
|
140
|
+
canonicalBytes([encoded, "base64"]);
|
|
141
|
+
const preview = decodePreviewSwapReturnData([encoded, "base64"]);
|
|
142
|
+
const request = requests[index];
|
|
143
|
+
const fromBase = request.side === "bids";
|
|
144
|
+
if (
|
|
145
|
+
Object.keys(preview.assetIn).join() !== (fromBase ? "base" : "quote") ||
|
|
146
|
+
Object.keys(preview.assetOut).join() !== (fromBase ? "quote" : "base") ||
|
|
147
|
+
raw(preview.exactAssetIn) !== request.amount ||
|
|
148
|
+
raw(preview.startPriceNad) === 0n ||
|
|
149
|
+
raw(preview.totalFeeRateNad) > 1_000_000_000n ||
|
|
150
|
+
raw(preview.divergenceFeeRateNad) + raw(preview.volatilityFeeRateNad) >
|
|
151
|
+
raw(preview.totalFeeRateNad)
|
|
152
|
+
)
|
|
153
|
+
throw new Error("Native depth quote does not match its request");
|
|
154
|
+
// SwapPreview.amountOut is the vault debit. The real swap applies the
|
|
155
|
+
// output mint's transfer fee once more before crediting the user's account.
|
|
156
|
+
const transferFees = getTransferFeeConfig(fromBase ? quote : base);
|
|
157
|
+
const outputTransferFee = transferFees
|
|
158
|
+
? calculateEpochFee(transferFees, epoch, raw(preview.amountOut))
|
|
159
|
+
: 0n;
|
|
160
|
+
return { side: request.side, preview, outputTransferFee, slot };
|
|
161
|
+
});
|
|
162
|
+
return {
|
|
163
|
+
market,
|
|
164
|
+
quotes,
|
|
165
|
+
slot,
|
|
166
|
+
epoch,
|
|
167
|
+
// Include the source account bytes: a read-only preview does not persist
|
|
168
|
+
// its report in the market account. Different feeds cannot be combined
|
|
169
|
+
// into one apparent depth snapshot even when market bytes are unchanged.
|
|
170
|
+
state: infos
|
|
171
|
+
.filter((_, index) => index !== 4)
|
|
172
|
+
.map((info) => `${info.owner}:${info.data.toString("base64")}`)
|
|
173
|
+
.join("|"),
|
|
174
|
+
};
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
/** One read-only native preview for each requested cumulative input. */
|
|
178
|
+
export async function previewVirtualBookBatch(
|
|
179
|
+
program: Program<Dusk>,
|
|
180
|
+
snapshot: DuskVirtualBookSnapshot,
|
|
181
|
+
requests: VirtualBookQuoteRequest[],
|
|
182
|
+
options: PreviewSimulationOptions = {}
|
|
183
|
+
) {
|
|
184
|
+
if (program.programId.toBase58() !== snapshot.programId)
|
|
185
|
+
throw new Error("Depth SDK deployment mismatch");
|
|
186
|
+
if (!requests.length || requests.length > BATCH_SIZE)
|
|
187
|
+
throw new Error("Depth batches require one to four requests");
|
|
188
|
+
const { account, market } = snapshot;
|
|
189
|
+
const floor = Math.max(snapshot.slot, options.minContextSlot ?? 0);
|
|
190
|
+
const instructions = await Promise.all(
|
|
191
|
+
requests.map(({ side, amount }) => {
|
|
192
|
+
if (side !== "bids" && side !== "asks") throw new Error("Invalid depth side");
|
|
193
|
+
if (raw(amount) === 0n) throw new Error("Invalid depth amount");
|
|
194
|
+
const fromBase = side === "bids";
|
|
195
|
+
return new DuskWrite(program).instruction("previewSwap", { exactAssetIn: new BN(amount.toString()) }, {
|
|
196
|
+
accounts: {
|
|
197
|
+
market: new PublicKey(market),
|
|
198
|
+
futarchyAuthority: deriveFutarchyAuthorityAddress(program.programId)[0],
|
|
199
|
+
assetInMint: fromBase ? account.baseSide.assetMint : account.quoteSide.assetMint,
|
|
200
|
+
assetOutMint: fromBase ? account.quoteSide.assetMint : account.baseSide.assetMint,
|
|
201
|
+
},
|
|
202
|
+
oraclePolicy: account.oracle.policy,
|
|
203
|
+
});
|
|
204
|
+
})
|
|
205
|
+
);
|
|
206
|
+
const response = await simulatePreviewWithContext(program, instructions, {
|
|
207
|
+
...options,
|
|
208
|
+
feePayer: options.feePayer ?? DEFAULT_READONLY_PUBLIC_KEY,
|
|
209
|
+
commitment: "confirmed",
|
|
210
|
+
minContextSlot: floor,
|
|
211
|
+
accounts: [
|
|
212
|
+
market,
|
|
213
|
+
account.baseSide.assetMint,
|
|
214
|
+
account.quoteSide.assetMint,
|
|
215
|
+
deriveFutarchyAuthorityAddress(program.programId)[0],
|
|
216
|
+
SYSVAR_CLOCK_PUBKEY,
|
|
217
|
+
...oracleReadAccounts(account.oracle.policy).map(meta => meta.pubkey),
|
|
218
|
+
],
|
|
219
|
+
});
|
|
220
|
+
return {
|
|
221
|
+
...decodeDuskVirtualBookBatch(program, snapshot, requests, response, floor),
|
|
222
|
+
observedAt: response.observedAt,
|
|
223
|
+
};
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
export interface VirtualBookQuoteOptions extends PreviewSimulationOptions {
|
|
227
|
+
groupingBps?: number;
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
/** Sample the curve, then price cumulative sizes through bounded native batches. */
|
|
231
|
+
export async function previewVirtualBookQuotes(
|
|
232
|
+
program: Program<Dusk>,
|
|
233
|
+
snapshot: DuskVirtualBookSnapshot,
|
|
234
|
+
options: VirtualBookQuoteOptions = {}
|
|
235
|
+
): Promise<DuskVirtualBookQuotes> {
|
|
236
|
+
options.signal?.throwIfAborted();
|
|
237
|
+
const groupingBps = options.groupingBps ?? 10;
|
|
238
|
+
if (program.programId.toBase58() !== snapshot.programId)
|
|
239
|
+
throw new Error("Depth SDK deployment mismatch");
|
|
240
|
+
const curve = projectDuskVirtualBookCurve(snapshot, groupingBps);
|
|
241
|
+
if (!curve) throw new Error("Market depth curve unavailable");
|
|
242
|
+
const requests: VirtualBookQuoteRequest[] = [];
|
|
243
|
+
for (const side of ["bids", "asks"] as const) {
|
|
244
|
+
let previous = 0n;
|
|
245
|
+
const decimals =
|
|
246
|
+
side === "bids"
|
|
247
|
+
? snapshot.account.baseSide.assetDecimals
|
|
248
|
+
: snapshot.account.quoteSide.assetDecimals;
|
|
249
|
+
for (const level of curve[side]) {
|
|
250
|
+
const decimal = (side === "bids" ? level.total : level.quoteTotal).toFixed(decimals);
|
|
251
|
+
const [whole, fraction = ""] = decimal.split(".");
|
|
252
|
+
if (!/^(0|[1-9][0-9]*)(\.[0-9]*)?$/.test(decimal)) throw new Error("Invalid depth amount");
|
|
253
|
+
const amount = raw(
|
|
254
|
+
BigInt(whole) * 10n ** BigInt(decimals) + BigInt(fraction.padEnd(decimals, "0") || "0")
|
|
255
|
+
);
|
|
256
|
+
if (amount <= previous) continue;
|
|
257
|
+
requests.push({ side, amount });
|
|
258
|
+
previous = amount;
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
const batches: VirtualBookQuoteRequest[][] = [];
|
|
262
|
+
const batchSize = snapshot.account.oracle.policy.lambdaBps > 0 ? 1 : BATCH_SIZE;
|
|
263
|
+
for (let i = 0; i < requests.length; i += batchSize)
|
|
264
|
+
batches.push(requests.slice(i, i + batchSize));
|
|
265
|
+
const results = await Promise.all(
|
|
266
|
+
batches.map((batch) => previewVirtualBookBatch(program, snapshot, batch, options))
|
|
267
|
+
);
|
|
268
|
+
options.signal?.throwIfAborted();
|
|
269
|
+
return combineVirtualBookBatches(snapshot, groupingBps, curve.mid, results);
|
|
270
|
+
}
|
|
271
|
+
|
|
272
|
+
/** Refuse to combine observations across trades, fee changes, epochs or stale banks. */
|
|
273
|
+
export function combineVirtualBookBatches(
|
|
274
|
+
snapshot: DuskVirtualBookSnapshot,
|
|
275
|
+
groupingBps: number,
|
|
276
|
+
emptyMid: number,
|
|
277
|
+
results: Awaited<ReturnType<typeof previewVirtualBookBatch>>[]
|
|
278
|
+
): DuskVirtualBookQuotes {
|
|
279
|
+
const first = results[0];
|
|
280
|
+
const lastSlot = Math.max(snapshot.slot, ...results.map((result) => result.slot));
|
|
281
|
+
const firstQuoteSlot = results.length
|
|
282
|
+
? Math.min(...results.map((result) => result.slot))
|
|
283
|
+
: snapshot.slot;
|
|
284
|
+
if (
|
|
285
|
+
lastSlot - firstQuoteSlot > 8 ||
|
|
286
|
+
results.some(
|
|
287
|
+
(result) =>
|
|
288
|
+
result.slot < snapshot.slot || result.state !== first.state || result.epoch !== first.epoch
|
|
289
|
+
)
|
|
290
|
+
)
|
|
291
|
+
throw new Error("Market depth changed during native previews");
|
|
292
|
+
const allQuotes = results.flatMap((result) => result.quotes);
|
|
293
|
+
const start = allQuotes[0]?.preview.startPriceNad.toString();
|
|
294
|
+
if (allQuotes.some((quote) => quote.preview.startPriceNad.toString() !== start))
|
|
295
|
+
throw new Error("Market depth price changed during native previews");
|
|
296
|
+
return {
|
|
297
|
+
...snapshot,
|
|
298
|
+
account: first?.market ?? snapshot.account,
|
|
299
|
+
slot: lastSlot,
|
|
300
|
+
firstQuoteSlot,
|
|
301
|
+
groupingBps,
|
|
302
|
+
mid: start ? Number(start) / 1e9 : emptyMid,
|
|
303
|
+
quotes: {
|
|
304
|
+
bids: allQuotes.filter((quote) => quote.side === "bids"),
|
|
305
|
+
asks: allQuotes.filter((quote) => quote.side === "asks"),
|
|
306
|
+
},
|
|
307
|
+
};
|
|
308
|
+
}
|