@gainsnetwork/sdk 1.5.0 → 1.5.1-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/lib/constants.d.ts +4 -0
- package/lib/constants.js +10 -4
- package/lib/contracts/utils/pairs.js +4 -0
- package/lib/trade/types.d.ts +5 -1
- package/lib/trade/types.js +4 -0
- package/package.json +1 -1
- package/lib/contracts/types/generated/GFarmTradingStorageV5.d.ts +0 -1911
- package/lib/contracts/types/generated/GFarmTradingStorageV5.js +0 -2
- package/lib/contracts/types/generated/GNSBorrowingFees.d.ts +0 -1067
- package/lib/contracts/types/generated/GNSBorrowingFees.js +0 -2
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.d.ts +0 -979
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_3_2.js +0 -2
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.d.ts +0 -1058
- package/lib/contracts/types/generated/GNSBorrowingFeesV6_4.js +0 -2
- package/lib/contracts/types/generated/GNSNftRewardsV6.d.ts +0 -533
- package/lib/contracts/types/generated/GNSNftRewardsV6.js +0 -2
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.d.ts +0 -613
- package/lib/contracts/types/generated/GNSNftRewardsV6_3_1.js +0 -2
- package/lib/contracts/types/generated/GNSPairInfosV6_1.d.ts +0 -911
- package/lib/contracts/types/generated/GNSPairInfosV6_1.js +0 -2
- package/lib/contracts/types/generated/GNSPairsStorageV6.d.ts +0 -660
- package/lib/contracts/types/generated/GNSPairsStorageV6.js +0 -2
- package/lib/contracts/types/generated/GNSTrading.d.ts +0 -758
- package/lib/contracts/types/generated/GNSTrading.js +0 -2
- package/lib/contracts/types/generated/GNSTradingCallbacks.d.ts +0 -875
- package/lib/contracts/types/generated/GNSTradingCallbacks.js +0 -2
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.d.ts +0 -806
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_3_2.js +0 -2
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.d.ts +0 -821
- package/lib/contracts/types/generated/GNSTradingCallbacksV6_4.js +0 -2
- package/lib/contracts/types/generated/GNSTradingStorage.d.ts +0 -1387
- package/lib/contracts/types/generated/GNSTradingStorage.js +0 -2
- package/lib/contracts/types/generated/GTokenV6_3_2.d.ts +0 -1838
- package/lib/contracts/types/generated/GTokenV6_3_2.js +0 -2
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.d.ts +0 -83
- package/lib/contracts/types/generated/factories/GFarmTradingStorageV5__factory.js +0 -2691
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.d.ts +0 -88
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_3_2__factory.js +0 -1654
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.d.ts +0 -113
- package/lib/contracts/types/generated/factories/GNSBorrowingFeesV6_4__factory.js +0 -1742
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.d.ts +0 -124
- package/lib/contracts/types/generated/factories/GNSBorrowingFees__factory.js +0 -1784
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.d.ts +0 -100
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6_3_1__factory.js +0 -1116
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.d.ts +0 -100
- package/lib/contracts/types/generated/factories/GNSNftRewardsV6__factory.js +0 -1003
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.d.ts +0 -98
- package/lib/contracts/types/generated/factories/GNSPairInfosV6_1__factory.js +0 -1485
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.d.ts +0 -117
- package/lib/contracts/types/generated/factories/GNSPairsStorageV6__factory.js +0 -1265
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.d.ts +0 -82
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_3_2__factory.js +0 -1273
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.d.ts +0 -82
- package/lib/contracts/types/generated/factories/GNSTradingCallbacksV6_4__factory.js +0 -1326
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.d.ts +0 -113
- package/lib/contracts/types/generated/factories/GNSTradingCallbacks__factory.js +0 -1428
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.d.ts +0 -96
- package/lib/contracts/types/generated/factories/GNSTradingStorage__factory.js +0 -2241
- package/lib/contracts/types/generated/factories/GNSTrading__factory.d.ts +0 -95
- package/lib/contracts/types/generated/factories/GNSTrading__factory.js +0 -1071
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.d.ts +0 -110
- package/lib/contracts/types/generated/factories/GTokenV6_3_2__factory.js +0 -2682
- package/lib/markets/oi/fetcher.d.ts +0 -58
- package/lib/markets/oi/fetcher.js +0 -181
- package/lib/markets/oi/validation.d.ts +0 -80
- package/lib/markets/oi/validation.js +0 -172
- package/lib/pricing/depthBands/converter.d.ts +0 -65
- package/lib/pricing/depthBands/converter.js +0 -155
- package/lib/pricing/depthBands/decoder.d.ts +0 -32
- package/lib/pricing/depthBands/decoder.js +0 -109
- package/lib/pricing/depthBands/encoder.d.ts +0 -19
- package/lib/pricing/depthBands/encoder.js +0 -105
- package/lib/pricing/depthBands/index.d.ts +0 -8
- package/lib/pricing/depthBands/index.js +0 -26
- package/lib/pricing/depthBands/types.d.ts +0 -49
- package/lib/pricing/depthBands/types.js +0 -10
- package/lib/pricing/depthBands/validator.d.ts +0 -22
- package/lib/pricing/depthBands/validator.js +0 -113
- package/lib/trade/effectiveLeverage/builder.d.ts +0 -23
- package/lib/trade/effectiveLeverage/builder.js +0 -30
- package/lib/trade/fees/holdingFees/index.d.ts +0 -46
- package/lib/trade/fees/holdingFees/index.js +0 -105
- package/lib/trade/fees/holdingFees/types.d.ts +0 -23
- package/lib/trade/fees/holdingFees/types.js +0 -5
- package/lib/trade/fees/trading/holdingFees.d.ts +0 -28
- package/lib/trade/fees/trading/holdingFees.js +0 -66
- package/lib/trade/fees/trading/holdingFeesStructured.d.ts +0 -28
- package/lib/trade/fees/trading/holdingFeesStructured.js +0 -66
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @dev Fetchers for retrieving OI data from contracts
|
|
3
|
-
* @dev Consolidates the three OI storage systems into unified format
|
|
4
|
-
*/
|
|
5
|
-
import { ethers } from "ethers";
|
|
6
|
-
import { ChainId } from "../../contracts/types";
|
|
7
|
-
import { UnifiedPairOi } from "./types";
|
|
8
|
-
/**
|
|
9
|
-
* @dev Fetches all OI data for a single pair
|
|
10
|
-
* @param chainId Target chain
|
|
11
|
-
* @param collateralIndex Collateral type
|
|
12
|
-
* @param pairIndex Trading pair
|
|
13
|
-
* @param signer Ethers signer
|
|
14
|
-
* @returns Unified PairOi structure with all OI data
|
|
15
|
-
*/
|
|
16
|
-
export declare function fetchPairOi(chainId: ChainId, collateralIndex: number, pairIndex: number, signer: ethers.Signer): Promise<UnifiedPairOi>;
|
|
17
|
-
/**
|
|
18
|
-
* @dev Fetches OI data for multiple pairs efficiently
|
|
19
|
-
* @param chainId Target chain
|
|
20
|
-
* @param collateralIndex Collateral type
|
|
21
|
-
* @param pairIndices Array of trading pairs
|
|
22
|
-
* @param signer Ethers signer
|
|
23
|
-
* @returns Array of unified PairOi structures
|
|
24
|
-
*/
|
|
25
|
-
export declare function fetchMultiplePairOi(chainId: ChainId, collateralIndex: number, pairIndices: number[], signer: ethers.Signer): Promise<UnifiedPairOi[]>;
|
|
26
|
-
/**
|
|
27
|
-
* @dev Creates OI context for fee calculations
|
|
28
|
-
* @param chainId Target chain
|
|
29
|
-
* @param collateralIndex Collateral type
|
|
30
|
-
* @param pairIndex Trading pair
|
|
31
|
-
* @param signer Ethers signer
|
|
32
|
-
* @returns OI data formatted for SDK calculations
|
|
33
|
-
*/
|
|
34
|
-
export declare function createOiContext(chainId: ChainId, collateralIndex: number, pairIndex: number, signer: ethers.Signer): Promise<{
|
|
35
|
-
pairOi: UnifiedPairOi;
|
|
36
|
-
currentPrice: number;
|
|
37
|
-
computed: {
|
|
38
|
-
totalDynamicOi: {
|
|
39
|
-
long: number;
|
|
40
|
-
short: number;
|
|
41
|
-
};
|
|
42
|
-
totalStaticOi: {
|
|
43
|
-
long: number;
|
|
44
|
-
short: number;
|
|
45
|
-
};
|
|
46
|
-
skew: number;
|
|
47
|
-
};
|
|
48
|
-
}>;
|
|
49
|
-
/**
|
|
50
|
-
* @dev Fetches only the OI data needed for specific use cases
|
|
51
|
-
* @param chainId Target chain
|
|
52
|
-
* @param collateralIndex Collateral type
|
|
53
|
-
* @param pairIndex Trading pair
|
|
54
|
-
* @param useCase Which OI systems to fetch
|
|
55
|
-
* @param signer Ethers signer
|
|
56
|
-
* @returns Partial OI data based on use case
|
|
57
|
-
*/
|
|
58
|
-
export declare function fetchOiForUseCase(chainId: ChainId, collateralIndex: number, pairIndex: number, useCase: "skew" | "funding" | "borrowingV1" | "limits", signer: ethers.Signer): Promise<Partial<UnifiedPairOi>>;
|
|
@@ -1,181 +0,0 @@
|
|
|
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;
|
|
@@ -1,80 +0,0 @@
|
|
|
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
|
-
};
|
|
@@ -1,172 +0,0 @@
|
|
|
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;
|
|
@@ -1,65 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* @dev Depth bands conversion utilities
|
|
3
|
-
*/
|
|
4
|
-
import { type DepthBandsInput } from "./types";
|
|
5
|
-
/**
|
|
6
|
-
* Convert raw depth data from exchanges to depth bands input format
|
|
7
|
-
* @param totalDepthUsd Total depth in USD (already in 6 decimal precision)
|
|
8
|
-
* @param depthLevels Array of depth levels with cumulative USD amounts
|
|
9
|
-
* @returns Formatted depth bands input
|
|
10
|
-
*/
|
|
11
|
-
export declare function formatDepthBandsInput(totalDepthUsd: number, depthLevels: Array<{
|
|
12
|
-
cumulativeUsd: number;
|
|
13
|
-
}>): DepthBandsInput;
|
|
14
|
-
/**
|
|
15
|
-
* Convert percentage-based depth configuration to cumulative USD values
|
|
16
|
-
* @param totalDepthUsd Total depth in USD
|
|
17
|
-
* @param percentages Array of 30 cumulative percentages (0-100)
|
|
18
|
-
* @returns Depth bands input with cumulative USD values
|
|
19
|
-
*/
|
|
20
|
-
export declare function percentagesToDepthBands(totalDepthUsd: number, percentages: number[]): DepthBandsInput;
|
|
21
|
-
/**
|
|
22
|
-
* Convert basis points array to percentage array
|
|
23
|
-
* @param bps Array of values in basis points
|
|
24
|
-
* @returns Array of percentages (0-100)
|
|
25
|
-
*/
|
|
26
|
-
export declare function bpsToPercentages(bps: number[]): number[];
|
|
27
|
-
/**
|
|
28
|
-
* Convert percentage array to basis points array
|
|
29
|
-
* @param percentages Array of percentages (0-100)
|
|
30
|
-
* @returns Array of values in basis points
|
|
31
|
-
*/
|
|
32
|
-
export declare function percentagesToBps(percentages: number[]): number[];
|
|
33
|
-
/**
|
|
34
|
-
* Convert parts per million (ppm) to percentage
|
|
35
|
-
* @param ppm Value in parts per million
|
|
36
|
-
* @returns Percentage value
|
|
37
|
-
*/
|
|
38
|
-
export declare function ppmToPercentage(ppm: number): number;
|
|
39
|
-
/**
|
|
40
|
-
* Convert percentage to parts per million (ppm)
|
|
41
|
-
* @param percentage Percentage value
|
|
42
|
-
* @returns Value in parts per million
|
|
43
|
-
*/
|
|
44
|
-
export declare function percentageToPpm(percentage: number): number;
|
|
45
|
-
/**
|
|
46
|
-
* Create a linear depth bands configuration
|
|
47
|
-
* @param totalDepthUsd Total depth in USD
|
|
48
|
-
* @returns Linear depth bands from 0% to 100%
|
|
49
|
-
*/
|
|
50
|
-
export declare function createLinearDepthBands(totalDepthUsd: number): DepthBandsInput;
|
|
51
|
-
/**
|
|
52
|
-
* Create an exponential depth bands configuration
|
|
53
|
-
* @param totalDepthUsd Total depth in USD
|
|
54
|
-
* @param factor Exponential factor (higher = more concentrated near 0)
|
|
55
|
-
* @returns Exponential depth bands configuration
|
|
56
|
-
*/
|
|
57
|
-
export declare function createExponentialDepthBands(totalDepthUsd: number, factor?: number): DepthBandsInput;
|
|
58
|
-
/**
|
|
59
|
-
* Create a concentrated depth bands configuration
|
|
60
|
-
* @param totalDepthUsd Total depth in USD
|
|
61
|
-
* @param concentrationPoint Percentage (0-100) where to concentrate liquidity
|
|
62
|
-
* @param concentrationStrength How much to concentrate (0-1, higher = more concentrated)
|
|
63
|
-
* @returns Concentrated depth bands configuration
|
|
64
|
-
*/
|
|
65
|
-
export declare function createConcentratedDepthBands(totalDepthUsd: number, concentrationPoint?: number, concentrationStrength?: number): DepthBandsInput;
|