@gainsnetwork/sdk 1.3.0-rc6 → 1.4.0-rc1
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/lib/backend/globalTrades/index.js +10 -10
- package/lib/backend/tradingVariables/backend.types.d.ts +4 -11
- package/lib/backend/tradingVariables/converter.d.ts +3 -7
- package/lib/backend/tradingVariables/converter.js +63 -71
- package/lib/backend/tradingVariables/index.js +8 -10
- package/lib/backend/tradingVariables/types.d.ts +2 -4
- package/lib/constants.d.ts +4 -0
- package/lib/constants.js +6 -1
- package/lib/contracts/addresses.d.ts +1 -1
- package/lib/contracts/addresses.js +4 -8
- package/lib/contracts/addresses.json +0 -29
- package/lib/contracts/index.d.ts +1 -1
- package/lib/contracts/index.js +3 -3
- package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +299 -608
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +313 -1996
- package/lib/contracts/utils/borrowingFees.js +20 -9
- package/lib/contracts/utils/openTrades.js +20 -11
- package/lib/contracts/utils/pairs.d.ts +2 -13
- package/lib/contracts/utils/pairs.js +34 -80
- package/lib/index.d.ts +0 -1
- package/lib/index.js +0 -1
- package/lib/markets/forex.js +1 -1
- package/lib/markets/leverage/builder.js +2 -2
- package/lib/markets/price/index.d.ts +0 -1
- package/lib/markets/price/index.js +0 -1
- package/lib/markets/price/types.d.ts +0 -27
- package/lib/trade/fees/borrowing/builder.js +3 -2
- package/lib/trade/fees/borrowing/converter.js +1 -5
- package/lib/trade/fees/borrowing/index.js +5 -5
- package/lib/trade/fees/borrowingV2/builder.js +4 -3
- package/lib/trade/fees/borrowingV2/converter.js +1 -1
- package/lib/trade/fees/borrowingV2/fetcher.js +32 -26
- package/lib/trade/fees/borrowingV2/index.js +3 -3
- package/lib/trade/fees/converter.js +22 -22
- package/lib/trade/fees/fundingFees/builder.js +7 -6
- package/lib/trade/fees/fundingFees/converter.js +1 -1
- package/lib/trade/fees/fundingFees/fetcher.js +25 -16
- package/lib/trade/fees/fundingFees/index.js +3 -2
- package/lib/trade/fees/tiers/index.js +2 -1
- package/lib/trade/fees/trading/index.js +3 -5
- package/lib/trade/liquidation/builder.js +2 -1
- package/lib/trade/liquidation/index.js +6 -4
- package/lib/trade/liquidation.d.ts +12 -0
- package/lib/trade/liquidation.js +55 -0
- package/lib/trade/oiWindows.js +2 -1
- package/lib/trade/pnl/builder.js +2 -1
- package/lib/trade/pnl/converter.js +1 -1
- package/lib/trade/pnl/index.js +7 -4
- package/lib/trade/pnl.d.ts +10 -0
- package/lib/trade/pnl.js +33 -0
- package/lib/trade/priceImpact/close/builder.js +2 -1
- package/lib/trade/priceImpact/close/index.js +1 -4
- package/lib/trade/priceImpact/cumulVol/builder.js +11 -21
- package/lib/trade/priceImpact/cumulVol/converter.d.ts +0 -63
- package/lib/trade/priceImpact/cumulVol/converter.js +1 -97
- package/lib/trade/priceImpact/cumulVol/index.d.ts +6 -7
- package/lib/trade/priceImpact/cumulVol/index.js +41 -149
- package/lib/trade/priceImpact/open/builder.js +2 -1
- package/lib/trade/priceImpact/open/index.js +1 -7
- package/lib/trade/priceImpact/skew/builder.js +3 -2
- package/lib/trade/priceImpact/skew/converter.js +1 -1
- package/lib/trade/priceImpact/skew/fetcher.js +33 -24
- package/lib/trade/types.d.ts +5 -1
- package/lib/trade/types.js +4 -0
- package/package.json +2 -2
- package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +0 -1911
- package/lib/contracts/types/generated/GFarmTradingStorageV5.js +0 -2
- package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +0 -1067
- package/lib/contracts/types/generated/GNSBorrowingFees.js +0 -2
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +0 -979
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +0 -2
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +0 -1058
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +0 -2
- package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +0 -533
- package/lib/contracts/types/generated/GNSNftRewardsV6.js +0 -2
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +0 -613
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +0 -2
- package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +0 -911
- package/lib/contracts/types/generated/GNSPairInfosV6_1.js +0 -2
- package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +0 -660
- package/lib/contracts/types/generated/GNSPairsStorageV6.js +0 -2
- package/lib/contracts/types/generated/GNSTrading.d.ts +0 -758
- package/lib/contracts/types/generated/GNSTrading.js +0 -2
- package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +0 -875
- package/lib/contracts/types/generated/GNSTradingCallbacks.js +0 -2
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +0 -806
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +0 -2
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +0 -821
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +0 -2
- package/lib/contracts/types/generated/GNSTradingStorage.d.ts +0 -1387
- package/lib/contracts/types/generated/GNSTradingStorage.js +0 -2
- package/lib/contracts/types/generated/GTokenV6_3_2.d.ts +0 -1838
- package/lib/contracts/types/generated/GTokenV6_3_2.js +0 -2
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +0 -83
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +0 -2691
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.d.ts +0 -88
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.js +0 -1654
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.d.ts +0 -113
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.js +0 -1742
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +0 -124
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +0 -1784
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +0 -100
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +0 -1116
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +0 -100
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +0 -1003
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +0 -98
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +0 -1485
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +0 -117
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +0 -1265
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +0 -82
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +0 -1273
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +0 -82
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +0 -1326
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +0 -113
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +0 -1428
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +0 -96
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +0 -2241
- package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +0 -95
- package/lib/contracts/types/generated/factories/GNSTrading__factory.js +0 -1071
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +0 -110
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +0 -2682
- package/lib/markets/oi/fetcher.d.ts +0 -58
- package/lib/markets/oi/fetcher.js +0 -181
- package/lib/markets/oi/validation.d.ts +0 -80
- package/lib/markets/oi/validation.js +0 -172
- package/lib/markets/price/signedPrices.d.ts +0 -36
- package/lib/markets/price/signedPrices.js +0 -181
- package/lib/pricing/depthBands/converter.d.ts +0 -65
- package/lib/pricing/depthBands/converter.js +0 -155
- package/lib/pricing/depthBands/decoder.d.ts +0 -32
- package/lib/pricing/depthBands/decoder.js +0 -109
- package/lib/pricing/depthBands/encoder.d.ts +0 -19
- package/lib/pricing/depthBands/encoder.js +0 -105
- package/lib/pricing/depthBands/index.d.ts +0 -8
- package/lib/pricing/depthBands/index.js +0 -26
- package/lib/pricing/depthBands/types.d.ts +0 -49
- package/lib/pricing/depthBands/types.js +0 -10
- package/lib/pricing/depthBands/validator.d.ts +0 -22
- package/lib/pricing/depthBands/validator.js +0 -113
- package/lib/pricing/depthBands.d.ts +0 -39
- package/lib/pricing/depthBands.js +0 -94
- package/lib/pricing/index.d.ts +0 -4
- package/lib/pricing/index.js +0 -20
- package/lib/trade/effectiveLeverage/builder.d.ts +0 -23
- package/lib/trade/effectiveLeverage/builder.js +0 -30
- package/lib/trade/fees/holdingFees/index.d.ts +0 -46
- package/lib/trade/fees/holdingFees/index.js +0 -105
- package/lib/trade/fees/holdingFees/types.d.ts +0 -23
- package/lib/trade/fees/holdingFees/types.js +0 -5
- package/lib/trade/fees/trading/holdingFees.d.ts +0 -28
- package/lib/trade/fees/trading/holdingFees.js +0 -66
- package/lib/trade/fees/trading/holdingFeesStructured.d.ts +0 -28
- package/lib/trade/fees/trading/holdingFeesStructured.js +0 -66
- package/lib/trade/priceImpact/cumulVol/types.d.ts +0 -11
- package/lib/trade/priceImpact/cumulVol/types.js +0 -2
|
@@ -1,181 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.isValidSignedPricesOrderType = exports.isValidSignedPricesChain = exports.validateSignedPricesPairs = exports.fetchSignedLookbackPricesWithRetry = exports.fetchSignedPricesWithRetry = exports.fetchSignedLookbackPrices = exports.fetchSignedPrices = void 0;
|
|
4
|
-
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
5
|
-
const types_1 = require("../../contracts/types");
|
|
6
|
-
const trade_1 = require("../../trade");
|
|
7
|
-
const fetchSignedPrices = async (input) => {
|
|
8
|
-
const { minAnswers, timeoutMs, oracles, chain } = {
|
|
9
|
-
minAnswers: input.chain === types_1.ChainId.ARBITRUM_SEPOLIA ? 2 : 3,
|
|
10
|
-
timeoutMs: 1000,
|
|
11
|
-
...input,
|
|
12
|
-
};
|
|
13
|
-
if (!(0, exports.isValidSignedPricesChain)(chain))
|
|
14
|
-
throw new Error(`Invalid chain ${chain}`);
|
|
15
|
-
const { valid, pairs } = (0, exports.validateSignedPricesPairs)(input.pairs);
|
|
16
|
-
if (!valid)
|
|
17
|
-
throw new Error(`Invalid pairs array`);
|
|
18
|
-
return await initiateSignedPricesRequest(oracles, "signPrices", JSON.stringify({ pairs, chain }), minAnswers, timeoutMs);
|
|
19
|
-
};
|
|
20
|
-
exports.fetchSignedPrices = fetchSignedPrices;
|
|
21
|
-
const fetchSignedLookbackPrices = async (input) => {
|
|
22
|
-
const { minAnswers, timeoutMs, oracles, trader, tradeIndex, pair, orderType, currentBlock, fromBlock, chain, } = {
|
|
23
|
-
minAnswers: input.chain === types_1.ChainId.ARBITRUM_SEPOLIA ? 2 : 3,
|
|
24
|
-
timeoutMs: 6000,
|
|
25
|
-
...input,
|
|
26
|
-
};
|
|
27
|
-
if (!(0, exports.isValidSignedPricesChain)(chain))
|
|
28
|
-
throw new Error(`Invalid chain ${chain}`);
|
|
29
|
-
if (!(0, exports.isValidSignedPricesOrderType)(orderType))
|
|
30
|
-
throw new Error(`Invalid orderType ${orderType}`);
|
|
31
|
-
if (isNaN(pair))
|
|
32
|
-
throw new Error(`Invalid pair ${pair}`);
|
|
33
|
-
if (isNaN(tradeIndex) || tradeIndex < 0)
|
|
34
|
-
throw new Error(`Invalid tradeIndex ${tradeIndex}`);
|
|
35
|
-
if (!currentBlock || !fromBlock)
|
|
36
|
-
throw new Error(`Invalid block numbers`);
|
|
37
|
-
return await initiateSignedPricesRequest(oracles, "signLookbackPrices", JSON.stringify({
|
|
38
|
-
trader,
|
|
39
|
-
tradeIndex,
|
|
40
|
-
pair,
|
|
41
|
-
orderType,
|
|
42
|
-
currentBlock,
|
|
43
|
-
fromBlock,
|
|
44
|
-
chain,
|
|
45
|
-
}), minAnswers, timeoutMs);
|
|
46
|
-
};
|
|
47
|
-
exports.fetchSignedLookbackPrices = fetchSignedLookbackPrices;
|
|
48
|
-
// Helper to determine if an error is likely transient
|
|
49
|
-
const isTransientError = (error) => {
|
|
50
|
-
const message = (error instanceof Error ? error.message : String(error))?.toLowerCase() ||
|
|
51
|
-
"";
|
|
52
|
-
return (message.includes("timeout") ||
|
|
53
|
-
message.includes("aborted") ||
|
|
54
|
-
message.includes("not enough valid signed prices") ||
|
|
55
|
-
message.includes("network") ||
|
|
56
|
-
message.includes("fetch failed"));
|
|
57
|
-
};
|
|
58
|
-
// Fetch signed prices with retry logic for transient failures
|
|
59
|
-
const fetchSignedPricesWithRetry = async (input, retryOptions) => {
|
|
60
|
-
const { maxRetries = 2, retryDelayMs = 500, backoffMultiplier = 1.5, } = retryOptions || {};
|
|
61
|
-
let lastError;
|
|
62
|
-
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
63
|
-
try {
|
|
64
|
-
return await (0, exports.fetchSignedPrices)(input);
|
|
65
|
-
}
|
|
66
|
-
catch (error) {
|
|
67
|
-
lastError = error;
|
|
68
|
-
// Don't retry non-transient errors or if we're out of retries
|
|
69
|
-
if (attempt === maxRetries || !isTransientError(error)) {
|
|
70
|
-
throw error;
|
|
71
|
-
}
|
|
72
|
-
// Wait with exponential backoff before retry
|
|
73
|
-
const delay = Math.floor(retryDelayMs * Math.pow(backoffMultiplier, attempt));
|
|
74
|
-
await new Promise(resolve => setTimeout(resolve, delay));
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
throw lastError;
|
|
78
|
-
};
|
|
79
|
-
exports.fetchSignedPricesWithRetry = fetchSignedPricesWithRetry;
|
|
80
|
-
// Fetch lookback prices with retry logic
|
|
81
|
-
const fetchSignedLookbackPricesWithRetry = async (input, retryOptions) => {
|
|
82
|
-
const { maxRetries = 2, retryDelayMs = 1000, backoffMultiplier = 2, } = retryOptions || {};
|
|
83
|
-
let lastError;
|
|
84
|
-
for (let attempt = 0; attempt <= maxRetries; attempt++) {
|
|
85
|
-
try {
|
|
86
|
-
return await (0, exports.fetchSignedLookbackPrices)(input);
|
|
87
|
-
}
|
|
88
|
-
catch (error) {
|
|
89
|
-
lastError = error;
|
|
90
|
-
// Don't retry non-transient errors or if we're out of retries
|
|
91
|
-
if (attempt === maxRetries || !isTransientError(error)) {
|
|
92
|
-
throw error;
|
|
93
|
-
}
|
|
94
|
-
// Wait with exponential backoff before retry
|
|
95
|
-
const delay = Math.floor(retryDelayMs * Math.pow(backoffMultiplier, attempt));
|
|
96
|
-
await new Promise(resolve => setTimeout(resolve, delay));
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
throw lastError;
|
|
100
|
-
};
|
|
101
|
-
exports.fetchSignedLookbackPricesWithRetry = fetchSignedLookbackPricesWithRetry;
|
|
102
|
-
// @todo optional filtering to minAnswers best responses
|
|
103
|
-
const initiateSignedPricesRequest = async (oracles, request, requestBody, minAnswers, timeoutMs) => {
|
|
104
|
-
try {
|
|
105
|
-
// Fetch signed prices from all oracles in parallel
|
|
106
|
-
const signedPrices = await Promise.allSettled(oracles.map(oracle => _getSignedPricesFromSigner(`${oracle.url}/${request}`, requestBody, oracle?.key, timeoutMs)));
|
|
107
|
-
// Filter out failed requests and null responses, then sort by signerId
|
|
108
|
-
const successfulResponses = signedPrices.filter(res => res.status === "fulfilled" && res.value !== null // Filter out failed or null responses
|
|
109
|
-
)
|
|
110
|
-
// Extract `value`
|
|
111
|
-
.map((res) => res.value)
|
|
112
|
-
// Sort by signerId, contracts expect signerId ascending
|
|
113
|
-
.sort((a, b) => a.signedData.signerId - b.signedData.signerId);
|
|
114
|
-
// Ensure we have at least `minAnswers` valid responses
|
|
115
|
-
if (successfulResponses.length < minAnswers) {
|
|
116
|
-
throw new Error(`Not enough valid signed prices. Wanted ${minAnswers} but got ${successfulResponses.length}`);
|
|
117
|
-
}
|
|
118
|
-
return successfulResponses;
|
|
119
|
-
}
|
|
120
|
-
catch (e) {
|
|
121
|
-
console.error("Error processing signed prices", e);
|
|
122
|
-
throw e;
|
|
123
|
-
}
|
|
124
|
-
};
|
|
125
|
-
const _getSignedPricesFromSigner = async (url, requestBody, authKey, timeoutMs) => {
|
|
126
|
-
try {
|
|
127
|
-
const controller = new AbortController();
|
|
128
|
-
const timeout = setTimeout(() => {
|
|
129
|
-
controller.abort();
|
|
130
|
-
}, timeoutMs || 2000);
|
|
131
|
-
const response = await fetch(`${url}`, {
|
|
132
|
-
method: "POST",
|
|
133
|
-
headers: {
|
|
134
|
-
"Content-Type": "application/json",
|
|
135
|
-
"x-api-key": authKey || "",
|
|
136
|
-
},
|
|
137
|
-
body: requestBody,
|
|
138
|
-
signal: controller.signal,
|
|
139
|
-
});
|
|
140
|
-
clearTimeout(timeout);
|
|
141
|
-
if (!response.ok) {
|
|
142
|
-
throw new Error(`Failed to fetch signed prices from ${url}: ${response.statusText}`);
|
|
143
|
-
}
|
|
144
|
-
return (await response.json());
|
|
145
|
-
}
|
|
146
|
-
catch (error) {
|
|
147
|
-
console.error(`Error fetching signed prices from ${url}:`, {
|
|
148
|
-
error: error?.message,
|
|
149
|
-
});
|
|
150
|
-
return null;
|
|
151
|
-
}
|
|
152
|
-
};
|
|
153
|
-
const validateSignedPricesPairs = (pairs) => {
|
|
154
|
-
if (!Array.isArray(pairs) || pairs?.length === 0 || pairs.some(p => isNaN(p)))
|
|
155
|
-
return { valid: false, pairs: [] };
|
|
156
|
-
// Pairs must always be in ascending order
|
|
157
|
-
return { valid: true, pairs: [...new Set(pairs)].sort((a, b) => a - b) };
|
|
158
|
-
};
|
|
159
|
-
exports.validateSignedPricesPairs = validateSignedPricesPairs;
|
|
160
|
-
const isValidSignedPricesChain = (chainId) => {
|
|
161
|
-
return (!isNaN(chainId) &&
|
|
162
|
-
[
|
|
163
|
-
types_1.ChainId.POLYGON,
|
|
164
|
-
types_1.ChainId.BASE,
|
|
165
|
-
types_1.ChainId.ARBITRUM,
|
|
166
|
-
types_1.ChainId.ARBITRUM_SEPOLIA,
|
|
167
|
-
types_1.ChainId.APECHAIN,
|
|
168
|
-
].includes(chainId));
|
|
169
|
-
};
|
|
170
|
-
exports.isValidSignedPricesChain = isValidSignedPricesChain;
|
|
171
|
-
const isValidSignedPricesOrderType = (orderType) => {
|
|
172
|
-
return (!isNaN(orderType) &&
|
|
173
|
-
[
|
|
174
|
-
trade_1.PendingOrderType.LIMIT_OPEN,
|
|
175
|
-
trade_1.PendingOrderType.STOP_OPEN,
|
|
176
|
-
trade_1.PendingOrderType.TP_CLOSE,
|
|
177
|
-
trade_1.PendingOrderType.SL_CLOSE,
|
|
178
|
-
trade_1.PendingOrderType.LIQ_CLOSE,
|
|
179
|
-
].includes(orderType));
|
|
180
|
-
};
|
|
181
|
-
exports.isValidSignedPricesOrderType = isValidSignedPricesOrderType;
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @dev Depth bands conversion utilities
|
|
3
|
-
*/
|
|
4
|
-
import { type DepthBandsInput } from "./types";
|
|
5
|
-
/**
|
|
6
|
-
* Convert raw depth data from exchanges to depth bands input format
|
|
7
|
-
* @param totalDepthUsd Total depth in USD (already in 6 decimal precision)
|
|
8
|
-
* @param depthLevels Array of depth levels with cumulative USD amounts
|
|
9
|
-
* @returns Formatted depth bands input
|
|
10
|
-
*/
|
|
11
|
-
export declare function formatDepthBandsInput(totalDepthUsd: number, depthLevels: Array<{
|
|
12
|
-
cumulativeUsd: number;
|
|
13
|
-
}>): DepthBandsInput;
|
|
14
|
-
/**
|
|
15
|
-
* Convert percentage-based depth configuration to cumulative USD values
|
|
16
|
-
* @param totalDepthUsd Total depth in USD
|
|
17
|
-
* @param percentages Array of 30 cumulative percentages (0-100)
|
|
18
|
-
* @returns Depth bands input with cumulative USD values
|
|
19
|
-
*/
|
|
20
|
-
export declare function percentagesToDepthBands(totalDepthUsd: number, percentages: number[]): DepthBandsInput;
|
|
21
|
-
/**
|
|
22
|
-
* Convert basis points array to percentage array
|
|
23
|
-
* @param bps Array of values in basis points
|
|
24
|
-
* @returns Array of percentages (0-100)
|
|
25
|
-
*/
|
|
26
|
-
export declare function bpsToPercentages(bps: number[]): number[];
|
|
27
|
-
/**
|
|
28
|
-
* Convert percentage array to basis points array
|
|
29
|
-
* @param percentages Array of percentages (0-100)
|
|
30
|
-
* @returns Array of values in basis points
|
|
31
|
-
*/
|
|
32
|
-
export declare function percentagesToBps(percentages: number[]): number[];
|
|
33
|
-
/**
|
|
34
|
-
* Convert parts per million (ppm) to percentage
|
|
35
|
-
* @param ppm Value in parts per million
|
|
36
|
-
* @returns Percentage value
|
|
37
|
-
*/
|
|
38
|
-
export declare function ppmToPercentage(ppm: number): number;
|
|
39
|
-
/**
|
|
40
|
-
* Convert percentage to parts per million (ppm)
|
|
41
|
-
* @param percentage Percentage value
|
|
42
|
-
* @returns Value in parts per million
|
|
43
|
-
*/
|
|
44
|
-
export declare function percentageToPpm(percentage: number): number;
|
|
45
|
-
/**
|
|
46
|
-
* Create a linear depth bands configuration
|
|
47
|
-
* @param totalDepthUsd Total depth in USD
|
|
48
|
-
* @returns Linear depth bands from 0% to 100%
|
|
49
|
-
*/
|
|
50
|
-
export declare function createLinearDepthBands(totalDepthUsd: number): DepthBandsInput;
|
|
51
|
-
/**
|
|
52
|
-
* Create an exponential depth bands configuration
|
|
53
|
-
* @param totalDepthUsd Total depth in USD
|
|
54
|
-
* @param factor Exponential factor (higher = more concentrated near 0)
|
|
55
|
-
* @returns Exponential depth bands configuration
|
|
56
|
-
*/
|
|
57
|
-
export declare function createExponentialDepthBands(totalDepthUsd: number, factor?: number): DepthBandsInput;
|
|
58
|
-
/**
|
|
59
|
-
* Create a concentrated depth bands configuration
|
|
60
|
-
* @param totalDepthUsd Total depth in USD
|
|
61
|
-
* @param concentrationPoint Percentage (0-100) where to concentrate liquidity
|
|
62
|
-
* @param concentrationStrength How much to concentrate (0-1, higher = more concentrated)
|
|
63
|
-
* @returns Concentrated depth bands configuration
|
|
64
|
-
*/
|
|
65
|
-
export declare function createConcentratedDepthBands(totalDepthUsd: number, concentrationPoint?: number, concentrationStrength?: number): DepthBandsInput;
|
|
@@ -1,155 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @dev Depth bands conversion utilities
|
|
4
|
-
*/
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.createConcentratedDepthBands = exports.createExponentialDepthBands = exports.createLinearDepthBands = exports.percentageToPpm = exports.ppmToPercentage = exports.percentagesToBps = exports.bpsToPercentages = exports.percentagesToDepthBands = exports.formatDepthBandsInput = void 0;
|
|
7
|
-
const types_1 = require("./types");
|
|
8
|
-
/**
|
|
9
|
-
* Convert raw depth data from exchanges to depth bands input format
|
|
10
|
-
* @param totalDepthUsd Total depth in USD (already in 6 decimal precision)
|
|
11
|
-
* @param depthLevels Array of depth levels with cumulative USD amounts
|
|
12
|
-
* @returns Formatted depth bands input
|
|
13
|
-
*/
|
|
14
|
-
function formatDepthBandsInput(totalDepthUsd, depthLevels) {
|
|
15
|
-
// Ensure we have exactly 30 levels
|
|
16
|
-
if (depthLevels.length !== 30) {
|
|
17
|
-
throw new Error(`Expected 30 depth levels, got ${depthLevels.length}`);
|
|
18
|
-
}
|
|
19
|
-
const cumulativeDepthUsd = depthLevels.map((level) => level.cumulativeUsd);
|
|
20
|
-
return {
|
|
21
|
-
totalDepthUsd,
|
|
22
|
-
cumulativeDepthUsd,
|
|
23
|
-
};
|
|
24
|
-
}
|
|
25
|
-
exports.formatDepthBandsInput = formatDepthBandsInput;
|
|
26
|
-
/**
|
|
27
|
-
* Convert percentage-based depth configuration to cumulative USD values
|
|
28
|
-
* @param totalDepthUsd Total depth in USD
|
|
29
|
-
* @param percentages Array of 30 cumulative percentages (0-100)
|
|
30
|
-
* @returns Depth bands input with cumulative USD values
|
|
31
|
-
*/
|
|
32
|
-
function percentagesToDepthBands(totalDepthUsd, percentages) {
|
|
33
|
-
if (percentages.length !== 30) {
|
|
34
|
-
throw new Error(`Expected 30 percentages, got ${percentages.length}`);
|
|
35
|
-
}
|
|
36
|
-
const cumulativeDepthUsd = percentages.map((percentage) => {
|
|
37
|
-
if (percentage < 0 || percentage > 100) {
|
|
38
|
-
throw new Error(`Invalid percentage: ${percentage}. Must be between 0 and 100`);
|
|
39
|
-
}
|
|
40
|
-
return (percentage * totalDepthUsd) / 100;
|
|
41
|
-
});
|
|
42
|
-
return {
|
|
43
|
-
totalDepthUsd,
|
|
44
|
-
cumulativeDepthUsd,
|
|
45
|
-
};
|
|
46
|
-
}
|
|
47
|
-
exports.percentagesToDepthBands = percentagesToDepthBands;
|
|
48
|
-
/**
|
|
49
|
-
* Convert basis points array to percentage array
|
|
50
|
-
* @param bps Array of values in basis points
|
|
51
|
-
* @returns Array of percentages (0-100)
|
|
52
|
-
*/
|
|
53
|
-
function bpsToPercentages(bps) {
|
|
54
|
-
return bps.map((value) => (value * 100) / types_1.HUNDRED_P_BPS);
|
|
55
|
-
}
|
|
56
|
-
exports.bpsToPercentages = bpsToPercentages;
|
|
57
|
-
/**
|
|
58
|
-
* Convert percentage array to basis points array
|
|
59
|
-
* @param percentages Array of percentages (0-100)
|
|
60
|
-
* @returns Array of values in basis points
|
|
61
|
-
*/
|
|
62
|
-
function percentagesToBps(percentages) {
|
|
63
|
-
return percentages.map((percentage) => {
|
|
64
|
-
const bps = Math.round((percentage * types_1.HUNDRED_P_BPS) / 100);
|
|
65
|
-
if (bps > types_1.HUNDRED_P_BPS) {
|
|
66
|
-
throw new Error(`Percentage ${percentage}% converts to ${bps} bps, which exceeds maximum ${types_1.HUNDRED_P_BPS} bps`);
|
|
67
|
-
}
|
|
68
|
-
return bps;
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
exports.percentagesToBps = percentagesToBps;
|
|
72
|
-
/**
|
|
73
|
-
* Convert parts per million (ppm) to percentage
|
|
74
|
-
* @param ppm Value in parts per million
|
|
75
|
-
* @returns Percentage value
|
|
76
|
-
*/
|
|
77
|
-
function ppmToPercentage(ppm) {
|
|
78
|
-
return ppm / 10000;
|
|
79
|
-
}
|
|
80
|
-
exports.ppmToPercentage = ppmToPercentage;
|
|
81
|
-
/**
|
|
82
|
-
* Convert percentage to parts per million (ppm)
|
|
83
|
-
* @param percentage Percentage value
|
|
84
|
-
* @returns Value in parts per million
|
|
85
|
-
*/
|
|
86
|
-
function percentageToPpm(percentage) {
|
|
87
|
-
return Math.round(percentage * 10000);
|
|
88
|
-
}
|
|
89
|
-
exports.percentageToPpm = percentageToPpm;
|
|
90
|
-
/**
|
|
91
|
-
* Create a linear depth bands configuration
|
|
92
|
-
* @param totalDepthUsd Total depth in USD
|
|
93
|
-
* @returns Linear depth bands from 0% to 100%
|
|
94
|
-
*/
|
|
95
|
-
function createLinearDepthBands(totalDepthUsd) {
|
|
96
|
-
const cumulativeDepthUsd = [];
|
|
97
|
-
for (let i = 0; i < 30; i++) {
|
|
98
|
-
// Linear progression from 0 to 100%
|
|
99
|
-
const percentage = ((i + 1) / 30) * 100;
|
|
100
|
-
cumulativeDepthUsd.push((percentage * totalDepthUsd) / 100);
|
|
101
|
-
}
|
|
102
|
-
return {
|
|
103
|
-
totalDepthUsd,
|
|
104
|
-
cumulativeDepthUsd,
|
|
105
|
-
};
|
|
106
|
-
}
|
|
107
|
-
exports.createLinearDepthBands = createLinearDepthBands;
|
|
108
|
-
/**
|
|
109
|
-
* Create an exponential depth bands configuration
|
|
110
|
-
* @param totalDepthUsd Total depth in USD
|
|
111
|
-
* @param factor Exponential factor (higher = more concentrated near 0)
|
|
112
|
-
* @returns Exponential depth bands configuration
|
|
113
|
-
*/
|
|
114
|
-
function createExponentialDepthBands(totalDepthUsd, factor = 2) {
|
|
115
|
-
const cumulativeDepthUsd = [];
|
|
116
|
-
for (let i = 0; i < 30; i++) {
|
|
117
|
-
// Exponential curve: y = (e^(factor * x) - 1) / (e^factor - 1)
|
|
118
|
-
const x = (i + 1) / 30;
|
|
119
|
-
const percentage = ((Math.exp(factor * x) - 1) / (Math.exp(factor) - 1)) * 100;
|
|
120
|
-
cumulativeDepthUsd.push((percentage * totalDepthUsd) / 100);
|
|
121
|
-
}
|
|
122
|
-
return {
|
|
123
|
-
totalDepthUsd,
|
|
124
|
-
cumulativeDepthUsd,
|
|
125
|
-
};
|
|
126
|
-
}
|
|
127
|
-
exports.createExponentialDepthBands = createExponentialDepthBands;
|
|
128
|
-
/**
|
|
129
|
-
* Create a concentrated depth bands configuration
|
|
130
|
-
* @param totalDepthUsd Total depth in USD
|
|
131
|
-
* @param concentrationPoint Percentage (0-100) where to concentrate liquidity
|
|
132
|
-
* @param concentrationStrength How much to concentrate (0-1, higher = more concentrated)
|
|
133
|
-
* @returns Concentrated depth bands configuration
|
|
134
|
-
*/
|
|
135
|
-
function createConcentratedDepthBands(totalDepthUsd, concentrationPoint = 20, concentrationStrength = 0.7) {
|
|
136
|
-
const cumulativeDepthUsd = [];
|
|
137
|
-
for (let i = 0; i < 30; i++) {
|
|
138
|
-
const x = (i + 1) / 30;
|
|
139
|
-
const targetX = concentrationPoint / 100;
|
|
140
|
-
// Sigmoid-like concentration around the target point
|
|
141
|
-
const steepness = 10 * concentrationStrength;
|
|
142
|
-
const sigmoid = 1 / (1 + Math.exp(-steepness * (x - targetX)));
|
|
143
|
-
// Blend between linear and concentrated
|
|
144
|
-
const linear = x;
|
|
145
|
-
const percentage = (linear * (1 - concentrationStrength) + sigmoid * concentrationStrength) * 100;
|
|
146
|
-
cumulativeDepthUsd.push(Math.min((percentage * totalDepthUsd) / 100, totalDepthUsd));
|
|
147
|
-
}
|
|
148
|
-
// Ensure last value is exactly totalDepthUsd
|
|
149
|
-
cumulativeDepthUsd[29] = totalDepthUsd;
|
|
150
|
-
return {
|
|
151
|
-
totalDepthUsd,
|
|
152
|
-
cumulativeDepthUsd,
|
|
153
|
-
};
|
|
154
|
-
}
|
|
155
|
-
exports.createConcentratedDepthBands = createConcentratedDepthBands;
|
|
@@ -1,32 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @dev Depth bands decoding functions
|
|
3
|
-
*/
|
|
4
|
-
import { type DecodedDepthBands, type DecodedDepthBandsMapping, type EncodedDepthBands, type PairDepthBands } from "./types";
|
|
5
|
-
/**
|
|
6
|
-
* Decode depth bands from encoded slots
|
|
7
|
-
* @param encoded Encoded depth bands
|
|
8
|
-
* @returns Decoded depth bands with totalDepthUsd and band percentages
|
|
9
|
-
*/
|
|
10
|
-
export declare function decodeDepthBands(encoded: EncodedDepthBands): DecodedDepthBands;
|
|
11
|
-
/**
|
|
12
|
-
* Decode pair depth bands (both directions)
|
|
13
|
-
* @param pairBands Encoded pair depth bands
|
|
14
|
-
* @returns Decoded depth bands for above and below
|
|
15
|
-
*/
|
|
16
|
-
export declare function decodePairDepthBands(pairBands: PairDepthBands): {
|
|
17
|
-
above: DecodedDepthBands;
|
|
18
|
-
below: DecodedDepthBands;
|
|
19
|
-
};
|
|
20
|
-
/**
|
|
21
|
-
* Decode depth bands mapping (global offsets)
|
|
22
|
-
* @param slot1 First slot containing bands 0-13
|
|
23
|
-
* @param slot2 Second slot containing bands 14-29
|
|
24
|
-
* @returns Decoded mapping with band offset values in ppm
|
|
25
|
-
*/
|
|
26
|
-
export declare function decodeDepthBandsMapping(slot1: bigint, slot2: bigint): DecodedDepthBandsMapping;
|
|
27
|
-
/**
|
|
28
|
-
* Convert decoded band percentages back to cumulative depth USD
|
|
29
|
-
* @param decoded Decoded depth bands
|
|
30
|
-
* @returns Array of cumulative depth values in USD
|
|
31
|
-
*/
|
|
32
|
-
export declare function bandsToCumulativeDepth(decoded: DecodedDepthBands): number[];
|
|
@@ -1,109 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @dev Depth bands decoding functions
|
|
4
|
-
*/
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.bandsToCumulativeDepth = exports.decodeDepthBandsMapping = exports.decodePairDepthBands = exports.decodeDepthBands = void 0;
|
|
7
|
-
const types_1 = require("./types");
|
|
8
|
-
/**
|
|
9
|
-
* Get a specific band value from encoded slots
|
|
10
|
-
* @param slot1 First slot containing totalDepthUsd and bands 0-13
|
|
11
|
-
* @param slot2 Second slot containing bands 14-29
|
|
12
|
-
* @param bandIndex Index of the band to retrieve (0-29)
|
|
13
|
-
* @returns Band value
|
|
14
|
-
*/
|
|
15
|
-
function getBandValue(slot1, slot2, bandIndex) {
|
|
16
|
-
if (bandIndex < 0 || bandIndex >= types_1.DEPTH_BANDS_COUNT) {
|
|
17
|
-
throw new Error(`Invalid band index: must be 0-${types_1.DEPTH_BANDS_COUNT - 1}, got ${bandIndex}`);
|
|
18
|
-
}
|
|
19
|
-
if (bandIndex < types_1.DEPTH_BANDS_PER_SLOT1) {
|
|
20
|
-
// Get from slot1 (skip the first 32 bits which contain totalDepthUsd)
|
|
21
|
-
const shift = 32 + bandIndex * 16;
|
|
22
|
-
return Number((slot1 >> BigInt(shift)) & BigInt(0xffff));
|
|
23
|
-
}
|
|
24
|
-
else {
|
|
25
|
-
// Get from slot2
|
|
26
|
-
const shift = (bandIndex - types_1.DEPTH_BANDS_PER_SLOT1) * 16;
|
|
27
|
-
return Number((slot2 >> BigInt(shift)) & BigInt(0xffff));
|
|
28
|
-
}
|
|
29
|
-
}
|
|
30
|
-
/**
|
|
31
|
-
* Get total depth USD from encoded slot1
|
|
32
|
-
* @param slot1 First slot containing totalDepthUsd as uint32
|
|
33
|
-
* @returns Total depth in USD (6 decimal precision)
|
|
34
|
-
*/
|
|
35
|
-
function getTotalDepthUsd(slot1) {
|
|
36
|
-
return Number(slot1 & BigInt(0xffffffff));
|
|
37
|
-
}
|
|
38
|
-
/**
|
|
39
|
-
* Decode depth bands from encoded slots
|
|
40
|
-
* @param encoded Encoded depth bands
|
|
41
|
-
* @returns Decoded depth bands with totalDepthUsd and band percentages
|
|
42
|
-
*/
|
|
43
|
-
function decodeDepthBands(encoded) {
|
|
44
|
-
const { slot1, slot2 } = encoded;
|
|
45
|
-
const totalDepthUsd = getTotalDepthUsd(slot1);
|
|
46
|
-
const bands = [];
|
|
47
|
-
for (let i = 0; i < types_1.DEPTH_BANDS_COUNT; i++) {
|
|
48
|
-
bands.push(getBandValue(slot1, slot2, i));
|
|
49
|
-
}
|
|
50
|
-
return { totalDepthUsd, bands };
|
|
51
|
-
}
|
|
52
|
-
exports.decodeDepthBands = decodeDepthBands;
|
|
53
|
-
/**
|
|
54
|
-
* Decode pair depth bands (both directions)
|
|
55
|
-
* @param pairBands Encoded pair depth bands
|
|
56
|
-
* @returns Decoded depth bands for above and below
|
|
57
|
-
*/
|
|
58
|
-
function decodePairDepthBands(pairBands) {
|
|
59
|
-
const above = decodeDepthBands({
|
|
60
|
-
slot1: pairBands.aboveSlot1,
|
|
61
|
-
slot2: pairBands.aboveSlot2,
|
|
62
|
-
});
|
|
63
|
-
const below = decodeDepthBands({
|
|
64
|
-
slot1: pairBands.belowSlot1,
|
|
65
|
-
slot2: pairBands.belowSlot2,
|
|
66
|
-
});
|
|
67
|
-
return { above, below };
|
|
68
|
-
}
|
|
69
|
-
exports.decodePairDepthBands = decodePairDepthBands;
|
|
70
|
-
/**
|
|
71
|
-
* Decode depth bands mapping (global offsets)
|
|
72
|
-
* @param slot1 First slot containing bands 0-13
|
|
73
|
-
* @param slot2 Second slot containing bands 14-29
|
|
74
|
-
* @returns Decoded mapping with band offset values in ppm
|
|
75
|
-
*/
|
|
76
|
-
function decodeDepthBandsMapping(slot1, slot2) {
|
|
77
|
-
const bands = [];
|
|
78
|
-
// Note: For mapping, there's no totalDepthUsd, all bits are used for bands
|
|
79
|
-
for (let i = 0; i < types_1.DEPTH_BANDS_COUNT; i++) {
|
|
80
|
-
if (i < types_1.DEPTH_BANDS_PER_SLOT1) {
|
|
81
|
-
// Get from slot1
|
|
82
|
-
const shift = i * 16;
|
|
83
|
-
bands.push(Number((slot1 >> BigInt(shift)) & BigInt(0xffff)));
|
|
84
|
-
}
|
|
85
|
-
else {
|
|
86
|
-
// Get from slot2
|
|
87
|
-
const shift = (i - types_1.DEPTH_BANDS_PER_SLOT1) * 16;
|
|
88
|
-
bands.push(Number((slot2 >> BigInt(shift)) & BigInt(0xffff)));
|
|
89
|
-
}
|
|
90
|
-
}
|
|
91
|
-
return { bands };
|
|
92
|
-
}
|
|
93
|
-
exports.decodeDepthBandsMapping = decodeDepthBandsMapping;
|
|
94
|
-
/**
|
|
95
|
-
* Convert decoded band percentages back to cumulative depth USD
|
|
96
|
-
* @param decoded Decoded depth bands
|
|
97
|
-
* @returns Array of cumulative depth values in USD
|
|
98
|
-
*/
|
|
99
|
-
function bandsToCumulativeDepth(decoded) {
|
|
100
|
-
const { totalDepthUsd, bands } = decoded;
|
|
101
|
-
const cumulativeDepthUsd = [];
|
|
102
|
-
for (const bandBps of bands) {
|
|
103
|
-
// Convert from basis points to USD
|
|
104
|
-
const depthUsd = (bandBps * totalDepthUsd) / 10000;
|
|
105
|
-
cumulativeDepthUsd.push(depthUsd);
|
|
106
|
-
}
|
|
107
|
-
return cumulativeDepthUsd;
|
|
108
|
-
}
|
|
109
|
-
exports.bandsToCumulativeDepth = bandsToCumulativeDepth;
|
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @dev Depth bands encoding functions
|
|
3
|
-
*/
|
|
4
|
-
import { type DepthBandsInput, type EncodedDepthBands } from "./types";
|
|
5
|
-
/**
|
|
6
|
-
* Encode depth bands data into two uint256 slots
|
|
7
|
-
* @param input Depth bands input data
|
|
8
|
-
* @returns Encoded depth bands with two slots
|
|
9
|
-
*/
|
|
10
|
-
export declare function encodeDepthBands(input: DepthBandsInput): EncodedDepthBands;
|
|
11
|
-
/**
|
|
12
|
-
* Encode depth bands mapping (global offsets for all pairs)
|
|
13
|
-
* @param bands Array of 30 band offset values in parts per million (ppm)
|
|
14
|
-
* @returns Encoded mapping with two slots
|
|
15
|
-
*/
|
|
16
|
-
export declare function encodeDepthBandsMapping(bands: number[]): {
|
|
17
|
-
slot1: bigint;
|
|
18
|
-
slot2: bigint;
|
|
19
|
-
};
|
|
@@ -1,105 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* @dev Depth bands encoding functions
|
|
4
|
-
*/
|
|
5
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.encodeDepthBandsMapping = exports.encodeDepthBands = void 0;
|
|
7
|
-
const types_1 = require("./types");
|
|
8
|
-
/**
|
|
9
|
-
* Encode depth bands data into two uint256 slots
|
|
10
|
-
* @param input Depth bands input data
|
|
11
|
-
* @returns Encoded depth bands with two slots
|
|
12
|
-
*/
|
|
13
|
-
function encodeDepthBands(input) {
|
|
14
|
-
const { totalDepthUsd, cumulativeDepthUsd } = input;
|
|
15
|
-
// Validate input
|
|
16
|
-
if (cumulativeDepthUsd.length !== types_1.DEPTH_BANDS_COUNT) {
|
|
17
|
-
throw new Error(`Invalid cumulativeDepthUsd length: expected ${types_1.DEPTH_BANDS_COUNT}, got ${cumulativeDepthUsd.length}`);
|
|
18
|
-
}
|
|
19
|
-
// Validate totalDepthUsd range (must fit in uint32)
|
|
20
|
-
const maxUint32 = Math.pow(2, 32) - 1;
|
|
21
|
-
if (totalDepthUsd < 0 || totalDepthUsd > maxUint32) {
|
|
22
|
-
throw new Error(`Invalid totalDepthUsd: must be between 0 and ${maxUint32}, got ${totalDepthUsd}`);
|
|
23
|
-
}
|
|
24
|
-
// Convert cumulative depths to band percentages
|
|
25
|
-
const bandPercentagesBps = [];
|
|
26
|
-
let prevCumulativeDepth = 0;
|
|
27
|
-
for (let i = 0; i < types_1.DEPTH_BANDS_COUNT; i++) {
|
|
28
|
-
const currentCumulativeDepth = cumulativeDepthUsd[i];
|
|
29
|
-
// Validate cumulative depth is non-decreasing
|
|
30
|
-
if (currentCumulativeDepth < prevCumulativeDepth) {
|
|
31
|
-
throw new Error(`Invalid cumulative depth at band ${i}: ${currentCumulativeDepth} is less than previous ${prevCumulativeDepth}`);
|
|
32
|
-
}
|
|
33
|
-
// Calculate percentage of total depth (in basis points)
|
|
34
|
-
const percentageBps = totalDepthUsd > 0
|
|
35
|
-
? Math.round((currentCumulativeDepth * types_1.HUNDRED_P_BPS) / totalDepthUsd)
|
|
36
|
-
: 0;
|
|
37
|
-
// Validate percentage doesn't exceed 100%
|
|
38
|
-
if (percentageBps > types_1.HUNDRED_P_BPS) {
|
|
39
|
-
throw new Error(`Band ${i} percentage exceeds 100%: ${percentageBps} bps`);
|
|
40
|
-
}
|
|
41
|
-
// Validate percentage fits in uint16
|
|
42
|
-
const maxUint16 = Math.pow(2, 16) - 1;
|
|
43
|
-
if (percentageBps > maxUint16) {
|
|
44
|
-
throw new Error(`Band ${i} percentage too large for uint16: ${percentageBps}`);
|
|
45
|
-
}
|
|
46
|
-
bandPercentagesBps.push(percentageBps);
|
|
47
|
-
prevCumulativeDepth = currentCumulativeDepth;
|
|
48
|
-
}
|
|
49
|
-
// Validate that final band reaches 100% (or close to it due to rounding)
|
|
50
|
-
const finalPercentage = bandPercentagesBps[types_1.DEPTH_BANDS_COUNT - 1];
|
|
51
|
-
if (totalDepthUsd > 0 && Math.abs(finalPercentage - types_1.HUNDRED_P_BPS) > 10) {
|
|
52
|
-
throw new Error(`Final band does not reach 100%: ${finalPercentage} bps`);
|
|
53
|
-
}
|
|
54
|
-
// Pack into slot1: totalDepthUsd (32 bits) + bands 0-13 (14 * 16 bits)
|
|
55
|
-
let slot1 = BigInt(totalDepthUsd);
|
|
56
|
-
for (let i = 0; i < types_1.DEPTH_BANDS_PER_SLOT1; i++) {
|
|
57
|
-
const shift = 32 + i * 16;
|
|
58
|
-
slot1 |= BigInt(bandPercentagesBps[i]) << BigInt(shift);
|
|
59
|
-
}
|
|
60
|
-
// Pack into slot2: bands 14-29 (16 * 16 bits)
|
|
61
|
-
let slot2 = BigInt(0);
|
|
62
|
-
for (let i = types_1.DEPTH_BANDS_PER_SLOT1; i < types_1.DEPTH_BANDS_COUNT; i++) {
|
|
63
|
-
const shift = (i - types_1.DEPTH_BANDS_PER_SLOT1) * 16;
|
|
64
|
-
slot2 |= BigInt(bandPercentagesBps[i]) << BigInt(shift);
|
|
65
|
-
}
|
|
66
|
-
return { slot1, slot2 };
|
|
67
|
-
}
|
|
68
|
-
exports.encodeDepthBands = encodeDepthBands;
|
|
69
|
-
/**
|
|
70
|
-
* Encode depth bands mapping (global offsets for all pairs)
|
|
71
|
-
* @param bands Array of 30 band offset values in parts per million (ppm)
|
|
72
|
-
* @returns Encoded mapping with two slots
|
|
73
|
-
*/
|
|
74
|
-
function encodeDepthBandsMapping(bands) {
|
|
75
|
-
// Validate input
|
|
76
|
-
if (bands.length !== types_1.DEPTH_BANDS_COUNT) {
|
|
77
|
-
throw new Error(`Invalid bands length: expected ${types_1.DEPTH_BANDS_COUNT}, got ${bands.length}`);
|
|
78
|
-
}
|
|
79
|
-
// Validate each band value and monotonic increase
|
|
80
|
-
let prevBand = 0;
|
|
81
|
-
const maxUint16 = Math.pow(2, 16) - 1;
|
|
82
|
-
for (let i = 0; i < bands.length; i++) {
|
|
83
|
-
const band = bands[i];
|
|
84
|
-
if (band < 0 || band > maxUint16) {
|
|
85
|
-
throw new Error(`Band ${i} value out of range: must be 0-${maxUint16}, got ${band}`);
|
|
86
|
-
}
|
|
87
|
-
if (band < prevBand) {
|
|
88
|
-
throw new Error(`Band ${i} value ${band} is less than previous band ${prevBand}`);
|
|
89
|
-
}
|
|
90
|
-
prevBand = band;
|
|
91
|
-
}
|
|
92
|
-
// Pack into slot1: bands 0-13 (14 * 16 bits)
|
|
93
|
-
let slot1 = BigInt(0);
|
|
94
|
-
for (let i = 0; i < types_1.DEPTH_BANDS_PER_SLOT1; i++) {
|
|
95
|
-
slot1 |= BigInt(bands[i]) << BigInt(i * 16);
|
|
96
|
-
}
|
|
97
|
-
// Pack into slot2: bands 14-29 (16 * 16 bits)
|
|
98
|
-
let slot2 = BigInt(0);
|
|
99
|
-
for (let i = types_1.DEPTH_BANDS_PER_SLOT1; i < types_1.DEPTH_BANDS_COUNT; i++) {
|
|
100
|
-
const shift = (i - types_1.DEPTH_BANDS_PER_SLOT1) * 16;
|
|
101
|
-
slot2 |= BigInt(bands[i]) << BigInt(shift);
|
|
102
|
-
}
|
|
103
|
-
return { slot1, slot2 };
|
|
104
|
-
}
|
|
105
|
-
exports.encodeDepthBandsMapping = encodeDepthBandsMapping;
|