@gmx-io/sdk 0.1.1-alpha-1 → 0.1.2

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.
Files changed (77) hide show
  1. package/build/src/configs/chains.d.ts +3 -0
  2. package/build/src/configs/chains.js +6 -0
  3. package/build/src/configs/contracts.d.ts +220 -1
  4. package/build/src/configs/contracts.js +67 -8
  5. package/build/src/configs/markets.js +14 -0
  6. package/build/src/configs/tokens.js +40 -5
  7. package/build/src/modules/markets/index.js +1 -7
  8. package/build/src/modules/markets/query-builders.d.ts +1 -1
  9. package/build/src/modules/markets/types.d.ts +4 -1
  10. package/build/src/modules/orders/utils.js +1 -1
  11. package/build/src/modules/positions/positions.js +17 -4
  12. package/build/src/modules/utils/utils.d.ts +1 -1
  13. package/build/src/modules/utils/utils.js +1 -1
  14. package/build/src/prebuilt/hashedKinkModelMarketRatesKeys.json +16 -0
  15. package/build/src/prebuilt/hashedMarketConfigKeys.json +100 -0
  16. package/build/src/prebuilt/hashedMarketValuesKeys.json +30 -0
  17. package/build/src/types/common.d.ts +1 -0
  18. package/build/src/types/fees.d.ts +2 -1
  19. package/build/src/types/markets.d.ts +7 -19
  20. package/build/src/types/orders.d.ts +3 -5
  21. package/build/src/types/positions.d.ts +11 -4
  22. package/build/src/types/trade.d.ts +1 -0
  23. package/build/src/types/tradeHistory.d.ts +4 -1
  24. package/build/src/utils/__tests__/LruCache.spec.d.ts +1 -0
  25. package/build/src/utils/__tests__/LruCache.spec.js +42 -0
  26. package/build/src/utils/__tests__/bigmath.spec.d.ts +1 -0
  27. package/build/src/utils/__tests__/bigmath.spec.js +37 -0
  28. package/build/src/utils/__tests__/hash.spec.d.ts +1 -0
  29. package/build/src/utils/__tests__/hash.spec.js +40 -0
  30. package/build/src/utils/__tests__/markets.spec.d.ts +1 -0
  31. package/build/src/utils/__tests__/markets.spec.js +264 -0
  32. package/build/src/utils/__tests__/numbers.spec.d.ts +1 -0
  33. package/build/src/utils/__tests__/numbers.spec.js +56 -0
  34. package/build/src/utils/__tests__/objects.spec.d.ts +1 -0
  35. package/build/src/utils/__tests__/objects.spec.js +58 -0
  36. package/build/src/utils/__tests__/positions.spec.d.ts +1 -0
  37. package/build/src/utils/__tests__/positions.spec.js +177 -0
  38. package/build/src/utils/__tests__/prices.spec.d.ts +1 -0
  39. package/build/src/utils/__tests__/prices.spec.js +58 -0
  40. package/build/src/utils/__tests__/referrals.spec.d.ts +1 -0
  41. package/build/src/utils/__tests__/referrals.spec.js +17 -0
  42. package/build/src/utils/__tests__/subgraph.spec.d.ts +1 -0
  43. package/build/src/utils/__tests__/subgraph.spec.js +162 -0
  44. package/build/src/utils/__tests__/tokens.spec.d.ts +1 -0
  45. package/build/src/utils/__tests__/tokens.spec.js +82 -0
  46. package/build/src/utils/__tests__/trade.spec.d.ts +1 -0
  47. package/build/src/utils/__tests__/trade.spec.js +35 -0
  48. package/build/src/utils/bigmath.d.ts +1 -1
  49. package/build/src/utils/bigmath.js +6 -1
  50. package/build/src/utils/callContract.js +5 -1
  51. package/build/src/utils/contracts.d.ts +4 -0
  52. package/build/src/utils/contracts.js +2 -0
  53. package/build/src/utils/fees/executionFee.d.ts +1 -33
  54. package/build/src/utils/fees/executionFee.js +2 -48
  55. package/build/src/utils/fees/executionFee.spec.d.ts +1 -0
  56. package/build/src/utils/fees/executionFee.spec.js +64 -0
  57. package/build/src/utils/fees/index.d.ts +2 -0
  58. package/build/src/utils/fees/index.js +5 -0
  59. package/build/src/utils/markets.d.ts +9 -2
  60. package/build/src/utils/markets.js +33 -10
  61. package/build/src/utils/orders.js +0 -8
  62. package/build/src/utils/positions.js +3 -3
  63. package/build/src/utils/prices.d.ts +1 -6
  64. package/build/src/utils/prices.js +0 -9
  65. package/build/src/utils/referrals.d.ts +2 -0
  66. package/build/src/utils/referrals.js +17 -15
  67. package/build/src/utils/swapStats.d.ts +1 -0
  68. package/build/src/utils/swapStats.js +12 -1
  69. package/build/src/utils/testUtil.d.ts +5 -5
  70. package/build/src/utils/tokens.d.ts +4 -0
  71. package/build/src/utils/tokens.js +4 -0
  72. package/build/tsconfig.tsbuildinfo +1 -1
  73. package/package.json +2 -3
  74. package/build/src/types/amounts.d.ts +0 -223
  75. package/build/src/types/amounts.js +0 -17
  76. package/build/src/types/syntheticsEvents.d.ts +0 -88
  77. /package/build/src/types/{syntheticsEvents.js → common.js} +0 -0
