@d8x/perpetuals-sdk 2.7.3 → 2.7.5

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
@@ -637,15 +637,26 @@ export function pmExitFee(varphi: number, varphiBar: number, mu_m: number, m_0:
637
637
  const pLiq = varphi + mu_m - m_0;
638
638
  const kappa = calcKappa(varphiBar, pLiq, sigt);
639
639
  let fee = prdMktLvgFee(kappa, varphi, m_0);
640
+ // console.log({ pLiq, kappa, varphi });
640
641
  return fee;
641
642
  }
642
643
 
643
- export function pmOpenFee(varphi: number, varphiBar: number, mu_m: number, m_0: number, sigt: number, jump: number) {
644
- const pLiq = varphi + mu_m - m_0;
644
+ export function pmOpenFee(
645
+ varphi: number,
646
+ varphiBar: number,
647
+ lvg: number,
648
+ mu_m: number,
649
+ m_0: number,
650
+ sigt: number,
651
+ jump: number
652
+ ) {
653
+ let fee = Math.min(varphiBar, m_0) / lvg;
654
+ const pLiq = varphi + mu_m - fee;
645
655
  const kappa = calcKappa(varphiBar, pLiq, sigt);
646
656
  const es = calcJumpRisk(kappa, varphiBar, jump);
647
- const feecap = (1 - varphiBar) * varphiBar - (1 - varphiBar) * m_0;
648
- let fee = prdMktLvgFee(kappa, varphi, m_0);
657
+ const feecap = (1 - varphiBar) * varphiBar - (1 - varphiBar) * fee;
658
+ // let fee = prdMktLvgFee(kappa, varphi, m_0);
659
+ // console.log({ m_0, pLiq, lvg, kappa, es, feecap, fee, varphi, varphiBar });
649
660
  fee = fee + es;
650
661
  if (fee > feecap) {
651
662
  fee = feecap;
@@ -666,6 +677,41 @@ function firstNonZeroNum(numDec: bigint) {
666
677
  return pos;
667
678
  }
668
679
 
680
+ function decodeUint16Float(num: bigint) {
681
+ // uint16 sgnNum = num >> 15;
682
+ const sgnNum = BigInt.asUintN(16, num >> 15n);
683
+ // uint16 sgnE = (num >> 14) & 1;
684
+ const sgnE = BigInt.asUintN(16, (num >> 14n) & 1n);
685
+ // uint16 val = (num >> 4) & ((2 ** 10) - 1);
686
+ const val = BigInt.asUintN(16, (num >> 4n) & (2n ** 10n - 1n));
687
+ // uint16 exponent = num & ((2 ** 4) - 1);
688
+ const exponent = BigInt.asUintN(16, num & (2n ** 4n - 1n));
689
+
690
+ // //convert val abcde to normalized form a.bcde
691
+ // int128 v = int128(uint128(val)) * ONE_64x64;
692
+ let v = ABK64x64ToFloat(BigInt.asIntN(128, BigInt.asUintN(128, val)) * ONE_64x64);
693
+ // uint256 exponent1 = first_nonzeronum(val);
694
+ let exponent1 = firstNonZeroNum(val);
695
+ if (exponent1 > 0n) {
696
+ exponent1 -= 1n;
697
+ }
698
+ // v = v.div(ABDKMath64x64.pow(10 * ONE_64x64, exponent1));
699
+ v = v / ABK64x64ToFloat(10n * ONE_64x64) ** Number(exponent1);
700
+ if (sgnE == 1n) {
701
+ // v = v.div(ABDKMath64x64.pow(10 * ONE_64x64, uint256(exponent)));
702
+ v = v / ABK64x64ToFloat(10n * ONE_64x64) ** Number(exponent);
703
+ } else {
704
+ // v = v.mul(ABDKMath64x64.pow(10 * ONE_64x64, uint256(exponent)));
705
+ v = v * ABK64x64ToFloat(10n * ONE_64x64) ** Number(exponent);
706
+ }
707
+ if (sgnNum == 1n) {
708
+ // v = v.neg();
709
+ v = -v;
710
+ }
711
+ // console.log({ num, v, fV: floatToABK64x64(v) });
712
+ return v;
713
+ }
714
+
669
715
  function decodeUint24Float(num: number | bigint) {
670
716
  const n = BigInt(num);
671
717
 
@@ -688,23 +734,72 @@ function decodeUint24Float(num: number | bigint) {
688
734
 
689
735
  export function extractLvgFeeParams(conf: bigint) {
690
736
  // static replication of _checkOracleStatus and _extractLvgFeeParams
691
- const param = BigInt.asIntN(64, conf >> 16n); // int64(conf >> 16);
692
- const enc = BigInt.asUintN(64, BigInt.asUintN(128, param)); // uint64(uint128(_param));
693
- const j = BigInt.asUintN(64, enc >> 44n); // uint64 j = enc >> 44; //top 4 bits
694
- const st = BigInt.asUintN(64, enc & ((BigInt.asUintN(64, 1n) << 44n) - 1n)); // uint64 st = enc & ((uint64(1) << 44) - 1); // low 44 bits are sigt (padded 24-bit)
695
- const jump = ABK64x64ToFloat(BigInt.asUintN(128, BigInt.asIntN(64, j)) * BigInt.asIntN(128, 922337203685477580n)); // jump = int128(int64(j)) * int128(922337203685477580); // times 0.05 in ABDK
696
-
737
+ // int64(conf >> 16);
738
+ const param = BigInt.asIntN(64, conf >> 16n);
739
+ // uint64(uint128(_param));
740
+ const enc = BigInt.asUintN(64, BigInt.asUintN(128, param));
741
+ // uint64 j = enc >> 44; //top 4 bits
742
+ const j = BigInt.asUintN(64, enc >> 44n);
743
+ // uint64 st = enc & ((uint64(1) << 44) - 1); // low 44 bits are sigt (padded 24-bit)
744
+ const st = BigInt.asUintN(64, enc & ((BigInt.asUintN(64, 1n) << 44n) - 1n));
745
+ // jump = int128(int64(j)) * int128(922337203685477580); // times 0.05 in ABDK
746
+ const jump = ABK64x64ToFloat(BigInt.asIntN(128, BigInt.asIntN(64, j))) * 0.5;
697
747
  const sigt = decodeUint24Float(BigInt.asUintN(24, st & ((1n << 24n) - 1n))); // uint24(st & ((1 << 24) - 1));
698
-
748
+ // console.log({ jump, sigt });
699
749
  return { jump, sigt };
700
750
  }
701
751
 
752
+ export function decodePriceImpact(amount: bigint, params: bigint) {
753
+ // uint32 params;
754
+ // if (_amount > 0) {
755
+ // params = uint32(_params & ((2 ** 32) - 1));
756
+ // } else {
757
+ // params = uint32(_params >> 32);
758
+ // }
759
+ // console.log({ params2sided: params });
760
+ params = BigInt.asUintN(
761
+ 32,
762
+ amount > 0n ? BigInt.asUintN(32, params) & BigInt.asUintN(32, 2n ** 32n - 1n) : BigInt.asUintN(32, params) >> 32n
763
+ );
764
+ // console.log({ amountSign: amount > 0n, params });
765
+ // int128 a = decodeUint16Float(uint16(params >> 16));
766
+ // int128 m = decodeUint16Float(uint16(params & ((2 ** 16) - 1)));
767
+ // int128 l = a.add(_amount.abs().mul(m));
768
+ const a = decodeUint16Float(BigInt.asUintN(16, params >> 16n));
769
+ const m = decodeUint16Float(BigInt.asUintN(16, params & (2n ** 16n - 1n)));
770
+ const l = a + Math.abs(ABK64x64ToFloat(amount)) * m;
771
+
772
+ // if (l < 0x200000000000000000) {
773
+ // // here if impact is not close to overflow
774
+ // return _amount < 0 ? -l.exp() : l.exp();
775
+ // }
776
+ let dp: number;
777
+ if (l < ABK64x64ToFloat(BigInt("0x200000000000000000"))) {
778
+ dp = amount < 0n ? -Math.exp(l) : Math.exp(l);
779
+ } else {
780
+ dp = (amount < 0n ? -1 : 1) * ABK64x64ToFloat(BigInt("0x40000000000000000000000000000000"));
781
+ }
782
+ // // return a very big number
783
+ // return
784
+ // _amount < 0
785
+ // ? -int128(0x40000000000000000000000000000000)
786
+ // : int128(0x40000000000000000000000000000000);
787
+
788
+ return { a, m, l, dp };
789
+ }
790
+
702
791
  //sigt = sigma*sqrt(t)
703
792
  function calcKappa(varphi: number, varphiLiq: number, sigt: number): number {
704
793
  const p = 1 - varphi;
705
794
  const pStar = 1 - varphiLiq;
706
795
  const x0 = Math.log(p / (1 - p));
707
796
  const a = Math.log(pStar / (1 - pStar));
797
+ // a - x0 = log( p*(1-p*) / (1-p)p* )
798
+ const cdfArg = Math.log((p * (1 - pStar)) / (pStar * (1 - p))) / sigt;
799
+ // console.log({ p, pStar, x0, a, sigt, cdfarg: (a - x0) / sigt });
800
+ if (isNaN(cdfArg)) {
801
+ return pStar * (1 - p) < p * (1 - pStar) ? 0 : 2;
802
+ }
708
803
  const kappa = 2 * (1 - cdfNormalStd((a - x0) / sigt));
709
804
  return kappa;
710
805
  }
@@ -721,14 +816,26 @@ function prdMktLvgFee(kappa: number, varphi: number, m0: number): number {
721
816
  return f;
722
817
  }
723
818
 
724
- function cdfNormalStd(x: number) {
725
- const t = 1 / (1 + 0.2315419 * Math.abs(x));
726
- const d = 0.3989423 * Math.exp((-x * x) / 2);
727
- let prob = d * t * (0.3193815 + t * (-0.3565638 + t * (1.781478 + t * (-1.821256 + t * 1.330274))));
728
- if (x > 0) {
729
- prob = 1 - prob;
819
+ export function erfc(x: number): number {
820
+ if (x < -10) {
821
+ return 0;
822
+ }
823
+ if (x > 10) {
824
+ return 1;
730
825
  }
731
- return prob;
826
+ const z = Math.abs(x);
827
+ const t = 1 / (1 + z / 2);
828
+ // prettier-ignore
829
+ const r = t * Math.exp(-z * z - 1.26551223 + t * (1.00002368 +
830
+ t * (0.37409196 + t * (0.09678418 + t * (-0.18628806 +
831
+ t * (0.27886807 + t * (-1.13520398 + t * (1.48851587 +
832
+ t * (-0.82215223 + t * 0.17087277)))))))));
833
+
834
+ return x >= 0 ? r : 2 - r;
835
+ }
836
+
837
+ function cdfNormalStd(x: number) {
838
+ return 0.5 * erfc(-x / Math.sqrt(2));
732
839
  }
733
840
  /**
734
841
  * Margin balance for prediction markets
package/src/marketData.ts CHANGED
@@ -505,7 +505,7 @@ export default class MarketData extends PerpetualDataHandler {
505
505
  perpId,
506
506
  floatToABK64x64(tradeAmountBC),
507
507
  [fS2, fS3],
508
- indexPriceInfo.conf & ((1n << 16n) - 1n),
508
+ 10n*(indexPriceInfo.conf & ((1n << 16n) - 1n)),
509
509
  indexPriceInfo.predMktCLOBParams,
510
510
  ]),
511
511
  },
@@ -847,8 +847,8 @@ export default class MarketData extends PerpetualDataHandler {
847
847
  tradeAmtBC: number,
848
848
  traderPosBC: number,
849
849
  traderLockedInQC: number,
850
+ traderLeverage: number,
850
851
  mu_m: number,
851
- mr: number,
852
852
  m_0: number,
853
853
  conf: bigint
854
854
  ): number {
@@ -865,12 +865,13 @@ export default class MarketData extends PerpetualDataHandler {
865
865
  : 2 - traderLockedInQC / traderPosBC;
866
866
  let fee: number;
867
867
  const { jump, sigt } = extractLvgFeeParams(conf);
868
+
868
869
  if (isClose && !isFlip) {
869
870
  // exit fee
870
871
  fee = pmExitFee(varphi, varphiBar, mu_m, m_0, sigt, jump);
871
872
  } else {
872
873
  // open fee
873
- fee = pmOpenFee(varphi, varphiBar, mu_m, mr, sigt, jump);
874
+ fee = pmOpenFee(varphi, varphiBar, traderLeverage, mu_m, m_0, sigt, jump);
874
875
  }
875
876
  if (fee < 0.001) {
876
877
  fee = 0.001;
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const D8X_SDK_VERSION = "2.7.3";
1
+ export const D8X_SDK_VERSION = "2.7.5";