@d8x/perpetuals-sdk 2.1.26-alpha2 → 2.1.28-alpha2

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/constants.ts CHANGED
@@ -4,6 +4,7 @@ import { NodeSDKConfig } from "./nodeSDKTypes";
4
4
  export const ERC20_ABI = require("./abi/ERC20.json");
5
5
  export const MOCK_TOKEN_SWAP_ABI = require("./abi/MockTokenSwap.json");
6
6
  export const PROXY_ABI = require("./abi/IPerpetualManager.json");
7
+ export const PROXY_ZKEVM_ABI = require("./abi-zkevm/IPerpetualManager.json");
7
8
  export const LOB_FACTORY_ABI = require("./abi/LimitOrderBookFactory.json");
8
9
  export const LOB_ABI = require("./abi/LimitOrderBook.json");
9
10
  export const SHARE_TOKEN_ABI = require("./abi/ShareToken.json");
@@ -57,7 +58,7 @@ export const DEFAULT_CONFIG_TESTNET_NAME = "testnet";
57
58
 
58
59
  let defaultConfigs = require("./config/defaultConfig.json") as NodeSDKConfig[];
59
60
  defaultConfigs.map((config) => {
60
- config.proxyABI = PROXY_ABI;
61
+ config.proxyABI = config.proxyABILocation.includes("abi-zkevm") ? PROXY_ZKEVM_ABI : PROXY_ABI;
61
62
  config.lobABI = LOB_ABI;
62
63
  config.lobFactoryABI = LOB_FACTORY_ABI;
63
64
  config.shareTokenABI = SHARE_TOKEN_ABI;
package/src/d8XMath.ts CHANGED
@@ -440,7 +440,7 @@ export function probToPrice(prob: number) {
440
440
 
441
441
  // shannon entropy
442
442
  export function entropy(prob: number) {
443
- if (prob < 1e-15 || prob - 1 > 1e-15) {
443
+ if (prob < 1e-15 || prob > 1 - 1e-15) {
444
444
  return 0;
445
445
  }
446
446
  return -prob * Math.log2(prob) - (1 - prob) * Math.log2(1 - prob);
@@ -778,23 +778,24 @@ function pmExcessCashAtLvg(
778
778
  }
779
779
 
780
780
  /**
781
- * Find maximal trade size (short dir=-1 or long dir=1) for prediction
781
+ * Find maximal *affordable* trade size (short dir=-1 or long dir=1) for prediction
782
782
  * markets at provided leverage and incorporating the current position
783
783
  * and wallet balance.
784
784
  * Factors in lot size and global max short/long
785
- * @param dir
786
- * @param lvg
787
- * @param walletBalCC
788
- * @param slippage slippage percent
789
- * @param currentPosition
790
- * @param currentCashCC
791
- * @param currentLockedInValue
792
- * @param S2
793
- * @param Sm
794
- * @param S3
795
- * @param maxShort global max short order size (sign irrelevant)
796
- * @param maxLong global max long order size (positive)
797
- * @returns max trade size
785
+ * @param dir direction of trade (-1 sell, 1 buy)
786
+ * @param lvg leverage of the trade
787
+ * @param walletBalCC wallet balance of the trader (collateral currency)
788
+ * @param slippage slippage percent used to estimate a traded price
789
+ * @param currentPosition position in base currency of the trader
790
+ * @param currentCashCC this is the cash available net of unpaid funding (often called available cash)
791
+ * @param currentLockedInValue average entry price * signed position size in base currency, in margin account
792
+ * @param S2 current index price of the form 1+p (regardless whether short or long)
793
+ * @param Sm current mark price (not just the mark price index but including the ema-premium from the contract)
794
+ * @param S3 current collateral to quote index price
795
+ * @param glblMaxTrade global max short or long order size that we retreive, e.g., from position risk (sign irrelevant)
796
+ * based on long: (*ℓ+n) * (1-p) - m (1-p) s = F → n = (F+m*(1-p)*s)/(1-p)-ℓ*
797
+ * short: (s+n)*p - m p *ℓ* = F →n = (F+m*p**ℓ*)/p-s
798
+ * @returns max *signed* trade size
798
799
  */
799
800
  export function pmFindMaxPersonalTradeSizeAtLeverage(
800
801
  dir: number,
@@ -807,8 +808,7 @@ export function pmFindMaxPersonalTradeSizeAtLeverage(
807
808
  S2: number,
808
809
  Sm: number,
809
810
  S3: number,
810
- maxShort: number,
811
- maxLong: number
811
+ glblMaxTrade: number
812
812
  ): number {
813
813
  if (dir < 0) {
814
814
  dir = -1;
@@ -880,17 +880,17 @@ export function pmFindMaxPersonalTradeSizeAtLeverage(
880
880
  // Newton algorithm failed,
881
881
  // choose new starting value
882
882
  if (dir > 0) {
883
- sNew = Math.random() * (maxLong - currentPosition);
883
+ sNew = Math.random() * (glblMaxTrade - currentPosition);
884
884
  } else {
885
- sNew = -Math.random() * (Math.abs(maxShort) + currentPosition);
885
+ sNew = -Math.random() * (Math.abs(glblMaxTrade) + currentPosition);
886
886
  }
887
887
  }
888
888
  // ensure trade maximal trade sNew does not exceed
889
889
  // the contract limits
890
- if (sNew < -Math.abs(maxShort)) {
891
- sNew = -Math.abs(maxShort);
892
- } else if (sNew > maxLong) {
893
- sNew = maxLong;
890
+ if (sNew < -Math.abs(glblMaxTrade)) {
891
+ sNew = -Math.abs(glblMaxTrade);
892
+ } else if (sNew > glblMaxTrade) {
893
+ sNew = glblMaxTrade;
894
894
  }
895
895
  // round trade size down to lot
896
896
  sNew = Math.sign(sNew) * Math.floor(Math.abs(sNew) / lot) * lot;
package/src/marketData.ts CHANGED
@@ -571,13 +571,13 @@ export default class MarketData extends PerpetualDataHandler {
571
571
  "getMaxSignedOpenTradeSizeForPos",
572
572
  encodedResults[2].returnData
573
573
  )[0] as bigint;
574
- const maxLongTrade = Math.max(0, ABK64x64ToFloat(fMaxLong) - signedPositionNotionalBaseCCY);
574
+ const maxLongTrade = ABK64x64ToFloat(fMaxLong);
575
575
  // max sell
576
576
  const fMaxShort = this.proxyContract.interface.decodeFunctionResult(
577
577
  "getMaxSignedOpenTradeSizeForPos",
578
578
  encodedResults[3].returnData
579
579
  )[0] as bigint;
580
- const maxShortTrade = Math.max(0, Math.abs(ABK64x64ToFloat(fMaxShort)) - signedPositionNotionalBaseCCY);
580
+ const maxShortTrade = ABK64x64ToFloat(fMaxShort);
581
581
  return { account: account, ammPrice: ammPrice, maxShortTrade: maxShortTrade, maxLongTrade: maxLongTrade };
582
582
  }
583
583
 
package/src/version.ts CHANGED
@@ -1 +1 @@
1
- export const D8X_SDK_VERSION = "2.1.26-alpha2";
1
+ export const D8X_SDK_VERSION = "2.1.28-alpha2";