@@ -1,5 +1,5 @@
1
1
  import { BASIS_POINTS_DIVISOR } from "../configs/factors";
2
- import { NATIVE_TOKEN_ADDRESS } from "../configs/tokens";
2
+ import { getTokenVisualMultiplier, NATIVE_TOKEN_ADDRESS } from "../configs/tokens";
3
3
  import { applyFactor, PRECISION } from "./numbers";
4
4
  import { getByKey } from "./objects";
5
5
  import { convertToContractTokenPrices, convertToUsd, getMidPrice } from "./tokens";
@@ -13,7 +13,8 @@ export function getMarketIndexName(p) {
13
13
  if (isSpotOnly) {
14
14
  return `SWAP-ONLY`;
15
15
  }
16
- return `${firstToken.baseSymbol || firstToken.symbol}/USD`;
16
+ const prefix = getTokenVisualMultiplier(firstToken);
17
+ return `${prefix}${firstToken.baseSymbol || firstToken.symbol}/USD`;
17
18
  }
18
19
  export function getMarketPoolName(p) {
19
20
  const { longToken, shortToken } = p;
@@ -35,6 +36,9 @@ export function getContractMarketPrices(tokensData, market) {
35
36
  shortTokenPrice: shortToken && convertToContractTokenPrices(shortToken.prices, shortToken.decimals),
36
37
  };
37
38
  }
