@gainsnetwork/sdk 0.2.67-rc6 → 0.2.67-rc8
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/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 +312 -0
- package/lib/backend/tradingVariables/backend.types.js +2 -0
- package/lib/backend/tradingVariables/converter.d.ts +31 -0
- package/lib/backend/tradingVariables/converter.js +330 -0
- package/lib/backend/tradingVariables/index.d.ts +5 -0
- package/lib/backend/tradingVariables/index.js +95 -0
- package/lib/backend/tradingVariables/types.d.ts +109 -0
- package/lib/backend/tradingVariables/types.js +14 -0
- package/lib/constants.js +2 -3
- 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/GNSMultiCollatDiamond.d.ts +2140 -286
- 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/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/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/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 +4502 -427
- 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/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.js +65 -142
- package/lib/contracts/utils/openLimitOrders.d.ts +8 -0
- package/lib/contracts/utils/openLimitOrders.js +88 -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/oi/converter.d.ts +63 -0
- package/lib/markets/oi/converter.js +103 -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 +10 -0
- package/lib/markets/oi/index.js +37 -0
- package/lib/markets/oi/types.d.ts +82 -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/trade/fees/borrowing/builder.d.ts +14 -0
- package/lib/trade/fees/borrowing/builder.js +33 -0
- 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 +110 -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 +146 -0
- package/lib/trade/fees/fundingFees/index.js +346 -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/tiers/converter.d.ts +54 -0
- package/lib/trade/fees/tiers/converter.js +81 -0
- 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 +30 -0
- package/lib/trade/fees/trading/converter.js +43 -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/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 +26 -0
- package/lib/trade/liquidation/index.js +142 -0
- package/lib/trade/liquidation/types.d.ts +59 -0
- package/lib/trade/liquidation/types.js +2 -0
- package/lib/trade/openLimitOrder.d.ts +2 -0
- package/lib/trade/openLimitOrder.js +23 -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 +77 -0
- package/lib/trade/pnl/index.js +270 -0
- package/lib/trade/pnl/types.d.ts +114 -0
- package/lib/trade/pnl/types.js +5 -0
- package/lib/trade/priceImpact/close/index.d.ts +21 -0
- package/lib/trade/priceImpact/close/index.js +131 -0
- package/lib/trade/priceImpact/close/types.d.ts +43 -0
- package/lib/trade/priceImpact/close/types.js +5 -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 +107 -0
- package/lib/trade/priceImpact/cumulVol/index.js +228 -0
- package/lib/trade/priceImpact/index.d.ts +12 -0
- package/lib/trade/priceImpact/index.js +59 -0
- package/lib/trade/priceImpact/open/index.d.ts +22 -0
- package/lib/trade/priceImpact/open/index.js +76 -0
- package/lib/trade/priceImpact/open/types.d.ts +41 -0
- package/lib/trade/priceImpact/open/types.js +5 -0
- package/lib/trade/priceImpact/skew/converter.d.ts +77 -0
- package/lib/trade/priceImpact/skew/converter.js +171 -0
- package/lib/trade/priceImpact/skew/fetcher.d.ts +63 -0
- package/lib/trade/priceImpact/skew/fetcher.js +168 -0
- package/lib/trade/priceImpact/skew/index.d.ts +58 -0
- package/lib/trade/priceImpact/skew/index.js +179 -0
- package/lib/trade/priceImpact/skew/types.d.ts +55 -0
- package/lib/trade/priceImpact/skew/types.js +5 -0
- package/lib/trade/utils.d.ts +18 -0
- package/lib/trade/utils.js +30 -0
- package/package.json +1 -1
|
@@ -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,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Main export file for OI module
|
|
3
|
+
* @dev Provides unified Open Interest management functionality
|
|
4
|
+
*/
|
|
5
|
+
export { UnifiedPairOi, GroupOi, OiUsageMetadata, ComputedOi } from "./types";
|
|
6
|
+
export { convertBeforeV10Collateral, convertCollateralOi, convertTokenOi, convertPairOi, convertPairOiArray, computeOiValues, } from "./converter";
|
|
7
|
+
export { fetchPairOi, fetchMultiplePairOi, createOiContext, fetchOiForUseCase, } from "./fetcher";
|
|
8
|
+
export { withinMaxPairOi, calculateDynamicOi, getRemainingOiCapacity, withinMaxGroupOiDynamic, getGroupDynamicOi, validateOiLimits, } from "./validation";
|
|
9
|
+
import { OiUsageMetadata } from "./types";
|
|
10
|
+
export declare const OI_USAGE: OiUsageMetadata;
|
|
@@ -0,0 +1,37 @@
|
|
|
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.OI_USAGE = exports.validateOiLimits = exports.getGroupDynamicOi = exports.withinMaxGroupOiDynamic = exports.getRemainingOiCapacity = exports.calculateDynamicOi = exports.withinMaxPairOi = exports.fetchOiForUseCase = exports.createOiContext = exports.fetchMultiplePairOi = exports.fetchPairOi = exports.computeOiValues = exports.convertPairOiArray = exports.convertPairOi = exports.convertTokenOi = exports.convertCollateralOi = exports.convertBeforeV10Collateral = void 0;
|
|
8
|
+
// Converters
|
|
9
|
+
var converter_1 = require("./converter");
|
|
10
|
+
Object.defineProperty(exports, "convertBeforeV10Collateral", { enumerable: true, get: function () { return converter_1.convertBeforeV10Collateral; } });
|
|
11
|
+
Object.defineProperty(exports, "convertCollateralOi", { enumerable: true, get: function () { return converter_1.convertCollateralOi; } });
|
|
12
|
+
Object.defineProperty(exports, "convertTokenOi", { enumerable: true, get: function () { return converter_1.convertTokenOi; } });
|
|
13
|
+
Object.defineProperty(exports, "convertPairOi", { enumerable: true, get: function () { return converter_1.convertPairOi; } });
|
|
14
|
+
Object.defineProperty(exports, "convertPairOiArray", { enumerable: true, get: function () { return converter_1.convertPairOiArray; } });
|
|
15
|
+
Object.defineProperty(exports, "computeOiValues", { enumerable: true, get: function () { return converter_1.computeOiValues; } });
|
|
16
|
+
// Fetchers
|
|
17
|
+
var fetcher_1 = require("./fetcher");
|
|
18
|
+
Object.defineProperty(exports, "fetchPairOi", { enumerable: true, get: function () { return fetcher_1.fetchPairOi; } });
|
|
19
|
+
Object.defineProperty(exports, "fetchMultiplePairOi", { enumerable: true, get: function () { return fetcher_1.fetchMultiplePairOi; } });
|
|
20
|
+
Object.defineProperty(exports, "createOiContext", { enumerable: true, get: function () { return fetcher_1.createOiContext; } });
|
|
21
|
+
Object.defineProperty(exports, "fetchOiForUseCase", { enumerable: true, get: function () { return fetcher_1.fetchOiForUseCase; } });
|
|
22
|
+
// Validation
|
|
23
|
+
var validation_1 = require("./validation");
|
|
24
|
+
Object.defineProperty(exports, "withinMaxPairOi", { enumerable: true, get: function () { return validation_1.withinMaxPairOi; } });
|
|
25
|
+
Object.defineProperty(exports, "calculateDynamicOi", { enumerable: true, get: function () { return validation_1.calculateDynamicOi; } });
|
|
26
|
+
Object.defineProperty(exports, "getRemainingOiCapacity", { enumerable: true, get: function () { return validation_1.getRemainingOiCapacity; } });
|
|
27
|
+
Object.defineProperty(exports, "withinMaxGroupOiDynamic", { enumerable: true, get: function () { return validation_1.withinMaxGroupOiDynamic; } });
|
|
28
|
+
Object.defineProperty(exports, "getGroupDynamicOi", { enumerable: true, get: function () { return validation_1.getGroupDynamicOi; } });
|
|
29
|
+
Object.defineProperty(exports, "validateOiLimits", { enumerable: true, get: function () { return validation_1.validateOiLimits; } });
|
|
30
|
+
exports.OI_USAGE = {
|
|
31
|
+
borrowingV1: ["beforeV10Collateral", "token"],
|
|
32
|
+
fundingFees: ["token"],
|
|
33
|
+
skewImpact: ["token"],
|
|
34
|
+
maxPairOi: ["beforeV10Collateral", "token"],
|
|
35
|
+
maxGroupOi: ["beforeV10Collateral", "token"],
|
|
36
|
+
maxSkew: ["token"], // V10+ only: uses token OI
|
|
37
|
+
};
|
|
@@ -0,0 +1,82 @@
|
|
|
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
|
+
/**
|
|
41
|
+
* @dev Group OI remains unchanged - only used by borrowing v1
|
|
42
|
+
* @dev Re-export existing type for consistency
|
|
43
|
+
*/
|
|
44
|
+
export { OpenInterest as GroupOi } from "../../trade/types";
|
|
45
|
+
/**
|
|
46
|
+
* @dev Metadata describing which OI systems are used by different features
|
|
47
|
+
* @dev Helps consumers understand OI usage patterns
|
|
48
|
+
*/
|
|
49
|
+
export interface OiUsageMetadata {
|
|
50
|
+
borrowingV1: Array<"beforeV10Collateral" | "token">;
|
|
51
|
+
fundingFees: Array<"token">;
|
|
52
|
+
skewImpact: Array<"token">;
|
|
53
|
+
maxPairOi: Array<"beforeV10Collateral" | "token">;
|
|
54
|
+
maxGroupOi: Array<"beforeV10Collateral" | "token">;
|
|
55
|
+
maxSkew: Array<"token">;
|
|
56
|
+
}
|
|
57
|
+
/**
|
|
58
|
+
* @dev Helper type for computed OI values
|
|
59
|
+
*/
|
|
60
|
+
export interface ComputedOi {
|
|
61
|
+
/**
|
|
62
|
+
* @dev Total OI using static values: beforeV10Collateral + collateral
|
|
63
|
+
* @dev Used for administrative operations
|
|
64
|
+
*/
|
|
65
|
+
totalStaticCollateral: {
|
|
66
|
+
long: number;
|
|
67
|
+
short: number;
|
|
68
|
+
};
|
|
69
|
+
/**
|
|
70
|
+
* @dev Total OI using dynamic values: beforeV10Collateral + (token * price)
|
|
71
|
+
* @dev Used for real-time calculations like fees
|
|
72
|
+
*/
|
|
73
|
+
totalDynamicCollateral: {
|
|
74
|
+
long: number;
|
|
75
|
+
short: number;
|
|
76
|
+
};
|
|
77
|
+
/**
|
|
78
|
+
* @dev Net skew in tokens (v10+ only)
|
|
79
|
+
* @dev Positive = more longs, negative = more shorts
|
|
80
|
+
*/
|
|
81
|
+
skewToken: number;
|
|
82
|
+
}
|
|
@@ -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,14 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Context builder for borrowing v1 fees
|
|
3
|
+
*/
|
|
4
|
+
import { GlobalTradingVariablesType } from "../../../backend/tradingVariables/types";
|
|
5
|
+
import { GetBorrowingFeeContext } from "./index";
|
|
6
|
+
/**
|
|
7
|
+
* @dev Builds borrowing v1 context from global trading variables
|
|
8
|
+
* @dev Returns full array-based context required for v1 borrowing fee calculations
|
|
9
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
10
|
+
* @param collateralIndex Collateral index (1-based)
|
|
11
|
+
* @param currentBlock Current block number
|
|
12
|
+
* @returns Full borrowing context with all pairs and groups or undefined if data not available
|
|
13
|
+
*/
|
|
14
|
+
export declare const buildBorrowingV1Context: (globalTradingVariables: GlobalTradingVariablesType, collateralIndex: number, currentBlock: number) => GetBorrowingFeeContext | undefined;
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Context builder for borrowing v1 fees
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.buildBorrowingV1Context = void 0;
|
|
7
|
+
/**
|
|
8
|
+
* @dev Builds borrowing v1 context from global trading variables
|
|
9
|
+
* @dev Returns full array-based context required for v1 borrowing fee calculations
|
|
10
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
11
|
+
* @param collateralIndex Collateral index (1-based)
|
|
12
|
+
* @param currentBlock Current block number
|
|
13
|
+
* @returns Full borrowing context with all pairs and groups or undefined if data not available
|
|
14
|
+
*/
|
|
15
|
+
const buildBorrowingV1Context = (globalTradingVariables, collateralIndex, currentBlock) => {
|
|
16
|
+
var _a;
|
|
17
|
+
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
18
|
+
if (!(collateral === null || collateral === void 0 ? void 0 : collateral.pairBorrowingFees) || !(collateral === null || collateral === void 0 ? void 0 : collateral.groupBorrowingFees)) {
|
|
19
|
+
return undefined;
|
|
20
|
+
}
|
|
21
|
+
const pairs = collateral.pairBorrowingFees;
|
|
22
|
+
const groups = collateral.groupBorrowingFees;
|
|
23
|
+
if (pairs.length === 0 || groups.length === 0) {
|
|
24
|
+
return undefined;
|
|
25
|
+
}
|
|
26
|
+
return {
|
|
27
|
+
currentBlock,
|
|
28
|
+
pairs,
|
|
29
|
+
groups,
|
|
30
|
+
collateralPriceUsd: ((_a = collateral.prices) === null || _a === void 0 ? void 0 : _a.collateralPriceUsd) || 1,
|
|
31
|
+
};
|
|
32
|
+
};
|
|
33
|
+
exports.buildBorrowingV1Context = buildBorrowingV1Context;
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { GlobalTradingVariablesType } from "../../../backend/tradingVariables/types";
|
|
2
|
+
import { GetPairBorrowingFeeV2Context } from "./types";
|
|
3
|
+
/**
|
|
4
|
+
* @dev Builds borrowing v2 sub-context for a specific pair
|
|
5
|
+
*/
|
|
6
|
+
export declare const buildBorrowingV2Context: (globalTradingVariables: GlobalTradingVariablesType, collateralIndex: number, pairIndex: number, currentTimestamp: number) => GetPairBorrowingFeeV2Context | undefined;
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildBorrowingV2Context = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* @dev Builds borrowing v2 sub-context for a specific pair
|
|
6
|
+
*/
|
|
7
|
+
const buildBorrowingV2Context = (globalTradingVariables, collateralIndex, pairIndex, currentTimestamp) => {
|
|
8
|
+
var _a, _b;
|
|
9
|
+
const collateral = globalTradingVariables.collaterals[collateralIndex - 1];
|
|
10
|
+
if (!(collateral === null || collateral === void 0 ? void 0 : collateral.pairBorrowingFeesV2)) {
|
|
11
|
+
return undefined;
|
|
12
|
+
}
|
|
13
|
+
const params = (_a = collateral.pairBorrowingFeesV2.params) === null || _a === void 0 ? void 0 : _a[pairIndex];
|
|
14
|
+
const data = (_b = collateral.pairBorrowingFeesV2.data) === null || _b === void 0 ? void 0 : _b[pairIndex];
|
|
15
|
+
if (!params || !data) {
|
|
16
|
+
return undefined;
|
|
17
|
+
}
|
|
18
|
+
return {
|
|
19
|
+
params,
|
|
20
|
+
data,
|
|
21
|
+
currentTimestamp,
|
|
22
|
+
};
|
|
23
|
+
};
|
|
24
|
+
exports.buildBorrowingV2Context = buildBorrowingV2Context;
|