@gmx-io/sdk 1.8.2 → 1.9.1
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 +1 -1
- package/build/cjs/scripts/subsquid-codegen.js +1 -1
- package/build/cjs/src/abis/ArbSys.js +11 -0
- package/build/cjs/src/abis/index.js +2 -0
- package/build/cjs/src/clients/v1/testUtil.js +1 -1
- package/build/cjs/src/clients/v2/index.js +8 -1
- package/build/cjs/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +0 -88
- package/build/cjs/src/codegen/prebuilt/hashedMarketConfigKeys.json +0 -748
- package/build/cjs/src/codegen/prebuilt/hashedMarketValuesKeys.json +0 -165
- package/build/cjs/src/configs/__tests__/markets.spec.js +1 -16
- package/build/cjs/src/configs/api.js +10 -1
- package/build/cjs/src/configs/contracts.js +2 -0
- package/build/cjs/src/configs/dataStore.js +13 -3
- package/build/cjs/src/configs/markets.js +0 -77
- package/build/cjs/src/configs/tokens.js +0 -90
- package/build/cjs/src/test/fixtures/arbitrumMarkets.json +0 -72
- package/build/cjs/src/test/mock.js +12 -1
- package/build/cjs/src/utils/__tests__/fees.spec.js +36 -0
- package/build/cjs/src/utils/__tests__/markets.spec.js +233 -0
- package/build/cjs/src/utils/__tests__/positionPnlCap.spec.js +30 -3
- package/build/cjs/src/utils/__tests__/positions.spec.js +5 -5
- package/build/cjs/src/utils/__tests__/prices.spec.js +97 -0
- package/build/cjs/src/utils/fees/index.js +8 -5
- package/build/cjs/src/utils/fees/priceImpact.js +4 -9
- package/build/cjs/src/utils/markets/utils.js +57 -1
- package/build/cjs/src/utils/numbers/index.js +1 -0
- package/build/cjs/src/utils/numbers/prbMath.js +154 -0
- package/build/cjs/src/utils/positions/utils.js +66 -43
- package/build/cjs/src/utils/prices/utils.js +28 -1
- package/build/cjs/src/utils/stats/__tests__/api.spec.js +100 -0
- package/build/cjs/src/utils/stats/api.js +32 -0
- package/build/cjs/src/utils/stats/types.js +4 -0
- package/build/cjs/src/utils/trade/__tests__/increase.spec.js +580 -0
- package/build/cjs/src/utils/trade/__tests__/increaseInvariants.spec.js +258 -0
- package/build/cjs/src/utils/trade/__tests__/increaseMarginCheck.spec.js +837 -0
- package/build/cjs/src/utils/trade/increase.js +255 -32
- package/build/cjs/src/utils/trade/increaseMarginCheck.js +179 -0
- package/build/cjs/src/utils/trade/index.js +1 -0
- package/build/cjs/src/utils/trade/trade.js +11 -1
- package/build/cjs/src/utils/yield/api.js +9 -1
- package/build/esm/scripts/subsquid-codegen.js +1 -1
- package/build/esm/src/abis/ArbSys.js +9 -0
- package/build/esm/src/abis/index.js +2 -0
- package/build/esm/src/clients/v1/testUtil.js +1 -1
- package/build/esm/src/clients/v2/index.js +5 -1
- package/build/esm/src/codegen/prebuilt/hashedKinkModelMarketRatesKeys.json +0 -88
- package/build/esm/src/codegen/prebuilt/hashedMarketConfigKeys.json +0 -748
- package/build/esm/src/codegen/prebuilt/hashedMarketValuesKeys.json +0 -165
- package/build/esm/src/configs/__tests__/markets.spec.js +2 -17
- package/build/esm/src/configs/api.js +8 -0
- package/build/esm/src/configs/contracts.js +2 -0
- package/build/esm/src/configs/dataStore.js +8 -0
- package/build/esm/src/configs/markets.js +0 -77
- package/build/esm/src/configs/tokens.js +0 -90
- package/build/esm/src/test/fixtures/arbitrumMarkets.json +0 -72
- package/build/esm/src/test/mock.js +10 -0
- package/build/esm/src/utils/__tests__/fees.spec.js +37 -1
- package/build/esm/src/utils/__tests__/markets.spec.js +234 -1
- package/build/esm/src/utils/__tests__/positionPnlCap.spec.js +30 -3
- package/build/esm/src/utils/__tests__/positions.spec.js +5 -5
- package/build/esm/src/utils/__tests__/prices.spec.js +98 -1
- package/build/esm/src/utils/fees/index.js +8 -5
- package/build/esm/src/utils/fees/priceImpact.js +5 -10
- package/build/esm/src/utils/markets/utils.js +57 -2
- package/build/esm/src/utils/numbers/index.js +1 -0
- package/build/esm/src/utils/numbers/prbMath.js +149 -0
- package/build/esm/src/utils/positions/utils.js +63 -42
- package/build/esm/src/utils/prices/utils.js +25 -0
- package/build/esm/src/utils/stats/__tests__/api.spec.js +98 -0
- package/build/esm/src/utils/stats/api.js +26 -0
- package/build/esm/src/utils/stats/types.js +1 -0
- package/build/esm/src/utils/trade/__tests__/increase.spec.js +578 -0
- package/build/esm/src/utils/trade/__tests__/increaseInvariants.spec.js +256 -0
- package/build/esm/src/utils/trade/__tests__/increaseMarginCheck.spec.js +835 -0
- package/build/esm/src/utils/trade/increase.js +243 -20
- package/build/esm/src/utils/trade/increaseMarginCheck.js +173 -0
- package/build/esm/src/utils/trade/index.js +1 -0
- package/build/esm/src/utils/trade/trade.js +10 -1
- package/build/esm/src/utils/yield/api.js +7 -0
- package/build/tsconfig.tsbuildinfo +1 -1
- package/build/types/src/abis/ArbSys.d.ts +12 -0
- package/build/types/src/abis/index.d.ts +11 -0
- package/build/types/src/clients/v2/index.d.ts +6 -3
- package/build/types/src/codegen/prebuilt/index.d.ts +73 -1074
- package/build/types/src/configs/api.d.ts +1 -0
- package/build/types/src/configs/contracts.d.ts +2 -0
- package/build/types/src/configs/dataStore.d.ts +4 -0
- package/build/types/src/test/mock.d.ts +2 -0
- package/build/types/src/utils/buildUrl/utils.d.ts +1 -1
- package/build/types/src/utils/buyback/types.d.ts +7 -0
- package/build/types/src/utils/fees/index.d.ts +1 -1
- package/build/types/src/utils/markets/utils.d.ts +7 -0
- package/build/types/src/utils/numbers/index.d.ts +1 -0
- package/build/types/src/utils/numbers/prbMath.d.ts +7 -0
- package/build/types/src/utils/positions/utils.d.ts +9 -0
- package/build/types/src/utils/prices/utils.d.ts +10 -0
- package/build/types/src/utils/stats/__tests__/api.spec.d.ts +1 -0
- package/build/types/src/utils/stats/api.d.ts +11 -0
- package/build/types/src/utils/stats/types.d.ts +106 -0
- package/build/types/src/utils/trade/__tests__/increase.spec.d.ts +1 -0
- package/build/types/src/utils/trade/__tests__/increaseInvariants.spec.d.ts +1 -0
- package/build/types/src/utils/trade/__tests__/increaseMarginCheck.spec.d.ts +1 -0
- package/build/types/src/utils/trade/increase.d.ts +3 -0
- package/build/types/src/utils/trade/increaseMarginCheck.d.ts +61 -0
- package/build/types/src/utils/trade/index.d.ts +1 -0
- package/build/types/src/utils/trade/trade.d.ts +2 -0
- package/build/types/src/utils/yield/api.d.ts +4 -1
- package/build/types/src/utils/yield/types.d.ts +18 -0
- package/package.json +3 -3
|
@@ -549,15 +549,6 @@
|
|
|
549
549
|
"isListed": true,
|
|
550
550
|
"listingDate": "2024-11-22T00:00:00.000Z"
|
|
551
551
|
},
|
|
552
|
-
{
|
|
553
|
-
"name": "MKR/USD [ETH-USDC]",
|
|
554
|
-
"marketToken": "0x2aE5c5Cd4843cf588AA8D1289894318130acc823",
|
|
555
|
-
"indexToken": "0x8904De84c3bB3B7D2383F934Af40FcB3Ef82F28b",
|
|
556
|
-
"longToken": "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
|
|
557
|
-
"shortToken": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
|
558
|
-
"isListed": true,
|
|
559
|
-
"listingDate": "2025-03-13T00:00:00.000Z"
|
|
560
|
-
},
|
|
561
552
|
{
|
|
562
553
|
"name": "ETH/USD [ETH-USDC]",
|
|
563
554
|
"marketToken": "0x70d95587d40A2caf56bd97485aB3Eec10Bee6336",
|
|
@@ -657,24 +648,6 @@
|
|
|
657
648
|
"isListed": true,
|
|
658
649
|
"listingDate": "2024-11-21T00:00:00.000Z"
|
|
659
650
|
},
|
|
660
|
-
{
|
|
661
|
-
"name": "SWAP-ONLY [USDC-DAI]",
|
|
662
|
-
"marketToken": "0xe2fEDb9e6139a182B98e7C2688ccFa3e9A53c665",
|
|
663
|
-
"indexToken": "0x0000000000000000000000000000000000000000",
|
|
664
|
-
"longToken": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
|
665
|
-
"shortToken": "0xDA10009cBd5D07dd0CeCc66161FC93D7c9000da1",
|
|
666
|
-
"isListed": true,
|
|
667
|
-
"listingDate": "2023-08-08T00:00:00.000Z"
|
|
668
|
-
},
|
|
669
|
-
{
|
|
670
|
-
"name": "KTA/USD [ETH-USDC]",
|
|
671
|
-
"marketToken": "0x970b730b5dD18de53A230eE8F4af088dBC3a6F8d",
|
|
672
|
-
"indexToken": "0x96Ee343E36E8642627FAEa235D57a9FEC8a6e34f",
|
|
673
|
-
"longToken": "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
|
|
674
|
-
"shortToken": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
|
675
|
-
"isListed": true,
|
|
676
|
-
"listingDate": "2025-09-18T00:00:00.000Z"
|
|
677
|
-
},
|
|
678
651
|
{
|
|
679
652
|
"name": "CHZ/USD [ETH-USDC]",
|
|
680
653
|
"marketToken": "0x3600592DdEd7e6E0B05029DfB637fFc5A85D6f6B",
|
|
@@ -756,15 +729,6 @@
|
|
|
756
729
|
"isListed": true,
|
|
757
730
|
"listingDate": "2024-07-17T00:00:00.000Z"
|
|
758
731
|
},
|
|
759
|
-
{
|
|
760
|
-
"name": "AI16Z/USD [WBTC.b-USDC]",
|
|
761
|
-
"marketToken": "0xD60f1BA6a76979eFfE706BF090372Ebc0A5bF169",
|
|
762
|
-
"indexToken": "0xBb69bd9dc152C2c0F083507641a46193d2B61EBb",
|
|
763
|
-
"longToken": "0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f",
|
|
764
|
-
"shortToken": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
|
765
|
-
"isListed": true,
|
|
766
|
-
"listingDate": "2025-01-30T00:00:00.000Z"
|
|
767
|
-
},
|
|
768
732
|
{
|
|
769
733
|
"name": "CVX/USD [ETH-USDC]",
|
|
770
734
|
"marketToken": "0x41E3bC5B72384C8B26b559B7d16C2B81Fd36fbA2",
|
|
@@ -810,15 +774,6 @@
|
|
|
810
774
|
"isListed": true,
|
|
811
775
|
"listingDate": "2025-08-21T00:00:00.000Z"
|
|
812
776
|
},
|
|
813
|
-
{
|
|
814
|
-
"name": "MELANIA/USD [ETH-USDC]",
|
|
815
|
-
"marketToken": "0x12fD1A4BdB96219E637180Ff5293409502b2951D",
|
|
816
|
-
"indexToken": "0xfa4F8E582214eBCe1A08eB2a65e08082053E441F",
|
|
817
|
-
"longToken": "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
|
|
818
|
-
"shortToken": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
|
819
|
-
"isListed": true,
|
|
820
|
-
"listingDate": "2025-01-23T00:00:00.000Z"
|
|
821
|
-
},
|
|
822
777
|
{
|
|
823
778
|
"name": "AVNT/USD [ETH-USDC]",
|
|
824
779
|
"marketToken": "0xCEff9D261A96Cb78Df35f9333ba9F2F4CFcb8a68",
|
|
@@ -828,15 +783,6 @@
|
|
|
828
783
|
"isListed": true,
|
|
829
784
|
"listingDate": "2025-10-09T00:00:00.000Z"
|
|
830
785
|
},
|
|
831
|
-
{
|
|
832
|
-
"name": "IP/USD [WBTC.b-USDC]",
|
|
833
|
-
"marketToken": "0x5ff52BE1968107D7886a8E9A64874A45c8F5D96a",
|
|
834
|
-
"indexToken": "0xe7304E9F4ca383BD6312a5179cdC7B40B96A6F5a",
|
|
835
|
-
"longToken": "0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f",
|
|
836
|
-
"shortToken": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
|
837
|
-
"isListed": true,
|
|
838
|
-
"listingDate": "2026-01-23T00:00:00.000Z"
|
|
839
|
-
},
|
|
840
786
|
{
|
|
841
787
|
"name": "LINK/USD [ETH-USDC]",
|
|
842
788
|
"marketToken": "0xF913B4748031EF569898ED91e5BA0d602bB93298",
|
|
@@ -1008,15 +954,6 @@
|
|
|
1008
954
|
"isListed": true,
|
|
1009
955
|
"listingDate": "2024-03-03T00:00:00.000Z"
|
|
1010
956
|
},
|
|
1011
|
-
{
|
|
1012
|
-
"name": "PI/USD [WBTC.b-USDC]",
|
|
1013
|
-
"marketToken": "0x39AC3C494950A4363D739201BA5A0861265C9ae5",
|
|
1014
|
-
"indexToken": "0xd1738d37401a0A71f7E382d2cFeCD3ab69687017",
|
|
1015
|
-
"longToken": "0x2f2a2543B76A4166549F7aaB2e75Bef0aefC5B0f",
|
|
1016
|
-
"shortToken": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
|
1017
|
-
"isListed": true,
|
|
1018
|
-
"listingDate": "2025-06-02T00:00:00.000Z"
|
|
1019
|
-
},
|
|
1020
957
|
{
|
|
1021
958
|
"name": "APT/USD [ETH-USDC]",
|
|
1022
959
|
"marketToken": "0x66A69c8eb98A7efE22A22611d1967dfec786a708",
|
|
@@ -1098,15 +1035,6 @@
|
|
|
1098
1035
|
"isListed": true,
|
|
1099
1036
|
"listingDate": "2025-03-06T00:00:00.000Z"
|
|
1100
1037
|
},
|
|
1101
|
-
{
|
|
1102
|
-
"name": "TON/USD [ETH-USDC]",
|
|
1103
|
-
"marketToken": "0x15c6eBD4175ffF9EE3c2615c556fCf62D2d9499c",
|
|
1104
|
-
"indexToken": "0xB2f7cefaeEb08Aa347705ac829a7b8bE2FB560f3",
|
|
1105
|
-
"longToken": "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1",
|
|
1106
|
-
"shortToken": "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
|
1107
|
-
"isListed": true,
|
|
1108
|
-
"listingDate": "2024-11-07T00:00:00.000Z"
|
|
1109
|
-
},
|
|
1110
1038
|
{
|
|
1111
1039
|
"name": "OP/USD [OP-USDC]",
|
|
1112
1040
|
"marketToken": "0x4fDd333FF9cA409df583f306B6F5a7fFdE790739",
|
|
@@ -155,3 +155,13 @@ data = "0x1", to = "0x6352a56caadC4F1E25CD6c75970Fa768A3304e64", receiver = "0x1
|
|
|
155
155
|
},
|
|
156
156
|
};
|
|
157
157
|
}
|
|
158
|
+
/** Deterministic PRNG for property-style specs: a fixed seed keeps a failing case reproducible from the run alone. */
|
|
159
|
+
export function createSeededRandom(seed) {
|
|
160
|
+
let a = seed >>> 0;
|
|
161
|
+
return () => {
|
|
162
|
+
a = (a + 0x6d2b79f5) >>> 0;
|
|
163
|
+
let t = Math.imul(a ^ (a >>> 15), 1 | a);
|
|
164
|
+
t = (t + Math.imul(t ^ (t >>> 7), 61 | t)) ^ t;
|
|
165
|
+
return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
|
|
166
|
+
};
|
|
167
|
+
}
|
|
@@ -2,7 +2,7 @@ import { zeroHash } from "viem";
|
|
|
2
2
|
import { describe, expect, it } from "vitest";
|
|
3
3
|
import { USD_DECIMALS } from "../../configs/factors";
|
|
4
4
|
import { mockMarketsInfoData, mockTokensData, usdToToken } from "../../test/mock";
|
|
5
|
-
import { getFundingFactorPerPeriod, getPriceImpactForPosition, getPriceImpactForSwap, getPriceImpactUsd, } from "../fees";
|
|
5
|
+
import { getFundingFactorPerPeriod, getPositionFee, getPriceImpactForPosition, getPriceImpactForSwap, getPriceImpactUsd, } from "../fees";
|
|
6
6
|
import { numberToBigint } from "../numbers";
|
|
7
7
|
import { convertToTokenAmountForIncrease } from "../tokens";
|
|
8
8
|
import { getDecreasePositionSizeDeltaInTokens } from "../trade/decrease";
|
|
@@ -324,3 +324,39 @@ describe("position price impact", () => {
|
|
|
324
324
|
expect(getPriceImpactForSwap(marketWithPositionInventoryChanges, marketWithPositionInventoryChanges.longToken, marketWithPositionInventoryChanges.shortToken, 100n * dollar, -100n * dollar)).toEqual(expected);
|
|
325
325
|
});
|
|
326
326
|
});
|
|
327
|
+
describe("getPositionFee", () => {
|
|
328
|
+
// 0.1% of 100 000 → 100 of opening fee
|
|
329
|
+
const marketInfo = {
|
|
330
|
+
positionFeeFactorForBalanceWasImproved: toFactor("0.05%"),
|
|
331
|
+
positionFeeFactorForBalanceWasNotImproved: toFactor("0.1%"),
|
|
332
|
+
};
|
|
333
|
+
const sizeDeltaUsd = 100000n * dollar;
|
|
334
|
+
// 10% of the fee is rebated, half of it goes back to the trader → 5 of discount
|
|
335
|
+
const referralInfo = { totalRebateFactor: toFactor("10%"), discountFactor: toFactor("50%") };
|
|
336
|
+
it("keeps the referral discount when no pro tier is set", () => {
|
|
337
|
+
const noPro = getPositionFee(marketInfo, sizeDeltaUsd, false, referralInfo);
|
|
338
|
+
const zeroPro = getPositionFee(marketInfo, sizeDeltaUsd, false, referralInfo, undefined, 0n);
|
|
339
|
+
expect(noPro.discountUsd).toBe(5n * dollar);
|
|
340
|
+
expect(noPro.positionFeeUsd).toBe(95n * dollar);
|
|
341
|
+
expect(zeroPro).toEqual(noPro);
|
|
342
|
+
});
|
|
343
|
+
it("takes the bigger of the pro and referral discounts, never their sum", () => {
|
|
344
|
+
const bigPro = getPositionFee(marketInfo, sizeDeltaUsd, false, referralInfo, undefined, toFactor("10%"));
|
|
345
|
+
expect(bigPro.discountUsd).toBe(10n * dollar);
|
|
346
|
+
expect(bigPro.positionFeeUsd).toBe(90n * dollar);
|
|
347
|
+
const smallPro = getPositionFee(marketInfo, sizeDeltaUsd, false, referralInfo, undefined, toFactor("3%"));
|
|
348
|
+
expect(smallPro.discountUsd).toBe(5n * dollar);
|
|
349
|
+
expect(smallPro.positionFeeUsd).toBe(95n * dollar);
|
|
350
|
+
});
|
|
351
|
+
it("is indifferent when the pro discount equals the referral one", () => {
|
|
352
|
+
const equalPro = getPositionFee(marketInfo, sizeDeltaUsd, false, referralInfo, undefined, toFactor("5%"));
|
|
353
|
+
expect(equalPro.discountUsd).toBe(5n * dollar);
|
|
354
|
+
expect(equalPro.positionFeeUsd).toBe(95n * dollar);
|
|
355
|
+
});
|
|
356
|
+
it("applies the pro discount without a referral code", () => {
|
|
357
|
+
const proOnly = getPositionFee(marketInfo, sizeDeltaUsd, false, undefined, undefined, toFactor("10%"));
|
|
358
|
+
expect(proOnly.totalRebateUsd).toBe(0n);
|
|
359
|
+
expect(proOnly.discountUsd).toBe(10n * dollar);
|
|
360
|
+
expect(proOnly.positionFeeUsd).toBe(90n * dollar);
|
|
361
|
+
});
|
|
362
|
+
});
|
|
@@ -1,8 +1,10 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
2
|
import { ARBITRUM } from "../../configs/chains";
|
|
3
3
|
import { TOKENS } from "../../configs/tokens";
|
|
4
|
-
import {
|
|
4
|
+
import { mockMarketsInfoData, mockTokensData } from "../../test/mock";
|
|
5
|
+
import { getMarketFullName, getMarketIndexName, getMarketPoolName, getContractMarketPrices, getTokenPoolType, getPoolUsdWithoutPnl, getCappedPoolPnl, getMaxLeverageByMinCollateralFactor, getMaxAllowedLeverage, getOppositeCollateral, getAvailableUsdLiquidityForCollateral, getReservedUsd, getMarketDivisor, getMarketPnl, getOpenInterestUsd, getOpenInterestInTokens, getPositiveMarketPnl, getPriceForPnl, getMarketInfoWithOpenInterestDelta, getOpenInterestForBalance, } from "../markets";
|
|
5
6
|
import { BASIS_POINTS_DIVISOR, expandDecimals } from "../numbers";
|
|
7
|
+
import { convertToUsd } from "../tokens";
|
|
6
8
|
function getToken(symbol) {
|
|
7
9
|
return TOKENS[ARBITRUM].find((token) => token.symbol === symbol);
|
|
8
10
|
}
|
|
@@ -438,3 +440,234 @@ describe("getPriceForPnl", () => {
|
|
|
438
440
|
expect(getPriceForPnl({ minPrice: 1000n, maxPrice: 2000n }, false, false)).toBe(2000n);
|
|
439
441
|
});
|
|
440
442
|
});
|
|
443
|
+
describe("getMarketInfoWithOpenInterestDelta", () => {
|
|
444
|
+
const tokensData = mockTokensData();
|
|
445
|
+
function buildMarket(overrides = {}) {
|
|
446
|
+
return mockMarketsInfoData(tokensData, ["ETH-ETH-USDC"], {
|
|
447
|
+
"ETH-ETH-USDC": {
|
|
448
|
+
longInterestUsd: expandDecimals(1000, 30),
|
|
449
|
+
shortInterestUsd: expandDecimals(2000, 30),
|
|
450
|
+
longInterestInTokens: expandDecimals(1, 18),
|
|
451
|
+
shortInterestInTokens: expandDecimals(2, 18),
|
|
452
|
+
...overrides,
|
|
453
|
+
},
|
|
454
|
+
})["ETH-ETH-USDC"];
|
|
455
|
+
}
|
|
456
|
+
it("returns the market unchanged when there is no delta", () => {
|
|
457
|
+
const marketInfo = buildMarket();
|
|
458
|
+
expect(getMarketInfoWithOpenInterestDelta({
|
|
459
|
+
marketInfo,
|
|
460
|
+
collateralToken: marketInfo.shortToken,
|
|
461
|
+
isLong: true,
|
|
462
|
+
sizeDeltaUsd: 0n,
|
|
463
|
+
sizeDeltaInTokens: 0n,
|
|
464
|
+
})).toBe(marketInfo);
|
|
465
|
+
});
|
|
466
|
+
it("shifts only the long side for a long increase", () => {
|
|
467
|
+
const marketInfo = buildMarket();
|
|
468
|
+
const next = getMarketInfoWithOpenInterestDelta({
|
|
469
|
+
marketInfo,
|
|
470
|
+
collateralToken: marketInfo.shortToken,
|
|
471
|
+
isLong: true,
|
|
472
|
+
sizeDeltaUsd: expandDecimals(500, 30),
|
|
473
|
+
sizeDeltaInTokens: expandDecimals(5, 17),
|
|
474
|
+
});
|
|
475
|
+
expect(next.longInterestUsd).toBe(expandDecimals(1500, 30));
|
|
476
|
+
expect(next.longInterestInTokens).toBe(expandDecimals(15, 17));
|
|
477
|
+
expect(next.shortInterestUsd).toBe(marketInfo.shortInterestUsd);
|
|
478
|
+
expect(next.shortInterestInTokens).toBe(marketInfo.shortInterestInTokens);
|
|
479
|
+
// the source market must not be mutated, it is shared across selectors
|
|
480
|
+
expect(marketInfo.longInterestUsd).toBe(expandDecimals(1000, 30));
|
|
481
|
+
});
|
|
482
|
+
it("shifts only the short side for a short increase", () => {
|
|
483
|
+
const marketInfo = buildMarket();
|
|
484
|
+
const next = getMarketInfoWithOpenInterestDelta({
|
|
485
|
+
marketInfo,
|
|
486
|
+
collateralToken: marketInfo.shortToken,
|
|
487
|
+
isLong: false,
|
|
488
|
+
sizeDeltaUsd: expandDecimals(500, 30),
|
|
489
|
+
sizeDeltaInTokens: expandDecimals(5, 17),
|
|
490
|
+
});
|
|
491
|
+
expect(next.shortInterestUsd).toBe(expandDecimals(2500, 30));
|
|
492
|
+
expect(next.shortInterestInTokens).toBe(expandDecimals(25, 17));
|
|
493
|
+
expect(next.longInterestUsd).toBe(marketInfo.longInterestUsd);
|
|
494
|
+
expect(next.longInterestInTokens).toBe(marketInfo.longInterestInTokens);
|
|
495
|
+
});
|
|
496
|
+
it("carries the delta into the open interest used for balance in both modes", () => {
|
|
497
|
+
const sizeDeltaUsd = expandDecimals(500, 30);
|
|
498
|
+
const sizeDeltaInTokens = expandDecimals(5, 17);
|
|
499
|
+
const usdMarket = buildMarket({ useOpenInterestInTokensForBalance: false });
|
|
500
|
+
const usdNext = getMarketInfoWithOpenInterestDelta({
|
|
501
|
+
marketInfo: usdMarket,
|
|
502
|
+
collateralToken: usdMarket.shortToken,
|
|
503
|
+
isLong: true,
|
|
504
|
+
sizeDeltaUsd,
|
|
505
|
+
sizeDeltaInTokens,
|
|
506
|
+
});
|
|
507
|
+
expect(getOpenInterestForBalance(usdNext, true) - getOpenInterestForBalance(usdMarket, true)).toBe(sizeDeltaUsd);
|
|
508
|
+
const tokensMarket = buildMarket({ useOpenInterestInTokensForBalance: true });
|
|
509
|
+
const tokensNext = getMarketInfoWithOpenInterestDelta({
|
|
510
|
+
marketInfo: tokensMarket,
|
|
511
|
+
collateralToken: tokensMarket.shortToken,
|
|
512
|
+
isLong: true,
|
|
513
|
+
sizeDeltaUsd,
|
|
514
|
+
sizeDeltaInTokens,
|
|
515
|
+
});
|
|
516
|
+
const midPrice = (tokensMarket.indexToken.prices.minPrice + tokensMarket.indexToken.prices.maxPrice) / 2n;
|
|
517
|
+
expect(getOpenInterestForBalance(tokensNext, true) - getOpenInterestForBalance(tokensMarket, true)).toBe(convertToUsd(sizeDeltaInTokens, tokensMarket.indexToken.decimals, midPrice));
|
|
518
|
+
});
|
|
519
|
+
describe("per-collateral open interest projection", () => {
|
|
520
|
+
const sizeDeltaUsd = expandDecimals(500, 30);
|
|
521
|
+
const sizeDeltaInTokens = expandDecimals(5, 17);
|
|
522
|
+
const split = {
|
|
523
|
+
longInterestUsdUsingLongToken: expandDecimals(300, 30),
|
|
524
|
+
longInterestUsdUsingShortToken: expandDecimals(700, 30),
|
|
525
|
+
shortInterestUsdUsingLongToken: expandDecimals(1200, 30),
|
|
526
|
+
shortInterestUsdUsingShortToken: expandDecimals(800, 30),
|
|
527
|
+
longInterestInTokensUsingLongToken: expandDecimals(3, 17),
|
|
528
|
+
longInterestInTokensUsingShortToken: expandDecimals(7, 17),
|
|
529
|
+
shortInterestInTokensUsingLongToken: expandDecimals(12, 17),
|
|
530
|
+
shortInterestInTokensUsingShortToken: expandDecimals(8, 17),
|
|
531
|
+
};
|
|
532
|
+
it("moves a long increase with short-token collateral into the short-token leg", () => {
|
|
533
|
+
const marketInfo = buildMarket(split);
|
|
534
|
+
const next = getMarketInfoWithOpenInterestDelta({
|
|
535
|
+
marketInfo,
|
|
536
|
+
collateralToken: marketInfo.shortToken,
|
|
537
|
+
isLong: true,
|
|
538
|
+
sizeDeltaUsd,
|
|
539
|
+
sizeDeltaInTokens,
|
|
540
|
+
});
|
|
541
|
+
expect(next.longInterestUsdUsingShortToken).toBe(expandDecimals(1200, 30));
|
|
542
|
+
expect(next.longInterestInTokensUsingShortToken).toBe(expandDecimals(12, 17));
|
|
543
|
+
expect(next.longInterestUsdUsingLongToken).toBe(split.longInterestUsdUsingLongToken);
|
|
544
|
+
expect(next.longInterestInTokensUsingLongToken).toBe(split.longInterestInTokensUsingLongToken);
|
|
545
|
+
expect(next.longInterestUsd).toBe(next.longInterestUsdUsingLongToken + next.longInterestUsdUsingShortToken);
|
|
546
|
+
expect(next.longInterestInTokens).toBe(next.longInterestInTokensUsingLongToken + next.longInterestInTokensUsingShortToken);
|
|
547
|
+
expect(next.shortInterestUsdUsingLongToken).toBe(split.shortInterestUsdUsingLongToken);
|
|
548
|
+
expect(next.shortInterestUsdUsingShortToken).toBe(split.shortInterestUsdUsingShortToken);
|
|
549
|
+
});
|
|
550
|
+
it("moves a long increase with long-token collateral into the long-token leg", () => {
|
|
551
|
+
const marketInfo = buildMarket(split);
|
|
552
|
+
const next = getMarketInfoWithOpenInterestDelta({
|
|
553
|
+
marketInfo,
|
|
554
|
+
collateralToken: marketInfo.longToken,
|
|
555
|
+
isLong: true,
|
|
556
|
+
sizeDeltaUsd,
|
|
557
|
+
sizeDeltaInTokens,
|
|
558
|
+
});
|
|
559
|
+
expect(next.longInterestUsdUsingLongToken).toBe(expandDecimals(800, 30));
|
|
560
|
+
expect(next.longInterestInTokensUsingLongToken).toBe(expandDecimals(8, 17));
|
|
561
|
+
expect(next.longInterestUsdUsingShortToken).toBe(split.longInterestUsdUsingShortToken);
|
|
562
|
+
expect(next.longInterestInTokensUsingShortToken).toBe(split.longInterestInTokensUsingShortToken);
|
|
563
|
+
expect(next.longInterestUsd).toBe(next.longInterestUsdUsingLongToken + next.longInterestUsdUsingShortToken);
|
|
564
|
+
});
|
|
565
|
+
it("moves a short increase into the collateral leg of the short side", () => {
|
|
566
|
+
const marketInfo = buildMarket(split);
|
|
567
|
+
const next = getMarketInfoWithOpenInterestDelta({
|
|
568
|
+
marketInfo,
|
|
569
|
+
collateralToken: marketInfo.shortToken,
|
|
570
|
+
isLong: false,
|
|
571
|
+
sizeDeltaUsd,
|
|
572
|
+
sizeDeltaInTokens,
|
|
573
|
+
});
|
|
574
|
+
expect(next.shortInterestUsdUsingShortToken).toBe(expandDecimals(1300, 30));
|
|
575
|
+
expect(next.shortInterestInTokensUsingShortToken).toBe(expandDecimals(13, 17));
|
|
576
|
+
expect(next.shortInterestUsdUsingLongToken).toBe(split.shortInterestUsdUsingLongToken);
|
|
577
|
+
expect(next.shortInterestInTokensUsingLongToken).toBe(split.shortInterestInTokensUsingLongToken);
|
|
578
|
+
expect(next.shortInterestUsd).toBe(next.shortInterestUsdUsingLongToken + next.shortInterestUsdUsingShortToken);
|
|
579
|
+
expect(next.shortInterestInTokens).toBe(next.shortInterestInTokensUsingLongToken + next.shortInterestInTokensUsingShortToken);
|
|
580
|
+
expect(next.longInterestUsdUsingShortToken).toBe(split.longInterestUsdUsingShortToken);
|
|
581
|
+
});
|
|
582
|
+
it("keeps the per-collateral pnl split consistent with the projected totals", () => {
|
|
583
|
+
const marketInfo = buildMarket(split);
|
|
584
|
+
const next = getMarketInfoWithOpenInterestDelta({
|
|
585
|
+
marketInfo,
|
|
586
|
+
collateralToken: marketInfo.shortToken,
|
|
587
|
+
isLong: true,
|
|
588
|
+
sizeDeltaUsd,
|
|
589
|
+
sizeDeltaInTokens,
|
|
590
|
+
});
|
|
591
|
+
expect(getPositiveMarketPnl(next, true, false)).toBe(getMarketPnl(next, true, false));
|
|
592
|
+
});
|
|
593
|
+
it("leaves the per-collateral fields undefined when the market does not carry them", () => {
|
|
594
|
+
const marketInfo = buildMarket();
|
|
595
|
+
const next = getMarketInfoWithOpenInterestDelta({
|
|
596
|
+
marketInfo,
|
|
597
|
+
collateralToken: marketInfo.shortToken,
|
|
598
|
+
isLong: true,
|
|
599
|
+
sizeDeltaUsd,
|
|
600
|
+
sizeDeltaInTokens,
|
|
601
|
+
});
|
|
602
|
+
expect(next.longInterestUsdUsingShortToken).toBeUndefined();
|
|
603
|
+
expect(next.longInterestInTokensUsingShortToken).toBeUndefined();
|
|
604
|
+
expect(next.longInterestUsd).toBe(expandDecimals(1500, 30));
|
|
605
|
+
});
|
|
606
|
+
});
|
|
607
|
+
describe("virtual inventory projection", () => {
|
|
608
|
+
// the contract's `applyDeltaToVirtualInventoryForPositions` no-ops on a zero virtualTokenId
|
|
609
|
+
const VIRTUAL_INDEX_TOKEN_ID = `0x${"1".repeat(64)}`;
|
|
610
|
+
const sizeDeltaUsd = expandDecimals(500, 30);
|
|
611
|
+
const sizeDeltaInTokens = expandDecimals(5, 17);
|
|
612
|
+
it("decreases the net-short virtual inventory for a long increase on a usd-OI market", () => {
|
|
613
|
+
const marketInfo = buildMarket({
|
|
614
|
+
virtualIndexTokenId: VIRTUAL_INDEX_TOKEN_ID,
|
|
615
|
+
useOpenInterestInTokensForBalance: false,
|
|
616
|
+
virtualInventoryForPositions: expandDecimals(300, 30),
|
|
617
|
+
});
|
|
618
|
+
const next = getMarketInfoWithOpenInterestDelta({
|
|
619
|
+
marketInfo,
|
|
620
|
+
collateralToken: marketInfo.shortToken,
|
|
621
|
+
isLong: true,
|
|
622
|
+
sizeDeltaUsd,
|
|
623
|
+
sizeDeltaInTokens,
|
|
624
|
+
});
|
|
625
|
+
expect(next.virtualInventoryForPositions).toBe(-expandDecimals(200, 30));
|
|
626
|
+
expect(next.virtualInventoryForPositionsInTokens).toBe(marketInfo.virtualInventoryForPositionsInTokens);
|
|
627
|
+
});
|
|
628
|
+
it("increases the net-short virtual inventory for a short increase on a usd-OI market", () => {
|
|
629
|
+
const marketInfo = buildMarket({
|
|
630
|
+
virtualIndexTokenId: VIRTUAL_INDEX_TOKEN_ID,
|
|
631
|
+
useOpenInterestInTokensForBalance: false,
|
|
632
|
+
virtualInventoryForPositions: expandDecimals(300, 30),
|
|
633
|
+
});
|
|
634
|
+
const next = getMarketInfoWithOpenInterestDelta({
|
|
635
|
+
marketInfo,
|
|
636
|
+
collateralToken: marketInfo.shortToken,
|
|
637
|
+
isLong: false,
|
|
638
|
+
sizeDeltaUsd,
|
|
639
|
+
sizeDeltaInTokens,
|
|
640
|
+
});
|
|
641
|
+
expect(next.virtualInventoryForPositions).toBe(expandDecimals(800, 30));
|
|
642
|
+
});
|
|
643
|
+
it("shifts the token-denominated virtual inventory on a token-OI market", () => {
|
|
644
|
+
const marketInfo = buildMarket({
|
|
645
|
+
virtualIndexTokenId: VIRTUAL_INDEX_TOKEN_ID,
|
|
646
|
+
useOpenInterestInTokensForBalance: true,
|
|
647
|
+
virtualInventoryForPositionsInTokens: expandDecimals(1, 18),
|
|
648
|
+
});
|
|
649
|
+
const next = getMarketInfoWithOpenInterestDelta({
|
|
650
|
+
marketInfo,
|
|
651
|
+
collateralToken: marketInfo.shortToken,
|
|
652
|
+
isLong: true,
|
|
653
|
+
sizeDeltaUsd,
|
|
654
|
+
sizeDeltaInTokens,
|
|
655
|
+
});
|
|
656
|
+
expect(next.virtualInventoryForPositionsInTokens).toBe(expandDecimals(5, 17));
|
|
657
|
+
expect(next.virtualInventoryForPositions).toBe(marketInfo.virtualInventoryForPositions);
|
|
658
|
+
});
|
|
659
|
+
it("leaves the virtual inventory untouched when the market has none configured", () => {
|
|
660
|
+
// the default mock has virtualIndexTokenId = zeroHash and zero inventory
|
|
661
|
+
const marketInfo = buildMarket();
|
|
662
|
+
const next = getMarketInfoWithOpenInterestDelta({
|
|
663
|
+
marketInfo,
|
|
664
|
+
collateralToken: marketInfo.shortToken,
|
|
665
|
+
isLong: true,
|
|
666
|
+
sizeDeltaUsd,
|
|
667
|
+
sizeDeltaInTokens,
|
|
668
|
+
});
|
|
669
|
+
expect(next.virtualInventoryForPositions).toBe(0n);
|
|
670
|
+
expect(next.virtualInventoryForPositionsInTokens).toBe(0n);
|
|
671
|
+
});
|
|
672
|
+
});
|
|
673
|
+
});
|
|
@@ -42,10 +42,37 @@ function getLongPositionPnl(marketInfo) {
|
|
|
42
42
|
isLong: true,
|
|
43
43
|
});
|
|
44
44
|
}
|
|
45
|
+
describe("per-position pnl cap — pool pnl price side", () => {
|
|
46
|
+
// the contract passes maximize = true into getPositivePnl: a long's pool pnl is valued at the index
|
|
47
|
+
// max price, so with a spread the cap ratio is smaller than at the min price
|
|
48
|
+
const ETH_MAX_PRICE = usd(2200);
|
|
49
|
+
function createSpreadMarketInfo() {
|
|
50
|
+
const tokensData = mockTokensData({ ETH: { prices: { minPrice: ETH_PRICE, maxPrice: ETH_MAX_PRICE } } });
|
|
51
|
+
return mockMarketsInfoData(tokensData, [MARKET_KEY], {
|
|
52
|
+
[MARKET_KEY]: {
|
|
53
|
+
longPoolAmount: eth(500),
|
|
54
|
+
maxPnlFactorForTradersLong: expandDecimals(9, 29),
|
|
55
|
+
longInterestUsd: usd(1000000),
|
|
56
|
+
longInterestInTokens: eth(1000),
|
|
57
|
+
longInterestUsdUsingLongToken: usd(1000000),
|
|
58
|
+
longInterestInTokensUsingLongToken: eth(1000),
|
|
59
|
+
longInterestUsdUsingShortToken: 0n,
|
|
60
|
+
longInterestInTokensUsingShortToken: 0n,
|
|
61
|
+
},
|
|
62
|
+
})[MARKET_KEY];
|
|
63
|
+
}
|
|
64
|
+
it("values a long's pool pnl at the index max price, as the contract does", () => {
|
|
65
|
+
// pool pnl: 1 000 ETH bought for $1 000 000 → +$1 000 000 at the min price, +$1 200 000 at the max price;
|
|
66
|
+
// the cap is 90% of the $1 000 000 pool → the position keeps 900/1200 of its pnl, not 900/1000
|
|
67
|
+
const pnl = getLongPositionPnl(createSpreadMarketInfo());
|
|
68
|
+
expect(pnl).toBe(usd(75000));
|
|
69
|
+
expect(pnl).not.toBe(usd(90000));
|
|
70
|
+
});
|
|
71
|
+
});
|
|
45
72
|
describe("per-position pnl cap (v2.2c parity)", () => {
|
|
46
73
|
it("caps against the positive-only pool pnl", () => {
|
|
47
74
|
const marketInfo = createMarketInfo();
|
|
48
|
-
expect(getPositiveMarketPnl(marketInfo, true,
|
|
75
|
+
expect(getPositiveMarketPnl(marketInfo, true, false)).toBe(usd(1000000));
|
|
49
76
|
expect(getLongPositionPnl(marketInfo)).toBe(usd(90000));
|
|
50
77
|
});
|
|
51
78
|
it("leaves the net pool pnl used by gm pool value untouched", () => {
|
|
@@ -59,7 +86,7 @@ describe("per-position pnl cap (v2.2c parity)", () => {
|
|
|
59
86
|
longInterestUsdUsingShortToken: undefined,
|
|
60
87
|
longInterestInTokensUsingShortToken: undefined,
|
|
61
88
|
});
|
|
62
|
-
expect(getPositiveMarketPnl(marketInfo, true,
|
|
89
|
+
expect(getPositiveMarketPnl(marketInfo, true, false)).toBe(usd(500000));
|
|
63
90
|
expect(getLongPositionPnl(marketInfo)).toBe(usd(100000));
|
|
64
91
|
});
|
|
65
92
|
it("matches the net pool pnl when both collateral buckets are winning", () => {
|
|
@@ -69,7 +96,7 @@ describe("per-position pnl cap (v2.2c parity)", () => {
|
|
|
69
96
|
longInterestUsd: usd(2000000),
|
|
70
97
|
longInterestInTokens: eth(1600),
|
|
71
98
|
});
|
|
72
|
-
expect(getPositiveMarketPnl(marketInfo, true,
|
|
99
|
+
expect(getPositiveMarketPnl(marketInfo, true, false)).toBe(usd(1200000));
|
|
73
100
|
expect(getMarketPnl(marketInfo, true, true)).toBe(usd(1200000));
|
|
74
101
|
expect(getLongPositionPnl(marketInfo)).toBe(usd(75000));
|
|
75
102
|
});
|
|
@@ -5,14 +5,14 @@ import { getCappedPoolPnl, getPoolUsdWithoutPnl, getPositiveMarketPnl } from "..
|
|
|
5
5
|
import { expandDecimals, USD_DECIMALS } from "../numbers";
|
|
6
6
|
import { getEntryPrice, getLeverage, getLiquidationPrice, getMinCollateralUsdForLiquidationPrice, getPositionKey, getPositionNetValue, getPositionNetValueAfterAllFees, getPositionPendingFeesUsd, getPositionPnlAfterAllFees, getPositionPnlAfterFees, getPositionPnlUsd, getPositionValueUsd, parsePositionKey, } from "../positions";
|
|
7
7
|
import { convertToUsd, getIsEquivalentTokens } from "../tokens";
|
|
8
|
-
vi.mock("../markets", () => ({
|
|
9
|
-
...vi.importActual("../markets"),
|
|
8
|
+
vi.mock("../markets", async () => ({
|
|
9
|
+
...(await vi.importActual("../markets")),
|
|
10
10
|
getPositiveMarketPnl: vi.fn(),
|
|
11
11
|
getPoolUsdWithoutPnl: vi.fn(),
|
|
12
12
|
getCappedPoolPnl: vi.fn(),
|
|
13
13
|
}));
|
|
14
|
-
vi.mock("../tokens", () => ({
|
|
15
|
-
...vi.importActual("../tokens"),
|
|
14
|
+
vi.mock("../tokens", async () => ({
|
|
15
|
+
...(await vi.importActual("../tokens")),
|
|
16
16
|
convertToUsd: vi.fn(),
|
|
17
17
|
getIsEquivalentTokens: vi.fn(),
|
|
18
18
|
}));
|
|
@@ -83,7 +83,7 @@ describe("getPositionPnlUsd", () => {
|
|
|
83
83
|
markPrice: 10n,
|
|
84
84
|
isLong: true,
|
|
85
85
|
});
|
|
86
|
-
expect(getPositiveMarketPnl).toHaveBeenCalledWith(marketInfo, true,
|
|
86
|
+
expect(getPositiveMarketPnl).toHaveBeenCalledWith(marketInfo, true, false);
|
|
87
87
|
expect(result).toBe(expandDecimals(800, USD_DECIMALS));
|
|
88
88
|
});
|
|
89
89
|
});
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { describe, expect, it } from "vitest";
|
|
2
2
|
import { OrderType } from "../orders/types";
|
|
3
|
-
import { getMarkPrice, getOrderThresholdType, getShouldUseMaxPrice } from "../prices";
|
|
3
|
+
import { getIncreaseEvaluationIndexPrice, getIsIncreaseOrderExecutableNow, getMarkPrice, getOrderThresholdType, getShouldUseMaxPrice, } from "../prices";
|
|
4
4
|
import { TriggerThresholdType } from "../trade/types";
|
|
5
5
|
describe("getMarkPrice", () => {
|
|
6
6
|
it("returns maxPrice if getShouldUseMaxPrice => true", () => {
|
|
@@ -66,3 +66,100 @@ describe("getTriggerThresholdType", () => {
|
|
|
66
66
|
expect(result).toBeUndefined();
|
|
67
67
|
});
|
|
68
68
|
});
|
|
69
|
+
describe("getIsIncreaseOrderExecutableNow", () => {
|
|
70
|
+
const prices = { minPrice: 1000n, maxPrice: 1100n };
|
|
71
|
+
it("is always true for a market increase, regardless of the trigger price", () => {
|
|
72
|
+
expect(getIsIncreaseOrderExecutableNow({
|
|
73
|
+
orderType: OrderType.MarketIncrease,
|
|
74
|
+
isLong: true,
|
|
75
|
+
triggerPrice: undefined,
|
|
76
|
+
indexTokenPrices: prices,
|
|
77
|
+
})).toBe(true);
|
|
78
|
+
});
|
|
79
|
+
it("is false without a usable trigger price", () => {
|
|
80
|
+
for (const triggerPrice of [undefined, 0n, -1n]) {
|
|
81
|
+
expect(getIsIncreaseOrderExecutableNow({
|
|
82
|
+
orderType: OrderType.LimitIncrease,
|
|
83
|
+
isLong: true,
|
|
84
|
+
triggerPrice,
|
|
85
|
+
indexTokenPrices: prices,
|
|
86
|
+
})).toBe(false);
|
|
87
|
+
}
|
|
88
|
+
});
|
|
89
|
+
it("is false for order types that are not increases", () => {
|
|
90
|
+
expect(getIsIncreaseOrderExecutableNow({
|
|
91
|
+
orderType: OrderType.LimitDecrease,
|
|
92
|
+
isLong: true,
|
|
93
|
+
triggerPrice: 5000n,
|
|
94
|
+
indexTokenPrices: prices,
|
|
95
|
+
})).toBe(false);
|
|
96
|
+
expect(getIsIncreaseOrderExecutableNow({
|
|
97
|
+
orderType: undefined,
|
|
98
|
+
isLong: true,
|
|
99
|
+
triggerPrice: 5000n,
|
|
100
|
+
indexTokenPrices: prices,
|
|
101
|
+
})).toBe(false);
|
|
102
|
+
});
|
|
103
|
+
// a long limit increase buys at maxPrice, so it triggers once the max price falls to the trigger
|
|
104
|
+
it.each([
|
|
105
|
+
["long, max above the trigger", true, 1099n, false],
|
|
106
|
+
["long, max exactly at the trigger", true, 1100n, true],
|
|
107
|
+
["long, max below the trigger", true, 1101n, true],
|
|
108
|
+
// a short limit increase sells at minPrice, so it triggers once the min price rises to the trigger
|
|
109
|
+
["short, min below the trigger", false, 1001n, false],
|
|
110
|
+
["short, min exactly at the trigger", false, 1000n, true],
|
|
111
|
+
["short, min above the trigger", false, 999n, true],
|
|
112
|
+
])("LimitIncrease — %s", (_name, isLong, triggerPrice, expected) => {
|
|
113
|
+
expect(getIsIncreaseOrderExecutableNow({
|
|
114
|
+
orderType: OrderType.LimitIncrease,
|
|
115
|
+
isLong,
|
|
116
|
+
triggerPrice,
|
|
117
|
+
indexTokenPrices: prices,
|
|
118
|
+
})).toBe(expected);
|
|
119
|
+
});
|
|
120
|
+
it.each([
|
|
121
|
+
["long, max below the trigger", true, 1101n, false],
|
|
122
|
+
["long, max exactly at the trigger", true, 1100n, true],
|
|
123
|
+
["long, max above the trigger", true, 1099n, true],
|
|
124
|
+
["short, min above the trigger", false, 999n, false],
|
|
125
|
+
["short, min exactly at the trigger", false, 1000n, true],
|
|
126
|
+
["short, min below the trigger", false, 1001n, true],
|
|
127
|
+
])("StopIncrease — %s", (_name, isLong, triggerPrice, expected) => {
|
|
128
|
+
expect(getIsIncreaseOrderExecutableNow({
|
|
129
|
+
orderType: OrderType.StopIncrease,
|
|
130
|
+
isLong,
|
|
131
|
+
triggerPrice,
|
|
132
|
+
indexTokenPrices: prices,
|
|
133
|
+
})).toBe(expected);
|
|
134
|
+
});
|
|
135
|
+
});
|
|
136
|
+
describe("getIncreaseEvaluationIndexPrice", () => {
|
|
137
|
+
it("keeps current prices for a market increase", () => {
|
|
138
|
+
expect(getIncreaseEvaluationIndexPrice({
|
|
139
|
+
orderType: OrderType.MarketIncrease,
|
|
140
|
+
triggerPrice: undefined,
|
|
141
|
+
})).toBeUndefined();
|
|
142
|
+
expect(getIncreaseEvaluationIndexPrice({
|
|
143
|
+
orderType: OrderType.MarketIncrease,
|
|
144
|
+
triggerPrice: 1000n,
|
|
145
|
+
})).toBeUndefined();
|
|
146
|
+
});
|
|
147
|
+
it("keeps current prices without a usable trigger price", () => {
|
|
148
|
+
for (const triggerPrice of [undefined, 0n, -1n]) {
|
|
149
|
+
expect(getIncreaseEvaluationIndexPrice({
|
|
150
|
+
orderType: OrderType.LimitIncrease,
|
|
151
|
+
triggerPrice,
|
|
152
|
+
})).toBeUndefined();
|
|
153
|
+
}
|
|
154
|
+
});
|
|
155
|
+
it.each([
|
|
156
|
+
// amounts are always sized at the trigger price, so the evaluation stays there as well —
|
|
157
|
+
// including the window where the order is already executable (prices 1000 / 1100)
|
|
158
|
+
[OrderType.LimitIncrease, 900n, 900n],
|
|
159
|
+
[OrderType.LimitIncrease, 1200n, 1200n],
|
|
160
|
+
[OrderType.StopIncrease, 1200n, 1200n],
|
|
161
|
+
[OrderType.StopIncrease, 900n, 900n],
|
|
162
|
+
])("orderType=%s trigger=%s → %s", (orderType, triggerPrice, expected) => {
|
|
163
|
+
expect(getIncreaseEvaluationIndexPrice({ orderType, triggerPrice })).toBe(expected);
|
|
164
|
+
});
|
|
165
|
+
});
|
|
@@ -25,17 +25,20 @@ export function getSwapFee(marketInfo, swapAmount, balanceWasImproved, swapPrici
|
|
|
25
25
|
}
|
|
26
26
|
return applyFactor(swapAmount, factor);
|
|
27
27
|
}
|
|
28
|
-
export function getPositionFee(marketInfo, sizeDeltaUsd, balanceWasImproved, referralInfo, uiFeeFactor) {
|
|
28
|
+
export function getPositionFee(marketInfo, sizeDeltaUsd, balanceWasImproved, referralInfo, uiFeeFactor, proDiscountFactor) {
|
|
29
29
|
const factor = balanceWasImproved
|
|
30
30
|
? marketInfo.positionFeeFactorForBalanceWasImproved
|
|
31
31
|
: marketInfo.positionFeeFactorForBalanceWasNotImproved;
|
|
32
32
|
let positionFeeUsd = applyFactor(sizeDeltaUsd, factor);
|
|
33
33
|
const uiFeeUsd = applyFactor(sizeDeltaUsd, uiFeeFactor ?? 0n);
|
|
34
|
-
|
|
35
|
-
|
|
34
|
+
const totalRebateUsd = referralInfo ? applyFactor(positionFeeUsd, referralInfo.totalRebateFactor) : 0n;
|
|
35
|
+
let discountUsd = referralInfo ? applyFactor(totalRebateUsd, referralInfo.discountFactor) : 0n;
|
|
36
|
+
if (proDiscountFactor !== undefined && proDiscountFactor > 0n) {
|
|
37
|
+
const proDiscountUsd = applyFactor(positionFeeUsd, proDiscountFactor);
|
|
38
|
+
if (proDiscountUsd > discountUsd) {
|
|
39
|
+
discountUsd = proDiscountUsd;
|
|
40
|
+
}
|
|
36
41
|
}
|
|
37
|
-
const totalRebateUsd = applyFactor(positionFeeUsd, referralInfo.totalRebateFactor);
|
|
38
|
-
const discountUsd = applyFactor(totalRebateUsd, referralInfo.discountFactor);
|
|
39
42
|
positionFeeUsd = positionFeeUsd - discountUsd;
|
|
40
43
|
return {
|
|
41
44
|
positionFeeUsd,
|