39
+ /**
40
+ * Apart from usual cases, returns `long` for single token backed markets.
41
+ */
38
42
  export function getTokenPoolType(marketInfo, tokenAddress) {
39
43
  const { longToken, shortToken } = marketInfo;
40
44
  if (longToken.address === shortToken.address && tokenAddress === longToken.address) {
@@ -64,14 +68,7 @@ export function getPoolUsdWithoutPnl(marketInfo, isLong, priceType) {
64
68
  return convertToUsd(poolAmount, token.decimals, price);
65
69
  }
66
70
  export function getCappedPoolPnl(p) {
67
- const { marketInfo, poolUsd, isLong, maximize } = p;
68
- let poolPnl;
69
- if (isLong) {
70
- poolPnl = maximize ? marketInfo.pnlLongMax : marketInfo.pnlLongMin;
71
- }
72
- else {
73
- poolPnl = maximize ? marketInfo.pnlShortMax : marketInfo.pnlShortMin;
74
- }
71
+ const { marketInfo, poolUsd, poolPnl, isLong } = p;
75
72
  if (poolPnl < 0) {
76
73
  return poolPnl;
77
74
  }
@@ -127,3 +124,29 @@ export function getReservedUsd(marketInfo, isLong) {
127
124
  export function getMarketDivisor({ longTokenAddress, shortTokenAddress, }) {
128
125
  return longTokenAddress === shortTokenAddress ? 2n : 1n;
129
126
  }
127
+ export function getMarketPnl(marketInfo, isLong, forMaxPoolValue) {
128
+ const maximize = !forMaxPoolValue;
129
+ const openInterestUsd = getOpenInterestUsd(marketInfo, isLong);
130
+ const openInterestInTokens = getOpenInterestInTokens(marketInfo, isLong);
131
+ if (openInterestUsd === 0n || openInterestInTokens === 0n) {
132
+ return 0n;
133
+ }
134
+ const price = getPriceForPnl(marketInfo.indexToken.prices, isLong, maximize);
135
+ const openInterestValue = convertToUsd(openInterestInTokens, marketInfo.indexToken.decimals, price);
136
+ const pnl = isLong ? openInterestValue - openInterestUsd : openInterestUsd - openInterestValue;
137
+ return pnl;
138
+ }
139
+ export function getOpenInterestUsd(marketInfo, isLong) {
140
+ return isLong ? marketInfo.longInterestUsd : marketInfo.shortInterestUsd;
141
+ }
142
+ export function getOpenInterestInTokens(marketInfo, isLong) {
143
+ return isLong ? marketInfo.longInterestInTokens : marketInfo.shortInterestInTokens;
144
+ }
145
+ export function getPriceForPnl(prices, isLong, maximize) {
146
+ // for long positions, pick the larger price to maximize pnl
147
+ // for short positions, pick the smaller price to maximize pnl
148
+ if (isLong) {
149
+ return maximize ? prices.maxPrice : prices.minPrice;
150
+ }
151
+ return maximize ? prices.minPrice : prices.maxPrice;
152
+ }
@@ -96,13 +96,6 @@ export function getOrderInfo(p) {
96
96
  if (!marketInfo || !indexToken || !initialCollateralToken || !targetCollateralToken) {
97
97
  return undefined;
98
98
  }
99
- // const title = getPositionOrderTitle({
100
- // orderType: order.orderType,
101
- // isLong: order.isLong,
102
- // indexToken,
103
- // sizeDeltaUsd: order.sizeDeltaUsd,
104
- // });
105
- const title = "TODO";
106
99
  const acceptablePrice = parseContractPrice(order.contractAcceptablePrice, indexToken.decimals);
107
100
  const triggerPrice = parseContractPrice(order.contractTriggerPrice, indexToken.decimals);
108
101
  const swapPathStats = getSwapPathStats({
@@ -117,7 +110,6 @@ export function getOrderInfo(p) {
117
110
  const triggerThresholdType = getTriggerThresholdType(order.orderType, order.isLong);
118
111
  const orderInfo = {
119
112
  ...order,
120
- title,
121
113
  swapPathStats,
122
114
  marketInfo,
123
115
  indexToken,
@@ -1,6 +1,6 @@
1
1
  import { bigMath } from "./bigmath";
2
2
  import { applyFactor, expandDecimals } from "./numbers";
3
- import { getCappedPoolPnl, getPoolUsdWithoutPnl } from "./markets";
3
+ import { getCappedPoolPnl, getMarketPnl, getPoolUsdWithoutPnl } from "./markets";
4
4
  import { convertToUsd, getIsEquivalentTokens } from "./tokens";
5
5
  import { BASIS_POINTS_DIVISOR_BIGINT } from "../configs/factors";
6
6
  import { getPositionFee, getPriceImpactForPosition } from "./fees";
@@ -25,13 +25,13 @@ export function getPositionPnlUsd(p) {
25
25
  if (totalPnl <= 0) {
26
26
  return totalPnl;
27
27
  }
28
- const poolPnl = isLong ? p.marketInfo.pnlLongMax : p.marketInfo.pnlShortMax;
28
+ const poolPnl = getMarketPnl(marketInfo, isLong, true);
29
29
  const poolUsd = getPoolUsdWithoutPnl(marketInfo, isLong, "minPrice");
30
30
  const cappedPnl = getCappedPoolPnl({
31
31
  marketInfo,
32
32
  poolUsd,
33
+ poolPnl,
33
34
  isLong,
34
- maximize: true,
35
35
  });
36
36
  const WEI_PRECISION = expandDecimals(1, 18);
37
37
  if (cappedPnl !== poolPnl && cappedPnl > 0 && poolPnl > 0) {
@@ -1,5 +1,5 @@
1
1
  import { OrderType } from "../types/orders";
2
- import { Token, TokenPrices } from "../types/tokens";
2
+ import { TokenPrices } from "../types/tokens";
3
3
  import { TriggerThresholdType } from "../types/trade";
4
4
  export declare function getMarkPrice(p: {
5
5
  prices: TokenPrices;
@@ -8,8 +8,3 @@ export declare function getMarkPrice(p: {
8
8
  }): bigint;
9
9
  export declare function getShouldUseMaxPrice(isIncrease: boolean, isLong: boolean): boolean;
10
10
  export declare function getTriggerThresholdType(orderType: OrderType, isLong: boolean): TriggerThresholdType;
11
- export declare function getEntryPrice(p: {
12
- sizeInUsd: bigint;
13
- sizeInTokens: bigint;
14
- indexToken: Token;
15
- }): bigint | undefined;
@@ -1,7 +1,5 @@
1
1
  import { OrderType } from "../types/orders";
2
2
  import { TriggerThresholdType } from "../types/trade";
3
- import { expandDecimals } from "./numbers";
4
- import { bigMath } from "./bigmath";
5
3
  export function getMarkPrice(p) {
6
4
  const { prices, isIncrease, isLong } = p;
7
5
  const shouldUseMaxPrice = getShouldUseMaxPrice(isIncrease, isLong);
@@ -25,10 +23,3 @@ export function getTriggerThresholdType(orderType, isLong) {
25
23
  }
26
24
  throw new Error("Invalid trigger order type");
27
25
  }
28
- export function getEntryPrice(p) {
29
- const { sizeInUsd, sizeInTokens, indexToken } = p;
30
- if (sizeInTokens <= 0) {
31
- return undefined;
32
- }
33
- return bigMath.mulDiv(sizeInUsd, expandDecimals(1, indexToken.decimals), sizeInTokens);
34
- }
@@ -1,2 +1,4 @@
1
+ export declare const MAX_REFERRAL_CODE_LENGTH = 20;
1
2
  import { Hash } from "viem";
2
3
  export declare function decodeReferralCode(hexCode?: Hash): string;
4
+ export declare function encodeReferralCode(code: string): `0x${string}`;
@@ -1,27 +1,29 @@
1
- // import { MAX_REFERRAL_CODE_LENGTH } from "lib/legacy";
2
- import { decodeAbiParameters } from "viem";
3
- import { hexToBytes } from "viem/utils";
1
+ export const MAX_REFERRAL_CODE_LENGTH = 20;
2
+ import { padHex, stringToHex, zeroHash } from "viem";
3
+ import { bytesToString, hexToBytes } from "viem/utils";
4
4
  export function decodeReferralCode(hexCode) {
5
- if (!hexCode || hexCode === "0x0000000000000000000000000000000000000000000000000000000000000000") {
5
+ if (!hexCode || hexCode === zeroHash) {
6
6
  return "";
7
7
  }
8
8
  try {
9
- const decoded = decodeAbiParameters([{ type: "bytes32" }], hexCode);
10
- return decoded[0];
9
+ const bytes = hexToBytes(hexCode);
10
+ if (bytes.length !== 32)
11
+ throw new Error();
12
+ return bytesToString(bytes).replace(/\0+$/, "");
11
13
  }
12
14
  catch (ex) {
13
15
  let code = "";
14
- const hexBytes = hexToBytes(hexCode);
16
+ const cleaned = hexCode.substring(2);
15
17
  for (let i = 0; i < 32; i++) {
16
- code += String.fromCharCode(hexBytes[i]);
18
+ code += String.fromCharCode(parseInt(cleaned.substring(i * 2, i * 2 + 2), 16));
17
19
  }
18
20
  return code.trim();
19
21
  }
20
22
  }
21
- // export function encodeReferralCode(code) {
22
- // let final = code.replace(/[^\w_]/g, ""); // replace everything other than numbers, string and underscor to ''
23
- // if (final.length > MAX_REFERRAL_CODE_LENGTH) {
24
- // return ethers.ZeroHash;
25
- // }
26
- // return ethers.encodeBytes32String(final);
27
- // }
23
+ export function encodeReferralCode(code) {
24
+ let final = code.replace(/[^\w_]/g, ""); // replace everything other than numbers, string and underscor to ''
25
+ if (final.length > MAX_REFERRAL_CODE_LENGTH) {
26
+ return zeroHash;
27
+ }
28
+ return padHex(stringToHex(final), { size: 32, dir: "right" });
29
+ }
@@ -1,5 +1,6 @@
1
1
  import { MarketInfo, MarketsInfoData } from "../types/markets";
2
2
  import { SwapPathStats, SwapStats } from "../types/trade";
3
+ export declare function getSwapCapacityUsd(marketInfo: MarketInfo, isLong: boolean): bigint;
3
4
  export declare function getSwapPathOutputAddresses(p: {
4
5
  marketsInfoData: MarketsInfoData;
5
6
  initialCollateralAddress: string;
@@ -1,8 +1,16 @@
1
1
  import { NATIVE_TOKEN_ADDRESS } from "../configs/tokens";
2
2
  import { getByKey } from "./objects";
3
3
  import { getAvailableUsdLiquidityForCollateral, getOppositeCollateral, getTokenPoolType } from "./markets";
4
- import { convertToTokenAmount, convertToUsd } from "./tokens";
4
+ import { convertToTokenAmount, convertToUsd, getMidPrice } from "./tokens";
5
5
  import { applySwapImpactWithCap, getPriceImpactForSwap, getSwapFee } from "./fees";
6
+ export function getSwapCapacityUsd(marketInfo, isLong) {
7
+ const poolAmount = isLong ? marketInfo.longPoolAmount : marketInfo.shortPoolAmount;
8
+ const maxPoolAmount = isLong ? marketInfo.maxLongPoolAmount : marketInfo.maxShortPoolAmount;
9
+ const capacityAmount = maxPoolAmount - poolAmount;
10
+ const token = isLong ? marketInfo.longToken : marketInfo.shortToken;
11
+ const capacityUsd = convertToUsd(capacityAmount, token.decimals, getMidPrice(token.prices));
12
+ return capacityUsd;
13
+ }
6
14
  export function getSwapPathOutputAddresses(p) {
7
15
  const { marketsInfoData, initialCollateralAddress, swapPath, wrappedNativeTokenAddress, shouldUnwrapNativeToken, isIncrease, } = p;
8
16
  if (swapPath.length === 0) {
@@ -176,6 +184,8 @@ export function getSwapStats(p) {
176
184
  usdOut = 0n;
177
185
  }
178
186
  amountOut = convertToTokenAmount(usdOut, tokenOut.decimals, priceOut);
187
+ const capacityUsd = getSwapCapacityUsd(marketInfo, getTokenPoolType(marketInfo, tokenInAddress) === "long");
188
+ const isOutCapacity = capacityUsd < usdInAfterFees;
179
189
  const liquidity = getAvailableUsdLiquidityForCollateral(marketInfo, getTokenPoolType(marketInfo, tokenOutAddress) === "long");
180
190
  const isOutLiquidity = liquidity < usdOut;
181
191
  return {
@@ -193,5 +203,6 @@ export function getSwapStats(p) {
193
203
  amountOut,
194
204
  usdOut,
195
205
  isOutLiquidity,
206
+ isOutCapacity,
196
207
  };
197
208
  }
@@ -65,13 +65,13 @@ export declare const arbitrumSdkConfig: {
65
65
  [x: `bytes6[${string}]`]: undefined;
66
66
  [x: `bytes3[${string}]`]: undefined;
67
67
  [x: `int[${string}]`]: undefined;
68
+ [x: `int56[${string}]`]: undefined;
68
69
  [x: `int40[${string}]`]: undefined;
69
70
  [x: `int32[${string}]`]: undefined;
70
71
  [x: `int24[${string}]`]: undefined;
71
72
  [x: `int16[${string}]`]: undefined;
72
73
  [x: `int8[${string}]`]: undefined;
73
74
  [x: `int48[${string}]`]: undefined;
74
- [x: `int56[${string}]`]: undefined;
75
75
  [x: `int64[${string}]`]: undefined;
76
76
  [x: `int72[${string}]`]: undefined;
77
77
  [x: `int80[${string}]`]: undefined;
@@ -98,10 +98,10 @@ export declare const arbitrumSdkConfig: {
98
98
  [x: `int248[${string}]`]: undefined;
99
99
  [x: `int256[${string}]`]: undefined;
100
100
  [x: `uint[${string}]`]: undefined;
101
+ [x: `uint56[${string}]`]: undefined;
101
102
  [x: `uint40[${string}]`]: undefined;
102
103
  [x: `uint24[${string}]`]: undefined;
103
104
  [x: `uint16[${string}]`]: undefined;
104
- [x: `uint56[${string}]`]: undefined;
105
105
  [x: `uint64[${string}]`]: undefined;
106
106
  [x: `uint72[${string}]`]: undefined;
107
107
  [x: `uint80[${string}]`]: undefined;
@@ -166,13 +166,13 @@ export declare const arbitrumSdkConfig: {
166
166
  bytes7?: undefined;
167
167
  bytes6?: undefined;
168
168
  bytes3?: undefined;
169
+ int56?: undefined;
169
170
  int40?: undefined;
170
171
  int32?: undefined;
171
172
  int24?: undefined;
172
173
  int16?: undefined;
173
174
  int8?: undefined;
174
175
  int48?: undefined;
175
- int56?: undefined;
176
176
  int64?: undefined;
177
177
  int72?: undefined;
178
178
  int80?: undefined;
@@ -198,10 +198,10 @@ export declare const arbitrumSdkConfig: {
198
198
  int240?: undefined;
199
199
  int248?: undefined;
200
200
  int256?: undefined;
201
+ uint56?: undefined;
201
202
  uint40?: undefined;
202
203
  uint24?: undefined;
203
204
  uint16?: undefined;
204
- uint56?: undefined;
205
205
  uint64?: undefined;
206
206
  uint72?: undefined;
207
207
  uint80?: undefined;
@@ -277,13 +277,13 @@ export declare const arbitrumSdkConfig: {
277
277
  nonce: TBlockTag extends "pending" ? null : `0x${string}`;
278
278
  hash: TBlockTag extends "pending" ? null : `0x${string}`;
279
279
  timestamp: bigint;
280
+ gasLimit: bigint;
280
281
  logsBloom: TBlockTag extends "pending" ? null : `0x${string}`;
281
282
  baseFeePerGas: bigint | null;
282
283
  blobGasUsed: bigint;
283
284
  difficulty: bigint;
284
285
  excessBlobGas: bigint;
285
286
  extraData: `0x${string}`;
286
- gasLimit: bigint;
287
287
  gasUsed: bigint;
288
288
  miner: `0x${string}`;
289
289
  mixHash: `0x${string}`;
@@ -10,6 +10,10 @@ export declare function convertToUsd(tokenAmount: bigint | undefined, tokenDecim
10
10
  export declare function getMidPrice(prices: TokenPrices): bigint;
11
11
  export declare function getIsEquivalentTokens(token1: Token, token2: Token): boolean;
12
12
  export declare function getTokenData(tokensData?: TokensData, address?: string, convertTo?: "wrapped" | "native"): import("../types/tokens").TokenData | undefined;
13
+ /**
14
+ * Even though its not a generic function, it return the same type as the input.
15
+ * If `TokenData` is passed, it returns `TokenData`, if `Token` is passed, it returns `Token`.
16
+ */
13
17
  export declare function getTokensRatioByAmounts(p: {
14
18
  fromToken: Token;
15
19
  toToken: Token;
@@ -52,6 +52,10 @@ export function getTokenData(tokensData, address, convertTo) {
52
52
  }
53
53
  return token;
54
54
  }
55
+ /**
56
+ * Even though its not a generic function, it return the same type as the input.
57
+ * If `TokenData` is passed, it returns `TokenData`, if `Token` is passed, it returns `Token`.
58
+ */
55
59
  export function getTokensRatioByAmounts(p) {
56
60
  const { fromToken, toToken, fromTokenAmount, toTokenAmount } = p;
57
61
  const adjustedFromAmount = (fromTokenAmount * PRECISION) / expandDecimals(1, fromToken.decimals);