@d8x/perpetuals-sdk 2.6.13 → 2.6.14

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/src/marketData.ts CHANGED
@@ -2031,8 +2031,8 @@ export default class MarketData extends PerpetualDataHandler {
2031
2031
 
2032
2032
  allSym.add(MarketData.getIndexSymbol(_symbolList, sInfo));
2033
2033
 
2034
- if (sInfo!.S3Symbol != "") {
2035
- allSym.add(sInfo!.S3Symbol);
2034
+ if (sInfo.S3Symbol != "") {
2035
+ allSym.add(sInfo.S3Symbol);
2036
2036
  }
2037
2037
  }
2038
2038
  let allSymArr = Array.from(allSym.values());
@@ -2146,7 +2146,7 @@ export default class MarketData extends PerpetualDataHandler {
2146
2146
  continue;
2147
2147
  }
2148
2148
  collectedIds.push(id);
2149
- let S2 = floatToABK64x64(_idxPriceMap.get(MarketData.getIndexSymbol(_symbolList, info))![0]);
2149
+ let S2 = floatToABK64x64(_idxPriceMap.get(MarketData.getIndexSymbol(_symbolList, info))![0]); // 'has(key)' checked above
2150
2150
  let S3 = 0n;
2151
2151
  if (info.S3Symbol != "" && _idxPriceMap.has(info.S3Symbol)) {
2152
2152
  S3 = floatToABK64x64(_idxPriceMap.get(info.S3Symbol)![0]);
@@ -2403,9 +2403,9 @@ export default class MarketData extends PerpetualDataHandler {
2403
2403
  const idxPriceS2Pair = idxPriceMap.get(MarketData.getIndexSymbol(_symbolList, info))!;
2404
2404
  let idxPriceS3Pair: [number, boolean] = [0, false];
2405
2405
  perp.isMarketClosed = idxPriceS2Pair[1];
2406
- if (info.S3Symbol != "") {
2406
+ if (info.S3Symbol != "" && idxPriceMap.has(info.S3Symbol)) {
2407
2407
  idxPriceS3Pair = idxPriceMap.get(info.S3Symbol)!;
2408
- perp.isMarketClosed = perp.isMarketClosed || idxPriceS3Pair![1];
2408
+ perp.isMarketClosed = perp.isMarketClosed || idxPriceS3Pair[1];
2409
2409
  }
2410
2410
  perp.indexPrice = idxPriceS2Pair[0];
2411
2411
  let indexS3 = 1;
@@ -2420,14 +2420,16 @@ export default class MarketData extends PerpetualDataHandler {
2420
2420
  let markPrice: number;
2421
2421
  if (idxPriceMap.has(emaKey)) {
2422
2422
  let ema: number;
2423
- let res = idxPriceMap.get(emaKey);
2424
- ema = res![0];
2423
+ let res = idxPriceMap.get(emaKey)!; // checked
2424
+ ema = res[0];
2425
2425
  markPrice = Math.min(Math.max(ema + perp.markPremium, 1), 2);
2426
2426
  } else {
2427
2427
  markPrice = perp.indexPrice * (1 + perp.markPremium);
2428
2428
  }
2429
2429
  perp.markPrice = markPrice;
2430
- perp.midPrice = midPriceMap.get(symbol3s!)!;
2430
+ if (symbol3s && midPriceMap.has(symbol3s)) {
2431
+ perp.midPrice = midPriceMap.get(symbol3s)!; // checked
2432
+ }
2431
2433
  }
2432
2434
  // which pool?
2433
2435
  const poolId = info?.poolId ?? 1;
@@ -202,7 +202,7 @@ export default class PerpetualDataHandler {
202
202
  }
203
203
  this.proxyContract = IPerpetualManager__factory.connect(this.proxyAddr, signerOrProvider);
204
204
  this.multicall = Multicall3__factory.connect(this.config.multicall ?? MULTICALL_ADDRESS, this.signerOrProvider);
205
- await this.fetchSymbolList();
205
+ await this.fetchSymbolList(); // static from sync-hub
206
206
  await this._fillSymbolMaps();
207
207
  } finally {
208
208
  this.refreshLocked = false;
@@ -469,13 +469,8 @@ export default class PerpetualDataHandler {
469
469
  // the prices of price-feeds to the index price required, e.g.
470
470
  // BTC-USDC : BTC-USD / USDC-USD
471
471
  this.priceFeedGetter.initializeTriangulations(requiredPairs);
472
- // ensure all feed prices can be fetched
472
+
473
473
  this.setRequiredSymbols();
474
- try {
475
- await this.priceFeedGetter.fetchFeedPrices(this.requiredSymbols);
476
- } catch (e) {
477
- console.log("price feeds unreachable:", e);
478
- }
479
474
  }
480
475
 
481
476
  // setRequiredSymbols determines which symbols (of the form BTC-USD)
@@ -898,17 +893,17 @@ export default class PerpetualDataHandler {
898
893
  fMaintenanceMarginRate: ABDK29ToFloat(Number(orig.fMaintenanceMarginRate)), // parameter: maintenance margin
899
894
  perpetualState: PERP_STATE_STR[Number(orig.state)], // Perpetual AMM state
900
895
  eCollateralCurrency: Number(orig.eCollateralCurrency), //parameter: in what currency is the collateral held?
901
- S2BaseCCY: contractSymbolToSymbol(fromBytes4(Buffer.from(orig.S2BaseCCY!.toString())), _symbolList), //base currency of S2
902
- S2QuoteCCY: contractSymbolToSymbol(fromBytes4(Buffer.from(orig.S2QuoteCCY!.toString())), _symbolList), //quote currency of S2
896
+ S2BaseCCY: contractSymbolToSymbol(fromBytes4(Buffer.from(orig.S2BaseCCY.toString())), _symbolList), //base currency of S2
897
+ S2QuoteCCY: contractSymbolToSymbol(fromBytes4(Buffer.from(orig.S2QuoteCCY.toString())), _symbolList), //quote currency of S2
903
898
  incentiveSpreadBps: Number(orig.incentiveSpreadTbps) / 10, //parameter: maximum spread added to the PD
904
899
  minimalSpreadBps: Number(orig.minimalSpreadBps), //parameter: minimal half-spread between long and short perpetual price
905
- S3BaseCCY: contractSymbolToSymbol(fromBytes4(Buffer.from(orig.S3BaseCCY!.toString())), _symbolList), //base currency of S3
906
- S3QuoteCCY: contractSymbolToSymbol(fromBytes4(Buffer.from(orig.S3QuoteCCY!.toString())), _symbolList), //quote currency of S3
900
+ S3BaseCCY: contractSymbolToSymbol(fromBytes4(Buffer.from(orig.S3BaseCCY.toString())), _symbolList), //base currency of S3
901
+ S3QuoteCCY: contractSymbolToSymbol(fromBytes4(Buffer.from(orig.S3QuoteCCY.toString())), _symbolList), //quote currency of S3
907
902
  fSigma3: ABDK29ToFloat(Number(orig.fSigma3)), // parameter: volatility of quanto-quote pair
908
903
  fRho23: ABDK29ToFloat(Number(orig.fRho23)), // parameter: correlation of quanto/base returns
909
904
  liquidationPenaltyRateBps: Number(orig.liquidationPenaltyRateTbps) / 10, //parameter: penalty if AMM closes the position and not the trader
910
- currentMarkPremiumRatePrice: ABK64x64ToFloat(BigInt(orig.currentMarkPremiumRate!.fPrice)), //relative diff to index price EMA, used for markprice.
911
- currentMarkPremiumRateTime: Number(orig.currentMarkPremiumRate!.time), //relative diff to index price EMA, used for markprice.
905
+ currentMarkPremiumRatePrice: ABK64x64ToFloat(BigInt(orig.currentMarkPremiumRate.fPrice)), //relative diff to index price EMA, used for markprice.
906
+ currentMarkPremiumRateTime: Number(orig.currentMarkPremiumRate.time), //relative diff to index price EMA, used for markprice.
912
907
  premiumRatesEMA: ABK64x64ToFloat(BigInt(orig.premiumRatesEMA)), // EMA of premium rate
913
908
  fUnitAccumulatedFunding: ABK64x64ToFloat(BigInt(orig.fUnitAccumulatedFunding)), //accumulated funding in collateral currency
914
909
  fOpenInterest: ABK64x64ToFloat(BigInt(orig.fOpenInterest)), //open interest is the larger of the amount of long and short positions in base currency
@@ -931,17 +926,17 @@ export default class PerpetualDataHandler {
931
926
  fMaximalTradeSizeBumpUp: ABK64x64ToFloat(Number(orig.fMaximalTradeSizeBumpUp)), // parameter: >1, users can create a maximal position of size fMaximalTradeSizeBumpUp*fCurrentAMMExposureEMA
932
927
  iLastTargetPoolSizeTime: Number(orig.iLastTargetPoolSizeTime), //timestamp (seconds) since last update of fTargetDFSize and fTargetAMMFundSize
933
928
  fStressReturnS3: [
934
- ABK64x64ToFloat(BigInt(orig.fStressReturnS3![0])),
935
- ABK64x64ToFloat(BigInt(orig.fStressReturnS3![1])),
929
+ ABK64x64ToFloat(BigInt(orig.fStressReturnS3[0])),
930
+ ABK64x64ToFloat(BigInt(orig.fStressReturnS3[1])),
936
931
  ], // parameter: negative and positive stress returns for quanto-quote asset
937
- fDFLambda: [ABK64x64ToFloat(BigInt(orig.fDFLambda![0])), ABK64x64ToFloat(BigInt(orig.fDFLambda![1]))], // parameter: EMA lambda for AMM and trader exposure K,k: EMA*lambda + (1-lambda)*K. 0 regular lambda, 1 if current value exceeds past
932
+ fDFLambda: [ABK64x64ToFloat(BigInt(orig.fDFLambda[0])), ABK64x64ToFloat(BigInt(orig.fDFLambda[1]))], // parameter: EMA lambda for AMM and trader exposure K,k: EMA*lambda + (1-lambda)*K. 0 regular lambda, 1 if current value exceeds past
938
933
  fCurrentAMMExposureEMA: [
939
- ABK64x64ToFloat(BigInt(orig.fCurrentAMMExposureEMA![0])),
940
- ABK64x64ToFloat(BigInt(orig.fCurrentAMMExposureEMA![1])),
934
+ ABK64x64ToFloat(BigInt(orig.fCurrentAMMExposureEMA[0])),
935
+ ABK64x64ToFloat(BigInt(orig.fCurrentAMMExposureEMA[1])),
941
936
  ], // 0: negative aggregated exposure (storing negative value), 1: positive
942
937
  fStressReturnS2: [
943
- ABK64x64ToFloat(BigInt(orig.fStressReturnS2![0])),
944
- ABK64x64ToFloat(BigInt(orig.fStressReturnS2![1])),
938
+ ABK64x64ToFloat(BigInt(orig.fStressReturnS2[0])),
939
+ ABK64x64ToFloat(BigInt(orig.fStressReturnS2[1])),
945
940
  ], // parameter: negative and positive stress returns for base-quote asset
946
941
  };
947
942
  if (isNaN(v.minimalSpreadBps)) {
@@ -2374,12 +2369,13 @@ export default class PerpetualDataHandler {
2374
2369
  * @returns Index symbol
2375
2370
  */
2376
2371
  public static getIndexSymbol(_symbolList: Map<string, string>, info: PerpetualStaticInfo) {
2377
- return PerpetualDataHandler.isPredictionMarketStatic(info!)
2378
- ? PerpetualDataHandler._getBySingleValue(_symbolList, info.S2Symbol)! +
2379
- "-" +
2380
- info!.S2Symbol.split("-")[1] +
2381
- ":84532"
2382
- : info!.S2Symbol;
2372
+ if (PerpetualDataHandler.isPredictionMarketStatic(info)) {
2373
+ const slot = PerpetualDataHandler._getBySingleValue(_symbolList, info.S2Symbol);
2374
+ if (slot !== undefined) {
2375
+ return slot + "-" + info.S2Symbol.split("-")[1] + ":84532";
2376
+ }
2377
+ }
2378
+ return info.S2Symbol;
2383
2379
  }
2384
2380
 
2385
2381
  /**
@@ -89,14 +89,14 @@ export default class PolyMktsPxFeed {
89
89
  const pxObj = parsed?.price as PriceFeedJson | undefined;
90
90
  const marketClosed = Boolean(parsed?.metadata.market_closed);
91
91
 
92
- const px = pxObj == undefined ? 0 : Number(pxObj.price) * Math.pow(10, pxObj.expo);
93
- const ema = emaObj == undefined ? 0 : Number(emaObj.price) * Math.pow(10, emaObj.expo);
92
+ const s2 = pxObj == undefined || pxObj.price == "NaN" ? 0 : Number(pxObj.price) * Math.pow(10, pxObj.expo);
93
+ const ema = emaObj == undefined || emaObj.price == "NaN" ? 0 : Number(emaObj.price) * Math.pow(10, emaObj.expo);
94
94
  const params = emaObj?.conf == undefined ? 0n : BigInt(emaObj.conf);
95
95
  const conf = pxObj?.conf == undefined ? 0n : BigInt(pxObj.conf);
96
96
  const info = {
97
- s2: px,
98
- ema: ema,
99
- s2MktClosed: marketClosed,
97
+ s2,
98
+ ema,
99
+ s2MktClosed: marketClosed || s2 == 0 || ema == 0,
100
100
  conf: conf,
101
101
  predMktCLOBParams: params,
102
102
  } as PredMktPriceInfo;
package/src/priceFeeds.ts CHANGED
@@ -410,8 +410,8 @@ export default class PriceFeeds {
410
410
  if (polyPxs[k] == undefined) {
411
411
  continue;
412
412
  }
413
- resultPrices.set(sym, [polyPxs[k]!.s2, polyPxs[k]!.s2MktClosed, undefined, undefined]);
414
- resultPrices.set(sym + ":ema", [polyPxs[k]!.ema, polyPxs[k]!.s2MktClosed, undefined, undefined]);
413
+ resultPrices.set(sym, [polyPxs[k].s2, polyPxs[k].s2MktClosed, undefined, undefined]);
414
+ resultPrices.set(sym + ":ema", [polyPxs[k].ema, polyPxs[k].s2MktClosed, undefined, undefined]);
415
415
  }
416
416
  return resultPrices;
417
417
  }
@@ -421,7 +421,7 @@ export default class PriceFeeds {
421
421
  for (let k = 0; k < symbols.length; k++) {
422
422
  let sym = symbols[k];
423
423
  const feed = this.onChainPxFeeds.get(sym);
424
- let price = await feed!.getPrice();
424
+ let price = (await feed?.getPrice()) || 0;
425
425
  prices.push(price);
426
426
  }
427
427
  return prices;
@@ -491,9 +491,9 @@ export default class PriceFeeds {
491
491
 
492
492
  for (let j = 0; j < info.length; j++) {
493
493
  if (symbols.has(feedId)) {
494
- let v = symbols.get(feedId);
495
- v!.push(info[j].symbol);
496
- symbols.set(feedId, v!);
494
+ let v = symbols.get(feedId)!;
495
+ v.push(info[j].symbol);
496
+ symbols.set(feedId, v);
497
497
  } else {
498
498
  symbols.set(feedId, [info[j].symbol]);
499
499
  }
@@ -537,7 +537,7 @@ export default class PriceFeeds {
537
537
  let price = decNToFloat(BigInt(pxInfo.price), -pxInfo.expo);
538
538
  prices.push(price);
539
539
  priceFeedUpdates.push(data[k].vaas[0]);
540
- let isMarketClosed = tsSecNow - pxInfo.publish_time > this.THRESHOLD_MARKET_CLOSED_SEC;
540
+ let isMarketClosed = pxInfo.price == 0n || tsSecNow - pxInfo.publish_time > this.THRESHOLD_MARKET_CLOSED_SEC;
541
541
  mktClosed.push(isMarketClosed);
542
542
  timestamps.push(pxInfo.publish_time);
543
543
  }
@@ -629,7 +629,12 @@ export default class PriceFeeds {
629
629
  // see also fetchPriceQuery for idx
630
630
  const idx = k % values.binary.data.length;
631
631
  vaas.push("0x" + Buffer.from(values.binary.data[idx], "base64").toString("hex"));
632
- prices.push(values.parsed[k].price);
632
+ // check price data
633
+ const price = values.parsed[k].price;
634
+ if (price.price.toString() == "NaN") {
635
+ price.price = 0n;
636
+ }
637
+ prices.push(price);
633
638
  }
634
639
  return { vaas, prices };
635
640
  }
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const D8X_SDK_VERSION = "2.6.13";
1
+ export const D8X_SDK_VERSION = "2.6.14";