@gainsnetwork/sdk 0.2.71-rc4 → 1.0.0-rc1
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 +2 -2
- package/lib/backend/globalTrades/index.d.ts +11 -0
- package/lib/backend/globalTrades/index.js +69 -0
- package/lib/backend/index.d.ts +3 -0
- package/lib/backend/index.js +28 -0
- package/lib/backend/tradingVariables/backend.types.d.ts +318 -0
- package/lib/backend/tradingVariables/backend.types.js +2 -0
- package/lib/backend/tradingVariables/converter.d.ts +34 -0
- package/lib/backend/tradingVariables/converter.js +338 -0
- package/lib/backend/tradingVariables/index.d.ts +5 -0
- package/lib/backend/tradingVariables/index.js +96 -0
- package/lib/backend/tradingVariables/types.d.ts +113 -0
- package/lib/backend/tradingVariables/types.js +14 -0
- package/lib/constants.d.ts +0 -3
- package/lib/constants.js +7 -9
- package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +1911 -0
- package/lib/contracts/types/generated/GFarmTradingStorageV5.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +1067 -0
- package/lib/contracts/types/generated/GNSBorrowingFees.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +979 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +2 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +1058 -0
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +2 -0
- package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +2134 -293
- package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +533 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6.js +2 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +613 -0
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +2 -0
- package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +911 -0
- package/lib/contracts/types/generated/GNSPairInfosV6_1.js +2 -0
- package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +660 -0
- package/lib/contracts/types/generated/GNSPairsStorageV6.js +2 -0
- package/lib/contracts/types/generated/GNSTrading.d.ts +758 -0
- package/lib/contracts/types/generated/GNSTrading.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +875 -0
- package/lib/contracts/types/generated/GNSTradingCallbacks.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +806 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +2 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +821 -0
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +2 -0
- package/lib/contracts/types/generated/GNSTradingStorage.d.ts +1387 -0
- package/lib/contracts/types/generated/GNSTradingStorage.js +2 -0
- package/lib/contracts/types/generated/GToken.d.ts +78 -107
- package/lib/contracts/types/generated/GTokenV6_3_2.d.ts +1838 -0
- package/lib/contracts/types/generated/GTokenV6_3_2.js +2 -0
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +83 -0
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +2691 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.d.ts +88 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.js +1654 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.js +1742 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +124 -0
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +1784 -0
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.d.ts +90 -53
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +4496 -430
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +1116 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +100 -0
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +1003 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +98 -0
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +1485 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +117 -0
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +1265 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +1273 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +82 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +1326 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +113 -0
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +1428 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +96 -0
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +2241 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +95 -0
- package/lib/contracts/types/generated/factories/GNSTrading__factory.js +1071 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +110 -0
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +2682 -0
- package/lib/contracts/types/generated/factories/GToken__factory.d.ts +7 -0
- package/lib/contracts/types/generated/factories/GToken__factory.js +69 -142
- package/lib/contracts/types/index.d.ts +2 -1
- package/lib/contracts/types/index.js +1 -0
- package/lib/contracts/utils/openTrades.d.ts +1 -0
- package/lib/contracts/utils/openTrades.js +94 -56
- package/lib/contracts/utils/pairs.js +0 -3
- package/lib/index.d.ts +2 -0
- package/lib/index.js +5 -0
- package/lib/markets/collateral/converter.d.ts +5 -0
- package/lib/markets/collateral/converter.js +11 -0
- package/lib/markets/collateral/index.d.ts +1 -0
- package/lib/markets/collateral/index.js +17 -0
- package/lib/markets/collateral/types.d.ts +7 -0
- package/lib/markets/collateral/types.js +2 -0
- package/lib/markets/holdingFees/index.d.ts +46 -0
- package/lib/markets/holdingFees/index.js +105 -0
- package/lib/markets/holdingFees/types.d.ts +23 -0
- package/lib/markets/holdingFees/types.js +5 -0
- package/lib/markets/index.d.ts +5 -0
- package/lib/markets/index.js +5 -0
- package/lib/markets/leverage/builder.d.ts +12 -0
- package/lib/markets/leverage/builder.js +25 -0
- package/lib/markets/leverage/getMarketLeverageRestrictions.d.ts +7 -0
- package/lib/markets/leverage/getMarketLeverageRestrictions.js +38 -0
- package/lib/markets/leverage/index.d.ts +3 -0
- package/lib/markets/leverage/index.js +19 -0
- package/lib/markets/leverage/types.d.ts +15 -0
- package/lib/markets/leverage/types.js +2 -0
- package/lib/markets/oi/converter.d.ts +62 -0
- package/lib/markets/oi/converter.js +102 -0
- package/lib/markets/oi/fetcher.d.ts +58 -0
- package/lib/markets/oi/fetcher.js +181 -0
- package/lib/markets/oi/index.d.ts +49 -0
- package/lib/markets/oi/index.js +77 -0
- package/lib/markets/oi/types.d.ts +73 -0
- package/lib/markets/oi/types.js +6 -0
- package/lib/markets/oi/validation.d.ts +80 -0
- package/lib/markets/oi/validation.js +172 -0
- package/lib/markets/price/builder.d.ts +25 -0
- package/lib/markets/price/builder.js +69 -0
- package/lib/markets/price/index.d.ts +6 -0
- package/lib/markets/price/index.js +22 -0
- package/lib/markets/price/marketPrice.d.ts +13 -0
- package/lib/markets/price/marketPrice.js +35 -0
- package/lib/markets/price/types.d.ts +23 -0
- package/lib/markets/price/types.js +5 -0
- package/lib/trade/counterTrade/index.d.ts +2 -0
- package/lib/trade/counterTrade/index.js +18 -0
- package/lib/trade/counterTrade/types.d.ts +7 -0
- package/lib/trade/counterTrade/types.js +2 -0
- package/lib/trade/counterTrade/validateCounterTrade.d.ts +10 -0
- package/lib/trade/counterTrade/validateCounterTrade.js +29 -0
- package/lib/trade/effectiveLeverage/builder.d.ts +23 -0
- package/lib/trade/effectiveLeverage/builder.js +30 -0
- package/lib/trade/effectiveLeverage/getTradeNewEffectiveLeverage.d.ts +23 -0
- package/lib/trade/effectiveLeverage/getTradeNewEffectiveLeverage.js +64 -0
- package/lib/trade/effectiveLeverage/index.d.ts +3 -0
- package/lib/trade/effectiveLeverage/index.js +22 -0
- package/lib/trade/effectiveLeverage/types.d.ts +33 -0
- package/lib/trade/effectiveLeverage/types.js +2 -0
- package/lib/trade/fees/borrowing/builder.d.ts +14 -0
- package/lib/trade/fees/borrowing/builder.js +33 -0
- package/lib/trade/fees/borrowing/index.d.ts +23 -2
- package/lib/trade/fees/borrowing/index.js +50 -16
- package/lib/trade/fees/borrowingV2/builder.d.ts +6 -0
- package/lib/trade/fees/borrowingV2/builder.js +24 -0
- package/lib/trade/fees/borrowingV2/converter.d.ts +75 -0
- package/lib/trade/fees/borrowingV2/converter.js +132 -0
- package/lib/trade/fees/borrowingV2/fetcher.d.ts +75 -0
- package/lib/trade/fees/borrowingV2/fetcher.js +185 -0
- package/lib/trade/fees/borrowingV2/index.d.ts +48 -0
- package/lib/trade/fees/borrowingV2/index.js +112 -0
- package/lib/trade/fees/borrowingV2/types.d.ts +95 -0
- package/lib/trade/fees/borrowingV2/types.js +5 -0
- package/lib/trade/fees/converter.d.ts +48 -0
- package/lib/trade/fees/converter.js +114 -0
- package/lib/trade/fees/fundingFees/builder.d.ts +9 -0
- package/lib/trade/fees/fundingFees/builder.js +35 -0
- package/lib/trade/fees/fundingFees/converter.d.ts +102 -0
- package/lib/trade/fees/fundingFees/converter.js +196 -0
- package/lib/trade/fees/fundingFees/fetcher.d.ts +66 -0
- package/lib/trade/fees/fundingFees/fetcher.js +150 -0
- package/lib/trade/fees/fundingFees/index.d.ts +124 -0
- package/lib/trade/fees/fundingFees/index.js +309 -0
- package/lib/trade/fees/fundingFees/pairContext.d.ts +33 -0
- package/lib/trade/fees/fundingFees/pairContext.js +17 -0
- package/lib/trade/fees/fundingFees/types.d.ts +77 -0
- package/lib/trade/fees/fundingFees/types.js +5 -0
- package/lib/trade/fees/holdingFees/index.d.ts +46 -0
- package/lib/trade/fees/holdingFees/index.js +105 -0
- package/lib/trade/fees/holdingFees/types.d.ts +23 -0
- package/lib/trade/fees/holdingFees/types.js +5 -0
- package/lib/trade/fees/index.d.ts +8 -2
- package/lib/trade/fees/index.js +67 -16
- package/lib/trade/fees/tiers/converter.d.ts +54 -0
- package/lib/trade/fees/tiers/converter.js +81 -0
- package/lib/trade/fees/tiers/index.d.ts +18 -0
- package/lib/trade/fees/tiers/index.js +45 -1
- package/lib/trade/fees/trading/builder.d.ts +18 -0
- package/lib/trade/fees/trading/builder.js +20 -0
- package/lib/trade/fees/trading/converter.d.ts +32 -0
- package/lib/trade/fees/trading/converter.js +47 -0
- package/lib/trade/fees/trading/holdingFees.d.ts +28 -0
- package/lib/trade/fees/trading/holdingFees.js +66 -0
- package/lib/trade/fees/trading/holdingFeesStructured.d.ts +28 -0
- package/lib/trade/fees/trading/holdingFeesStructured.js +66 -0
- package/lib/trade/fees/trading/index.d.ts +62 -0
- package/lib/trade/fees/trading/index.js +155 -0
- package/lib/trade/fees/trading/types.d.ts +48 -0
- package/lib/trade/fees/trading/types.js +5 -0
- package/lib/trade/index.d.ts +5 -2
- package/lib/trade/index.js +5 -2
- package/lib/trade/liquidation/builder.d.ts +25 -0
- package/lib/trade/liquidation/builder.js +59 -0
- package/lib/trade/liquidation/converter.d.ts +23 -0
- package/lib/trade/liquidation/converter.js +46 -0
- package/lib/trade/liquidation/index.d.ts +29 -0
- package/lib/trade/liquidation/index.js +218 -0
- package/lib/trade/liquidation/types.d.ts +43 -0
- package/lib/trade/liquidation/types.js +2 -0
- package/lib/trade/pnl/builder.d.ts +16 -0
- package/lib/trade/pnl/builder.js +44 -0
- package/lib/trade/pnl/converter.d.ts +47 -0
- package/lib/trade/pnl/converter.js +72 -0
- package/lib/trade/pnl/index.d.ts +89 -0
- package/lib/trade/pnl/index.js +302 -0
- package/lib/trade/pnl/types.d.ts +79 -0
- package/lib/trade/pnl/types.js +5 -0
- package/lib/trade/priceImpact/close/builder.d.ts +23 -0
- package/lib/trade/priceImpact/close/builder.js +45 -0
- package/lib/trade/priceImpact/close/index.d.ts +22 -0
- package/lib/trade/priceImpact/close/index.js +134 -0
- package/lib/trade/priceImpact/close/types.d.ts +47 -0
- package/lib/trade/priceImpact/close/types.js +5 -0
- package/lib/trade/priceImpact/cumulVol/builder.d.ts +22 -0
- package/lib/trade/priceImpact/cumulVol/builder.js +43 -0
- package/lib/trade/priceImpact/cumulVol/converter.d.ts +31 -0
- package/lib/trade/priceImpact/cumulVol/converter.js +59 -0
- package/lib/trade/priceImpact/cumulVol/index.d.ts +108 -0
- package/lib/trade/priceImpact/cumulVol/index.js +235 -0
- package/lib/trade/priceImpact/index.d.ts +21 -0
- package/lib/trade/priceImpact/index.js +79 -0
- package/lib/trade/priceImpact/open/builder.d.ts +21 -0
- package/lib/trade/priceImpact/open/builder.js +43 -0
- package/lib/trade/priceImpact/open/index.d.ts +23 -0
- package/lib/trade/priceImpact/open/index.js +78 -0
- package/lib/trade/priceImpact/open/types.d.ts +44 -0
- package/lib/trade/priceImpact/open/types.js +5 -0
- package/lib/trade/priceImpact/skew/builder.d.ts +12 -0
- package/lib/trade/priceImpact/skew/builder.js +28 -0
- package/lib/trade/priceImpact/skew/converter.d.ts +46 -0
- package/lib/trade/priceImpact/skew/converter.js +81 -0
- package/lib/trade/priceImpact/skew/fetcher.d.ts +60 -0
- package/lib/trade/priceImpact/skew/fetcher.js +169 -0
- package/lib/trade/priceImpact/skew/index.d.ts +53 -0
- package/lib/trade/priceImpact/skew/index.js +148 -0
- package/lib/trade/priceImpact/skew/types.d.ts +44 -0
- package/lib/trade/priceImpact/skew/types.js +5 -0
- package/lib/trade/spread.d.ts +5 -18
- package/lib/trade/spread.js +17 -106
- package/lib/trade/types.d.ts +109 -12
- package/lib/trade/types.js +0 -3
- package/lib/trade/utils.d.ts +18 -0
- package/lib/trade/utils.js +30 -0
- package/lib/vault/index.d.ts +3 -1
- package/lib/vault/index.js +2 -2
- package/package.json +2 -1
- package/lib/trade/liquidation.d.ts +0 -12
- package/lib/trade/liquidation.js +0 -55
- package/lib/trade/pnl.d.ts +0 -10
- package/lib/trade/pnl.js +0 -33
|
@@ -0,0 +1,148 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Skew price impact calculations for v10+ trades
|
|
4
|
+
* @dev Based on formula: (existingSkew + tradeSize/2) / skewDepth
|
|
5
|
+
*/
|
|
6
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
7
|
+
if (k2 === undefined) k2 = k;
|
|
8
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
9
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
10
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
11
|
+
}
|
|
12
|
+
Object.defineProperty(o, k2, desc);
|
|
13
|
+
}) : (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
o[k2] = m[k];
|
|
16
|
+
}));
|
|
17
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
18
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
19
|
+
}) : function(o, v) {
|
|
20
|
+
o["default"] = v;
|
|
21
|
+
});
|
|
22
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
23
|
+
if (mod && mod.__esModule) return mod;
|
|
24
|
+
var result = {};
|
|
25
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
26
|
+
__setModuleDefault(result, mod);
|
|
27
|
+
return result;
|
|
28
|
+
};
|
|
29
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
30
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
31
|
+
};
|
|
32
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
33
|
+
exports.SkewPriceImpact = exports.calculatePartialSizeToken = exports.getTradeSkewPriceImpact = exports.calculateSkewPriceImpactP = exports.getTradeSkewDirection = exports.getNetSkewCollateral = exports.getNetSkewToken = void 0;
|
|
34
|
+
// Constants
|
|
35
|
+
const PRICE_IMPACT_DIVIDER = 2; // Half price impact to match cumulative volume impact scale
|
|
36
|
+
/**
|
|
37
|
+
* @dev Calculates net skew in tokens (long - short)
|
|
38
|
+
* @param pairOi Pair OI data with long and short token amounts
|
|
39
|
+
* @returns Net skew in tokens (positive = long heavy, negative = short heavy)
|
|
40
|
+
*/
|
|
41
|
+
const getNetSkewToken = (pairOi) => {
|
|
42
|
+
return pairOi.oiLongToken - pairOi.oiShortToken;
|
|
43
|
+
};
|
|
44
|
+
exports.getNetSkewToken = getNetSkewToken;
|
|
45
|
+
/**
|
|
46
|
+
* @dev Calculates net skew in collateral
|
|
47
|
+
* @param netSkewToken Net skew in tokens
|
|
48
|
+
* @param currentPrice Current pair price
|
|
49
|
+
* @returns Net skew in collateral
|
|
50
|
+
*/
|
|
51
|
+
const getNetSkewCollateral = (netSkewToken, currentPrice) => {
|
|
52
|
+
return netSkewToken * currentPrice;
|
|
53
|
+
};
|
|
54
|
+
exports.getNetSkewCollateral = getNetSkewCollateral;
|
|
55
|
+
/**
|
|
56
|
+
* @dev Determines trade direction impact on skew
|
|
57
|
+
* @param long Is long position
|
|
58
|
+
* @param open Is opening (true) or closing (false)
|
|
59
|
+
* @returns Whether trade increases or decreases skew
|
|
60
|
+
*/
|
|
61
|
+
const getTradeSkewDirection = (long, open) => {
|
|
62
|
+
// Opening long or closing short increases positive skew
|
|
63
|
+
// Opening short or closing long increases negative skew
|
|
64
|
+
return (long && open) || (!long && !open);
|
|
65
|
+
};
|
|
66
|
+
exports.getTradeSkewDirection = getTradeSkewDirection;
|
|
67
|
+
/**
|
|
68
|
+
* @dev Core skew price impact calculation
|
|
69
|
+
* @param existingSkewToken Current net skew in tokens (signed)
|
|
70
|
+
* @param tradeSizeToken Trade size in tokens (always positive)
|
|
71
|
+
* @param skewDepth Skew depth in tokens
|
|
72
|
+
* @param tradePositiveSkew Whether trade increases skew in its direction
|
|
73
|
+
* @returns Price impact percentage (can be positive or negative)
|
|
74
|
+
*/
|
|
75
|
+
const calculateSkewPriceImpactP = (existingSkewToken, tradeSizeToken, skewDepth, tradePositiveSkew) => {
|
|
76
|
+
if (skewDepth === 0) {
|
|
77
|
+
return 0; // No impact if depth is 0
|
|
78
|
+
}
|
|
79
|
+
// Convert signed values based on trade direction
|
|
80
|
+
const tradeSkewMultiplier = tradePositiveSkew ? 1 : -1;
|
|
81
|
+
const signedExistingSkew = existingSkewToken;
|
|
82
|
+
const signedTradeSize = tradeSizeToken * tradeSkewMultiplier;
|
|
83
|
+
// (existingSkew + tradeSize/2) / skewDepth
|
|
84
|
+
const numerator = signedExistingSkew + signedTradeSize / 2;
|
|
85
|
+
const priceImpactP = numerator / skewDepth;
|
|
86
|
+
// Apply divider to match cumulative volume impact scale
|
|
87
|
+
return priceImpactP / PRICE_IMPACT_DIVIDER;
|
|
88
|
+
};
|
|
89
|
+
exports.calculateSkewPriceImpactP = calculateSkewPriceImpactP;
|
|
90
|
+
/**
|
|
91
|
+
* @dev Main function to calculate skew price impact for a trade
|
|
92
|
+
* @param input Trade parameters
|
|
93
|
+
* @param context Skew price impact context with depths and OI data
|
|
94
|
+
* @returns Skew price impact result
|
|
95
|
+
*/
|
|
96
|
+
const getTradeSkewPriceImpact = (input, context) => {
|
|
97
|
+
// Get skew depth and pair OI from simplified context
|
|
98
|
+
const { skewDepth, pairOiToken: pairOi } = context;
|
|
99
|
+
// Calculate net skew
|
|
100
|
+
const netSkewToken = (0, exports.getNetSkewToken)(pairOi);
|
|
101
|
+
// Determine trade direction
|
|
102
|
+
const tradePositiveSkew = (0, exports.getTradeSkewDirection)(input.long, input.open);
|
|
103
|
+
// Calculate price impact
|
|
104
|
+
const basePriceImpactP = (0, exports.calculateSkewPriceImpactP)(netSkewToken, 0, skewDepth, tradePositiveSkew);
|
|
105
|
+
// Calculate price impact
|
|
106
|
+
const totalPriceImpactP = (0, exports.calculateSkewPriceImpactP)(netSkewToken, input.positionSizeToken, skewDepth, tradePositiveSkew);
|
|
107
|
+
const tradePriceImpactP = totalPriceImpactP - basePriceImpactP;
|
|
108
|
+
// Determine trade direction relative to skew
|
|
109
|
+
let tradeDirection;
|
|
110
|
+
if (totalPriceImpactP > 0) {
|
|
111
|
+
tradeDirection = "increase";
|
|
112
|
+
}
|
|
113
|
+
else if (totalPriceImpactP < 0) {
|
|
114
|
+
tradeDirection = "decrease";
|
|
115
|
+
}
|
|
116
|
+
else {
|
|
117
|
+
tradeDirection = "neutral";
|
|
118
|
+
}
|
|
119
|
+
return {
|
|
120
|
+
basePriceImpactP,
|
|
121
|
+
tradePriceImpactP,
|
|
122
|
+
totalPriceImpactP,
|
|
123
|
+
netSkewToken,
|
|
124
|
+
netSkewCollateral: 0,
|
|
125
|
+
tradeDirection,
|
|
126
|
+
};
|
|
127
|
+
};
|
|
128
|
+
exports.getTradeSkewPriceImpact = getTradeSkewPriceImpact;
|
|
129
|
+
/**
|
|
130
|
+
* @dev Calculate position sizes for partial operations
|
|
131
|
+
* @param originalSizeCollateral Original position size in collateral
|
|
132
|
+
* @param deltaCollateral Position size delta in collateral
|
|
133
|
+
* @param originalSizeToken Original position size in tokens
|
|
134
|
+
* @returns Delta in tokens proportional to collateral delta
|
|
135
|
+
*/
|
|
136
|
+
const calculatePartialSizeToken = (originalSizeCollateral, deltaCollateral, originalSizeToken) => {
|
|
137
|
+
if (originalSizeCollateral === 0) {
|
|
138
|
+
return 0;
|
|
139
|
+
}
|
|
140
|
+
// For partial close/add, token delta is proportional to collateral delta
|
|
141
|
+
return (deltaCollateral * originalSizeToken) / originalSizeCollateral;
|
|
142
|
+
};
|
|
143
|
+
exports.calculatePartialSizeToken = calculatePartialSizeToken;
|
|
144
|
+
// Export namespace for types
|
|
145
|
+
exports.SkewPriceImpact = __importStar(require("./types"));
|
|
146
|
+
__exportStar(require("./converter"), exports);
|
|
147
|
+
__exportStar(require("./builder"), exports);
|
|
148
|
+
__exportStar(require("./fetcher"), exports);
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Skew price impact types for v10+ trades
|
|
3
|
+
*/
|
|
4
|
+
export type PairOiToken = {
|
|
5
|
+
oiLongToken: number;
|
|
6
|
+
oiShortToken: number;
|
|
7
|
+
};
|
|
8
|
+
export type PairOiCollateral = {
|
|
9
|
+
oiLongCollateral: number;
|
|
10
|
+
oiShortCollateral: number;
|
|
11
|
+
};
|
|
12
|
+
export type SkewPriceImpactInput = {
|
|
13
|
+
collateralIndex: number;
|
|
14
|
+
pairIndex: number;
|
|
15
|
+
long: boolean;
|
|
16
|
+
open: boolean;
|
|
17
|
+
positionSizeToken: number;
|
|
18
|
+
};
|
|
19
|
+
export type SkewPriceImpactResult = {
|
|
20
|
+
basePriceImpactP: number;
|
|
21
|
+
tradePriceImpactP: number;
|
|
22
|
+
totalPriceImpactP: number;
|
|
23
|
+
netSkewToken: number;
|
|
24
|
+
netSkewCollateral: number;
|
|
25
|
+
tradeDirection: "increase" | "decrease" | "neutral";
|
|
26
|
+
};
|
|
27
|
+
export type SkewPriceImpactContext = {
|
|
28
|
+
skewDepth: number;
|
|
29
|
+
pairOiToken: PairOiToken;
|
|
30
|
+
};
|
|
31
|
+
export type TradeSkewParams = {
|
|
32
|
+
collateralIndex: number;
|
|
33
|
+
pairIndex: number;
|
|
34
|
+
long: boolean;
|
|
35
|
+
open: boolean;
|
|
36
|
+
positionSizeCollateral: number;
|
|
37
|
+
currentPrice: number;
|
|
38
|
+
contractsVersion: number;
|
|
39
|
+
isCounterTrade?: boolean;
|
|
40
|
+
};
|
|
41
|
+
export type PositionSizeResult = {
|
|
42
|
+
positionSizeToken: number;
|
|
43
|
+
positionSizeCollateral: number;
|
|
44
|
+
};
|
package/lib/trade/spread.d.ts
CHANGED
|
@@ -1,18 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
createdBlock?: number;
|
|
7
|
-
liquidationParams?: LiquidationParams | undefined;
|
|
8
|
-
currentBlock?: number | undefined;
|
|
9
|
-
contractsVersion?: ContractsVersion | undefined;
|
|
10
|
-
protectionCloseFactorWhitelist?: boolean;
|
|
11
|
-
userPriceImpact?: UserPriceImpact | undefined;
|
|
12
|
-
} & Partial<PairFactor>;
|
|
13
|
-
export declare const getProtectionCloseFactor: (spreadCtx: SpreadContext | undefined) => number;
|
|
14
|
-
export declare const isProtectionCloseFactorActive: (spreadCtx: SpreadContext | undefined) => boolean | undefined;
|
|
15
|
-
export declare const getCumulativeFactor: (spreadCtx: SpreadContext | undefined) => number;
|
|
16
|
-
export declare const getLegacyFactor: (spreadCtx: SpreadContext | undefined) => number;
|
|
17
|
-
export declare const getSpreadWithPriceImpactP: (pairSpreadP: number, buy: boolean, collateral: number, leverage: number, pairDepth: PairDepth | undefined, oiWindowsSettings?: OiWindowsSettings | undefined, oiWindows?: OiWindows | undefined, spreadCtx?: SpreadContext | undefined) => number;
|
|
18
|
-
export declare const getSpreadP: (pairSpreadP: number | undefined, isLiquidation?: boolean | undefined, liquidationParams?: LiquidationParams | undefined, userPriceImpact?: UserPriceImpact | undefined) => number;
|
|
1
|
+
/**
|
|
2
|
+
* @dev Pure spread calculations without price impact
|
|
3
|
+
* @dev For price impact calculations, see priceImpact module
|
|
4
|
+
*/
|
|
5
|
+
export { getSpreadWithPriceImpactP, getSpreadWithCumulVolPriceImpactP, getTradeCumulVolPriceImpactP, getCumulVolPriceImpact, getProtectionCloseFactor, isProtectionCloseFactorActive, getCumulativeFactor, getLegacyFactor, getFixedSpreadP, getSpreadP, CumulVolContext as SpreadContext, } from "./priceImpact/cumulVol";
|
package/lib/trade/spread.js
CHANGED
|
@@ -1,108 +1,19 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Pure spread calculations without price impact
|
|
4
|
+
* @dev For price impact calculations, see priceImpact module
|
|
5
|
+
*/
|
|
2
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.getSpreadP = exports.
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
: spreadCtx.protectionCloseFactor;
|
|
17
|
-
const protectionCloseFactorMultiplier = ((_a = spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.userPriceImpact) === null || _a === void 0 ? void 0 : _a.cumulVolPriceImpactMultiplier) !== undefined &&
|
|
18
|
-
spreadCtx.userPriceImpact.cumulVolPriceImpactMultiplier > 0
|
|
19
|
-
? spreadCtx.userPriceImpact.cumulVolPriceImpactMultiplier
|
|
20
|
-
: 1;
|
|
21
|
-
return protectionCloseFactor * protectionCloseFactorMultiplier;
|
|
22
|
-
};
|
|
23
|
-
exports.getProtectionCloseFactor = getProtectionCloseFactor;
|
|
24
|
-
const isProtectionCloseFactorActive = (spreadCtx) => {
|
|
25
|
-
if (spreadCtx === undefined ||
|
|
26
|
-
spreadCtx.currentBlock === undefined ||
|
|
27
|
-
spreadCtx.createdBlock === undefined ||
|
|
28
|
-
spreadCtx.protectionCloseFactorBlocks === undefined ||
|
|
29
|
-
spreadCtx.protectionCloseFactor === undefined) {
|
|
30
|
-
return undefined;
|
|
31
|
-
}
|
|
32
|
-
return (spreadCtx.isPnlPositive === true &&
|
|
33
|
-
spreadCtx.isOpen === false &&
|
|
34
|
-
spreadCtx.protectionCloseFactor > 0 &&
|
|
35
|
-
spreadCtx.currentBlock <=
|
|
36
|
-
spreadCtx.createdBlock + spreadCtx.protectionCloseFactorBlocks &&
|
|
37
|
-
spreadCtx.protectionCloseFactorWhitelist !== true);
|
|
38
|
-
};
|
|
39
|
-
exports.isProtectionCloseFactorActive = isProtectionCloseFactorActive;
|
|
40
|
-
const getCumulativeFactor = (spreadCtx) => {
|
|
41
|
-
if (spreadCtx === undefined ||
|
|
42
|
-
spreadCtx.cumulativeFactor === undefined ||
|
|
43
|
-
spreadCtx.cumulativeFactor === 0) {
|
|
44
|
-
return constants_1.DEFAULT_CUMULATIVE_FACTOR;
|
|
45
|
-
}
|
|
46
|
-
return spreadCtx.cumulativeFactor;
|
|
47
|
-
};
|
|
48
|
-
exports.getCumulativeFactor = getCumulativeFactor;
|
|
49
|
-
const getLegacyFactor = (spreadCtx) => {
|
|
50
|
-
return (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2 ? 1 : 2;
|
|
51
|
-
};
|
|
52
|
-
exports.getLegacyFactor = getLegacyFactor;
|
|
53
|
-
const getSpreadWithPriceImpactP = (pairSpreadP, buy, collateral, leverage, pairDepth, oiWindowsSettings, oiWindows, spreadCtx) => {
|
|
54
|
-
if (pairSpreadP === undefined) {
|
|
55
|
-
return 0;
|
|
56
|
-
}
|
|
57
|
-
if (
|
|
58
|
-
// No spread or price impact when closing pre-v9.2 trades
|
|
59
|
-
((spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) === false &&
|
|
60
|
-
(spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.contractsVersion) === types_1.ContractsVersion.BEFORE_V9_2) ||
|
|
61
|
-
// No spread or price impact for opens when `pair.exemptOnOpen` is true
|
|
62
|
-
((spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) === true && (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.exemptOnOpen) === true) ||
|
|
63
|
-
// No spread or price impact for closes after `protectionCloseFactor` has expired
|
|
64
|
-
// when `pair.exemptAfterProtectionCloseFactor` is true
|
|
65
|
-
((spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) === false &&
|
|
66
|
-
(spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.exemptAfterProtectionCloseFactor) === true &&
|
|
67
|
-
(0, exports.isProtectionCloseFactorActive)(spreadCtx) !== true)) {
|
|
68
|
-
return 0;
|
|
69
|
-
}
|
|
70
|
-
const onePercentDepth = buy
|
|
71
|
-
? // if `long`
|
|
72
|
-
(spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false // assumes it's an open unless it's explicitly false
|
|
73
|
-
? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd
|
|
74
|
-
: pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
|
|
75
|
-
: // if `short`
|
|
76
|
-
(spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false
|
|
77
|
-
? pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthBelowUsd
|
|
78
|
-
: pairDepth === null || pairDepth === void 0 ? void 0 : pairDepth.onePercentDepthAboveUsd;
|
|
79
|
-
let activeOi = undefined;
|
|
80
|
-
if (oiWindowsSettings !== undefined) {
|
|
81
|
-
activeOi = (0, oiWindows_1.getActiveOi)((0, oiWindows_1.getCurrentOiWindowId)(oiWindowsSettings), oiWindowsSettings.windowsCount, oiWindows, (spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.isOpen) !== false ? buy : !buy);
|
|
82
|
-
}
|
|
83
|
-
if (!onePercentDepth || activeOi === undefined || collateral === undefined) {
|
|
84
|
-
return pairSpreadP / 2;
|
|
85
|
-
}
|
|
86
|
-
return ((0, exports.getSpreadP)(pairSpreadP, undefined, undefined, spreadCtx === null || spreadCtx === void 0 ? void 0 : spreadCtx.userPriceImpact) +
|
|
87
|
-
((activeOi * (0, exports.getCumulativeFactor)(spreadCtx) + (collateral * leverage) / 2) /
|
|
88
|
-
onePercentDepth /
|
|
89
|
-
100 /
|
|
90
|
-
(0, exports.getLegacyFactor)(spreadCtx)) *
|
|
91
|
-
(0, exports.getProtectionCloseFactor)(spreadCtx));
|
|
92
|
-
};
|
|
93
|
-
exports.getSpreadWithPriceImpactP = getSpreadWithPriceImpactP;
|
|
94
|
-
const getSpreadP = (pairSpreadP, isLiquidation, liquidationParams, userPriceImpact) => {
|
|
95
|
-
var _a;
|
|
96
|
-
const fixedSpreadP = (_a = userPriceImpact === null || userPriceImpact === void 0 ? void 0 : userPriceImpact.fixedSpreadP) !== null && _a !== void 0 ? _a : 0;
|
|
97
|
-
if (pairSpreadP === undefined || (pairSpreadP === 0 && fixedSpreadP === 0)) {
|
|
98
|
-
return 0;
|
|
99
|
-
}
|
|
100
|
-
const spreadP = pairSpreadP / 2 + fixedSpreadP;
|
|
101
|
-
return isLiquidation === true &&
|
|
102
|
-
liquidationParams !== undefined &&
|
|
103
|
-
liquidationParams.maxLiqSpreadP > 0 &&
|
|
104
|
-
spreadP > liquidationParams.maxLiqSpreadP
|
|
105
|
-
? liquidationParams.maxLiqSpreadP
|
|
106
|
-
: spreadP;
|
|
107
|
-
};
|
|
108
|
-
exports.getSpreadP = getSpreadP;
|
|
7
|
+
exports.getSpreadP = exports.getFixedSpreadP = exports.getLegacyFactor = exports.getCumulativeFactor = exports.isProtectionCloseFactorActive = exports.getProtectionCloseFactor = exports.getCumulVolPriceImpact = exports.getTradeCumulVolPriceImpactP = exports.getSpreadWithCumulVolPriceImpactP = exports.getSpreadWithPriceImpactP = void 0;
|
|
8
|
+
// Re-export from priceImpact/cumulVol for backward compatibility
|
|
9
|
+
var cumulVol_1 = require("./priceImpact/cumulVol");
|
|
10
|
+
Object.defineProperty(exports, "getSpreadWithPriceImpactP", { enumerable: true, get: function () { return cumulVol_1.getSpreadWithPriceImpactP; } });
|
|
11
|
+
Object.defineProperty(exports, "getSpreadWithCumulVolPriceImpactP", { enumerable: true, get: function () { return cumulVol_1.getSpreadWithCumulVolPriceImpactP; } });
|
|
12
|
+
Object.defineProperty(exports, "getTradeCumulVolPriceImpactP", { enumerable: true, get: function () { return cumulVol_1.getTradeCumulVolPriceImpactP; } });
|
|
13
|
+
Object.defineProperty(exports, "getCumulVolPriceImpact", { enumerable: true, get: function () { return cumulVol_1.getCumulVolPriceImpact; } });
|
|
14
|
+
Object.defineProperty(exports, "getProtectionCloseFactor", { enumerable: true, get: function () { return cumulVol_1.getProtectionCloseFactor; } });
|
|
15
|
+
Object.defineProperty(exports, "isProtectionCloseFactorActive", { enumerable: true, get: function () { return cumulVol_1.isProtectionCloseFactorActive; } });
|
|
16
|
+
Object.defineProperty(exports, "getCumulativeFactor", { enumerable: true, get: function () { return cumulVol_1.getCumulativeFactor; } });
|
|
17
|
+
Object.defineProperty(exports, "getLegacyFactor", { enumerable: true, get: function () { return cumulVol_1.getLegacyFactor; } });
|
|
18
|
+
Object.defineProperty(exports, "getFixedSpreadP", { enumerable: true, get: function () { return cumulVol_1.getFixedSpreadP; } });
|
|
19
|
+
Object.defineProperty(exports, "getSpreadP", { enumerable: true, get: function () { return cumulVol_1.getSpreadP; } });
|
package/lib/trade/types.d.ts
CHANGED
|
@@ -1,14 +1,37 @@
|
|
|
1
|
-
import { ITradingStorage, IBorrowingFees, IPairsStorage } from "../contracts/types/generated/GNSMultiCollatDiamond";
|
|
1
|
+
import { ITradingStorage, IBorrowingFees, IPairsStorage, IFundingFees } from "../contracts/types/generated/GNSMultiCollatDiamond";
|
|
2
2
|
import { BorrowingFee } from "./fees/borrowing";
|
|
3
3
|
import { FeeTier, TraderEnrollment, TraderInfo } from "./fees/tiers/types";
|
|
4
4
|
export type PairIndexes = {
|
|
5
5
|
[key: string]: PairIndex;
|
|
6
6
|
};
|
|
7
|
+
export type TradeFeesData = {
|
|
8
|
+
realizedTradingFeesCollateral: number;
|
|
9
|
+
realizedPnlCollateral: number;
|
|
10
|
+
manuallyRealizedNegativePnlCollateral: number;
|
|
11
|
+
alreadyTransferredNegativePnlCollateral: number;
|
|
12
|
+
virtualAvailableCollateralInDiamond: number;
|
|
13
|
+
initialAccFundingFeeP: number;
|
|
14
|
+
initialAccBorrowingFeeP: number;
|
|
15
|
+
};
|
|
16
|
+
export type UiRealizedPnlData = {
|
|
17
|
+
realizedTradingFeesCollateral: number;
|
|
18
|
+
realizedOldBorrowingFeesCollateral: number;
|
|
19
|
+
realizedNewBorrowingFeesCollateral: number;
|
|
20
|
+
realizedFundingFeesCollateral: number;
|
|
21
|
+
realizedPnlPartialCloseCollateral: number;
|
|
22
|
+
pnlWithdrawnCollateral: number;
|
|
23
|
+
};
|
|
24
|
+
export type CounterTradeSettings = {
|
|
25
|
+
maxLeverage: number;
|
|
26
|
+
feeRateMultiplier: number;
|
|
27
|
+
};
|
|
7
28
|
export type TradeContainer = {
|
|
8
29
|
trade: Trade;
|
|
9
30
|
tradeInfo: TradeInfo;
|
|
10
31
|
liquidationParams: LiquidationParams;
|
|
11
32
|
initialAccFees: TradeInitialAccFees;
|
|
33
|
+
tradeFeesData?: TradeFeesData;
|
|
34
|
+
uiRealizedPnlData?: UiRealizedPnlData;
|
|
12
35
|
receivedAt?: number;
|
|
13
36
|
};
|
|
14
37
|
export type Trade = {
|
|
@@ -24,6 +47,8 @@ export type Trade = {
|
|
|
24
47
|
openPrice: number;
|
|
25
48
|
sl: number;
|
|
26
49
|
tp: number;
|
|
50
|
+
isCounterTrade?: boolean;
|
|
51
|
+
positionSizeToken?: number;
|
|
27
52
|
};
|
|
28
53
|
export type TradeInfo = {
|
|
29
54
|
createdBlock: number;
|
|
@@ -96,6 +121,86 @@ export type TradeHistoryRecord = {
|
|
|
96
121
|
collateralDelta: number | null;
|
|
97
122
|
leverageDelta: number | null;
|
|
98
123
|
marketPrice: number | null;
|
|
124
|
+
meta: OpenTradeHistoryMetaData | CloseTradeHistoryMetaData | PartialCloseTradeHistoryMetaData | PartialOpenTradeHistoryMetaData | WithdrawPnLHistoryMetaData | LeverageUpdateHistoryMetaData;
|
|
125
|
+
};
|
|
126
|
+
export type HistoryPriceImpact = {
|
|
127
|
+
fixedSpreadP: number;
|
|
128
|
+
priceAfterImpact: number;
|
|
129
|
+
skewPriceImpactP: number;
|
|
130
|
+
positionSizeToken: number;
|
|
131
|
+
totalPriceImpactP: number;
|
|
132
|
+
cumulVolPriceImpactP: number;
|
|
133
|
+
};
|
|
134
|
+
export type OpenTradeHistoryMetaData = {
|
|
135
|
+
liqPrice: number;
|
|
136
|
+
amountSentToTrader: number;
|
|
137
|
+
percentProfit: number;
|
|
138
|
+
priceImpact: HistoryPriceImpact;
|
|
139
|
+
tradeFeesData: TradeFeesData;
|
|
140
|
+
uiRealizedPnlData: UiRealizedPnlData;
|
|
141
|
+
};
|
|
142
|
+
export type CloseTradeHistoryMetaData = OpenTradeHistoryMetaData;
|
|
143
|
+
export type PartialOpenTradeHistoryMetaData = {
|
|
144
|
+
newLeverage: number;
|
|
145
|
+
newLiqPrice: number;
|
|
146
|
+
newOpenPrice: number;
|
|
147
|
+
existingLiqPrice: number;
|
|
148
|
+
newCollateralAmount: number;
|
|
149
|
+
newEffectiveLeverage: number;
|
|
150
|
+
existingPnlCollateral: number;
|
|
151
|
+
openingFeesCollateral: number;
|
|
152
|
+
isCounterTradeValidated: boolean;
|
|
153
|
+
newPositionSizeCollateral: number;
|
|
154
|
+
oldPosSizePlusPnlCollateral: number;
|
|
155
|
+
positionSizeCollateralDelta: number;
|
|
156
|
+
counterTradeCollateralToReturn: number;
|
|
157
|
+
existingPositionSizeCollateral: number;
|
|
158
|
+
exceedingPositionSizeCollateral: number;
|
|
159
|
+
priceImpact: HistoryPriceImpact;
|
|
160
|
+
tradeFeesData: TradeFeesData;
|
|
161
|
+
uiRealizedPnlData: UiRealizedPnlData;
|
|
162
|
+
};
|
|
163
|
+
export type PartialCloseTradeHistoryMetaData = {
|
|
164
|
+
newLeverage: number;
|
|
165
|
+
newLiqPrice: number;
|
|
166
|
+
existingLiqPrice: number;
|
|
167
|
+
isLeverageUpdate: boolean;
|
|
168
|
+
existingPnlPercent: number;
|
|
169
|
+
newCollateralAmount: number;
|
|
170
|
+
closingFeeCollateral: number;
|
|
171
|
+
collateralSentToTrader: number;
|
|
172
|
+
pnlToRealizeCollateral: number;
|
|
173
|
+
partialNetPnlCollateral: number;
|
|
174
|
+
partialRawPnlCollateral: number;
|
|
175
|
+
positionSizeCollateralDelta: number;
|
|
176
|
+
availableCollateralInDiamond: number;
|
|
177
|
+
existingPositionSizeCollateral: number;
|
|
178
|
+
totalAvailableCollateralInDiamond: number;
|
|
179
|
+
priceImpact: HistoryPriceImpact;
|
|
180
|
+
tradeFeesData: TradeFeesData;
|
|
181
|
+
uiRealizedPnlData: UiRealizedPnlData;
|
|
182
|
+
};
|
|
183
|
+
export type LeverageUpdateHistoryMetaData = {
|
|
184
|
+
availableCollateralInDiamond: number;
|
|
185
|
+
existingLiqPrice: number;
|
|
186
|
+
govFeeCollateral: number;
|
|
187
|
+
newCollateralAmount: number;
|
|
188
|
+
newEffectiveLeverage: number;
|
|
189
|
+
newLeverage: number;
|
|
190
|
+
newLiqPrice: number;
|
|
191
|
+
totalTradeAvailableCollateralInDiamond: number;
|
|
192
|
+
tradeFeesData: TradeFeesData;
|
|
193
|
+
uiRealizedPnlData: UiRealizedPnlData;
|
|
194
|
+
};
|
|
195
|
+
export type WithdrawPnLHistoryMetaData = {
|
|
196
|
+
pnlPercent: number;
|
|
197
|
+
currentPairPrice: number;
|
|
198
|
+
pnlInputCollateral: number;
|
|
199
|
+
pnlWithdrawnCollateral: number;
|
|
200
|
+
withdrawablePositivePnlCollateral: number;
|
|
201
|
+
priceImpact: HistoryPriceImpact;
|
|
202
|
+
tradeFeesData: TradeFeesData;
|
|
203
|
+
uiRealizedPnlData: UiRealizedPnlData;
|
|
99
204
|
};
|
|
100
205
|
export type MarketOrder = {
|
|
101
206
|
trader: string;
|
|
@@ -143,6 +248,8 @@ export type TradeContainerRaw = {
|
|
|
143
248
|
tradeInfo: ITradingStorage.TradeInfoStruct;
|
|
144
249
|
liquidationParams: IPairsStorage.GroupLiquidationParamsStruct;
|
|
145
250
|
initialAccFees: IBorrowingFees.BorrowingInitialAccFeesStruct;
|
|
251
|
+
tradeFeesData: IFundingFees.TradeFeesDataStruct;
|
|
252
|
+
uiRealizedPnlData?: IFundingFees.UiRealizedPnlDataStruct;
|
|
146
253
|
};
|
|
147
254
|
export type OiWindowsSettings = {
|
|
148
255
|
startTs: number;
|
|
@@ -157,13 +264,6 @@ export type OiWindow = PairOi;
|
|
|
157
264
|
export type OiWindows = {
|
|
158
265
|
[key: string]: OiWindow;
|
|
159
266
|
};
|
|
160
|
-
export type CollateralConfig = {
|
|
161
|
-
collateral: string;
|
|
162
|
-
isActive: boolean;
|
|
163
|
-
precision: number;
|
|
164
|
-
precisionDelta: number;
|
|
165
|
-
decimals?: number;
|
|
166
|
-
};
|
|
167
267
|
export type FeeTiers = {
|
|
168
268
|
tiers: FeeTier[];
|
|
169
269
|
multipliers: number[];
|
|
@@ -619,8 +719,5 @@ export declare enum PairIndex {
|
|
|
619
719
|
SBETUSD = 393,
|
|
620
720
|
PLTRUSD = 394,
|
|
621
721
|
BIDUUSD = 395,
|
|
622
|
-
ROKUUSD = 396
|
|
623
|
-
LMTUSD = 397,
|
|
624
|
-
RIOTUSD = 398,
|
|
625
|
-
MARAUSD = 399
|
|
722
|
+
ROKUUSD = 396
|
|
626
723
|
}
|
package/lib/trade/types.js
CHANGED
|
@@ -426,7 +426,4 @@ var PairIndex;
|
|
|
426
426
|
PairIndex[PairIndex["PLTRUSD"] = 394] = "PLTRUSD";
|
|
427
427
|
PairIndex[PairIndex["BIDUUSD"] = 395] = "BIDUUSD";
|
|
428
428
|
PairIndex[PairIndex["ROKUUSD"] = 396] = "ROKUUSD";
|
|
429
|
-
PairIndex[PairIndex["LMTUSD"] = 397] = "LMTUSD";
|
|
430
|
-
PairIndex[PairIndex["RIOTUSD"] = 398] = "RIOTUSD";
|
|
431
|
-
PairIndex[PairIndex["MARAUSD"] = 399] = "MARAUSD";
|
|
432
429
|
})(PairIndex = exports.PairIndex || (exports.PairIndex = {}));
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Trade-specific utility functions
|
|
3
|
+
* @dev Common calculations and conversions used across trading modules
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @dev Converts position size from collateral to tokens
|
|
7
|
+
* @param positionSizeCollateral Position size in collateral tokens
|
|
8
|
+
* @param currentPrice Current pair price
|
|
9
|
+
* @returns Position size in tokens
|
|
10
|
+
*/
|
|
11
|
+
export declare const calculatePositionSizeToken: (positionSizeCollateral: number, currentPrice: number) => number;
|
|
12
|
+
/**
|
|
13
|
+
* @dev Converts position size from tokens to collateral
|
|
14
|
+
* @param positionSizeToken Position size in tokens
|
|
15
|
+
* @param currentPrice Current pair price
|
|
16
|
+
* @returns Position size in collateral tokens
|
|
17
|
+
*/
|
|
18
|
+
export declare const calculatePositionSizeCollateral: (positionSizeToken: number, currentPrice: number) => number;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Trade-specific utility functions
|
|
4
|
+
* @dev Common calculations and conversions used across trading modules
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.calculatePositionSizeCollateral = exports.calculatePositionSizeToken = void 0;
|
|
8
|
+
/**
|
|
9
|
+
* @dev Converts position size from collateral to tokens
|
|
10
|
+
* @param positionSizeCollateral Position size in collateral tokens
|
|
11
|
+
* @param currentPrice Current pair price
|
|
12
|
+
* @returns Position size in tokens
|
|
13
|
+
*/
|
|
14
|
+
const calculatePositionSizeToken = (positionSizeCollateral, currentPrice) => {
|
|
15
|
+
if (currentPrice === 0) {
|
|
16
|
+
throw new Error("Current price cannot be zero");
|
|
17
|
+
}
|
|
18
|
+
return positionSizeCollateral / currentPrice;
|
|
19
|
+
};
|
|
20
|
+
exports.calculatePositionSizeToken = calculatePositionSizeToken;
|
|
21
|
+
/**
|
|
22
|
+
* @dev Converts position size from tokens to collateral
|
|
23
|
+
* @param positionSizeToken Position size in tokens
|
|
24
|
+
* @param currentPrice Current pair price
|
|
25
|
+
* @returns Position size in collateral tokens
|
|
26
|
+
*/
|
|
27
|
+
const calculatePositionSizeCollateral = (positionSizeToken, currentPrice) => {
|
|
28
|
+
return positionSizeToken * currentPrice;
|
|
29
|
+
};
|
|
30
|
+
exports.calculatePositionSizeCollateral = calculatePositionSizeCollateral;
|
package/lib/vault/index.d.ts
CHANGED
|
@@ -4,5 +4,7 @@ type GetPendingAccBlockWeightedMarketCap = {
|
|
|
4
4
|
accBlockWeightedMarketCapLastStored: number;
|
|
5
5
|
marketCapPrecision?: number;
|
|
6
6
|
};
|
|
7
|
-
export declare const getPendingAccBlockWeightedMarketCap: (
|
|
7
|
+
export declare const getPendingAccBlockWeightedMarketCap: (input: {
|
|
8
|
+
currentBlock: number;
|
|
9
|
+
}, context: GetPendingAccBlockWeightedMarketCap) => number;
|
|
8
10
|
export {};
|
package/lib/vault/index.js
CHANGED
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getPendingAccBlockWeightedMarketCap = void 0;
|
|
4
|
-
const getPendingAccBlockWeightedMarketCap = (
|
|
4
|
+
const getPendingAccBlockWeightedMarketCap = (input, context) => {
|
|
5
5
|
const { marketCap, accBlockWeightedMarketCap, accBlockWeightedMarketCapLastStored, marketCapPrecision, } = context;
|
|
6
6
|
return (accBlockWeightedMarketCap +
|
|
7
|
-
(currentBlock - accBlockWeightedMarketCapLastStored) /
|
|
7
|
+
(input.currentBlock - accBlockWeightedMarketCapLastStored) /
|
|
8
8
|
Math.max(marketCap * (marketCapPrecision || 1e18), 1));
|
|
9
9
|
};
|
|
10
10
|
exports.getPendingAccBlockWeightedMarketCap = getPendingAccBlockWeightedMarketCap;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@gainsnetwork/sdk",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "1.0.0-rc1",
|
|
4
4
|
"description": "Gains Network SDK",
|
|
5
5
|
"main": "./lib/index.js",
|
|
6
6
|
"files": [
|
|
@@ -98,6 +98,7 @@
|
|
|
98
98
|
},
|
|
99
99
|
"dependencies": {
|
|
100
100
|
"@ethersproject/providers": "^5.7.2",
|
|
101
|
+
"@gainsnetwork/contests": "^0.0.1",
|
|
101
102
|
"ethcall": "^4.8.13",
|
|
102
103
|
"ethers": "^5.7.2",
|
|
103
104
|
"luxon": "^3.2.0"
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
import { GetBorrowingFeeContext, BorrowingFee } from "./fees";
|
|
2
|
-
import { Fee, LiquidationParams, Trade, UserPriceImpact } from "./types";
|
|
3
|
-
import { ContractsVersion } from "../contracts/types";
|
|
4
|
-
export type GetLiquidationPriceContext = GetBorrowingFeeContext & {
|
|
5
|
-
liquidationParams: LiquidationParams | undefined;
|
|
6
|
-
pairSpreadP: number | undefined;
|
|
7
|
-
collateralPriceUsd: number | undefined;
|
|
8
|
-
contractsVersion: ContractsVersion | undefined;
|
|
9
|
-
userPriceImpact?: UserPriceImpact | undefined;
|
|
10
|
-
};
|
|
11
|
-
export declare const getLiquidationPrice: (trade: Trade, fee: Fee, initialAccFees: BorrowingFee.InitialAccFees, context: GetLiquidationPriceContext) => number;
|
|
12
|
-
export declare const getLiqPnlThresholdP: (liquidationParams: LiquidationParams | undefined, leverage: number | undefined) => number;
|