@gmx-io/sdk 2.9.1 → 2.9.3
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/build/cjs/src/clients/v2/index.js +61 -41
- package/build/cjs/src/clients/v2/solana.spec.js +188 -0
- package/build/cjs/src/configs/api.js +4 -0
- package/build/cjs/src/utils/fees/index.js +7 -1
- package/build/cjs/src/utils/fees/priceImpact.js +118 -18
- package/build/cjs/src/utils/gmsol/__tests__/fixtures/nativeMath.json +3171 -0
- package/build/cjs/src/utils/gmsol/__tests__/nativeMath.spec.js +213 -0
- package/build/cjs/src/utils/gmsol/constants.js +12 -0
- package/build/cjs/src/utils/gmsol/fees.js +19 -0
- package/build/cjs/src/utils/gmsol/index.js +66 -0
- package/build/cjs/src/utils/gmsol/markets.js +206 -0
- package/build/cjs/src/utils/gmsol/numbers.js +230 -0
- package/build/cjs/src/utils/gmsol/positions.js +152 -0
- package/build/cjs/src/utils/gmsol/types.js +2 -0
- package/build/cjs/src/utils/http/http.js +3 -4
- package/build/cjs/src/utils/http/httpFallback.js +11 -1
- package/build/cjs/src/utils/http/solana.js +17 -0
- package/build/cjs/src/utils/http/validation.js +21 -1
- package/build/cjs/src/utils/http/validation.spec.js +45 -0
- package/build/cjs/src/utils/markets/__tests__/api.spec.js +53 -0
- package/build/cjs/src/utils/markets/api.js +5 -0
- package/build/cjs/src/utils/markets/utils.js +25 -9
- package/build/cjs/src/utils/orders/api.js +3 -0
- package/build/cjs/src/utils/positions/__tests__/liquidationPrice.spec.js +64 -0
- package/build/cjs/src/utils/positions/api.js +3 -0
- package/build/cjs/src/utils/positions/utils.js +88 -19
- package/build/cjs/src/utils/prices/utils.js +69 -14
- package/build/cjs/src/utils/referrals/api.js +41 -0
- package/build/cjs/src/utils/swap/swapRouting.js +4 -0
- package/build/cjs/src/utils/swap/swapStats.js +6 -1
- package/build/cjs/src/utils/trade/__tests__/gmsolTradeMath.spec.js +1012 -0
- package/build/cjs/src/utils/trade/decrease.js +266 -56
- package/build/cjs/src/utils/trade/decreaseMarginCheck.js +23 -4
- package/build/cjs/src/utils/trade/increase.js +86 -28
- package/build/cjs/src/utils/trade/increaseMarginCheck.js +51 -9
- package/build/cjs/src/utils/trade/twapIncreaseMarginCheck.js +8 -3
- package/build/esm/src/clients/v2/index.js +39 -19
- package/build/esm/src/clients/v2/solana.spec.js +160 -0
- package/build/esm/src/configs/api.js +5 -1
- package/build/esm/src/utils/fees/index.js +7 -1
- package/build/esm/src/utils/fees/priceImpact.js +111 -12
- package/build/esm/src/utils/gmsol/__tests__/fixtures/nativeMath.json +3171 -0
- package/build/esm/src/utils/gmsol/__tests__/nativeMath.spec.js +208 -0
- package/build/esm/src/utils/gmsol/constants.js +9 -0
- package/build/esm/src/utils/gmsol/fees.js +15 -0
- package/build/esm/src/utils/gmsol/index.js +46 -0
- package/build/esm/src/utils/gmsol/markets.js +193 -0
- package/build/esm/src/utils/gmsol/numbers.js +226 -0
- package/build/esm/src/utils/gmsol/positions.js +148 -0
- package/build/esm/src/utils/gmsol/types.js +1 -0
- package/build/esm/src/utils/http/http.js +3 -4
- package/build/esm/src/utils/http/httpFallback.js +11 -1
- package/build/esm/src/utils/http/solana.js +12 -0
- package/build/esm/src/utils/http/validation.js +18 -0
- package/build/esm/src/utils/http/validation.spec.js +43 -0
- package/build/esm/src/utils/markets/__tests__/api.spec.js +53 -0
- package/build/esm/src/utils/markets/api.js +6 -1
- package/build/esm/src/utils/markets/utils.js +23 -8
- package/build/esm/src/utils/orders/api.js +3 -0
- package/build/esm/src/utils/positions/__tests__/liquidationPrice.spec.js +62 -0
- package/build/esm/src/utils/positions/api.js +3 -0
- package/build/esm/src/utils/positions/utils.js +89 -20
- package/build/esm/src/utils/prices/utils.js +70 -15
- package/build/esm/src/utils/referrals/api.js +36 -0
- package/build/esm/src/utils/swap/swapRouting.js +4 -0
- package/build/esm/src/utils/swap/swapStats.js +7 -2
- package/build/esm/src/utils/trade/__tests__/gmsolTradeMath.spec.js +1010 -0
- package/build/esm/src/utils/trade/decrease.js +266 -56
- package/build/esm/src/utils/trade/decreaseMarginCheck.js +23 -4
- package/build/esm/src/utils/trade/increase.js +86 -28
- package/build/esm/src/utils/trade/increaseMarginCheck.js +53 -11
- package/build/esm/src/utils/trade/twapIncreaseMarginCheck.js +8 -3
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/types/src/clients/v2/index.d.ts +21 -12
- package/build/types/src/clients/v2/solana.spec.d.ts +1 -0
- package/build/types/src/utils/fees/index.d.ts +1 -1
- package/build/types/src/utils/fees/priceImpact.d.ts +7 -0
- package/build/types/src/utils/gmsol/__tests__/nativeMath.spec.d.ts +1 -0
- package/build/types/src/utils/gmsol/constants.d.ts +9 -0
- package/build/types/src/utils/gmsol/fees.d.ts +6 -0
- package/build/types/src/utils/gmsol/index.d.ts +38 -0
- package/build/types/src/utils/gmsol/markets.d.ts +49 -0
- package/build/types/src/utils/gmsol/numbers.d.ts +11 -0
- package/build/types/src/utils/gmsol/positions.d.ts +7 -0
- package/build/types/src/utils/gmsol/types.d.ts +105 -0
- package/build/types/src/utils/http/http.d.ts +2 -5
- package/build/types/src/utils/http/httpFallback.d.ts +2 -5
- package/build/types/src/utils/http/solana.d.ts +4 -0
- package/build/types/src/utils/http/types.d.ts +17 -4
- package/build/types/src/utils/http/validation.d.ts +8 -0
- package/build/types/src/utils/http/validation.spec.d.ts +1 -0
- package/build/types/src/utils/markets/hashKeys.d.ts +1 -1
- package/build/types/src/utils/markets/types.d.ts +3 -2
- package/build/types/src/utils/markets/utils.d.ts +6 -2
- package/build/types/src/utils/orders/api.d.ts +2 -2
- package/build/types/src/utils/positions/__tests__/liquidationPrice.spec.d.ts +1 -0
- package/build/types/src/utils/positions/api.d.ts +2 -2
- package/build/types/src/utils/positions/types.d.ts +1 -0
- package/build/types/src/utils/positions/utils.d.ts +6 -1
- package/build/types/src/utils/prices/types.d.ts +16 -0
- package/build/types/src/utils/prices/utils.d.ts +3 -11
- package/build/types/src/utils/referrals/api.d.ts +8 -0
- package/build/types/src/utils/referrals/types.d.ts +5 -0
- package/build/types/src/utils/rpc/createViemRpc.d.ts +25 -3
- package/build/types/src/utils/trade/__tests__/gmsolTradeMath.spec.d.ts +1 -0
- package/build/types/src/utils/trade/decrease.d.ts +5 -0
- package/build/types/src/utils/trade/decreaseMarginCheck.d.ts +2 -0
- package/build/types/src/utils/trade/increase.d.ts +2 -0
- package/build/types/src/utils/trade/increaseMarginCheck.d.ts +2 -0
- package/build/types/src/utils/trade/twapIncreaseMarginCheck.d.ts +1 -0
- package/build/types/src/utils/trade/types.d.ts +6 -0
- package/package.json +10 -1
|
@@ -0,0 +1,213 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const vitest_1 = require("vitest");
|
|
7
|
+
const constants_1 = require("../constants");
|
|
8
|
+
const fees_1 = require("../fees");
|
|
9
|
+
const markets_1 = require("../markets");
|
|
10
|
+
const numbers_1 = require("../numbers");
|
|
11
|
+
const positions_1 = require("../positions");
|
|
12
|
+
// mainnet market and position states decoded from the API fixtures in provenance, with their recorded native results
|
|
13
|
+
const nativeMath_json_1 = __importDefault(require("./fixtures/nativeMath.json"));
|
|
14
|
+
const UNIT = constants_1.GMSOL_PRECISION;
|
|
15
|
+
const IDENTIFIER_KEYS = [
|
|
16
|
+
"name",
|
|
17
|
+
"marketTokenMint",
|
|
18
|
+
"longTokenMint",
|
|
19
|
+
"shortTokenMint",
|
|
20
|
+
"store",
|
|
21
|
+
"owner",
|
|
22
|
+
"marketToken",
|
|
23
|
+
"collateralToken",
|
|
24
|
+
];
|
|
25
|
+
// get_market_token_price per entry, in the order of each market view's pricesExpected
|
|
26
|
+
const PRICE_VIEWS = [
|
|
27
|
+
["maxForTrader", true],
|
|
28
|
+
["maxForTrader", false],
|
|
29
|
+
["maxAfterDeposit", true],
|
|
30
|
+
["maxAfterWithdrawal", false],
|
|
31
|
+
];
|
|
32
|
+
// the program prices one whole GM token, which has 9 decimals, and returns $1 while the supply is 0
|
|
33
|
+
const GM_TOKEN_UNIT = 10n ** 9n;
|
|
34
|
+
const positionMarkets = new Map(nativeMath_json_1.default.positions.markets.map((entry) => [entry.name, entry]));
|
|
35
|
+
function parseBigints(value, key = "") {
|
|
36
|
+
if (Array.isArray(value)) {
|
|
37
|
+
return value.map((item) => parseBigints(item));
|
|
38
|
+
}
|
|
39
|
+
if (value !== null && typeof value === "object") {
|
|
40
|
+
return Object.fromEntries(Object.entries(value).map(([field, item]) => [field, parseBigints(item, field)]));
|
|
41
|
+
}
|
|
42
|
+
return (typeof value === "string" && !IDENTIFIER_KEYS.includes(key) ? BigInt(value) : value);
|
|
43
|
+
}
|
|
44
|
+
function getGmPrice(value, supply) {
|
|
45
|
+
return supply === 0n ? UNIT : numbers_1.gmsolMath.mulDiv(value, GM_TOKEN_UNIT, supply, "gm price");
|
|
46
|
+
}
|
|
47
|
+
(0, vitest_1.describe)("gmsol market math matches the store program", () => {
|
|
48
|
+
(0, vitest_1.it)("matches native borrowing and funding with fractional exponents FESOL-109", () => {
|
|
49
|
+
const base = parseBigints(nativeMath_json_1.default.marketViews[0].market);
|
|
50
|
+
const prices = {
|
|
51
|
+
index: { min: UNIT / 10n ** 9n, max: UNIT / 10n ** 9n },
|
|
52
|
+
long: { min: UNIT / 10n ** 6n, max: UNIT / 10n ** 6n },
|
|
53
|
+
short: { min: UNIT / 10n ** 6n, max: UNIT / 10n ** 6n },
|
|
54
|
+
};
|
|
55
|
+
for (const [exponent, borrowing, funding] of [
|
|
56
|
+
[UNIT / 2n, 2828427123200n, 33333333316666n],
|
|
57
|
+
[(UNIT * 3n) / 2n, 565685410479800n, 3333333328793333n],
|
|
58
|
+
[(UNIT * 7n) / 4n, 2127317204904000n, 10540925175750000n],
|
|
59
|
+
[UNIT, 40000000000000n, 333333333333333n],
|
|
60
|
+
[UNIT * 2n, 8000000000000000n, 33333333333333333n],
|
|
61
|
+
]) {
|
|
62
|
+
const market = {
|
|
63
|
+
...base,
|
|
64
|
+
primary: [500000000000n, 500000000000n],
|
|
65
|
+
openInterest: [100n * UNIT, 200n * UNIT],
|
|
66
|
+
openInterestInTokens: [100000000000n, 200000000000n],
|
|
67
|
+
skipBorrowingFeeForSmallerSide: true,
|
|
68
|
+
borrowingFeeOptimalUsageFactor: [0n, 0n],
|
|
69
|
+
borrowingFeeExponent: [exponent, exponent],
|
|
70
|
+
borrowingFeeFactor: [UNIT / 1000n, UNIT / 1000n],
|
|
71
|
+
funding: {
|
|
72
|
+
...base.funding,
|
|
73
|
+
exponent,
|
|
74
|
+
factor: UNIT / 100000n,
|
|
75
|
+
increaseFactorPerSecond: 0n,
|
|
76
|
+
maxFactorPerSecond: UNIT,
|
|
77
|
+
},
|
|
78
|
+
};
|
|
79
|
+
(0, vitest_1.expect)((0, markets_1.getGmsolBorrowingFactorPerSecond)({ market, prices, side: constants_1.GMSOL_LONG })).toBe(0n);
|
|
80
|
+
(0, vitest_1.expect)((0, markets_1.getGmsolBorrowingFactorPerSecond)({ market, prices, side: constants_1.GMSOL_SHORT })).toBe(borrowing);
|
|
81
|
+
(0, vitest_1.expect)((0, markets_1.getGmsolNextFunding)(market, 0)).toEqual({ factor: funding, longsPayShorts: false });
|
|
82
|
+
}
|
|
83
|
+
});
|
|
84
|
+
vitest_1.it.each(nativeMath_json_1.default.marketViews)("$name", (view) => {
|
|
85
|
+
const market = parseBigints(view.market);
|
|
86
|
+
const prices = parseBigints(view.prices);
|
|
87
|
+
// get_market_status with both maximize flags true, then both false
|
|
88
|
+
for (const { maximize, borrowing, pnl, reserved, poolWithoutPnl } of view.status) {
|
|
89
|
+
for (const side of constants_1.GMSOL_SIDES) {
|
|
90
|
+
(0, vitest_1.expect)((0, markets_1.getGmsolBorrowingFactorPerSecond)({ market, prices, side })).toBe(BigInt(borrowing[side]));
|
|
91
|
+
(0, vitest_1.expect)((0, markets_1.getGmsolPendingPnl)({ market, indexPrice: prices.index, side, maximize })).toBe(BigInt(pnl[side]));
|
|
92
|
+
(0, vitest_1.expect)((0, markets_1.getGmsolReservedValue)({ market, prices, side })).toBe(BigInt(reserved[side]));
|
|
93
|
+
(0, vitest_1.expect)((0, markets_1.getGmsolPoolValueWithoutPnl)({ market, prices, side, maximize })).toBe(BigInt(poolWithoutPnl[side]));
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
PRICE_VIEWS.forEach(([pnlFactorKind, maximize], i) => {
|
|
97
|
+
const poolValue = (0, markets_1.getGmsolPoolValue)({ market, prices, now: view.now, pnlFactorKind, maximize });
|
|
98
|
+
(0, vitest_1.expect)(getGmPrice(poolValue, BigInt(view.supply))).toBe(BigInt(view.pricesExpected[i]));
|
|
99
|
+
});
|
|
100
|
+
// the view returns the stored funding factor, so the next one is checked against the SDK
|
|
101
|
+
const long = BigInt(view.sdkNextFunding.long);
|
|
102
|
+
const short = BigInt(view.sdkNextFunding.short);
|
|
103
|
+
const funding = (0, markets_1.getGmsolNextFunding)(market, view.now);
|
|
104
|
+
(0, vitest_1.expect)(funding.factor).toBe(long > 0n ? long : short > 0n ? short : 0n);
|
|
105
|
+
if (funding.factor > 0n) {
|
|
106
|
+
(0, vitest_1.expect)(funding.longsPayShorts).toBe(long > 0n);
|
|
107
|
+
}
|
|
108
|
+
});
|
|
109
|
+
});
|
|
110
|
+
(0, vitest_1.describe)("getGmsolMarketStatus matches the gmsol SDK", () => {
|
|
111
|
+
vitest_1.it.each([
|
|
112
|
+
["SOL/USD[USDC-USDC]", "longs pay"],
|
|
113
|
+
["WTI/USD[USDC-USDC]", "shorts pay, the factor at its max"],
|
|
114
|
+
["LINK/USD[WSOL-USDC]", "shorts pay, liquidity capped by max open interest"],
|
|
115
|
+
["ETH/USD[WETH-WETH]", "a skew under $1 counts as none, so the min factor applies"],
|
|
116
|
+
["ASTER/USD[USDT-USDT]", "no open interest, so no funding"],
|
|
117
|
+
["NATGAS/USD[USDC-USDC]", "reserved value above the max reserve leaves no long liquidity"],
|
|
118
|
+
["AAPL/USD[USDC-USDC]", "closed stock session"],
|
|
119
|
+
])("%s: %s", (name) => {
|
|
120
|
+
const { market, prices, expected } = nativeMath_json_1.default.marketStatus.markets.find((entry) => entry.name === name);
|
|
121
|
+
const status = (0, markets_1.getGmsolMarketStatus)({
|
|
122
|
+
market: parseBigints(market),
|
|
123
|
+
prices: parseBigints(prices),
|
|
124
|
+
now: nativeMath_json_1.default.marketStatus.now,
|
|
125
|
+
});
|
|
126
|
+
// the SDK gives each side's rate, positive for the side that pays
|
|
127
|
+
const long = BigInt(expected.fundingRatePerSecondForLong);
|
|
128
|
+
const short = BigInt(expected.fundingRatePerSecondForShort);
|
|
129
|
+
(0, vitest_1.expect)(status.fundingFactorPerSecond).toBe(long > 0n ? long : short > 0n ? short : 0n);
|
|
130
|
+
if (status.fundingFactorPerSecond > 0n) {
|
|
131
|
+
(0, vitest_1.expect)(status.longsPayShorts).toBe(long > 0n);
|
|
132
|
+
}
|
|
133
|
+
(0, vitest_1.expect)(status.liquidityForLong).toBe(BigInt(expected.liquidityForLong));
|
|
134
|
+
(0, vitest_1.expect)(status.liquidityForShort).toBe(BigInt(expected.liquidityForShort));
|
|
135
|
+
});
|
|
136
|
+
});
|
|
137
|
+
(0, vitest_1.describe)("getGmsolPendingPositionFees matches gmsol", () => {
|
|
138
|
+
vitest_1.it.each(nativeMath_json_1.default.positions.positions)("$picked", ({ market, position, expected }) => {
|
|
139
|
+
const { market: state, prices } = positionMarkets.get(market);
|
|
140
|
+
const fees = (0, positions_1.getGmsolPendingPositionFees)({
|
|
141
|
+
market: parseBigints(state),
|
|
142
|
+
prices: parseBigints(prices),
|
|
143
|
+
now: nativeMath_json_1.default.positions.now,
|
|
144
|
+
position: parseBigints(position),
|
|
145
|
+
});
|
|
146
|
+
(0, vitest_1.expect)(fees).toEqual(parseBigints(expected));
|
|
147
|
+
});
|
|
148
|
+
});
|
|
149
|
+
function getBasisPointFactor(value) {
|
|
150
|
+
return (constants_1.GMSOL_PRECISION / 10000n) * value;
|
|
151
|
+
}
|
|
152
|
+
function createDiscountConfig(byRank, referredUser = 0n) {
|
|
153
|
+
return { maxRank: 9, byRank: Array.from({ length: 16 }, (_, rank) => byRank[rank] ?? 0n), referredUser };
|
|
154
|
+
}
|
|
155
|
+
(0, vitest_1.describe)("gmsol order fee discount", () => {
|
|
156
|
+
// the cases of gmsol's own tests in crates/programs/src/utils/store.rs at 8af1e70c
|
|
157
|
+
vitest_1.it.each([
|
|
158
|
+
{ name: "rank 0", store: createDiscountConfig({}), rank: 0, referred: false, factor: 0n },
|
|
159
|
+
{
|
|
160
|
+
name: "rank 1",
|
|
161
|
+
store: createDiscountConfig({ 1: getBasisPointFactor(250n) }),
|
|
162
|
+
rank: 1,
|
|
163
|
+
referred: false,
|
|
164
|
+
factor: getBasisPointFactor(250n),
|
|
165
|
+
},
|
|
166
|
+
{
|
|
167
|
+
name: "referred at rank 0",
|
|
168
|
+
store: createDiscountConfig({}, getBasisPointFactor(1000n)),
|
|
169
|
+
rank: 0,
|
|
170
|
+
referred: true,
|
|
171
|
+
factor: getBasisPointFactor(1000n),
|
|
172
|
+
},
|
|
173
|
+
{
|
|
174
|
+
name: "referred at rank 1",
|
|
175
|
+
store: createDiscountConfig({ 1: getBasisPointFactor(250n) }, getBasisPointFactor(1000n)),
|
|
176
|
+
rank: 1,
|
|
177
|
+
referred: true,
|
|
178
|
+
factor: getBasisPointFactor(1225n),
|
|
179
|
+
},
|
|
180
|
+
{
|
|
181
|
+
name: "max rank",
|
|
182
|
+
store: createDiscountConfig({ 9: getBasisPointFactor(2250n) }),
|
|
183
|
+
rank: 9,
|
|
184
|
+
referred: false,
|
|
185
|
+
factor: getBasisPointFactor(2250n),
|
|
186
|
+
},
|
|
187
|
+
{
|
|
188
|
+
name: "referred at rank 5",
|
|
189
|
+
store: createDiscountConfig({ 5: getBasisPointFactor(1250n) }, getBasisPointFactor(1000n)),
|
|
190
|
+
rank: 5,
|
|
191
|
+
referred: true,
|
|
192
|
+
factor: getBasisPointFactor(2125n),
|
|
193
|
+
},
|
|
194
|
+
{
|
|
195
|
+
name: "high discounts",
|
|
196
|
+
store: createDiscountConfig({ 0: getBasisPointFactor(5000n) }, getBasisPointFactor(4000n)),
|
|
197
|
+
rank: 0,
|
|
198
|
+
referred: true,
|
|
199
|
+
factor: getBasisPointFactor(7000n),
|
|
200
|
+
},
|
|
201
|
+
])("$name", ({ store, rank, referred, factor }) => {
|
|
202
|
+
(0, vitest_1.expect)((0, fees_1.getGmsolOrderFeeDiscountFactor)({ config: store, rank, isReferred: referred })).toBe(factor);
|
|
203
|
+
});
|
|
204
|
+
(0, vitest_1.it)("rejects a rank above the max rank", () => {
|
|
205
|
+
(0, vitest_1.expect)(() => (0, fees_1.getGmsolOrderFeeDiscountFactor)({ config: createDiscountConfig({}), rank: 10, isReferred: false })).toThrow("exceeds the max rank");
|
|
206
|
+
});
|
|
207
|
+
(0, vitest_1.it)("matches the native Store discounts", () => {
|
|
208
|
+
const config = parseBigints(nativeMath_json_1.default.feeDiscount.config);
|
|
209
|
+
for (const { rank, referred, factor } of nativeMath_json_1.default.feeDiscount.expected) {
|
|
210
|
+
(0, vitest_1.expect)((0, fees_1.getGmsolOrderFeeDiscountFactor)({ config, rank, isReferred: referred })).toBe(BigInt(factor));
|
|
211
|
+
}
|
|
212
|
+
});
|
|
213
|
+
});
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.GMSOL_SIDES = exports.GMSOL_SHORT = exports.GMSOL_LONG = exports.GMSOL_MAX_IMPACT_EXPONENT = exports.GMSOL_MAX_SIGNED = exports.GMSOL_MAX_UNSIGNED = exports.GMSOL_PRECISION_SCALE = exports.GMSOL_PRECISION = void 0;
|
|
4
|
+
const numbers_1 = require("../numbers");
|
|
5
|
+
exports.GMSOL_PRECISION = 10n ** 20n;
|
|
6
|
+
exports.GMSOL_PRECISION_SCALE = numbers_1.PRECISION / exports.GMSOL_PRECISION;
|
|
7
|
+
exports.GMSOL_MAX_UNSIGNED = (1n << 128n) - 1n;
|
|
8
|
+
exports.GMSOL_MAX_SIGNED = (1n << 127n) - 1n;
|
|
9
|
+
exports.GMSOL_MAX_IMPACT_EXPONENT = 128n * numbers_1.PRECISION;
|
|
10
|
+
exports.GMSOL_LONG = 0;
|
|
11
|
+
exports.GMSOL_SHORT = 1;
|
|
12
|
+
exports.GMSOL_SIDES = [exports.GMSOL_LONG, exports.GMSOL_SHORT];
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getGmsolOrderFeeDiscountFactor = void 0;
|
|
4
|
+
const constants_1 = require("./constants");
|
|
5
|
+
const numbers_1 = require("./numbers");
|
|
6
|
+
function getGmsolOrderFeeDiscountFactor({ config, rank, isReferred, }) {
|
|
7
|
+
const byRank = config.byRank[rank];
|
|
8
|
+
if (rank > config.maxRank || byRank === undefined) {
|
|
9
|
+
throw new Error(`rank ${rank} exceeds the max rank ${config.maxRank}`);
|
|
10
|
+
}
|
|
11
|
+
if (!isReferred) {
|
|
12
|
+
return byRank;
|
|
13
|
+
}
|
|
14
|
+
if (config.referredUser > constants_1.GMSOL_PRECISION) {
|
|
15
|
+
throw new Error("the referred user discount is above 1");
|
|
16
|
+
}
|
|
17
|
+
return numbers_1.gmsolMath.toUnsigned(config.referredUser + numbers_1.gmsolMath.applyFactor(byRank, constants_1.GMSOL_PRECISION - config.referredUser, "fee discount"), "fee discount");
|
|
18
|
+
}
|
|
19
|
+
exports.getGmsolOrderFeeDiscountFactor = getGmsolOrderFeeDiscountFactor;
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getGmsolExecutionFeeRefund = exports.getGmsolExecutionFee = exports.getGmsolIncreaseSizeDeltaInTokens = exports.getGmsolMidPrice = exports.getGmsolUsdAmount = exports.getGmsolPendingPositionFees = exports.gmsolMath = exports.getGmsolFractionalPower = exports.getGmsolPoolValue = exports.getGmsolPendingBorrowingFees = exports.getGmsolNextPositionImpactPoolAmount = exports.getGmsolNextFunding = exports.getGmsolMarketStatus = exports.getGmsolBorrowingFactorPerSecond = exports.getGmsolOrderFeeDiscountFactor = exports.GMSOL_PRECISION_SCALE = exports.GMSOL_PRECISION = void 0;
|
|
4
|
+
const bigmath_1 = require("../bigmath");
|
|
5
|
+
const numbers_1 = require("../numbers");
|
|
6
|
+
const constants_1 = require("./constants");
|
|
7
|
+
var constants_2 = require("./constants");
|
|
8
|
+
Object.defineProperty(exports, "GMSOL_PRECISION", { enumerable: true, get: function () { return constants_2.GMSOL_PRECISION; } });
|
|
9
|
+
Object.defineProperty(exports, "GMSOL_PRECISION_SCALE", { enumerable: true, get: function () { return constants_2.GMSOL_PRECISION_SCALE; } });
|
|
10
|
+
var fees_1 = require("./fees");
|
|
11
|
+
Object.defineProperty(exports, "getGmsolOrderFeeDiscountFactor", { enumerable: true, get: function () { return fees_1.getGmsolOrderFeeDiscountFactor; } });
|
|
12
|
+
var markets_1 = require("./markets");
|
|
13
|
+
Object.defineProperty(exports, "getGmsolBorrowingFactorPerSecond", { enumerable: true, get: function () { return markets_1.getGmsolBorrowingFactorPerSecond; } });
|
|
14
|
+
Object.defineProperty(exports, "getGmsolMarketStatus", { enumerable: true, get: function () { return markets_1.getGmsolMarketStatus; } });
|
|
15
|
+
Object.defineProperty(exports, "getGmsolNextFunding", { enumerable: true, get: function () { return markets_1.getGmsolNextFunding; } });
|
|
16
|
+
Object.defineProperty(exports, "getGmsolNextPositionImpactPoolAmount", { enumerable: true, get: function () { return markets_1.getGmsolNextPositionImpactPoolAmount; } });
|
|
17
|
+
Object.defineProperty(exports, "getGmsolPendingBorrowingFees", { enumerable: true, get: function () { return markets_1.getGmsolPendingBorrowingFees; } });
|
|
18
|
+
Object.defineProperty(exports, "getGmsolPoolValue", { enumerable: true, get: function () { return markets_1.getGmsolPoolValue; } });
|
|
19
|
+
var numbers_2 = require("./numbers");
|
|
20
|
+
Object.defineProperty(exports, "getGmsolFractionalPower", { enumerable: true, get: function () { return numbers_2.getGmsolFractionalPower; } });
|
|
21
|
+
Object.defineProperty(exports, "gmsolMath", { enumerable: true, get: function () { return numbers_2.gmsolMath; } });
|
|
22
|
+
var positions_1 = require("./positions");
|
|
23
|
+
Object.defineProperty(exports, "getGmsolPendingPositionFees", { enumerable: true, get: function () { return positions_1.getGmsolPendingPositionFees; } });
|
|
24
|
+
function getGmsolUsdAmount(value) {
|
|
25
|
+
return bigmath_1.bigMath.mulDiv(value, 1n, constants_1.GMSOL_PRECISION_SCALE) * constants_1.GMSOL_PRECISION_SCALE;
|
|
26
|
+
}
|
|
27
|
+
exports.getGmsolUsdAmount = getGmsolUsdAmount;
|
|
28
|
+
function getGmsolMidPrice(token) {
|
|
29
|
+
const scale = (0, numbers_1.expandDecimals)(1, token.decimals) * constants_1.GMSOL_PRECISION_SCALE;
|
|
30
|
+
return bigmath_1.bigMath.mulDiv(token.prices.minPrice + token.prices.maxPrice, 1n, 2n * scale) * scale;
|
|
31
|
+
}
|
|
32
|
+
exports.getGmsolMidPrice = getGmsolMidPrice;
|
|
33
|
+
/** The impact is settled in the entry size; it never becomes pending collateral. 0 means the order fills no tokens. */
|
|
34
|
+
function getGmsolIncreaseSizeDeltaInTokens(p) {
|
|
35
|
+
const { baseSizeDeltaInTokens, priceImpactDeltaUsd, indexToken, isLong } = p;
|
|
36
|
+
const unit = (0, numbers_1.expandDecimals)(1, indexToken.decimals);
|
|
37
|
+
const impactAmount = priceImpactDeltaUsd > 0n
|
|
38
|
+
? bigmath_1.bigMath.mulDiv(priceImpactDeltaUsd, unit, indexToken.prices.maxPrice)
|
|
39
|
+
: (0, numbers_1.roundUpMagnitudeDivision)(priceImpactDeltaUsd * unit, indexToken.prices.minPrice);
|
|
40
|
+
return bigmath_1.bigMath.max(baseSizeDeltaInTokens + (isLong ? impactAmount : -impactAmount), 0n);
|
|
41
|
+
}
|
|
42
|
+
exports.getGmsolIncreaseSizeDeltaInTokens = getGmsolIncreaseSizeDeltaInTokens;
|
|
43
|
+
/** Rent is a refundable deposit. The maximum keeper fee can be spent even on a cancelled execution. */
|
|
44
|
+
function getGmsolExecutionFee(p) {
|
|
45
|
+
const { maxExecutionLamports, orderRentLamports, escrowRentLamports, solPrice } = p;
|
|
46
|
+
if ([maxExecutionLamports, orderRentLamports, escrowRentLamports, solPrice].some((value) => value < 0n)) {
|
|
47
|
+
throw new Error("Execution fee inputs must be nonnegative");
|
|
48
|
+
}
|
|
49
|
+
const keeperFeeLamports = bigmath_1.bigMath.max(300000n, maxExecutionLamports);
|
|
50
|
+
const refundableRentLamports = orderRentLamports + escrowRentLamports;
|
|
51
|
+
return {
|
|
52
|
+
keeperFeeLamports,
|
|
53
|
+
keeperFeeUsd: bigmath_1.bigMath.mulDiv(keeperFeeLamports, solPrice, 1000000000n),
|
|
54
|
+
refundableRentLamports,
|
|
55
|
+
refundableRentUsd: bigmath_1.bigMath.mulDiv(refundableRentLamports, solPrice, 1000000000n),
|
|
56
|
+
requiredLamports: keeperFeeLamports + refundableRentLamports,
|
|
57
|
+
};
|
|
58
|
+
}
|
|
59
|
+
exports.getGmsolExecutionFee = getGmsolExecutionFee;
|
|
60
|
+
function getGmsolExecutionFeeRefund(p) {
|
|
61
|
+
const spent = p.ownerCancelledBeforeExecution
|
|
62
|
+
? 0n
|
|
63
|
+
: bigmath_1.bigMath.min(p.keeperFeeLamports, bigmath_1.bigMath.max(0n, p.executionLamportsSpent));
|
|
64
|
+
return { keeperCostLamports: spent, refundLamports: p.refundableRentLamports + p.keeperFeeLamports - spent };
|
|
65
|
+
}
|
|
66
|
+
exports.getGmsolExecutionFeeRefund = getGmsolExecutionFeeRefund;
|
|
@@ -0,0 +1,206 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getGmsolMarketStatus = exports.getGmsolNextFunding = exports.getGmsolPoolValue = exports.getGmsolNextPositionImpactPoolAmount = exports.getGmsolPendingBorrowingFees = exports.getGmsolNextCumulativeBorrowingFactor = exports.getGmsolBorrowingFactorPerSecond = exports.getGmsolPendingPnl = exports.getGmsolPoolValueWithoutPnl = exports.getGmsolReservedValue = void 0;
|
|
4
|
+
const bigmath_1 = require("../bigmath");
|
|
5
|
+
const constants_1 = require("./constants");
|
|
6
|
+
const numbers_1 = require("./numbers");
|
|
7
|
+
const MAX_PNL_FACTOR = {
|
|
8
|
+
maxAfterDeposit: ["maxPnlFactorForLongDeposit", "maxPnlFactorForShortDeposit"],
|
|
9
|
+
maxAfterWithdrawal: ["maxPnlFactorForLongWithdrawal", "maxPnlFactorForShortWithdrawal"],
|
|
10
|
+
maxForTrader: ["maxPnlFactorForLongTrader", "maxPnlFactorForShortTrader"],
|
|
11
|
+
};
|
|
12
|
+
function pickPrice(price, maximize) {
|
|
13
|
+
return maximize ? price.max : price.min;
|
|
14
|
+
}
|
|
15
|
+
function getSidePrice(prices, side) {
|
|
16
|
+
return side === constants_1.GMSOL_LONG ? prices.long : prices.short;
|
|
17
|
+
}
|
|
18
|
+
function getOtherSide(side) {
|
|
19
|
+
return side === constants_1.GMSOL_LONG ? constants_1.GMSOL_SHORT : constants_1.GMSOL_LONG;
|
|
20
|
+
}
|
|
21
|
+
function getGmsolReservedValue({ market, prices, side, }) {
|
|
22
|
+
return side === constants_1.GMSOL_LONG
|
|
23
|
+
? numbers_1.gmsolMath.toUnsigned(market.openInterestInTokens[constants_1.GMSOL_LONG] * prices.index.max, "reserved value")
|
|
24
|
+
: market.openInterest[constants_1.GMSOL_SHORT];
|
|
25
|
+
}
|
|
26
|
+
exports.getGmsolReservedValue = getGmsolReservedValue;
|
|
27
|
+
function getGmsolPoolValueWithoutPnl({ market, prices, side, maximize, }) {
|
|
28
|
+
return numbers_1.gmsolMath.toUnsigned(market.primary[side] * pickPrice(getSidePrice(prices, side), maximize), "pool value without pnl");
|
|
29
|
+
}
|
|
30
|
+
exports.getGmsolPoolValueWithoutPnl = getGmsolPoolValueWithoutPnl;
|
|
31
|
+
// maximizing values longs at the index ask and shorts at the bid
|
|
32
|
+
function getGmsolPendingPnl({ market, indexPrice, side, maximize, }) {
|
|
33
|
+
const openInterest = market.openInterest[side];
|
|
34
|
+
const openInterestInTokens = market.openInterestInTokens[side];
|
|
35
|
+
if (openInterest === 0n && openInterestInTokens === 0n) {
|
|
36
|
+
return 0n;
|
|
37
|
+
}
|
|
38
|
+
const price = (side === constants_1.GMSOL_LONG) !== maximize ? indexPrice.min : indexPrice.max;
|
|
39
|
+
const value = numbers_1.gmsolMath.toUnsigned(openInterestInTokens * price, "position value");
|
|
40
|
+
return numbers_1.gmsolMath.toSigned(side === constants_1.GMSOL_LONG ? value - openInterest : openInterest - value, "pending pnl");
|
|
41
|
+
}
|
|
42
|
+
exports.getGmsolPendingPnl = getGmsolPendingPnl;
|
|
43
|
+
function getGmsolBorrowingFactorPerSecond({ market, prices, side, }) {
|
|
44
|
+
const reserved = getGmsolReservedValue({ market, prices, side });
|
|
45
|
+
if (reserved === 0n) {
|
|
46
|
+
return 0n;
|
|
47
|
+
}
|
|
48
|
+
if (market.skipBorrowingFeeForSmallerSide && market.openInterest[side] < market.openInterest[getOtherSide(side)]) {
|
|
49
|
+
return 0n;
|
|
50
|
+
}
|
|
51
|
+
const poolValue = getGmsolPoolValueWithoutPnl({ market, prices, side, maximize: false });
|
|
52
|
+
if (poolValue === 0n) {
|
|
53
|
+
throw new Error("borrowing factor of an empty pool");
|
|
54
|
+
}
|
|
55
|
+
const optimal = market.borrowingFeeOptimalUsageFactor[side];
|
|
56
|
+
if (optimal === 0n) {
|
|
57
|
+
const reservedAfterExponent = numbers_1.gmsolMath.applyExponentFactor(reserved, market.borrowingFeeExponent[side], "borrowing");
|
|
58
|
+
const usage = numbers_1.gmsolMath.divToFactor(reservedAfterExponent, poolValue, "borrowing usage");
|
|
59
|
+
return numbers_1.gmsolMath.applyFactor(usage, market.borrowingFeeFactor[side], "borrowing factor");
|
|
60
|
+
}
|
|
61
|
+
let usage = numbers_1.gmsolMath.divToFactor(reserved, numbers_1.gmsolMath.applyFactor(poolValue, market.config.openInterestReserveFactor, "reservable pool value"), "reserve usage");
|
|
62
|
+
if (!market.ignoreOpenInterestForUsageFactor) {
|
|
63
|
+
const openInterestUsage = numbers_1.gmsolMath.divToFactor(market.openInterest[side], market.maxOpenInterest[side], "open interest usage");
|
|
64
|
+
usage = bigmath_1.bigMath.max(usage, openInterestUsage);
|
|
65
|
+
}
|
|
66
|
+
const base = market.borrowingFeeBaseFactor[side];
|
|
67
|
+
let factor = numbers_1.gmsolMath.applyFactor(usage, base, "borrowing factor");
|
|
68
|
+
if (usage > optimal && constants_1.GMSOL_PRECISION > optimal) {
|
|
69
|
+
const above = market.borrowingFeeAboveOptimalUsageFactor[side];
|
|
70
|
+
const additional = numbers_1.gmsolMath.mulDiv(above > base ? above - base : 0n, usage - optimal, constants_1.GMSOL_PRECISION - optimal, "borrowing factor");
|
|
71
|
+
factor = numbers_1.gmsolMath.toUnsigned(factor + additional, "borrowing factor");
|
|
72
|
+
}
|
|
73
|
+
return factor;
|
|
74
|
+
}
|
|
75
|
+
exports.getGmsolBorrowingFactorPerSecond = getGmsolBorrowingFactorPerSecond;
|
|
76
|
+
function getGmsolNextCumulativeBorrowingFactor({ market, prices, now, side, }) {
|
|
77
|
+
const duration = BigInt(Math.max(0, now - market.borrowingClock));
|
|
78
|
+
const increase = numbers_1.gmsolMath.toUnsigned(getGmsolBorrowingFactorPerSecond({ market, prices, side }) * duration, "borrowing increase");
|
|
79
|
+
return numbers_1.gmsolMath.toUnsigned(market.borrowingFactor[side] + increase, "cumulative borrowing factor");
|
|
80
|
+
}
|
|
81
|
+
exports.getGmsolNextCumulativeBorrowingFactor = getGmsolNextCumulativeBorrowingFactor;
|
|
82
|
+
function getGmsolPendingBorrowingFees({ market, prices, now, side, }) {
|
|
83
|
+
const cumulative = getGmsolNextCumulativeBorrowingFactor({ market, prices, now, side });
|
|
84
|
+
const pending = numbers_1.gmsolMath.applyFactor(market.openInterest[side], cumulative, "borrowing fees") - market.totalBorrowing[side];
|
|
85
|
+
if (pending < 0n) {
|
|
86
|
+
throw new Error("negative pending borrowing fees");
|
|
87
|
+
}
|
|
88
|
+
return pending;
|
|
89
|
+
}
|
|
90
|
+
exports.getGmsolPendingBorrowingFees = getGmsolPendingBorrowingFees;
|
|
91
|
+
function getGmsolNextPositionImpactPoolAmount(market, now) {
|
|
92
|
+
const current = market.positionImpactPoolAmount;
|
|
93
|
+
const factor = market.config.positionImpactDistributeFactor;
|
|
94
|
+
const floor = market.config.minPositionImpactPoolAmount;
|
|
95
|
+
if (factor === 0n || current <= floor) {
|
|
96
|
+
return current;
|
|
97
|
+
}
|
|
98
|
+
const duration = BigInt(Math.max(0, now - market.priceImpactDistributionClock));
|
|
99
|
+
const distributed = numbers_1.gmsolMath.applyFactor(duration, factor, "impact distribution");
|
|
100
|
+
return current - bigmath_1.bigMath.min(distributed, current - floor);
|
|
101
|
+
}
|
|
102
|
+
exports.getGmsolNextPositionImpactPoolAmount = getGmsolNextPositionImpactPoolAmount;
|
|
103
|
+
// signed, since losses can exceed the pool
|
|
104
|
+
function getGmsolPoolValue({ market, prices, now, pnlFactorKind, maximize, }) {
|
|
105
|
+
const sideValues = constants_1.GMSOL_SIDES.map((side) => getGmsolPoolValueWithoutPnl({ market, prices, side, maximize }));
|
|
106
|
+
const fees = numbers_1.gmsolMath.toUnsigned(constants_1.GMSOL_SIDES.reduce((sum, side) => sum + getGmsolPendingBorrowingFees({ market, prices, now, side }), 0n), "borrowing fees");
|
|
107
|
+
let value = sideValues[constants_1.GMSOL_LONG] +
|
|
108
|
+
sideValues[constants_1.GMSOL_SHORT] +
|
|
109
|
+
numbers_1.gmsolMath.applyFactor(fees, numbers_1.gmsolMath.toUnsigned(constants_1.GMSOL_PRECISION - market.config.borrowingFeeReceiverFactor, "pool share"), "pool borrowing fees");
|
|
110
|
+
for (const side of constants_1.GMSOL_SIDES) {
|
|
111
|
+
const pnl = getGmsolPendingPnl({ market, indexPrice: prices.index, side, maximize: !maximize });
|
|
112
|
+
const cap = numbers_1.gmsolMath.applyFactor(sideValues[side], market.config[MAX_PNL_FACTOR[pnlFactorKind][side]], "pnl cap");
|
|
113
|
+
value -= bigmath_1.bigMath.min(pnl, cap);
|
|
114
|
+
}
|
|
115
|
+
value -= numbers_1.gmsolMath.toUnsigned(getGmsolNextPositionImpactPoolAmount(market, now) * pickPrice(prices.index, !maximize), "impact pool value");
|
|
116
|
+
return numbers_1.gmsolMath.toSigned(value, "pool value");
|
|
117
|
+
}
|
|
118
|
+
exports.getGmsolPoolValue = getGmsolPoolValue;
|
|
119
|
+
// a port of the funding and liquidity parts of gmsol's MarketCalculations::status, with its rounding and overflow checks
|
|
120
|
+
function toSignedFactor(value) {
|
|
121
|
+
return numbers_1.gmsolMath.toSigned(value, "signed conversion");
|
|
122
|
+
}
|
|
123
|
+
function boundMagnitude(value, min, max) {
|
|
124
|
+
if (min > max) {
|
|
125
|
+
throw new Error("funding factor min is above max");
|
|
126
|
+
}
|
|
127
|
+
const magnitude = bigmath_1.bigMath.abs(value);
|
|
128
|
+
if (magnitude < min) {
|
|
129
|
+
return value < 0n ? -toSignedFactor(min) : toSignedFactor(min);
|
|
130
|
+
}
|
|
131
|
+
if (magnitude > max) {
|
|
132
|
+
return value < 0n ? -toSignedFactor(max) : toSignedFactor(max);
|
|
133
|
+
}
|
|
134
|
+
return value;
|
|
135
|
+
}
|
|
136
|
+
function getFundingChange({ params, saved, long, short, diffFactor, }) {
|
|
137
|
+
const sameDirection = (saved > 0n && long > short) || (saved < 0n && long < short);
|
|
138
|
+
if (!sameDirection || diffFactor > params.thresholdForStableFunding) {
|
|
139
|
+
return "increase";
|
|
140
|
+
}
|
|
141
|
+
return diffFactor < params.thresholdForDecreaseFunding ? "decrease" : "none";
|
|
142
|
+
}
|
|
143
|
+
// gmsol next_funding_factor_per_second: the rate moves from the saved one over the time since the funding clock
|
|
144
|
+
function getNextFundingFactorPerSecond({ params, saved, duration, long, short, }) {
|
|
145
|
+
const diff = bigmath_1.bigMath.abs(long - short);
|
|
146
|
+
if (diff === 0n && params.increaseFactorPerSecond === 0n) {
|
|
147
|
+
return { factor: 0n, longsPayShorts: true };
|
|
148
|
+
}
|
|
149
|
+
const diffFactor = numbers_1.gmsolMath.mulDiv(numbers_1.gmsolMath.applyExponentFactor(diff, params.exponent, "funding"), constants_1.GMSOL_PRECISION, numbers_1.gmsolMath.toUnsigned(long + short, "total"), "diff");
|
|
150
|
+
if (params.increaseFactorPerSecond === 0n) {
|
|
151
|
+
const factor = numbers_1.gmsolMath.applyFactor(diffFactor, params.factor, "funding factor");
|
|
152
|
+
return {
|
|
153
|
+
factor: bigmath_1.bigMath.min(factor, params.maxFactorPerSecond),
|
|
154
|
+
longsPayShorts: long > short,
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
const change = getFundingChange({ params, saved, long, short, diffFactor });
|
|
158
|
+
let next = saved;
|
|
159
|
+
if (change === "increase") {
|
|
160
|
+
const increase = numbers_1.gmsolMath.toUnsigned(numbers_1.gmsolMath.applyFactor(diffFactor, params.increaseFactorPerSecond, "increase") * duration, "increase");
|
|
161
|
+
next = numbers_1.gmsolMath.toSigned(saved + (long < short ? -toSignedFactor(increase) : toSignedFactor(increase)), "funding increase");
|
|
162
|
+
}
|
|
163
|
+
else if (change === "decrease" && saved !== 0n) {
|
|
164
|
+
const magnitude = bigmath_1.bigMath.abs(saved);
|
|
165
|
+
const decrease = numbers_1.gmsolMath.toUnsigned(params.decreaseFactorPerSecond * duration, "decrease");
|
|
166
|
+
if (magnitude <= decrease) {
|
|
167
|
+
next = saved > 0n ? 1n : -1n;
|
|
168
|
+
}
|
|
169
|
+
else {
|
|
170
|
+
next = saved > 0n ? toSignedFactor(magnitude - decrease) : -toSignedFactor(magnitude - decrease);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
const bounded = boundMagnitude(boundMagnitude(next, 0n, params.maxFactorPerSecond), params.minFactorPerSecond, params.maxFactorPerSecond);
|
|
174
|
+
return { factor: bigmath_1.bigMath.abs(bounded), longsPayShorts: bounded > 0n };
|
|
175
|
+
}
|
|
176
|
+
function getLiquidity({ market, prices, side, }) {
|
|
177
|
+
const reserved = getGmsolReservedValue({ market, prices, side });
|
|
178
|
+
const reserveFactor = bigmath_1.bigMath.min(market.config.reserveFactor, market.config.openInterestReserveFactor);
|
|
179
|
+
const maxLiquidity = bigmath_1.bigMath.min(numbers_1.gmsolMath.applyFactor(getGmsolPoolValueWithoutPnl({ market, prices, side, maximize: false }), reserveFactor, "max reserve"), market.maxOpenInterest[side]);
|
|
180
|
+
return maxLiquidity > reserved ? maxLiquidity - reserved : 0n;
|
|
181
|
+
}
|
|
182
|
+
function getGmsolNextFunding(market, now) {
|
|
183
|
+
const [long, short] = market.openInterest;
|
|
184
|
+
if (long === 0n || short === 0n) {
|
|
185
|
+
return { factor: 0n, longsPayShorts: true };
|
|
186
|
+
}
|
|
187
|
+
const duration = BigInt(Math.max(0, now - market.fundingClock));
|
|
188
|
+
return getNextFundingFactorPerSecond({
|
|
189
|
+
params: market.funding,
|
|
190
|
+
saved: market.fundingFactorPerSecond,
|
|
191
|
+
duration,
|
|
192
|
+
long,
|
|
193
|
+
short,
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
exports.getGmsolNextFunding = getGmsolNextFunding;
|
|
197
|
+
function getGmsolMarketStatus({ market, prices, now, }) {
|
|
198
|
+
const funding = getGmsolNextFunding(market, now);
|
|
199
|
+
return {
|
|
200
|
+
fundingFactorPerSecond: funding.factor,
|
|
201
|
+
longsPayShorts: funding.longsPayShorts,
|
|
202
|
+
liquidityForLong: getLiquidity({ market, prices, side: constants_1.GMSOL_LONG }),
|
|
203
|
+
liquidityForShort: getLiquidity({ market, prices, side: constants_1.GMSOL_SHORT }),
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
exports.getGmsolMarketStatus = getGmsolMarketStatus;
|