@zofai/zo-sdk 0.1.93 → 0.1.94
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/dist/consts/deployments-slp-mainnet.json +1 -1
- package/dist/consts/deployments-usdz-mainnet.json +1 -1
- package/dist/consts/deployments-zlp-mainnet.json +1 -1
- package/dist/implementations/SLPDataAPI.cjs +212 -47
- package/dist/implementations/SLPDataAPI.cjs.map +1 -1
- package/dist/implementations/SLPDataAPI.d.cts +8 -1
- package/dist/implementations/SLPDataAPI.d.cts.map +1 -1
- package/dist/implementations/SLPDataAPI.d.mts +8 -1
- package/dist/implementations/SLPDataAPI.d.mts.map +1 -1
- package/dist/implementations/SLPDataAPI.mjs +212 -47
- package/dist/implementations/SLPDataAPI.mjs.map +1 -1
- package/dist/implementations/USDZDataAPI.cjs +197 -44
- package/dist/implementations/USDZDataAPI.cjs.map +1 -1
- package/dist/implementations/USDZDataAPI.d.cts +8 -1
- package/dist/implementations/USDZDataAPI.d.cts.map +1 -1
- package/dist/implementations/USDZDataAPI.d.mts +8 -1
- package/dist/implementations/USDZDataAPI.d.mts.map +1 -1
- package/dist/implementations/USDZDataAPI.mjs +197 -44
- package/dist/implementations/USDZDataAPI.mjs.map +1 -1
- package/dist/implementations/ZLPDataAPI.cjs +200 -46
- package/dist/implementations/ZLPDataAPI.cjs.map +1 -1
- package/dist/implementations/ZLPDataAPI.d.cts +8 -1
- package/dist/implementations/ZLPDataAPI.d.cts.map +1 -1
- package/dist/implementations/ZLPDataAPI.d.mts +8 -1
- package/dist/implementations/ZLPDataAPI.d.mts.map +1 -1
- package/dist/implementations/ZLPDataAPI.mjs +200 -46
- package/dist/implementations/ZLPDataAPI.mjs.map +1 -1
- package/dist/interfaces/base.d.cts +22 -0
- package/dist/interfaces/base.d.cts.map +1 -1
- package/dist/interfaces/base.d.mts +22 -0
- package/dist/interfaces/base.d.mts.map +1 -1
- package/dist/interfaces/slp.d.cts +6 -1
- package/dist/interfaces/slp.d.cts.map +1 -1
- package/dist/interfaces/slp.d.mts +6 -1
- package/dist/interfaces/slp.d.mts.map +1 -1
- package/dist/interfaces/usdz.d.cts +6 -1
- package/dist/interfaces/usdz.d.cts.map +1 -1
- package/dist/interfaces/usdz.d.mts +6 -1
- package/dist/interfaces/usdz.d.mts.map +1 -1
- package/dist/interfaces/zlp.d.cts +6 -1
- package/dist/interfaces/zlp.d.cts.map +1 -1
- package/dist/interfaces/zlp.d.mts +6 -1
- package/dist/interfaces/zlp.d.mts.map +1 -1
- package/package.json +1 -1
- package/src/consts/deployments-slp-mainnet.json +1 -1
- package/src/consts/deployments-usdz-mainnet.json +1 -1
- package/src/consts/deployments-zlp-mainnet.json +1 -1
- package/src/implementations/SLPDataAPI.ts +235 -19
- package/src/implementations/USDZDataAPI.ts +221 -16
- package/src/implementations/ZLPDataAPI.ts +222 -17
- package/src/interfaces/base.ts +26 -0
- package/src/interfaces/slp.ts +6 -0
- package/src/interfaces/usdz.ts +6 -0
- package/src/interfaces/zlp.ts +6 -0
|
@@ -3,6 +3,12 @@
|
|
|
3
3
|
* ZLP DataAPI implementation
|
|
4
4
|
* Implements ZLP-specific data access methods
|
|
5
5
|
*/
|
|
6
|
+
var __classPrivateFieldGet = (this && this.__classPrivateFieldGet) || function (receiver, state, kind, f) {
|
|
7
|
+
if (kind === "a" && !f) throw new TypeError("Private accessor was defined without a getter");
|
|
8
|
+
if (typeof state === "function" ? receiver !== state || !f : !state.has(receiver)) throw new TypeError("Cannot read private member from an object whose class did not declare it");
|
|
9
|
+
return kind === "m" ? f : kind === "a" ? f.call(receiver) : f ? f.value : state.get(receiver);
|
|
10
|
+
};
|
|
11
|
+
var _ZLPDataAPI_instances, _a, _ZLPDataAPI_getTotalVaultsValueUsd, _ZLPDataAPI_getSwapImpactConfig, _ZLPDataAPI_getEmaVolatilityFeeConfig, _ZLPDataAPI_getMarketDynamicFieldObjectByKeySuffix, _ZLPDataAPI_parseSwapImpactConfig, _ZLPDataAPI_parseEmaVolatilityFeeConfig, _ZLPDataAPI_computeSwapImpactFeeValue, _ZLPDataAPI_computeEmaVolatilityFeeValue, _ZLPDataAPI_maxEmaDivergenceRate, _ZLPDataAPI_emaDivergenceRate;
|
|
6
12
|
import { SUI_CLOCK_OBJECT_ID } from "@mysten/sui/utils";
|
|
7
13
|
import { BaseDataAPI } from "../abstract/index.mjs";
|
|
8
14
|
import { LPToken, SECONDS_PER_EIGHT_HOUR, ZLP_TOKEN_DECIMALS } from "../consts/index.mjs";
|
|
@@ -10,6 +16,7 @@ import { joinSymbol, parseSymbolKey, parseValue, suiSymbolToSymbol } from "../ut
|
|
|
10
16
|
export class ZLPDataAPI extends BaseDataAPI {
|
|
11
17
|
constructor(network, provider, apiEndpoint, connectionURL) {
|
|
12
18
|
super(network, provider, apiEndpoint, connectionURL, LPToken.ZLP);
|
|
19
|
+
_ZLPDataAPI_instances.add(this);
|
|
13
20
|
}
|
|
14
21
|
async getStaked(owner) {
|
|
15
22
|
let rawCredentialsData = [];
|
|
@@ -43,7 +50,7 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
43
50
|
const credentials = rawCredentialsData
|
|
44
51
|
.filter((item) => item.data.type
|
|
45
52
|
=== `${this.sharedConfig.zoStaking.package}::pool::Credential<${this.consts.zoCore.package}::zlp::ZLP, 0x2::sui::SUI>`)
|
|
46
|
-
.map((item) =>
|
|
53
|
+
.map((item) => _a.parseCredential(item, pool));
|
|
47
54
|
return {
|
|
48
55
|
credentials,
|
|
49
56
|
amount: credentials.reduce((acc, cur) => acc + cur.amount, BigInt(0)),
|
|
@@ -58,7 +65,7 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
58
65
|
showContent: true,
|
|
59
66
|
},
|
|
60
67
|
});
|
|
61
|
-
return
|
|
68
|
+
return _a.parseStakePool(raw);
|
|
62
69
|
}
|
|
63
70
|
/**
|
|
64
71
|
* Creates vaults valuation for ZLP
|
|
@@ -140,7 +147,7 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
140
147
|
const vaultKeys = Object.keys(this.consts.zoCore.vaults);
|
|
141
148
|
const vaultData = await Promise.all(vaultKeys.map(async (vault) => {
|
|
142
149
|
const vaultInfo = await this.getVaultInfo(vault);
|
|
143
|
-
const reservingFeeDelta =
|
|
150
|
+
const reservingFeeDelta = _a.calculateVaultReservingFee(vaultInfo, vaultInfo.reservingFeeModel, Date.now() / 1000);
|
|
144
151
|
const totalVaultAmount = reservingFeeDelta + vaultInfo.liquidity + vaultInfo.reservedAmount;
|
|
145
152
|
const oraclePrice = (await this.getOraclePrice(vault)).getPriceUnchecked().getPriceAsNumberUnchecked();
|
|
146
153
|
const vaultValue = totalVaultAmount * oraclePrice / (10 ** this.consts.coins[vault].decimals);
|
|
@@ -152,10 +159,10 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
152
159
|
const [direction, tokenId] = parseSymbolKey(symbol);
|
|
153
160
|
const symbolInfo = await this.getSymbolInfo(tokenId, direction === 'long');
|
|
154
161
|
const price = (await this.getOraclePrice(tokenId)).getPriceUnchecked().getPriceAsNumberUnchecked();
|
|
155
|
-
const deltaSize =
|
|
162
|
+
const deltaSize = _a.calcDeltaSize(symbolInfo, price);
|
|
156
163
|
const oiState = await this.getSymbolOiFundingState(tokenId);
|
|
157
164
|
const pairedInfo = await this.getSymbolInfo(tokenId, direction !== 'long');
|
|
158
|
-
const fundingFeeDelta =
|
|
165
|
+
const fundingFeeDelta = _a.calculateSymbolFundingFee(symbolInfo, symbolInfo.fundingFeeModel, price, marketInfo.lpSupplyWithDecimals, Date.now() / 1000, oiState && oiState.enabled ? oiState : undefined, pairedInfo.openingSize);
|
|
159
166
|
return fundingFeeDelta + deltaSize;
|
|
160
167
|
});
|
|
161
168
|
const symbolValues = await Promise.all(symbolPromises);
|
|
@@ -184,7 +191,7 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
184
191
|
showContent: true,
|
|
185
192
|
},
|
|
186
193
|
});
|
|
187
|
-
return
|
|
194
|
+
return _a.parseMarketInfo(rawData);
|
|
188
195
|
}
|
|
189
196
|
/**
|
|
190
197
|
* Gets ZLP vault information
|
|
@@ -233,7 +240,7 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
233
240
|
showContent: true,
|
|
234
241
|
},
|
|
235
242
|
});
|
|
236
|
-
return
|
|
243
|
+
return _a.parsePositionConfig(rawData);
|
|
237
244
|
}
|
|
238
245
|
/**
|
|
239
246
|
* Gets ZLP symbol configuration
|
|
@@ -248,7 +255,7 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
248
255
|
value: { dummy_field: false },
|
|
249
256
|
},
|
|
250
257
|
});
|
|
251
|
-
return
|
|
258
|
+
return _a.parseSymbolConfig(rawData);
|
|
252
259
|
}
|
|
253
260
|
catch (e) {
|
|
254
261
|
// If the dynamic field doesn't exist, return null
|
|
@@ -269,7 +276,7 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
269
276
|
value: { dummy_field: false },
|
|
270
277
|
},
|
|
271
278
|
});
|
|
272
|
-
return
|
|
279
|
+
return _a.parseOiFundingState(rawData);
|
|
273
280
|
}
|
|
274
281
|
catch (e) {
|
|
275
282
|
// If the dynamic field doesn't exist, return null
|
|
@@ -291,7 +298,7 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
291
298
|
value: { dummy_field: false },
|
|
292
299
|
},
|
|
293
300
|
});
|
|
294
|
-
return
|
|
301
|
+
return _a.parsePriceImpactConfig(rawData);
|
|
295
302
|
}
|
|
296
303
|
catch (e) {
|
|
297
304
|
// If the dynamic field doesn't exist, return null (price impact not configured for this symbol)
|
|
@@ -299,6 +306,50 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
299
306
|
return null;
|
|
300
307
|
}
|
|
301
308
|
}
|
|
309
|
+
async calculateSwapFeeBreakdown(fromToken, toToken, fromAmount) {
|
|
310
|
+
const timestamp = Date.now() / 1000;
|
|
311
|
+
const fromDecimals = this.consts.coins[fromToken]?.decimals;
|
|
312
|
+
const toDecimals = this.consts.coins[toToken]?.decimals;
|
|
313
|
+
if (fromDecimals === undefined || toDecimals === undefined) {
|
|
314
|
+
throw new Error(`Unknown token decimals for swap: ${fromToken} -> ${toToken}`);
|
|
315
|
+
}
|
|
316
|
+
const fromFeed = await this.getOraclePrice(fromToken);
|
|
317
|
+
const toFeed = await this.getOraclePrice(toToken);
|
|
318
|
+
const fromPrice = fromFeed.getPriceUnchecked().getPriceAsNumberUnchecked();
|
|
319
|
+
const toPrice = toFeed.getPriceUnchecked().getPriceAsNumberUnchecked();
|
|
320
|
+
const swapValue = (fromAmount * fromPrice) / (10 ** fromDecimals);
|
|
321
|
+
const totalVaultsValue = await __classPrivateFieldGet(this, _ZLPDataAPI_instances, "m", _ZLPDataAPI_getTotalVaultsValueUsd).call(this, timestamp);
|
|
322
|
+
const rebaseFeeInRate = await this.rebaseFeeRate(fromToken, true, fromAmount);
|
|
323
|
+
const rebaseFeeInValue = swapValue * rebaseFeeInRate;
|
|
324
|
+
// Estimate out-amount by notional value (ignoring price impact / spread / fees).
|
|
325
|
+
const estimatedToAmount = toPrice !== 0
|
|
326
|
+
? (swapValue * (10 ** toDecimals)) / toPrice
|
|
327
|
+
: 0;
|
|
328
|
+
const rebaseFeeOutRate = await this.rebaseFeeRate(toToken, false, estimatedToAmount);
|
|
329
|
+
const rebaseFeeOutValue = swapValue * rebaseFeeOutRate;
|
|
330
|
+
const swapImpactCfg = await __classPrivateFieldGet(this, _ZLPDataAPI_instances, "m", _ZLPDataAPI_getSwapImpactConfig).call(this);
|
|
331
|
+
const swapImpactFeeValue = swapImpactCfg?.enabled
|
|
332
|
+
? __classPrivateFieldGet(_a, _a, "m", _ZLPDataAPI_computeSwapImpactFeeValue).call(_a, swapValue, totalVaultsValue, swapImpactCfg.impactMultiplier, swapImpactCfg.maxImpactRate)
|
|
333
|
+
: 0;
|
|
334
|
+
const emaCfg = await __classPrivateFieldGet(this, _ZLPDataAPI_instances, "m", _ZLPDataAPI_getEmaVolatilityFeeConfig).call(this);
|
|
335
|
+
const emaVolatilityFeeValue = emaCfg?.enabled
|
|
336
|
+
? __classPrivateFieldGet(_a, _a, "m", _ZLPDataAPI_computeEmaVolatilityFeeValue).call(_a, swapValue, __classPrivateFieldGet(_a, _a, "m", _ZLPDataAPI_maxEmaDivergenceRate).call(_a, fromFeed, toFeed), emaCfg.multiplier, emaCfg.maxFeeRate)
|
|
337
|
+
: 0;
|
|
338
|
+
const totalFeeValue = rebaseFeeInValue + rebaseFeeOutValue + swapImpactFeeValue + emaVolatilityFeeValue;
|
|
339
|
+
const totalFeeRate = swapValue !== 0 ? totalFeeValue / swapValue : 0;
|
|
340
|
+
return {
|
|
341
|
+
swapValue,
|
|
342
|
+
totalVaultsValue,
|
|
343
|
+
rebaseFeeInRate,
|
|
344
|
+
rebaseFeeOutRate,
|
|
345
|
+
rebaseFeeInValue,
|
|
346
|
+
rebaseFeeOutValue,
|
|
347
|
+
swapImpactFeeValue,
|
|
348
|
+
emaVolatilityFeeValue,
|
|
349
|
+
totalFeeValue,
|
|
350
|
+
totalFeeRate,
|
|
351
|
+
};
|
|
352
|
+
}
|
|
302
353
|
async getPositionCapInfoList(owner) {
|
|
303
354
|
const positionCapInfoList = [];
|
|
304
355
|
let cursor;
|
|
@@ -513,8 +564,8 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
513
564
|
showContent: true,
|
|
514
565
|
},
|
|
515
566
|
});
|
|
516
|
-
const model =
|
|
517
|
-
const exponent = await
|
|
567
|
+
const model = _a.parseRebaseFeeModel(rawData);
|
|
568
|
+
const exponent = await _a.getRebaseFeeExponent(this.provider, this.consts.zoCore.rebaseFeeModel, this.consts.zoCore.upgradedPackage);
|
|
518
569
|
return { ...model, exponent };
|
|
519
570
|
}
|
|
520
571
|
async fundingFeeRate(indexToken, long) {
|
|
@@ -528,9 +579,9 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
528
579
|
const lpSupplyAmount = (await this.getMarketInfo()).lpSupplyWithDecimals;
|
|
529
580
|
const model = symbol.fundingFeeModel;
|
|
530
581
|
const elapsed = SECONDS_PER_EIGHT_HOUR;
|
|
531
|
-
const deltaSize =
|
|
582
|
+
const deltaSize = _a.calcDeltaSize(symbol, price);
|
|
532
583
|
const pnlPerLp = (symbol.realisedPnl + symbol.unrealisedFundingFeeValue + deltaSize) / lpSupplyAmount;
|
|
533
|
-
return
|
|
584
|
+
return _a.calcFundingFeeRate(model, pnlPerLp, elapsed);
|
|
534
585
|
}
|
|
535
586
|
const longSymbol = await this.getSymbolInfo(indexToken, true);
|
|
536
587
|
const shortSymbol = await this.getSymbolInfo(indexToken, false);
|
|
@@ -540,7 +591,7 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
540
591
|
const elapsed = SECONDS_PER_EIGHT_HOUR;
|
|
541
592
|
const longSize = longSymbol.openingSize;
|
|
542
593
|
const shortSize = shortSymbol.openingSize;
|
|
543
|
-
const deltaRate =
|
|
594
|
+
const deltaRate = _a.calcOiFundingFeeRate(oiState.model, longSize, shortSize, elapsed, oiState.maxOiLong, oiState.maxOiShort);
|
|
544
595
|
return long ? deltaRate : -deltaRate;
|
|
545
596
|
}
|
|
546
597
|
async rebaseFeeRate(collateralToken, increase, amount) {
|
|
@@ -551,7 +602,7 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
551
602
|
const value = amount * (await this.getOraclePrice(collateralToken)).getPriceUnchecked().getPriceAsNumberUnchecked() / (10 ** this.consts.coins[collateralToken].decimals);
|
|
552
603
|
const vaultPromises = Object.keys(this.consts.zoCore.vaults).map(async (vault) => {
|
|
553
604
|
const vaultInfo = await this.getVaultInfo(vault);
|
|
554
|
-
const reservingFeeDelta =
|
|
605
|
+
const reservingFeeDelta = _a.calculateVaultReservingFee(vaultInfo, vaultInfo.reservingFeeModel, Date.now() / 1000);
|
|
555
606
|
const res = (reservingFeeDelta + vaultInfo.liquidity + vaultInfo.reservedAmount) * (await this.getOraclePrice(vault)).getPriceUnchecked().getPriceAsNumberUnchecked() / (10 ** this.consts.coins[vault].decimals);
|
|
556
607
|
if (collateralToken === vault) {
|
|
557
608
|
vaultValue = res;
|
|
@@ -563,13 +614,13 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
563
614
|
const targetRatio = Number.parseInt(this.consts.zoCore.vaults[collateralToken].weight, 10) / Object.values(this.consts.zoCore.vaults)
|
|
564
615
|
.map(e => Number.parseInt(e.weight, 10))
|
|
565
616
|
.reduce((acc, curr) => acc + curr, 0);
|
|
566
|
-
return
|
|
617
|
+
return _a.calcRebaseFeeRate(await this.getRebaseFeeModel(), increase, (vaultValue + value) / (totalVaultValue + value), targetRatio);
|
|
567
618
|
}
|
|
568
619
|
async reservingFeeRate(collateralToken, amount = 0) {
|
|
569
620
|
const vaultInfo = await this.getVaultInfo(collateralToken);
|
|
570
621
|
const vaultSupply = vaultInfo.liquidity + vaultInfo.reservedAmount + vaultInfo.unrealisedReservingFeeAmount + amount;
|
|
571
622
|
const utilization = vaultSupply ? ((vaultInfo.reservedAmount + amount) / vaultSupply) : 0;
|
|
572
|
-
return
|
|
623
|
+
return _a.calcReservingFeeRate(vaultInfo.reservingFeeModel, utilization, SECONDS_PER_EIGHT_HOUR);
|
|
573
624
|
}
|
|
574
625
|
async getHistory(trader, page, limit, orderType, symbol) {
|
|
575
626
|
const params = new URLSearchParams({
|
|
@@ -609,47 +660,68 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
609
660
|
const secondsRate = dailyRate * elapsed / SECONDS_PER_EIGHT_HOUR;
|
|
610
661
|
return pnlPerRate >= 0 ? -secondsRate : secondsRate;
|
|
611
662
|
}
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
663
|
+
/**
|
|
664
|
+
* OI funding rate matching Move compute_oi_funding_rate_capped.
|
|
665
|
+
* When both maxOiLong and maxOiShort are set and > 0, uses normalized skew (oi/cap);
|
|
666
|
+
* otherwise falls back to (long - short) / total.
|
|
667
|
+
*/
|
|
668
|
+
static calcOiFundingFeeRate(model, oiLong, oiShort, elapsed, maxOiLong, maxOiShort) {
|
|
669
|
+
let skew;
|
|
670
|
+
if (maxOiLong && maxOiShort && maxOiLong > 0 && maxOiShort > 0) {
|
|
671
|
+
const normLong = Math.min(oiLong / maxOiLong, 1);
|
|
672
|
+
const normShort = Math.min(oiShort / maxOiShort, 1);
|
|
673
|
+
skew = normLong - normShort;
|
|
674
|
+
}
|
|
675
|
+
else {
|
|
676
|
+
const total = oiLong + oiShort;
|
|
677
|
+
if (total === 0)
|
|
678
|
+
return 0;
|
|
679
|
+
skew = (oiLong - oiShort) / total;
|
|
680
|
+
}
|
|
681
|
+
if (skew === 0)
|
|
682
|
+
return 0;
|
|
683
|
+
const skewIsPositive = skew > 0;
|
|
684
|
+
const skewAbs = Math.abs(skew);
|
|
685
|
+
const exponentInt = Math.floor(model.exponent);
|
|
686
|
+
const skewPow = skewAbs ** exponentInt;
|
|
687
|
+
const dailyRate = Math.min(model.multiplier * skewPow, model.max);
|
|
688
|
+
const secondsRate = (dailyRate * elapsed) / SECONDS_PER_EIGHT_HOUR;
|
|
689
|
+
return skewIsPositive ? secondsRate : -secondsRate;
|
|
618
690
|
}
|
|
619
|
-
static calcAccFundingFeeRate(symbol, model, price, lpSupplyAmount, timestamp,
|
|
691
|
+
static calcAccFundingFeeRate(symbol, model, price, lpSupplyAmount, timestamp, oiState, pairedOpeningSize) {
|
|
620
692
|
if (symbol.lastUpdate > 0) {
|
|
621
693
|
const elapsed = timestamp - symbol.lastUpdate;
|
|
622
694
|
if (elapsed > 0) {
|
|
623
|
-
// Prefer OI-based delta when
|
|
624
|
-
if (
|
|
695
|
+
// Prefer OI-based delta when state and paired side are available
|
|
696
|
+
if (oiState?.enabled && oiState.model && typeof pairedOpeningSize === 'number') {
|
|
625
697
|
const longSize = symbol.long ? symbol.openingSize : pairedOpeningSize;
|
|
626
698
|
const shortSize = symbol.long ? pairedOpeningSize : symbol.openingSize;
|
|
627
|
-
const deltaRate =
|
|
699
|
+
const deltaRate = _a.calcOiFundingFeeRate(oiState.model, longSize, shortSize, elapsed, oiState.maxOiLong, oiState.maxOiShort);
|
|
628
700
|
const appliedRate = symbol.long ? deltaRate : -deltaRate;
|
|
629
701
|
return symbol.accFundingRate + appliedRate;
|
|
630
702
|
}
|
|
631
703
|
// Fallback to PnL-based funding delta
|
|
632
|
-
const deltaSize =
|
|
704
|
+
const deltaSize = _a.calcDeltaSize(symbol, price);
|
|
633
705
|
const pnlPerLp = (symbol.realisedPnl + symbol.unrealisedFundingFeeValue + deltaSize) / lpSupplyAmount;
|
|
634
|
-
return symbol.accFundingRate +
|
|
706
|
+
return symbol.accFundingRate + _a.calcFundingFeeRate(model, pnlPerLp, elapsed);
|
|
635
707
|
}
|
|
636
708
|
}
|
|
637
709
|
return symbol.accFundingRate;
|
|
638
710
|
}
|
|
639
|
-
static calculateSymbolFundingFee(symbol, model, price, lpSupplyAmount, timestamp,
|
|
640
|
-
const accFundingRate =
|
|
711
|
+
static calculateSymbolFundingFee(symbol, model, price, lpSupplyAmount, timestamp, oiState, pairedOpeningSize) {
|
|
712
|
+
const accFundingRate = _a.calcAccFundingFeeRate(symbol, model, price, lpSupplyAmount, timestamp, oiState, pairedOpeningSize);
|
|
641
713
|
return symbol.unrealisedFundingFeeValue + (accFundingRate - symbol.accFundingRate) * symbol.openingSize;
|
|
642
714
|
}
|
|
643
715
|
static calculatePositionReserveFee(position, vault, model, timestamp) {
|
|
644
|
-
const accReservingRate =
|
|
716
|
+
const accReservingRate = _a.calcAccReservingFeeRate(vault, model, timestamp);
|
|
645
717
|
return position.reservingFeeAmount + (accReservingRate - position.lastReservingRate) * position.reservedAmount;
|
|
646
718
|
}
|
|
647
719
|
static calcAccReservingFeeRate(vault, model, timestamp) {
|
|
648
720
|
if (vault.lastUpdate > 0) {
|
|
649
721
|
const elapsed = timestamp - vault.lastUpdate;
|
|
650
722
|
if (elapsed > 0) {
|
|
651
|
-
const utilization =
|
|
652
|
-
return vault.accReservingRate +
|
|
723
|
+
const utilization = _a.vaultUtilization(vault);
|
|
724
|
+
return vault.accReservingRate + _a.calcReservingFeeRate(model, utilization, elapsed);
|
|
653
725
|
}
|
|
654
726
|
}
|
|
655
727
|
return vault.accReservingRate;
|
|
@@ -697,7 +769,7 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
697
769
|
showContent: true,
|
|
698
770
|
},
|
|
699
771
|
});
|
|
700
|
-
const reservingFeeModel =
|
|
772
|
+
const reservingFeeModel = _a.parseReservingFeeModel(reservingFeeModelRaw);
|
|
701
773
|
return {
|
|
702
774
|
liquidity: parseValue(vaultFields.liquidity),
|
|
703
775
|
reservedAmount: parseValue(vaultFields.reserved_amount),
|
|
@@ -725,7 +797,7 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
725
797
|
showContent: true,
|
|
726
798
|
},
|
|
727
799
|
});
|
|
728
|
-
const fundingFeeModel =
|
|
800
|
+
const fundingFeeModel = _a.parseFundingFeeModel(fundingFeeModelRaw);
|
|
729
801
|
return {
|
|
730
802
|
objectId,
|
|
731
803
|
openingSize: parseValue(fields.opening_size),
|
|
@@ -804,15 +876,15 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
804
876
|
closed: positionFields.closed,
|
|
805
877
|
openTimestamp: parseValue(positionFields.open_timestamp),
|
|
806
878
|
};
|
|
807
|
-
positionInfo.reservingFeeAmount =
|
|
879
|
+
positionInfo.reservingFeeAmount = _a.calculatePositionReserveFee(positionInfo, await this.getVaultInfo(positionInfo.collateralToken), (await this.getVaultInfo(positionInfo.collateralToken)).reservingFeeModel, Date.now() / 1000);
|
|
808
880
|
// OI context for funding: fetch state and paired side size when enabled
|
|
809
881
|
const oiState = await this.getSymbolOiFundingState(positionInfo.indexToken);
|
|
810
882
|
const pairedSymbol = await this.getSymbolInfo(positionInfo.indexToken, !positionInfo.long);
|
|
811
|
-
positionInfo.fundingFeeValue =
|
|
883
|
+
positionInfo.fundingFeeValue = _a.calculatePositionFundingFee(positionInfo, await this.getSymbolInfo(positionInfo.indexToken, positionInfo.long), (await this.getSymbolInfo(positionInfo.indexToken, positionInfo.long)).fundingFeeModel, (await this.getOraclePrice(positionInfo.indexToken)).getPriceUnchecked().getPriceAsNumberUnchecked(), (await this.getMarketInfo()).lpSupplyWithDecimals, Date.now() / 1000, oiState && oiState.enabled ? oiState : undefined, pairedSymbol.openingSize);
|
|
812
884
|
return positionInfo;
|
|
813
885
|
}
|
|
814
|
-
static calculatePositionFundingFee(position, symbol, model, price, lpSupplyAmount, timestamp,
|
|
815
|
-
const accFundingRate =
|
|
886
|
+
static calculatePositionFundingFee(position, symbol, model, price, lpSupplyAmount, timestamp, oiState, pairedOpeningSize) {
|
|
887
|
+
const accFundingRate = _a.calcAccFundingFeeRate(symbol, model, price, lpSupplyAmount, timestamp, oiState, pairedOpeningSize);
|
|
816
888
|
return position.fundingFeeValue + (accFundingRate - position.lastFundingRate) * position.positionSize;
|
|
817
889
|
}
|
|
818
890
|
static parseRebaseFeeModel(raw) {
|
|
@@ -873,6 +945,8 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
873
945
|
exponent: parseValue(content.model.fields.exponent),
|
|
874
946
|
max: parseValue(content.model.fields.max),
|
|
875
947
|
},
|
|
948
|
+
maxOiLong: content.max_oi_long !== null && content.max_oi_long !== undefined ? parseValue(content.max_oi_long) : undefined,
|
|
949
|
+
maxOiShort: content.max_oi_short !== null && content.max_oi_short !== undefined ? parseValue(content.max_oi_short) : undefined,
|
|
876
950
|
};
|
|
877
951
|
}
|
|
878
952
|
static parsePriceImpactConfig(raw) {
|
|
@@ -1058,9 +1132,9 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
1058
1132
|
endSkew = 1;
|
|
1059
1133
|
}
|
|
1060
1134
|
// Compute average dynamic spread via integral
|
|
1061
|
-
const avgDynamicRate =
|
|
1135
|
+
const avgDynamicRate = _a.computeIntegralAverage(config.maxDynamicSpreadRate, startSkew, endSkew, config.impactExponent);
|
|
1062
1136
|
// Apply size scaling factor to dynamic spread
|
|
1063
|
-
const sizeFactor =
|
|
1137
|
+
const sizeFactor = _a.computeSizeFactor(newPositionSize, config.referenceSize);
|
|
1064
1138
|
const scaledDynamicRate = avgDynamicRate * sizeFactor;
|
|
1065
1139
|
// total_spread = base_spread + scaled_dynamic
|
|
1066
1140
|
let totalSpread = config.baseSpreadRate + scaledDynamicRate;
|
|
@@ -1131,9 +1205,9 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
1131
1205
|
const maxOiThisSide = isLong ? config.maxOiLong : config.maxOiShort;
|
|
1132
1206
|
const maxOiOpposite = isLong ? config.maxOiShort : config.maxOiLong;
|
|
1133
1207
|
// Compute average spread rate (uses integration for better accuracy on large trades)
|
|
1134
|
-
const spreadRate =
|
|
1208
|
+
const spreadRate = _a.computeAverageSpreadRate(config, currentOiThisSide, positionSizeValue, currentOiOpposite, maxOiThisSide, maxOiOpposite);
|
|
1135
1209
|
// Apply price impact
|
|
1136
|
-
const adjustedPrice =
|
|
1210
|
+
const adjustedPrice = _a.applyPriceImpact(oraclePrice, spreadRate, isLong, isOpening);
|
|
1137
1211
|
return {
|
|
1138
1212
|
spreadRate,
|
|
1139
1213
|
originalPrice: oraclePrice,
|
|
@@ -1249,7 +1323,7 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
1249
1323
|
accRewardPerShare: BigInt(content.acc_reward_per_share),
|
|
1250
1324
|
lockDuration: parseValue(content.lock_duration),
|
|
1251
1325
|
};
|
|
1252
|
-
|
|
1326
|
+
_a.refreshPool(pool, Math.floor(Date.now() / 1000));
|
|
1253
1327
|
return pool;
|
|
1254
1328
|
}
|
|
1255
1329
|
static refreshPool(pool, timestamp) {
|
|
@@ -1267,4 +1341,84 @@ export class ZLPDataAPI extends BaseDataAPI {
|
|
|
1267
1341
|
pool.lastUpdatedTime = calculationEndTime;
|
|
1268
1342
|
}
|
|
1269
1343
|
}
|
|
1344
|
+
_a = ZLPDataAPI, _ZLPDataAPI_instances = new WeakSet(), _ZLPDataAPI_getTotalVaultsValueUsd = async function _ZLPDataAPI_getTotalVaultsValueUsd(timestamp) {
|
|
1345
|
+
const vaultKeys = Object.keys(this.consts.zoCore.vaults);
|
|
1346
|
+
const vaultValues = await Promise.all(vaultKeys.map(async (vault) => {
|
|
1347
|
+
const vaultInfo = await this.getVaultInfo(vault);
|
|
1348
|
+
const reservingFeeDelta = _a.calculateVaultReservingFee(vaultInfo, vaultInfo.reservingFeeModel, timestamp);
|
|
1349
|
+
const totalVaultAmount = reservingFeeDelta + vaultInfo.liquidity + vaultInfo.reservedAmount;
|
|
1350
|
+
const oraclePrice = (await this.getOraclePrice(vault)).getPriceUnchecked().getPriceAsNumberUnchecked();
|
|
1351
|
+
const { decimals } = this.consts.coins[vault];
|
|
1352
|
+
return totalVaultAmount * oraclePrice / (10 ** decimals);
|
|
1353
|
+
}));
|
|
1354
|
+
return vaultValues.reduce((acc, curr) => acc + curr, 0);
|
|
1355
|
+
}, _ZLPDataAPI_getSwapImpactConfig = async function _ZLPDataAPI_getSwapImpactConfig() {
|
|
1356
|
+
const raw = await __classPrivateFieldGet(this, _ZLPDataAPI_instances, "m", _ZLPDataAPI_getMarketDynamicFieldObjectByKeySuffix).call(this, this.consts.zoCore.market, 'SwapImpactConfigKey');
|
|
1357
|
+
if (!raw)
|
|
1358
|
+
return null;
|
|
1359
|
+
return __classPrivateFieldGet(_a, _a, "m", _ZLPDataAPI_parseSwapImpactConfig).call(_a, raw);
|
|
1360
|
+
}, _ZLPDataAPI_getEmaVolatilityFeeConfig = async function _ZLPDataAPI_getEmaVolatilityFeeConfig() {
|
|
1361
|
+
const raw = await __classPrivateFieldGet(this, _ZLPDataAPI_instances, "m", _ZLPDataAPI_getMarketDynamicFieldObjectByKeySuffix).call(this, this.consts.zoCore.market, 'EmaVolatilityFeeConfigKey');
|
|
1362
|
+
if (!raw)
|
|
1363
|
+
return null;
|
|
1364
|
+
return __classPrivateFieldGet(_a, _a, "m", _ZLPDataAPI_parseEmaVolatilityFeeConfig).call(_a, raw);
|
|
1365
|
+
}, _ZLPDataAPI_getMarketDynamicFieldObjectByKeySuffix = async function _ZLPDataAPI_getMarketDynamicFieldObjectByKeySuffix(parentId, keyTypeSuffix) {
|
|
1366
|
+
let cursor;
|
|
1367
|
+
let hasNextPage = true;
|
|
1368
|
+
while (hasNextPage) {
|
|
1369
|
+
const page = await this.provider.getDynamicFields({ parentId, cursor });
|
|
1370
|
+
for (const field of page.data) {
|
|
1371
|
+
const type = field.name?.type;
|
|
1372
|
+
if (typeof type === 'string' && type.endsWith(`::${keyTypeSuffix}`)) {
|
|
1373
|
+
return await this.provider.getDynamicFieldObject({
|
|
1374
|
+
parentId,
|
|
1375
|
+
name: field.name,
|
|
1376
|
+
});
|
|
1377
|
+
}
|
|
1378
|
+
}
|
|
1379
|
+
hasNextPage = page.hasNextPage;
|
|
1380
|
+
cursor = page.nextCursor;
|
|
1381
|
+
}
|
|
1382
|
+
return null;
|
|
1383
|
+
}, _ZLPDataAPI_parseSwapImpactConfig = function _ZLPDataAPI_parseSwapImpactConfig(raw) {
|
|
1384
|
+
const { fields } = raw.data.content;
|
|
1385
|
+
return {
|
|
1386
|
+
id: fields.id.id,
|
|
1387
|
+
enabled: fields.enabled,
|
|
1388
|
+
impactMultiplier: parseValue(fields.impact_multiplier),
|
|
1389
|
+
maxImpactRate: parseValue(fields.max_impact_rate),
|
|
1390
|
+
};
|
|
1391
|
+
}, _ZLPDataAPI_parseEmaVolatilityFeeConfig = function _ZLPDataAPI_parseEmaVolatilityFeeConfig(raw) {
|
|
1392
|
+
const { fields } = raw.data.content;
|
|
1393
|
+
return {
|
|
1394
|
+
id: fields.id.id,
|
|
1395
|
+
enabled: fields.enabled,
|
|
1396
|
+
multiplier: parseValue(fields.multiplier),
|
|
1397
|
+
maxFeeRate: parseValue(fields.max_fee_rate),
|
|
1398
|
+
};
|
|
1399
|
+
}, _ZLPDataAPI_computeSwapImpactFeeValue = function _ZLPDataAPI_computeSwapImpactFeeValue(swapValue, totalVaultsValue, impactMultiplier, maxImpactRate) {
|
|
1400
|
+
if (swapValue <= 0 || totalVaultsValue <= 0)
|
|
1401
|
+
return 0;
|
|
1402
|
+
const utilization = swapValue / totalVaultsValue;
|
|
1403
|
+
const rawImpactRate = impactMultiplier * utilization;
|
|
1404
|
+
const impactRate = Math.min(rawImpactRate, maxImpactRate);
|
|
1405
|
+
return swapValue * impactRate;
|
|
1406
|
+
}, _ZLPDataAPI_computeEmaVolatilityFeeValue = function _ZLPDataAPI_computeEmaVolatilityFeeValue(swapValue, maxDiv, multiplier, maxFeeRate) {
|
|
1407
|
+
if (swapValue <= 0 || maxDiv <= 0)
|
|
1408
|
+
return 0;
|
|
1409
|
+
const rawFeeRate = multiplier * maxDiv;
|
|
1410
|
+
const feeRate = Math.min(rawFeeRate, maxFeeRate);
|
|
1411
|
+
return swapValue * feeRate;
|
|
1412
|
+
}, _ZLPDataAPI_maxEmaDivergenceRate = function _ZLPDataAPI_maxEmaDivergenceRate(sourceFeed, destFeed) {
|
|
1413
|
+
const sourceDiv = __classPrivateFieldGet(_a, _a, "m", _ZLPDataAPI_emaDivergenceRate).call(_a, sourceFeed);
|
|
1414
|
+
const destDiv = __classPrivateFieldGet(_a, _a, "m", _ZLPDataAPI_emaDivergenceRate).call(_a, destFeed);
|
|
1415
|
+
return Math.max(sourceDiv, destDiv);
|
|
1416
|
+
}, _ZLPDataAPI_emaDivergenceRate = function _ZLPDataAPI_emaDivergenceRate(priceFeed) {
|
|
1417
|
+
const price = priceFeed.getPriceUnchecked().getPriceAsNumberUnchecked();
|
|
1418
|
+
const ema = priceFeed.getEmaPriceUnchecked().getPriceAsNumberUnchecked();
|
|
1419
|
+
const denom = Math.abs(price);
|
|
1420
|
+
if (denom === 0)
|
|
1421
|
+
return 0;
|
|
1422
|
+
return Math.abs(price - ema) / denom;
|
|
1423
|
+
};
|
|
1270
1424
|
//# sourceMappingURL=ZLPDataAPI.mjs.map
|