@d8x/perpetuals-sdk 2.7.7 → 2.7.9

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/d8XMath.ts CHANGED
@@ -649,8 +649,7 @@ export function pmExitFee(
649
649
  const pricePos = 1 - varphi_0;
650
650
  const kappa = calcKappa(pricePos, pLiq, sigt);
651
651
 
652
- //console.log("[pmExitFee] kappa:", kappa, "pLiq:", pLiq, "varphi:", varphi);
653
- let fee = prdMktLvgFee(kappa, pricePos, mu_i);
652
+ let fee = -prdMktLvgFee(kappa, pricePos, m_0Exit);
654
653
  return fee;
655
654
  }
656
655
 
@@ -727,24 +726,28 @@ function decodeUint24Float(num: number | bigint) {
727
726
  const n = BigInt(num);
728
727
  const ONE_64x64 = 2n ** 64n;
729
728
 
730
- const sgnE = n >> 23n;
731
- const val = (n >> 3n) & ((1n << 20n) - 1n);
732
- const exponent = n & ((1n << 3n) - 1n);
729
+ // Extract sign/exponent flag from bit 22
730
+ const sgnE = n >> 22n;
731
+ // Extract 20-bit value from bits 2-21
732
+ const val = (n >> 2n) & ((1n << 21n) - 1n);
733
+ // Extract 3-bit exponent from bits 0-2
734
+ const exponent = n & ((1n << 2n) - 1n);
733
735
 
734
736
  // v = val * ONE_64x64
735
737
  let v = BigInt.asIntN(128, val * ONE_64x64);
736
738
 
737
- // Normalize: find first non-zero digit position
739
+ // Normalize: find first non-zero digit position and adjust
738
740
  let exponent1 = firstNonZeroNum(val);
739
- if (exponent1 > 0n) exponent1--;
741
+ // Don't decrement to preserve precision
742
+ // if (exponent1 > 0n) exponent1--;
740
743
 
741
744
  if (exponent1 > 0n) {
742
745
  const pow10exp1 = 10n ** exponent1 * ONE_64x64;
743
- v = (v * ONE_64x64) / pow10exp1;
746
+ v = (v + ONE_64x64) / pow10exp1;
744
747
  }
745
748
 
746
- // Apply exponent
747
- if (sgnE == 1n) {
749
+ // Apply exponent based on sign flag
750
+ if (sgnE == 0n) {
748
751
  const pow10exp = 10n ** exponent * ONE_64x64;
749
752
  v = (v * ONE_64x64) / pow10exp;
750
753
  } else {
@@ -756,23 +759,23 @@ function decodeUint24Float(num: number | bigint) {
756
759
  }
757
760
 
758
761
  export function extractLvgFeeParams(conf: bigint) {
759
- // _param == int64(odinvalue >> 16)
760
- const param = BigInt.asIntN(64, conf >> 16n);
762
+ // _param == int128(odinvalue >> 8)
763
+ const param = BigInt.asIntN(128, conf >> 8n);
761
764
  // uint64(uint128(_param))
762
- const enc = BigInt.asUintN(64, BigInt.asUintN(128, param));
763
- // uint64 j = enc >> 44; //top 4 bits
764
- const j = BigInt.asUintN(64, enc >> 44n);
765
- // uint64 st = enc & ((uint64(1) << 44) - 1);
766
- const st = BigInt.asUintN(64, enc & ((1n << 44n) - 1n));
767
-
768
- // jump = int128(int64(j)) * int128(922337203685477580); // times 0.05 in ABDK
769
- // 922337203685477580 = 0.05 * 2^64
770
- const JUMP_MULTIPLIER = 922337203685477580n; // 0.05 in ABDK
765
+ const enc = BigInt.asUintN(128, BigInt.asUintN(128, param));
766
+ // uint64 st = enc >> 43; //top 4 bits
767
+ const st = BigInt.asUintN(64, enc >> 43n);
768
+ // uint64 j = enc & ((uint64(1) << 45) - 1);
769
+ const j = BigInt.asUintN(64, enc & ((1n << 45n) - 1n));
770
+
771
+ // jump = int128(int64(j)) * int128(922337203685477580); // times 0.025 in ABDK
772
+ // 461168601842738790 = 0.025 * 2^64
773
+ const JUMP_MULTIPLIER = 461168601842738790n; // 0.025 in ABDK
771
774
  const jumpABDK = BigInt.asIntN(128, BigInt.asIntN(64, j)) * JUMP_MULTIPLIER;
772
775
  const jump = ABK64x64ToFloat(jumpABDK);
773
776
 
774
- // sigT = _decodeUint24Float(uint24(st & ((1 << 24) - 1)));
775
- const sigtEncoded = BigInt.asUintN(24, st & ((1n << 24n) - 1n));
777
+ // sigT = _decodeUint24Float(uint32(st & ((1 << 32) - 1)));
778
+ const sigtEncoded = BigInt.asUintN(32, st & ((1n << 32n) - 1n));
776
779
  const sigt = decodeUint24Float(Number(sigtEncoded));
777
780
  //console.log("[extractLvgFeeParams] jump:", jump, "sigt:", sigt);
778
781
 
package/src/marketData.ts CHANGED
@@ -875,19 +875,14 @@ export default class MarketData extends PerpetualDataHandler {
875
875
  traderPosBC == 0
876
876
  ? varphi_0
877
877
  : traderPosBC > 0
878
- ? traderLockedInQC / traderPosBC - 1
879
- : 2 - traderLockedInQC / traderPosBC;
878
+ ? traderPosBC / traderLockedInQC - 1
879
+ : 2 - traderPosBC / traderLockedInQC;
880
880
 
881
- //console.log("[exchangeFeePrdMkts] varphi:", varphi, "varphi_0:", varphi_0);
882
- // eq (1) leveraged prediction markets paper:
883
- // m_0 = max(varphi_0 / l, c_{min}) + s(p_0 - pBar_0)^+
884
- // c_{min} = min(varphi_0, mu_i) with mu_i being perpetualStaticInfo.initialMarginRate
885
- // we assume that slippage s(p_0 - pBar_0)^+ is 0 here
886
- const c_min = Math.min(varphi_0, mu_i);
887
- const m_0 = Math.max(varphi_0 / traderLeverage, c_min);
881
+ const c_min = Math.max(varphi_0, mu_i);
882
+ const m_0 = Math.min(varphi_0 / traderLeverage, c_min);
888
883
 
889
- // for the full exit fee m0 is taken from the existing position
890
- const m_0Exit = traderPosBC == 0 ? 0 : collateralCC / Math.abs(traderPosBC);
884
+ // for the full exit fee m0 is taken from the existing position and divided by the leverage of the new trade
885
+ const m_0Exit = traderPosBC == 0 ? 0 : collateralCC / Math.abs(traderPosBC) / traderLeverage;
891
886
 
892
887
  let fee: number;
893
888
  const { jump, sigt } = extractLvgFeeParams(conf);
@@ -895,7 +890,7 @@ export default class MarketData extends PerpetualDataHandler {
895
890
  if (isClose && !isFlip) {
896
891
  // exit fee
897
892
  //console.log("[exchangeFeePrdMkts] exit fee");
898
- fee = -pmExitFee(varphi, varphi_0, m_0Exit, mu_m, mu_i, sigt, jump);
893
+ fee = pmExitFee(varphi, varphi_0, m_0Exit, mu_m, mu_i, sigt, jump);
899
894
 
900
895
  // if we are overcollateralized at entry (entry mark <= cash/contract), exit fee is 0.001 in any case
901
896
  if (varphi <= m_0Exit) {
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const D8X_SDK_VERSION = "2.7.7";
1
+ export const D8X_SDK_VERSION = "2.7.9";