@gainsnetwork/sdk 0.2.71-rc4 → 1.0.0-rc2
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,181 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Fetchers for retrieving OI data from contracts
|
|
4
|
+
* @dev Consolidates the three OI storage systems into unified format
|
|
5
|
+
*/
|
|
6
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
7
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
8
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
9
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
10
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
11
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
12
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
13
|
+
});
|
|
14
|
+
};
|
|
15
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
+
exports.fetchOiForUseCase = exports.createOiContext = exports.fetchMultiplePairOi = exports.fetchPairOi = void 0;
|
|
17
|
+
const ethers_1 = require("ethers");
|
|
18
|
+
const contracts_1 = require("../../contracts");
|
|
19
|
+
const generated_1 = require("../../contracts/types/generated");
|
|
20
|
+
const converter_1 = require("./converter");
|
|
21
|
+
/**
|
|
22
|
+
* @dev Fetches all OI data for a single pair
|
|
23
|
+
* @param chainId Target chain
|
|
24
|
+
* @param collateralIndex Collateral type
|
|
25
|
+
* @param pairIndex Trading pair
|
|
26
|
+
* @param signer Ethers signer
|
|
27
|
+
* @returns Unified PairOi structure with all OI data
|
|
28
|
+
*/
|
|
29
|
+
function fetchPairOi(chainId, collateralIndex, pairIndex, signer) {
|
|
30
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
31
|
+
const addresses = (0, contracts_1.getContractAddressesForChain)(chainId);
|
|
32
|
+
const diamond = generated_1.GNSMultiCollatDiamond__factory.connect(addresses.gnsMultiCollatDiamond, signer);
|
|
33
|
+
// Fetch all three OI types in parallel
|
|
34
|
+
const [beforeV10Raw, afterV10Collateral, afterV10Token, maxOi, collateral] = yield Promise.all([
|
|
35
|
+
diamond.getPairOisBeforeV10Collateral(collateralIndex, pairIndex),
|
|
36
|
+
diamond.getPairOiAfterV10Collateral(collateralIndex, pairIndex),
|
|
37
|
+
diamond.getPairOiAfterV10Token(collateralIndex, pairIndex),
|
|
38
|
+
diamond.getPairMaxOi(collateralIndex, pairIndex),
|
|
39
|
+
diamond.getCollateral(collateralIndex),
|
|
40
|
+
]);
|
|
41
|
+
// Convert the beforeV10 format to match expected structure
|
|
42
|
+
const beforeV10 = {
|
|
43
|
+
long: beforeV10Raw.longOi,
|
|
44
|
+
short: beforeV10Raw.shortOi,
|
|
45
|
+
max: maxOi,
|
|
46
|
+
__placeholder: ethers_1.ethers.BigNumber.from(0),
|
|
47
|
+
};
|
|
48
|
+
return (0, converter_1.convertPairOi)(beforeV10, afterV10Collateral, afterV10Token, Number(collateral.precision));
|
|
49
|
+
});
|
|
50
|
+
}
|
|
51
|
+
exports.fetchPairOi = fetchPairOi;
|
|
52
|
+
/**
|
|
53
|
+
* @dev Fetches OI data for multiple pairs efficiently
|
|
54
|
+
* @param chainId Target chain
|
|
55
|
+
* @param collateralIndex Collateral type
|
|
56
|
+
* @param pairIndices Array of trading pairs
|
|
57
|
+
* @param signer Ethers signer
|
|
58
|
+
* @returns Array of unified PairOi structures
|
|
59
|
+
*/
|
|
60
|
+
function fetchMultiplePairOi(chainId, collateralIndex, pairIndices, signer) {
|
|
61
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
62
|
+
const addresses = (0, contracts_1.getContractAddressesForChain)(chainId);
|
|
63
|
+
const diamond = generated_1.GNSMultiCollatDiamond__factory.connect(addresses.gnsMultiCollatDiamond, signer);
|
|
64
|
+
// Get collateral precision once
|
|
65
|
+
const collateral = yield diamond.getCollateral(collateralIndex);
|
|
66
|
+
const precision = Number(collateral.precision);
|
|
67
|
+
// Batch fetch all OI data
|
|
68
|
+
const promises = pairIndices.map((pairIndex) => __awaiter(this, void 0, void 0, function* () {
|
|
69
|
+
const [beforeV10Raw, afterV10Collateral, afterV10Token, maxOi] = yield Promise.all([
|
|
70
|
+
diamond.getPairOisBeforeV10Collateral(collateralIndex, pairIndex),
|
|
71
|
+
diamond.getPairOiAfterV10Collateral(collateralIndex, pairIndex),
|
|
72
|
+
diamond.getPairOiAfterV10Token(collateralIndex, pairIndex),
|
|
73
|
+
diamond.getPairMaxOi(collateralIndex, pairIndex),
|
|
74
|
+
]);
|
|
75
|
+
// Convert the beforeV10 format to match expected structure
|
|
76
|
+
const beforeV10 = {
|
|
77
|
+
long: beforeV10Raw.longOi,
|
|
78
|
+
short: beforeV10Raw.shortOi,
|
|
79
|
+
max: maxOi,
|
|
80
|
+
__placeholder: ethers_1.ethers.BigNumber.from(0),
|
|
81
|
+
};
|
|
82
|
+
return {
|
|
83
|
+
beforeV10: beforeV10,
|
|
84
|
+
collateral: afterV10Collateral,
|
|
85
|
+
token: afterV10Token,
|
|
86
|
+
};
|
|
87
|
+
}));
|
|
88
|
+
const results = yield Promise.all(promises);
|
|
89
|
+
return (0, converter_1.convertPairOiArray)(results, precision);
|
|
90
|
+
});
|
|
91
|
+
}
|
|
92
|
+
exports.fetchMultiplePairOi = fetchMultiplePairOi;
|
|
93
|
+
/**
|
|
94
|
+
* @dev Creates OI context for fee calculations
|
|
95
|
+
* @param chainId Target chain
|
|
96
|
+
* @param collateralIndex Collateral type
|
|
97
|
+
* @param pairIndex Trading pair
|
|
98
|
+
* @param signer Ethers signer
|
|
99
|
+
* @returns OI data formatted for SDK calculations
|
|
100
|
+
*/
|
|
101
|
+
function createOiContext(chainId, collateralIndex, pairIndex, signer) {
|
|
102
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
+
// Fetch OI data
|
|
104
|
+
const pairOi = yield fetchPairOi(chainId, collateralIndex, pairIndex, signer);
|
|
105
|
+
// For now, use a placeholder price - in real usage, this would come from price feeds
|
|
106
|
+
// The actual price should be fetched from the price aggregator or oracle
|
|
107
|
+
const currentPrice = 1; // Placeholder - replace with actual price fetching
|
|
108
|
+
// Compute derived values
|
|
109
|
+
const totalDynamicOi = {
|
|
110
|
+
long: pairOi.beforeV10Collateral.long + pairOi.token.long * currentPrice,
|
|
111
|
+
short: pairOi.beforeV10Collateral.short + pairOi.token.short * currentPrice,
|
|
112
|
+
};
|
|
113
|
+
const totalStaticOi = {
|
|
114
|
+
long: pairOi.beforeV10Collateral.long + pairOi.collateral.long,
|
|
115
|
+
short: pairOi.beforeV10Collateral.short + pairOi.collateral.short,
|
|
116
|
+
};
|
|
117
|
+
const skew = pairOi.token.long - pairOi.token.short;
|
|
118
|
+
return {
|
|
119
|
+
pairOi,
|
|
120
|
+
currentPrice,
|
|
121
|
+
computed: {
|
|
122
|
+
totalDynamicOi,
|
|
123
|
+
totalStaticOi,
|
|
124
|
+
skew,
|
|
125
|
+
},
|
|
126
|
+
};
|
|
127
|
+
});
|
|
128
|
+
}
|
|
129
|
+
exports.createOiContext = createOiContext;
|
|
130
|
+
/**
|
|
131
|
+
* @dev Fetches only the OI data needed for specific use cases
|
|
132
|
+
* @param chainId Target chain
|
|
133
|
+
* @param collateralIndex Collateral type
|
|
134
|
+
* @param pairIndex Trading pair
|
|
135
|
+
* @param useCase Which OI systems to fetch
|
|
136
|
+
* @param signer Ethers signer
|
|
137
|
+
* @returns Partial OI data based on use case
|
|
138
|
+
*/
|
|
139
|
+
function fetchOiForUseCase(chainId, collateralIndex, pairIndex, useCase, signer) {
|
|
140
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
141
|
+
const addresses = (0, contracts_1.getContractAddressesForChain)(chainId);
|
|
142
|
+
const diamond = generated_1.GNSMultiCollatDiamond__factory.connect(addresses.gnsMultiCollatDiamond, signer);
|
|
143
|
+
switch (useCase) {
|
|
144
|
+
case "skew":
|
|
145
|
+
case "funding": {
|
|
146
|
+
// Only need token OI
|
|
147
|
+
const tokenOi = yield diamond.getPairOiAfterV10Token(collateralIndex, pairIndex);
|
|
148
|
+
return {
|
|
149
|
+
token: (0, converter_1.convertTokenOi)(tokenOi),
|
|
150
|
+
};
|
|
151
|
+
}
|
|
152
|
+
case "borrowingV1": {
|
|
153
|
+
// Need beforeV10 and token (for dynamic calculation)
|
|
154
|
+
const [beforeV10Raw, tokenOi, collateral, maxOi] = yield Promise.all([
|
|
155
|
+
diamond.getPairOisBeforeV10Collateral(collateralIndex, pairIndex),
|
|
156
|
+
diamond.getPairOiAfterV10Token(collateralIndex, pairIndex),
|
|
157
|
+
diamond.getCollateral(collateralIndex),
|
|
158
|
+
diamond.getPairMaxOi(collateralIndex, pairIndex),
|
|
159
|
+
]);
|
|
160
|
+
// Convert the beforeV10 format to match expected structure
|
|
161
|
+
const beforeV10 = {
|
|
162
|
+
long: beforeV10Raw.longOi,
|
|
163
|
+
short: beforeV10Raw.shortOi,
|
|
164
|
+
max: maxOi,
|
|
165
|
+
__placeholder: ethers_1.ethers.BigNumber.from(0),
|
|
166
|
+
};
|
|
167
|
+
return {
|
|
168
|
+
beforeV10Collateral: (0, converter_1.convertBeforeV10Collateral)(beforeV10, Number(collateral.precision)),
|
|
169
|
+
token: (0, converter_1.convertTokenOi)(tokenOi),
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
case "limits": {
|
|
173
|
+
// Need all OI for limit checks
|
|
174
|
+
return fetchPairOi(chainId, collateralIndex, pairIndex, signer);
|
|
175
|
+
}
|
|
176
|
+
default:
|
|
177
|
+
throw new Error(`Unknown use case: ${String(useCase)}`);
|
|
178
|
+
}
|
|
179
|
+
});
|
|
180
|
+
}
|
|
181
|
+
exports.fetchOiForUseCase = fetchOiForUseCase;
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Main export file for OI module
|
|
3
|
+
* @dev Provides unified Open Interest management functionality
|
|
4
|
+
*/
|
|
5
|
+
import { GenericPairOiContext } from "./types";
|
|
6
|
+
export declare const getPairTotalOisCollateral: (pairIndex: number, context: GenericPairOiContext) => {
|
|
7
|
+
long: number;
|
|
8
|
+
short: number;
|
|
9
|
+
};
|
|
10
|
+
/**
|
|
11
|
+
* @dev Returns pair total dynamic open interest (before v10 + after v10) in collateral tokens
|
|
12
|
+
* @param pairIndex index of pair
|
|
13
|
+
* @param context contains UnifiedPairOi array and current pair price
|
|
14
|
+
* @returns dynamic OI for long and short sides in collateral precision
|
|
15
|
+
*/
|
|
16
|
+
export declare const getPairTotalOisDynamicCollateral: (pairIndex: number, context: GenericPairOiContext & {
|
|
17
|
+
currentPairPrice: number;
|
|
18
|
+
}) => {
|
|
19
|
+
long: number;
|
|
20
|
+
short: number;
|
|
21
|
+
};
|
|
22
|
+
/**
|
|
23
|
+
* @dev Returns pair total dynamic open interest (before v10 + after v10) in collateral tokens on one side only
|
|
24
|
+
* @param pairIndex index of pair
|
|
25
|
+
* @param long true if long, false if short
|
|
26
|
+
* @param context contains UnifiedPairOi array and current pair price
|
|
27
|
+
* @returns dynamic OI for the specified side in collateral precision
|
|
28
|
+
*/
|
|
29
|
+
export declare const getPairTotalOiDynamicCollateral: (pairIndex: number, long: boolean, context: GenericPairOiContext & {
|
|
30
|
+
currentPairPrice: number;
|
|
31
|
+
}) => number;
|
|
32
|
+
/**
|
|
33
|
+
* @dev Returns pair open interest skew (v10 only) in tokens
|
|
34
|
+
* @param pairIndex index of pair
|
|
35
|
+
* @param context contains UnifiedPairOi array
|
|
36
|
+
* @returns skew in token amount (positive = more longs, negative = more shorts)
|
|
37
|
+
*/
|
|
38
|
+
export declare const getPairV10OiTokenSkewCollateral: (pairIndex: number, context: GenericPairOiContext) => number;
|
|
39
|
+
/**
|
|
40
|
+
* @dev Returns pair dynamic skew (v10 only) in collateral tokens
|
|
41
|
+
* @param pairIndex index of pair
|
|
42
|
+
* @param context contains UnifiedPairOi array and current pair price
|
|
43
|
+
* @returns dynamic skew in collateral precision
|
|
44
|
+
*/
|
|
45
|
+
export declare const getPairV10OiDynamicSkewCollateral: (pairIndex: number, context: GenericPairOiContext & {
|
|
46
|
+
currentPairPrice: number;
|
|
47
|
+
}) => number;
|
|
48
|
+
export { UnifiedPairOi, GroupOi, ComputedOi } from "./types";
|
|
49
|
+
export { convertBeforeV10Collateral, convertCollateralOi, convertTokenOi, convertPairOi, convertPairOiArray, computeOiValues, } from "./converter";
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Main export file for OI module
|
|
4
|
+
* @dev Provides unified Open Interest management functionality
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.computeOiValues = exports.convertPairOiArray = exports.convertPairOi = exports.convertTokenOi = exports.convertCollateralOi = exports.convertBeforeV10Collateral = exports.getPairV10OiDynamicSkewCollateral = exports.getPairV10OiTokenSkewCollateral = exports.getPairTotalOiDynamicCollateral = exports.getPairTotalOisDynamicCollateral = exports.getPairTotalOisCollateral = void 0;
|
|
8
|
+
const getPairTotalOisCollateral = (pairIndex, context) => {
|
|
9
|
+
return {
|
|
10
|
+
long: context.pairOis[pairIndex].beforeV10Collateral.long +
|
|
11
|
+
context.pairOis[pairIndex].collateral.long,
|
|
12
|
+
short: context.pairOis[pairIndex].beforeV10Collateral.short +
|
|
13
|
+
context.pairOis[pairIndex].collateral.short,
|
|
14
|
+
};
|
|
15
|
+
};
|
|
16
|
+
exports.getPairTotalOisCollateral = getPairTotalOisCollateral;
|
|
17
|
+
/**
|
|
18
|
+
* @dev Returns pair total dynamic open interest (before v10 + after v10) in collateral tokens
|
|
19
|
+
* @param pairIndex index of pair
|
|
20
|
+
* @param context contains UnifiedPairOi array and current pair price
|
|
21
|
+
* @returns dynamic OI for long and short sides in collateral precision
|
|
22
|
+
*/
|
|
23
|
+
const getPairTotalOisDynamicCollateral = (pairIndex, context) => {
|
|
24
|
+
const pairOi = context.pairOis[pairIndex];
|
|
25
|
+
// We have to use the initial collateral OIs for pre-v10 trades because we don't have OIs in token amount
|
|
26
|
+
const oiLongCollateralDynamicAfterV10 = pairOi.beforeV10Collateral.long +
|
|
27
|
+
pairOi.token.long * context.currentPairPrice;
|
|
28
|
+
const oiShortCollateralDynamicAfterV10 = pairOi.beforeV10Collateral.short +
|
|
29
|
+
pairOi.token.short * context.currentPairPrice;
|
|
30
|
+
return {
|
|
31
|
+
long: oiLongCollateralDynamicAfterV10,
|
|
32
|
+
short: oiShortCollateralDynamicAfterV10,
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
exports.getPairTotalOisDynamicCollateral = getPairTotalOisDynamicCollateral;
|
|
36
|
+
/**
|
|
37
|
+
* @dev Returns pair total dynamic open interest (before v10 + after v10) in collateral tokens on one side only
|
|
38
|
+
* @param pairIndex index of pair
|
|
39
|
+
* @param long true if long, false if short
|
|
40
|
+
* @param context contains UnifiedPairOi array and current pair price
|
|
41
|
+
* @returns dynamic OI for the specified side in collateral precision
|
|
42
|
+
*/
|
|
43
|
+
const getPairTotalOiDynamicCollateral = (pairIndex, long, context) => {
|
|
44
|
+
const dynamicOis = (0, exports.getPairTotalOisDynamicCollateral)(pairIndex, context);
|
|
45
|
+
return long ? dynamicOis.long : dynamicOis.short;
|
|
46
|
+
};
|
|
47
|
+
exports.getPairTotalOiDynamicCollateral = getPairTotalOiDynamicCollateral;
|
|
48
|
+
/**
|
|
49
|
+
* @dev Returns pair open interest skew (v10 only) in tokens
|
|
50
|
+
* @param pairIndex index of pair
|
|
51
|
+
* @param context contains UnifiedPairOi array
|
|
52
|
+
* @returns skew in token amount (positive = more longs, negative = more shorts)
|
|
53
|
+
*/
|
|
54
|
+
const getPairV10OiTokenSkewCollateral = (pairIndex, context) => {
|
|
55
|
+
const pairOi = context.pairOis[pairIndex];
|
|
56
|
+
return pairOi.token.long - pairOi.token.short;
|
|
57
|
+
};
|
|
58
|
+
exports.getPairV10OiTokenSkewCollateral = getPairV10OiTokenSkewCollateral;
|
|
59
|
+
/**
|
|
60
|
+
* @dev Returns pair dynamic skew (v10 only) in collateral tokens
|
|
61
|
+
* @param pairIndex index of pair
|
|
62
|
+
* @param context contains UnifiedPairOi array and current pair price
|
|
63
|
+
* @returns dynamic skew in collateral precision
|
|
64
|
+
*/
|
|
65
|
+
const getPairV10OiDynamicSkewCollateral = (pairIndex, context) => {
|
|
66
|
+
return ((0, exports.getPairV10OiTokenSkewCollateral)(pairIndex, context) *
|
|
67
|
+
context.currentPairPrice);
|
|
68
|
+
};
|
|
69
|
+
exports.getPairV10OiDynamicSkewCollateral = getPairV10OiDynamicSkewCollateral;
|
|
70
|
+
// Converters
|
|
71
|
+
var converter_1 = require("./converter");
|
|
72
|
+
Object.defineProperty(exports, "convertBeforeV10Collateral", { enumerable: true, get: function () { return converter_1.convertBeforeV10Collateral; } });
|
|
73
|
+
Object.defineProperty(exports, "convertCollateralOi", { enumerable: true, get: function () { return converter_1.convertCollateralOi; } });
|
|
74
|
+
Object.defineProperty(exports, "convertTokenOi", { enumerable: true, get: function () { return converter_1.convertTokenOi; } });
|
|
75
|
+
Object.defineProperty(exports, "convertPairOi", { enumerable: true, get: function () { return converter_1.convertPairOi; } });
|
|
76
|
+
Object.defineProperty(exports, "convertPairOiArray", { enumerable: true, get: function () { return converter_1.convertPairOiArray; } });
|
|
77
|
+
Object.defineProperty(exports, "computeOiValues", { enumerable: true, get: function () { return converter_1.computeOiValues; } });
|
|
@@ -0,0 +1,73 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Consolidated OI types for unified Open Interest management
|
|
3
|
+
* @dev Represents the three OI storage systems in Gains Network v10
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* @dev Unified pair OI structure combining all storage systems
|
|
7
|
+
*/
|
|
8
|
+
export interface UnifiedPairOi {
|
|
9
|
+
/**
|
|
10
|
+
* @dev Maximum allowed OI in collateral (applies to combined OI)
|
|
11
|
+
*/
|
|
12
|
+
maxCollateral: number;
|
|
13
|
+
/**
|
|
14
|
+
* @dev Pre-v10 trades OI stored in collateral amounts
|
|
15
|
+
* @dev Static values from when positions were opened
|
|
16
|
+
*/
|
|
17
|
+
beforeV10Collateral: {
|
|
18
|
+
long: number;
|
|
19
|
+
short: number;
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* @dev Post-v10 trades OI stored in collateral amounts
|
|
23
|
+
* @dev Initial collateral values when positions were opened
|
|
24
|
+
* @dev Used for administrative operations and static calculations
|
|
25
|
+
*/
|
|
26
|
+
collateral: {
|
|
27
|
+
long: number;
|
|
28
|
+
short: number;
|
|
29
|
+
};
|
|
30
|
+
/**
|
|
31
|
+
* @dev Post-v10 trades OI stored in token amounts
|
|
32
|
+
* @dev Used for dynamic calculations (multiply by current price)
|
|
33
|
+
* @dev Powers skew impact and funding fees
|
|
34
|
+
*/
|
|
35
|
+
token: {
|
|
36
|
+
long: number;
|
|
37
|
+
short: number;
|
|
38
|
+
};
|
|
39
|
+
}
|
|
40
|
+
export type GenericPairOiContext = {
|
|
41
|
+
pairOis: UnifiedPairOi[];
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* @dev Group OI remains unchanged - only used by borrowing v1
|
|
45
|
+
* @dev Re-export existing type for consistency
|
|
46
|
+
*/
|
|
47
|
+
export { OpenInterest as GroupOi } from "../../trade/types";
|
|
48
|
+
/**
|
|
49
|
+
* @dev Helper type for computed OI values
|
|
50
|
+
*/
|
|
51
|
+
export interface ComputedOi {
|
|
52
|
+
/**
|
|
53
|
+
* @dev Total OI using static values: beforeV10Collateral + collateral
|
|
54
|
+
* @dev Used for administrative operations
|
|
55
|
+
*/
|
|
56
|
+
totalStaticCollateral: {
|
|
57
|
+
long: number;
|
|
58
|
+
short: number;
|
|
59
|
+
};
|
|
60
|
+
/**
|
|
61
|
+
* @dev Total OI using dynamic values: beforeV10Collateral + (token * price)
|
|
62
|
+
* @dev Used for real-time calculations like fees
|
|
63
|
+
*/
|
|
64
|
+
totalDynamicCollateral: {
|
|
65
|
+
long: number;
|
|
66
|
+
short: number;
|
|
67
|
+
};
|
|
68
|
+
/**
|
|
69
|
+
* @dev Net skew in tokens (v10+ only)
|
|
70
|
+
* @dev Positive = more longs, negative = more shorts
|
|
71
|
+
*/
|
|
72
|
+
skewToken: number;
|
|
73
|
+
}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev OI Validation module
|
|
3
|
+
* @dev Provides validation functions for Open Interest limits
|
|
4
|
+
*/
|
|
5
|
+
import { UnifiedPairOi } from "./types";
|
|
6
|
+
import * as BorrowingFee from "../../trade/fees/borrowing/types";
|
|
7
|
+
import { PairIndex } from "../../trade/types";
|
|
8
|
+
/**
|
|
9
|
+
* @dev Check if a position would exceed per-pair OI limits
|
|
10
|
+
* @param pairOi Current OI data for the pair
|
|
11
|
+
* @param long Whether the position is long
|
|
12
|
+
* @param positionSizeCollateral Position size in collateral
|
|
13
|
+
* @param currentPrice Current collateral price in USD (required for dynamic OI)
|
|
14
|
+
* @returns true if within limits, false if would exceed
|
|
15
|
+
*/
|
|
16
|
+
export declare const withinMaxPairOi: (pairOi: UnifiedPairOi, long: boolean, positionSizeCollateral: number, currentPrice: number) => boolean;
|
|
17
|
+
/**
|
|
18
|
+
* @dev Calculate dynamic OI for a specific side
|
|
19
|
+
* @param pairOi OI data for the pair
|
|
20
|
+
* @param currentPrice Current collateral price in USD
|
|
21
|
+
* @param long Whether to calculate for long side
|
|
22
|
+
* @returns Dynamic OI in collateral value
|
|
23
|
+
*/
|
|
24
|
+
export declare const calculateDynamicOi: (pairOi: UnifiedPairOi, currentPrice: number, long: boolean) => number;
|
|
25
|
+
/**
|
|
26
|
+
* @dev Calculate remaining OI capacity for a side
|
|
27
|
+
* @param pairOi OI data for the pair
|
|
28
|
+
* @param currentPrice Current collateral price in USD
|
|
29
|
+
* @param long Whether to calculate for long side
|
|
30
|
+
* @returns Remaining capacity in collateral (0 if unlimited)
|
|
31
|
+
*/
|
|
32
|
+
export declare const getRemainingOiCapacity: (pairOi: UnifiedPairOi, currentPrice: number, long: boolean) => number;
|
|
33
|
+
/**
|
|
34
|
+
* @dev Updated group OI validation using dynamic OI
|
|
35
|
+
* @param pairIndex Index of the trading pair
|
|
36
|
+
* @param long Whether the position is long
|
|
37
|
+
* @param positionSizeCollateral Position size in collateral
|
|
38
|
+
* @param currentPrice Current collateral price in USD
|
|
39
|
+
* @param context Context with groups, pairs, and OI data
|
|
40
|
+
* @returns true if within group limits, false if would exceed
|
|
41
|
+
*/
|
|
42
|
+
export declare const withinMaxGroupOiDynamic: (pairIndex: PairIndex, long: boolean, positionSizeCollateral: number, currentPrice: number, context: {
|
|
43
|
+
groups: BorrowingFee.Group[];
|
|
44
|
+
pairs: BorrowingFee.Pair[];
|
|
45
|
+
pairOis: UnifiedPairOi[];
|
|
46
|
+
}) => boolean;
|
|
47
|
+
/**
|
|
48
|
+
* @dev Calculate total dynamic OI for a group
|
|
49
|
+
* @param groupIndex Index of the group
|
|
50
|
+
* @param currentPrice Current collateral price in USD
|
|
51
|
+
* @param context Context with pairs and OI data
|
|
52
|
+
* @returns Total dynamic OI for the group
|
|
53
|
+
*/
|
|
54
|
+
export declare const getGroupDynamicOi: (groupIndex: number, currentPrice: number, context: {
|
|
55
|
+
pairs: BorrowingFee.Pair[];
|
|
56
|
+
pairOis: UnifiedPairOi[];
|
|
57
|
+
}) => {
|
|
58
|
+
long: number;
|
|
59
|
+
short: number;
|
|
60
|
+
total: number;
|
|
61
|
+
};
|
|
62
|
+
/**
|
|
63
|
+
* @dev Check both pair and group OI limits
|
|
64
|
+
* @param pairIndex Index of the trading pair
|
|
65
|
+
* @param long Whether the position is long
|
|
66
|
+
* @param positionSizeCollateral Position size in collateral
|
|
67
|
+
* @param currentPrice Current collateral price in USD
|
|
68
|
+
* @param context Full context with all required data
|
|
69
|
+
* @returns Object with validation results
|
|
70
|
+
*/
|
|
71
|
+
export declare const validateOiLimits: (pairIndex: PairIndex, long: boolean, positionSizeCollateral: number, currentPrice: number, context: {
|
|
72
|
+
groups: BorrowingFee.Group[];
|
|
73
|
+
pairs: BorrowingFee.Pair[];
|
|
74
|
+
pairOis: UnifiedPairOi[];
|
|
75
|
+
}) => {
|
|
76
|
+
withinPairLimit: boolean;
|
|
77
|
+
withinGroupLimit: boolean;
|
|
78
|
+
pairRemainingCapacity: number;
|
|
79
|
+
groupRemainingCapacity: number;
|
|
80
|
+
};
|
|
@@ -0,0 +1,172 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev OI Validation module
|
|
4
|
+
* @dev Provides validation functions for Open Interest limits
|
|
5
|
+
*/
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
exports.validateOiLimits = exports.getGroupDynamicOi = exports.withinMaxGroupOiDynamic = exports.getRemainingOiCapacity = exports.calculateDynamicOi = exports.withinMaxPairOi = void 0;
|
|
8
|
+
const converter_1 = require("./converter");
|
|
9
|
+
/**
|
|
10
|
+
* @dev Check if a position would exceed per-pair OI limits
|
|
11
|
+
* @param pairOi Current OI data for the pair
|
|
12
|
+
* @param long Whether the position is long
|
|
13
|
+
* @param positionSizeCollateral Position size in collateral
|
|
14
|
+
* @param currentPrice Current collateral price in USD (required for dynamic OI)
|
|
15
|
+
* @returns true if within limits, false if would exceed
|
|
16
|
+
*/
|
|
17
|
+
const withinMaxPairOi = (pairOi, long, positionSizeCollateral, currentPrice) => {
|
|
18
|
+
// If maxCollateral is 0, unlimited OI allowed
|
|
19
|
+
if (pairOi.maxCollateral === 0) {
|
|
20
|
+
return true;
|
|
21
|
+
}
|
|
22
|
+
// Calculate current dynamic OI
|
|
23
|
+
const computed = (0, converter_1.computeOiValues)(pairOi, currentPrice);
|
|
24
|
+
const currentOi = long
|
|
25
|
+
? computed.totalDynamicCollateral.long
|
|
26
|
+
: computed.totalDynamicCollateral.short;
|
|
27
|
+
// Check if adding position would exceed max
|
|
28
|
+
const newOi = currentOi + positionSizeCollateral;
|
|
29
|
+
return newOi <= pairOi.maxCollateral;
|
|
30
|
+
};
|
|
31
|
+
exports.withinMaxPairOi = withinMaxPairOi;
|
|
32
|
+
/**
|
|
33
|
+
* @dev Calculate dynamic OI for a specific side
|
|
34
|
+
* @param pairOi OI data for the pair
|
|
35
|
+
* @param currentPrice Current collateral price in USD
|
|
36
|
+
* @param long Whether to calculate for long side
|
|
37
|
+
* @returns Dynamic OI in collateral value
|
|
38
|
+
*/
|
|
39
|
+
const calculateDynamicOi = (pairOi, currentPrice, long) => {
|
|
40
|
+
const computed = (0, converter_1.computeOiValues)(pairOi, currentPrice);
|
|
41
|
+
return long
|
|
42
|
+
? computed.totalDynamicCollateral.long
|
|
43
|
+
: computed.totalDynamicCollateral.short;
|
|
44
|
+
};
|
|
45
|
+
exports.calculateDynamicOi = calculateDynamicOi;
|
|
46
|
+
/**
|
|
47
|
+
* @dev Calculate remaining OI capacity for a side
|
|
48
|
+
* @param pairOi OI data for the pair
|
|
49
|
+
* @param currentPrice Current collateral price in USD
|
|
50
|
+
* @param long Whether to calculate for long side
|
|
51
|
+
* @returns Remaining capacity in collateral (0 if unlimited)
|
|
52
|
+
*/
|
|
53
|
+
const getRemainingOiCapacity = (pairOi, currentPrice, long) => {
|
|
54
|
+
// If maxCollateral is 0, unlimited capacity
|
|
55
|
+
if (pairOi.maxCollateral === 0) {
|
|
56
|
+
return 0; // Indicates unlimited
|
|
57
|
+
}
|
|
58
|
+
const dynamicOi = (0, exports.calculateDynamicOi)(pairOi, currentPrice, long);
|
|
59
|
+
const remaining = pairOi.maxCollateral - dynamicOi;
|
|
60
|
+
// Return 0 if already at or over capacity
|
|
61
|
+
return Math.max(0, remaining);
|
|
62
|
+
};
|
|
63
|
+
exports.getRemainingOiCapacity = getRemainingOiCapacity;
|
|
64
|
+
/**
|
|
65
|
+
* @dev Updated group OI validation using dynamic OI
|
|
66
|
+
* @param pairIndex Index of the trading pair
|
|
67
|
+
* @param long Whether the position is long
|
|
68
|
+
* @param positionSizeCollateral Position size in collateral
|
|
69
|
+
* @param currentPrice Current collateral price in USD
|
|
70
|
+
* @param context Context with groups, pairs, and OI data
|
|
71
|
+
* @returns true if within group limits, false if would exceed
|
|
72
|
+
*/
|
|
73
|
+
const withinMaxGroupOiDynamic = (pairIndex, long, positionSizeCollateral, currentPrice, context) => {
|
|
74
|
+
const pair = context.pairs[pairIndex];
|
|
75
|
+
if (!pair)
|
|
76
|
+
return false;
|
|
77
|
+
// Get group index from first group in pair's groups array
|
|
78
|
+
const groupIndex = pair.groups.length > 0 ? pair.groups[0].groupIndex : 0;
|
|
79
|
+
const group = context.groups[groupIndex];
|
|
80
|
+
if (!group)
|
|
81
|
+
return false;
|
|
82
|
+
// If maxOi is 0, unlimited OI allowed
|
|
83
|
+
if (group.oi.max === 0) {
|
|
84
|
+
return true;
|
|
85
|
+
}
|
|
86
|
+
// Calculate total dynamic OI for all pairs in group
|
|
87
|
+
let totalGroupOi = 0;
|
|
88
|
+
context.pairs.forEach((p, idx) => {
|
|
89
|
+
const pGroupIndex = p.groups.length > 0 ? p.groups[0].groupIndex : 0;
|
|
90
|
+
if (pGroupIndex === groupIndex && context.pairOis[idx]) {
|
|
91
|
+
const pairOi = context.pairOis[idx];
|
|
92
|
+
const computed = (0, converter_1.computeOiValues)(pairOi, currentPrice);
|
|
93
|
+
// Add both long and short OI for the pair
|
|
94
|
+
totalGroupOi +=
|
|
95
|
+
computed.totalDynamicCollateral.long +
|
|
96
|
+
computed.totalDynamicCollateral.short;
|
|
97
|
+
}
|
|
98
|
+
});
|
|
99
|
+
// Check if adding position would exceed group max
|
|
100
|
+
const newTotalOi = totalGroupOi + positionSizeCollateral;
|
|
101
|
+
return newTotalOi <= group.oi.max;
|
|
102
|
+
};
|
|
103
|
+
exports.withinMaxGroupOiDynamic = withinMaxGroupOiDynamic;
|
|
104
|
+
/**
|
|
105
|
+
* @dev Calculate total dynamic OI for a group
|
|
106
|
+
* @param groupIndex Index of the group
|
|
107
|
+
* @param currentPrice Current collateral price in USD
|
|
108
|
+
* @param context Context with pairs and OI data
|
|
109
|
+
* @returns Total dynamic OI for the group
|
|
110
|
+
*/
|
|
111
|
+
const getGroupDynamicOi = (groupIndex, currentPrice, context) => {
|
|
112
|
+
let longOi = 0;
|
|
113
|
+
let shortOi = 0;
|
|
114
|
+
context.pairs.forEach((p, idx) => {
|
|
115
|
+
const pGroupIndex = p.groups.length > 0 ? p.groups[0].groupIndex : 0;
|
|
116
|
+
if (pGroupIndex === groupIndex && context.pairOis[idx]) {
|
|
117
|
+
const pairOi = context.pairOis[idx];
|
|
118
|
+
const computed = (0, converter_1.computeOiValues)(pairOi, currentPrice);
|
|
119
|
+
longOi += computed.totalDynamicCollateral.long;
|
|
120
|
+
shortOi += computed.totalDynamicCollateral.short;
|
|
121
|
+
}
|
|
122
|
+
});
|
|
123
|
+
return {
|
|
124
|
+
long: longOi,
|
|
125
|
+
short: shortOi,
|
|
126
|
+
total: longOi + shortOi,
|
|
127
|
+
};
|
|
128
|
+
};
|
|
129
|
+
exports.getGroupDynamicOi = getGroupDynamicOi;
|
|
130
|
+
/**
|
|
131
|
+
* @dev Check both pair and group OI limits
|
|
132
|
+
* @param pairIndex Index of the trading pair
|
|
133
|
+
* @param long Whether the position is long
|
|
134
|
+
* @param positionSizeCollateral Position size in collateral
|
|
135
|
+
* @param currentPrice Current collateral price in USD
|
|
136
|
+
* @param context Full context with all required data
|
|
137
|
+
* @returns Object with validation results
|
|
138
|
+
*/
|
|
139
|
+
const validateOiLimits = (pairIndex, long, positionSizeCollateral, currentPrice, context) => {
|
|
140
|
+
const pairOi = context.pairOis[pairIndex];
|
|
141
|
+
if (!pairOi) {
|
|
142
|
+
return {
|
|
143
|
+
withinPairLimit: false,
|
|
144
|
+
withinGroupLimit: false,
|
|
145
|
+
pairRemainingCapacity: 0,
|
|
146
|
+
groupRemainingCapacity: 0,
|
|
147
|
+
};
|
|
148
|
+
}
|
|
149
|
+
// Check pair limits
|
|
150
|
+
const withinPairLimit = (0, exports.withinMaxPairOi)(pairOi, long, positionSizeCollateral, currentPrice);
|
|
151
|
+
const pairRemainingCapacity = (0, exports.getRemainingOiCapacity)(pairOi, currentPrice, long);
|
|
152
|
+
// Check group limits
|
|
153
|
+
const withinGroupLimit = (0, exports.withinMaxGroupOiDynamic)(pairIndex, long, positionSizeCollateral, currentPrice, context);
|
|
154
|
+
// Calculate group remaining capacity
|
|
155
|
+
const pair = context.pairs[pairIndex];
|
|
156
|
+
let groupRemainingCapacity = 0;
|
|
157
|
+
if (pair) {
|
|
158
|
+
const groupIndex = pair.groups.length > 0 ? pair.groups[0].groupIndex : 0;
|
|
159
|
+
const group = context.groups[groupIndex];
|
|
160
|
+
if (group && group.oi.max > 0) {
|
|
161
|
+
const groupOi = (0, exports.getGroupDynamicOi)(groupIndex, currentPrice, context);
|
|
162
|
+
groupRemainingCapacity = Math.max(0, group.oi.max - groupOi.total);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
return {
|
|
166
|
+
withinPairLimit,
|
|
167
|
+
withinGroupLimit,
|
|
168
|
+
pairRemainingCapacity,
|
|
169
|
+
groupRemainingCapacity,
|
|
170
|
+
};
|
|
171
|
+
};
|
|
172
|
+
exports.validateOiLimits = validateOiLimits;
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Builder functions for creating market price contexts
|
|
3
|
+
*/
|
|
4
|
+
import { MarketPriceContext } from "./types";
|
|
5
|
+
import { TradingVariablesCollateral } from "../../backend/tradingVariables/types";
|
|
6
|
+
/**
|
|
7
|
+
* @dev Builds a market price context for a specific pair from trading variables
|
|
8
|
+
* @param tradingVariables Trading variables for a specific collateral
|
|
9
|
+
* @param pairIndex Pair index to create context for
|
|
10
|
+
* @returns Market price context for the pair
|
|
11
|
+
*/
|
|
12
|
+
export declare const buildMarketPriceContext: (tradingVariables: TradingVariablesCollateral, pairIndex: number) => MarketPriceContext;
|
|
13
|
+
/**
|
|
14
|
+
* @dev Creates an empty market price context
|
|
15
|
+
* @returns Empty market price context
|
|
16
|
+
*/
|
|
17
|
+
export declare const createEmptyMarketPriceContext: () => MarketPriceContext;
|
|
18
|
+
/**
|
|
19
|
+
* @dev Creates a market price context with custom values
|
|
20
|
+
* @param skewDepth Skew depth in tokens
|
|
21
|
+
* @param oiLongToken Long OI in tokens
|
|
22
|
+
* @param oiShortToken Short OI in tokens
|
|
23
|
+
* @returns Market price context
|
|
24
|
+
*/
|
|
25
|
+
export declare const createMarketPriceContext: (skewDepth: number, oiLongToken: number, oiShortToken: number) => MarketPriceContext;
|