@gmx-io/sdk 0.1.1 → 0.2.0
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/README.md +31 -5
- package/build/src/configs/batch.d.ts +2 -3
- package/build/src/configs/chains.d.ts +5 -1
- package/build/src/configs/chains.js +9 -2
- package/build/src/configs/contracts.d.ts +220 -1
- package/build/src/configs/contracts.js +67 -8
- package/build/src/configs/markets.js +70 -0
- package/build/src/configs/tokens.js +128 -5
- package/build/src/modules/accounts/accounts.spec.js +1 -0
- package/build/src/modules/markets/index.js +2 -8
- package/build/src/modules/markets/markets.spec.js +1 -0
- package/build/src/modules/markets/query-builders.d.ts +1 -1
- package/build/src/modules/markets/query-builders.js +1 -1
- package/build/src/modules/markets/types.d.ts +4 -1
- package/build/src/modules/orders/orders.spec.js +1 -0
- package/build/src/modules/orders/utils.js +1 -1
- package/build/src/modules/positions/positions.d.ts +2 -2
- package/build/src/modules/positions/positions.js +21 -8
- package/build/src/modules/positions/positions.spec.js +2 -1
- package/build/src/modules/tokens/tokens.spec.js +1 -0
- package/build/src/modules/trades/trades.spec.js +1 -0
- package/build/src/modules/utils/utils.d.ts +1 -1
- package/build/src/modules/utils/utils.js +1 -1
- package/build/src/prebuilt/hashedKinkModelMarketRatesKeys.json +80 -0
- package/build/src/prebuilt/hashedMarketConfigKeys.json +500 -0
- package/build/src/prebuilt/hashedMarketValuesKeys.json +150 -0
- package/build/src/types/common.d.ts +1 -0
- package/build/src/types/fees.d.ts +2 -1
- package/build/src/types/markets.d.ts +7 -19
- package/build/src/types/orders.d.ts +3 -5
- package/build/src/types/positions.d.ts +11 -4
- package/build/src/types/trade.d.ts +1 -0
- package/build/src/types/tradeHistory.d.ts +4 -1
- package/build/src/utils/__tests__/LruCache.spec.d.ts +1 -0
- package/build/src/utils/__tests__/LruCache.spec.js +43 -0
- package/build/src/utils/__tests__/bigmath.spec.d.ts +1 -0
- package/build/src/utils/__tests__/bigmath.spec.js +38 -0
- package/build/src/utils/__tests__/fees.spec.d.ts +1 -0
- package/build/src/utils/__tests__/fees.spec.js +62 -0
- package/build/src/utils/__tests__/hash.spec.d.ts +1 -0
- package/build/src/utils/__tests__/hash.spec.js +41 -0
- package/build/src/utils/__tests__/markets.spec.d.ts +1 -0
- package/build/src/utils/__tests__/markets.spec.js +265 -0
- package/build/src/utils/__tests__/numbers.spec.d.ts +1 -0
- package/build/src/utils/__tests__/numbers.spec.js +81 -0
- package/build/src/utils/__tests__/objects.spec.d.ts +1 -0
- package/build/src/utils/__tests__/objects.spec.js +59 -0
- package/build/src/utils/__tests__/positions.spec.d.ts +1 -0
- package/build/src/utils/__tests__/positions.spec.js +178 -0
- package/build/src/utils/__tests__/prices.spec.d.ts +1 -0
- package/build/src/utils/__tests__/prices.spec.js +59 -0
- package/build/src/utils/__tests__/referrals.spec.d.ts +1 -0
- package/build/src/utils/__tests__/referrals.spec.js +18 -0
- package/build/src/utils/__tests__/subgraph.spec.d.ts +1 -0
- package/build/src/utils/__tests__/subgraph.spec.js +163 -0
- package/build/src/utils/__tests__/tokens.spec.d.ts +1 -0
- package/build/src/utils/__tests__/tokens.spec.js +83 -0
- package/build/src/utils/__tests__/trade.spec.d.ts +1 -0
- package/build/src/utils/__tests__/trade.spec.js +36 -0
- package/build/src/utils/bigmath.d.ts +1 -1
- package/build/src/utils/bigmath.js +6 -1
- package/build/src/utils/callContract.js +5 -1
- package/build/src/utils/common.d.ts +6 -1
- package/build/src/utils/common.js +9 -1
- package/build/src/utils/contracts.d.ts +4 -0
- package/build/src/utils/contracts.js +2 -0
- package/build/src/utils/fees/executionFee.d.ts +22 -21
- package/build/src/utils/fees/executionFee.js +26 -26
- package/build/src/utils/fees/executionFee.spec.d.ts +1 -0
- package/build/src/utils/fees/executionFee.spec.js +64 -0
- package/build/src/utils/fees/index.d.ts +2 -0
- package/build/src/utils/fees/index.js +21 -9
- package/build/src/utils/markets.d.ts +9 -2
- package/build/src/utils/markets.js +33 -10
- package/build/src/utils/multicall.js +4 -3
- package/build/src/utils/numbers.d.ts +2 -0
- package/build/src/utils/numbers.js +28 -0
- package/build/src/utils/orders.js +0 -8
- package/build/src/utils/positions.js +3 -3
- package/build/src/utils/prices.d.ts +1 -6
- package/build/src/utils/prices.js +0 -9
- package/build/src/utils/referrals.d.ts +2 -0
- package/build/src/utils/referrals.js +17 -15
- package/build/src/utils/swapStats.d.ts +1 -0
- package/build/src/utils/swapStats.js +12 -1
- package/build/src/utils/testUtil.d.ts +14314 -326
- package/build/src/utils/tokens.d.ts +4 -0
- package/build/src/utils/tokens.js +4 -0
- package/build/tsconfig.tsbuildinfo +1 -1
- package/package.json +6 -29
- package/build/src/types/amounts.d.ts +0 -223
- package/build/src/types/amounts.js +0 -17
- package/build/src/types/syntheticsEvents.d.ts +0 -88
- /package/build/src/types/{syntheticsEvents.js → common.js} +0 -0
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { ContractMarketPrices, Market, MarketInfo } from "../types/markets";
|
|
2
|
-
import { Token, TokensData } from "../types/tokens";
|
|
2
|
+
import { Token, TokenPrices, TokensData } from "../types/tokens";
|
|
3
3
|
export declare function getMarketFullName(p: {
|
|
4
4
|
longToken: Token;
|
|
5
5
|
shortToken: Token;
|
|
@@ -18,6 +18,9 @@ export declare function getMarketPoolName(p: {
|
|
|
18
18
|
shortToken: Token;
|
|
19
19
|
}): string;
|
|
20
20
|
export declare function getContractMarketPrices(tokensData: TokensData, market: Market): ContractMarketPrices | undefined;
|
|
21
|
+
/**
|
|
22
|
+
* Apart from usual cases, returns `long` for single token backed markets.
|
|
23
|
+
*/
|
|
21
24
|
export declare function getTokenPoolType(marketInfo: {
|
|
22
25
|
longToken: Token;
|
|
23
26
|
shortToken: Token;
|
|
@@ -26,8 +29,8 @@ export declare function getPoolUsdWithoutPnl(marketInfo: MarketInfo, isLong: boo
|
|
|
26
29
|
export declare function getCappedPoolPnl(p: {
|
|
27
30
|
marketInfo: MarketInfo;
|
|
28
31
|
poolUsd: bigint;
|
|
32
|
+
poolPnl: bigint;
|
|
29
33
|
isLong: boolean;
|
|
30
|
-
maximize: boolean;
|
|
31
34
|
}): bigint;
|
|
32
35
|
export declare function getMaxLeverageByMinCollateralFactor(minCollateralFactor: bigint | undefined): number;
|
|
33
36
|
export declare function getMaxAllowedLeverageByMinCollateralFactor(minCollateralFactor: bigint | undefined): number;
|
|
@@ -38,3 +41,7 @@ export declare function getMarketDivisor({ longTokenAddress, shortTokenAddress,
|
|
|
38
41
|
longTokenAddress: string;
|
|
39
42
|
shortTokenAddress: string;
|
|
40
43
|
}): 1n | 2n;
|
|
44
|
+
export declare function getMarketPnl(marketInfo: MarketInfo, isLong: boolean, forMaxPoolValue: boolean): bigint;
|
|
45
|
+
export declare function getOpenInterestUsd(marketInfo: MarketInfo, isLong: boolean): bigint;
|
|
46
|
+
export declare function getOpenInterestInTokens(marketInfo: MarketInfo, isLong: boolean): bigint;
|
|
47
|
+
export declare function getPriceForPnl(prices: TokenPrices, isLong: boolean, maximize: boolean): bigint;
|
|
@@ -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
|
-
|
|
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,
|
|
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
|
+
}
|
|
@@ -52,8 +52,6 @@ export class Multicall {
|
|
|
52
52
|
});
|
|
53
53
|
});
|
|
54
54
|
});
|
|
55
|
-
// const providerUrl = rpcUrl;
|
|
56
|
-
// const rpcProviderName = getProviderNameFromUrl(providerUrl);
|
|
57
55
|
const processResponse = (response) => {
|
|
58
56
|
const multicallResult = {
|
|
59
57
|
success: true,
|
|
@@ -94,14 +92,17 @@ export class Multicall {
|
|
|
94
92
|
});
|
|
95
93
|
return multicallResult;
|
|
96
94
|
};
|
|
95
|
+
const timeoutController = new AbortController();
|
|
97
96
|
const result = await Promise.race([
|
|
98
97
|
client.multicall({ contracts: encodedPayload }),
|
|
99
|
-
sleep(maxTimeout).then(() => Promise.reject(new Error("multicall timeout"))),
|
|
98
|
+
sleep(maxTimeout, timeoutController.signal).then(() => Promise.reject(new Error("multicall timeout"))),
|
|
100
99
|
])
|
|
101
100
|
.then((response) => {
|
|
101
|
+
timeoutController.abort();
|
|
102
102
|
return processResponse(response);
|
|
103
103
|
})
|
|
104
104
|
.catch((_viemError) => {
|
|
105
|
+
timeoutController.abort();
|
|
105
106
|
const e = new Error(_viemError.message.slice(0, 150));
|
|
106
107
|
/* eslint-disable-next-line */
|
|
107
108
|
console.error(e);
|
|
@@ -7,3 +7,5 @@ export declare function basisPointsToFloat(basisPoints: bigint): bigint;
|
|
|
7
7
|
export declare function getBasisPoints(numerator: bigint, denominator: bigint, shouldRoundUp?: boolean): bigint;
|
|
8
8
|
export declare function roundUpMagnitudeDivision(a: bigint, b: bigint): bigint;
|
|
9
9
|
export declare function applyFactor(value: bigint, factor: bigint): bigint;
|
|
10
|
+
export declare function numberToBigint(value: number, decimals: number): bigint;
|
|
11
|
+
export declare function bigintToNumber(value: bigint, decimals: number): number;
|
|
@@ -28,3 +28,31 @@ export function roundUpMagnitudeDivision(a, b) {
|
|
|
28
28
|
export function applyFactor(value, factor) {
|
|
29
29
|
return (value * factor) / PRECISION;
|
|
30
30
|
}
|
|
31
|
+
export function numberToBigint(value, decimals) {
|
|
32
|
+
const negative = value < 0;
|
|
33
|
+
if (negative)
|
|
34
|
+
value *= -1;
|
|
35
|
+
const int = Math.trunc(value);
|
|
36
|
+
let frac = value - int;
|
|
37
|
+
let res = BigInt(int);
|
|
38
|
+
for (let i = 0; i < decimals; i++) {
|
|
39
|
+
res *= 10n;
|
|
40
|
+
if (frac !== 0) {
|
|
41
|
+
frac *= 10;
|
|
42
|
+
const fracInt = Math.trunc(frac);
|
|
43
|
+
res += BigInt(fracInt);
|
|
44
|
+
frac -= fracInt;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
return negative ? -res : res;
|
|
48
|
+
}
|
|
49
|
+
export function bigintToNumber(value, decimals) {
|
|
50
|
+
const negative = value < 0;
|
|
51
|
+
if (negative)
|
|
52
|
+
value *= -1n;
|
|
53
|
+
const precision = 10n ** BigInt(decimals);
|
|
54
|
+
const int = value / precision;
|
|
55
|
+
const frac = value % precision;
|
|
56
|
+
const num = parseFloat(`${int}.${frac.toString().padStart(decimals, "0")}`);
|
|
57
|
+
return negative ? -num : num;
|
|
58
|
+
}
|
|
@@ -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 =
|
|
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 {
|
|
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,27 +1,29 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
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 ===
|
|
5
|
+
if (!hexCode || hexCode === zeroHash) {
|
|
6
6
|
return "";
|
|
7
7
|
}
|
|
8
8
|
try {
|
|
9
|
-
const
|
|
10
|
-
|
|
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
|
|
16
|
+
const cleaned = hexCode.substring(2);
|
|
15
17
|
for (let i = 0; i < 32; i++) {
|
|
16
|
-
code += String.fromCharCode(
|
|
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
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
}
|