@gainsnetwork/sdk 0.0.0-v10.rc1 → 0.0.0-v10.rc10
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 +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 +18 -1
- package/lib/constants.js +19 -2
- 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/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/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/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/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 +17 -0
- 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/index.d.ts +3 -0
- package/lib/markets/index.js +3 -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/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 +12 -0
- package/lib/markets/price/marketPrice.js +34 -0
- package/lib/markets/price/types.d.ts +23 -0
- package/lib/markets/price/types.js +5 -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 +24 -2
- package/lib/trade/fees/borrowing/index.js +51 -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/index.d.ts +7 -2
- package/lib/trade/fees/index.js +66 -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 +3 -2
- package/lib/trade/index.js +3 -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 +16 -0
- package/lib/trade/liquidation/index.js +123 -0
- package/lib/trade/liquidation/types.d.ts +42 -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 +77 -0
- package/lib/trade/pnl/index.js +270 -0
- package/lib/trade/pnl/types.d.ts +76 -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 +44 -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 +231 -0
- package/lib/trade/priceImpact/index.d.ts +13 -0
- package/lib/trade/priceImpact/index.js +64 -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 +79 -0
- package/lib/trade/priceImpact/open/types.d.ts +42 -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 +60 -0
- package/lib/trade/priceImpact/skew/index.js +173 -0
- package/lib/trade/priceImpact/skew/types.d.ts +42 -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 +44 -9
- package/lib/trade/types.js +17 -0
- 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,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Market price module exports
|
|
4
|
+
*/
|
|
5
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
6
|
+
if (k2 === undefined) k2 = k;
|
|
7
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
8
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
9
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
10
|
+
}
|
|
11
|
+
Object.defineProperty(o, k2, desc);
|
|
12
|
+
}) : (function(o, m, k, k2) {
|
|
13
|
+
if (k2 === undefined) k2 = k;
|
|
14
|
+
o[k2] = m[k];
|
|
15
|
+
}));
|
|
16
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
17
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
18
|
+
};
|
|
19
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
+
__exportStar(require("./types"), exports);
|
|
21
|
+
__exportStar(require("./marketPrice"), exports);
|
|
22
|
+
__exportStar(require("./builder"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Current market price calculation with skew impact
|
|
3
|
+
*/
|
|
4
|
+
import { MarketPriceResult, MarketPriceContext } from "./types";
|
|
5
|
+
/**
|
|
6
|
+
* @dev Calculates the current market price adjusted for skew impact
|
|
7
|
+
* @param pairIndex Trading pair index
|
|
8
|
+
* @param oraclePrice Oracle price for the pair
|
|
9
|
+
* @param context Market price context with depths and OI data
|
|
10
|
+
* @returns Current market price with skew impact applied
|
|
11
|
+
*/
|
|
12
|
+
export declare const getCurrentMarketPrice: (pairIndex: number, oraclePrice: number, context: MarketPriceContext) => MarketPriceResult;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Current market price calculation with skew impact
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getCurrentMarketPrice = void 0;
|
|
7
|
+
const skew_1 = require("../../trade/priceImpact/skew");
|
|
8
|
+
/**
|
|
9
|
+
* @dev Calculates the current market price adjusted for skew impact
|
|
10
|
+
* @param pairIndex Trading pair index
|
|
11
|
+
* @param oraclePrice Oracle price for the pair
|
|
12
|
+
* @param context Market price context with depths and OI data
|
|
13
|
+
* @returns Current market price with skew impact applied
|
|
14
|
+
*/
|
|
15
|
+
const getCurrentMarketPrice = (pairIndex, oraclePrice, context) => {
|
|
16
|
+
let skewImpactP = 0;
|
|
17
|
+
if (context.skewDepth > 0) {
|
|
18
|
+
const skewResult = (0, skew_1.getTradeSkewPriceImpact)({
|
|
19
|
+
collateralIndex: 0,
|
|
20
|
+
pairIndex,
|
|
21
|
+
long: true,
|
|
22
|
+
open: true,
|
|
23
|
+
positionSizeToken: 0, // Size 0 for current market price
|
|
24
|
+
}, context);
|
|
25
|
+
skewImpactP = skewResult.priceImpactP;
|
|
26
|
+
}
|
|
27
|
+
const marketPrice = oraclePrice * (1 + skewImpactP / 100);
|
|
28
|
+
return {
|
|
29
|
+
marketPrice,
|
|
30
|
+
skewImpactP,
|
|
31
|
+
oraclePrice,
|
|
32
|
+
};
|
|
33
|
+
};
|
|
34
|
+
exports.getCurrentMarketPrice = getCurrentMarketPrice;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Market price types for calculating price with skew impact
|
|
3
|
+
*/
|
|
4
|
+
export type MarketPriceInput = {
|
|
5
|
+
collateralIndex: number;
|
|
6
|
+
pairIndex: number;
|
|
7
|
+
oraclePrice: number;
|
|
8
|
+
long: boolean;
|
|
9
|
+
open: boolean;
|
|
10
|
+
positionSizeCollateral: number;
|
|
11
|
+
};
|
|
12
|
+
export type MarketPriceResult = {
|
|
13
|
+
marketPrice: number;
|
|
14
|
+
skewImpactP: number;
|
|
15
|
+
oraclePrice: number;
|
|
16
|
+
};
|
|
17
|
+
export type MarketPriceContext = {
|
|
18
|
+
skewDepth: number;
|
|
19
|
+
pairOiToken: {
|
|
20
|
+
oiLongToken: number;
|
|
21
|
+
oiShortToken: number;
|
|
22
|
+
};
|
|
23
|
+
};
|
|
@@ -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;
|
|
@@ -4,8 +4,25 @@ export type GetBorrowingFeeContext = {
|
|
|
4
4
|
currentBlock: number;
|
|
5
5
|
groups: BorrowingFee.Group[];
|
|
6
6
|
pairs: BorrowingFee.Pair[];
|
|
7
|
+
collateralPriceUsd: number;
|
|
7
8
|
};
|
|
8
|
-
|
|
9
|
+
/**
|
|
10
|
+
* @dev Calculates borrowing fees using v1 model (block-based with groups)
|
|
11
|
+
* @dev Still actively used by markets that haven't migrated to v2
|
|
12
|
+
* @dev Uses dynamic collateral OI - converts OI to USD for fee calculations
|
|
13
|
+
* @param posDai Position size in collateral
|
|
14
|
+
* @param pairIndex Trading pair index (required)
|
|
15
|
+
* @param long Whether position is long
|
|
16
|
+
* @param initialAccFees Initial accumulated fees when trade was opened
|
|
17
|
+
* @param context Context with current block, fee data, and collateral price
|
|
18
|
+
* @returns Borrowing fee in collateral tokens
|
|
19
|
+
*/
|
|
20
|
+
export declare const getBorrowingFee: (posDai: number, pairIndex: PairIndex | undefined, long: boolean, initialAccFees: BorrowingFee.InitialAccFees, context: GetBorrowingFeeContext) => number;
|
|
21
|
+
/**
|
|
22
|
+
* @deprecated Use withinMaxGroupOiDynamic from @gains-network/sdk/markets/oi instead
|
|
23
|
+
* @dev This function uses static OI which doesn't reflect current market values
|
|
24
|
+
* @dev The v10 contracts use dynamic OI (beforeV10 + afterV10Token * currentPrice)
|
|
25
|
+
*/
|
|
9
26
|
export declare const withinMaxGroupOi: (pairIndex: PairIndex, long: boolean, positionSizeCollateral: number, context: {
|
|
10
27
|
groups: BorrowingFee.Group[];
|
|
11
28
|
pairs: BorrowingFee.Pair[];
|
|
@@ -19,6 +36,7 @@ export declare const borrowingFeeUtils: {
|
|
|
19
36
|
getPairPendingAccFees: (pairIndex: PairIndex, currentBlock: number, context: {
|
|
20
37
|
pairs: BorrowingFee.Pair[];
|
|
21
38
|
openInterest: OpenInterest;
|
|
39
|
+
collateralPriceUsd: number;
|
|
22
40
|
}) => {
|
|
23
41
|
accFeeLong: number;
|
|
24
42
|
accFeeShort: number;
|
|
@@ -28,9 +46,11 @@ export declare const borrowingFeeUtils: {
|
|
|
28
46
|
getPairPendingAccFee: (pairIndex: PairIndex, currentBlock: number, long: boolean, context: {
|
|
29
47
|
pairs: BorrowingFee.Pair[];
|
|
30
48
|
openInterest: OpenInterest;
|
|
49
|
+
collateralPriceUsd: number;
|
|
31
50
|
}) => number;
|
|
32
51
|
getGroupPendingAccFees: (groupIndex: number, currentBlock: number, context: {
|
|
33
52
|
groups: BorrowingFee.Group[];
|
|
53
|
+
collateralPriceUsd: number;
|
|
34
54
|
}) => {
|
|
35
55
|
accFeeLong: number;
|
|
36
56
|
accFeeShort: number;
|
|
@@ -39,8 +59,9 @@ export declare const borrowingFeeUtils: {
|
|
|
39
59
|
};
|
|
40
60
|
getGroupPendingAccFee: (groupIndex: number, currentBlock: number, long: boolean, context: {
|
|
41
61
|
groups: BorrowingFee.Group[];
|
|
62
|
+
collateralPriceUsd: number;
|
|
42
63
|
}) => number;
|
|
43
|
-
getPendingAccFees: (accFeeLong: number, accFeeShort: number, oiLong: number, oiShort: number, feePerBlock: number, currentBlock: number, accLastUpdatedBlock: number, maxOi: number, feeExponent: number, feeCaps?: BorrowingFee.BorrowingFeePerBlockCap) => {
|
|
64
|
+
getPendingAccFees: (accFeeLong: number, accFeeShort: number, oiLong: number, oiShort: number, feePerBlock: number, currentBlock: number, accLastUpdatedBlock: number, maxOi: number, feeExponent: number, feeCaps?: BorrowingFee.BorrowingFeePerBlockCap, collateralPriceUsd?: number) => {
|
|
44
65
|
accFeeLong: number;
|
|
45
66
|
accFeeShort: number;
|
|
46
67
|
deltaLong: number;
|
|
@@ -56,3 +77,4 @@ export declare const borrowingFeeUtils: {
|
|
|
56
77
|
};
|
|
57
78
|
export * as BorrowingFee from "./types";
|
|
58
79
|
export * from "./converter";
|
|
80
|
+
export * from "./builder";
|
|
@@ -27,11 +27,25 @@ var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
|
27
27
|
};
|
|
28
28
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
29
|
exports.BorrowingFee = exports.borrowingFeeUtils = exports.withinMaxGroupOi = exports.getBorrowingFee = void 0;
|
|
30
|
+
/**
|
|
31
|
+
* @dev Calculates borrowing fees using v1 model (block-based with groups)
|
|
32
|
+
* @dev Still actively used by markets that haven't migrated to v2
|
|
33
|
+
* @dev Uses dynamic collateral OI - converts OI to USD for fee calculations
|
|
34
|
+
* @param posDai Position size in collateral
|
|
35
|
+
* @param pairIndex Trading pair index (required)
|
|
36
|
+
* @param long Whether position is long
|
|
37
|
+
* @param initialAccFees Initial accumulated fees when trade was opened
|
|
38
|
+
* @param context Context with current block, fee data, and collateral price
|
|
39
|
+
* @returns Borrowing fee in collateral tokens
|
|
40
|
+
*/
|
|
30
41
|
const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
31
|
-
if (
|
|
42
|
+
if (pairIndex === undefined) {
|
|
43
|
+
throw new Error("pairIndex is required for borrowing fee calculations");
|
|
44
|
+
}
|
|
45
|
+
const { pairs, groups } = context;
|
|
46
|
+
if (!groups || !pairs || !pairs[pairIndex]) {
|
|
32
47
|
return 0;
|
|
33
48
|
}
|
|
34
|
-
const { pairs } = context;
|
|
35
49
|
const pairGroups = pairs[pairIndex].groups;
|
|
36
50
|
const firstPairGroup = (pairGroups === null || pairGroups === void 0 ? void 0 : pairGroups.length) > 0 ? pairGroups[0] : undefined;
|
|
37
51
|
let fee = 0;
|
|
@@ -42,13 +56,19 @@ const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
|
42
56
|
? getPairPendingAccFee(pairIndex, context.currentBlock, long, {
|
|
43
57
|
pairs,
|
|
44
58
|
openInterest,
|
|
59
|
+
collateralPriceUsd: context.collateralPriceUsd,
|
|
45
60
|
})
|
|
46
61
|
: long
|
|
47
62
|
? firstPairGroup.pairAccFeeLong
|
|
48
63
|
: firstPairGroup.pairAccFeeShort) - initialAccFees.accPairFee;
|
|
49
64
|
}
|
|
50
65
|
for (let i = pairGroups.length; i > 0; i--) {
|
|
51
|
-
const { deltaGroup, deltaPair, beforeTradeOpen } = getPairGroupAccFeesDeltas(i - 1, pairGroups, initialAccFees, pairIndex, long,
|
|
66
|
+
const { deltaGroup, deltaPair, beforeTradeOpen } = getPairGroupAccFeesDeltas(i - 1, pairGroups, initialAccFees, pairIndex, long, {
|
|
67
|
+
currentBlock: context.currentBlock,
|
|
68
|
+
groups,
|
|
69
|
+
pairs,
|
|
70
|
+
collateralPriceUsd: context.collateralPriceUsd,
|
|
71
|
+
});
|
|
52
72
|
fee += Math.max(deltaGroup, deltaPair);
|
|
53
73
|
if (beforeTradeOpen) {
|
|
54
74
|
break;
|
|
@@ -57,6 +77,11 @@ const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
|
57
77
|
return (posDai * fee) / 100;
|
|
58
78
|
};
|
|
59
79
|
exports.getBorrowingFee = getBorrowingFee;
|
|
80
|
+
/**
|
|
81
|
+
* @deprecated Use withinMaxGroupOiDynamic from @gains-network/sdk/markets/oi instead
|
|
82
|
+
* @dev This function uses static OI which doesn't reflect current market values
|
|
83
|
+
* @dev The v10 contracts use dynamic OI (beforeV10 + afterV10Token * currentPrice)
|
|
84
|
+
*/
|
|
60
85
|
const withinMaxGroupOi = (pairIndex, long, positionSizeCollateral, context) => {
|
|
61
86
|
const { groups, pairs } = context;
|
|
62
87
|
if (!groups || !pairs) {
|
|
@@ -75,18 +100,19 @@ const getPairGroupIndex = (pairIndex, context) => {
|
|
|
75
100
|
return pairGroups.length == 0 ? 0 : pairGroups[0].groupIndex;
|
|
76
101
|
};
|
|
77
102
|
const getPairPendingAccFees = (pairIndex, currentBlock, context) => {
|
|
78
|
-
const { pairs, openInterest: { long, short }, } = context;
|
|
103
|
+
const { pairs, openInterest: { long, short }, collateralPriceUsd, } = context;
|
|
79
104
|
const pair = pairs[pairIndex];
|
|
80
|
-
return getPendingAccFees(pair.accFeeLong, pair.accFeeShort, long, short, pair.feePerBlock, currentBlock, pair.accLastUpdatedBlock, pair.oi.max, pair.feeExponent, pair.feePerBlockCap);
|
|
105
|
+
return getPendingAccFees(pair.accFeeLong, pair.accFeeShort, long, short, pair.feePerBlock, currentBlock, pair.accLastUpdatedBlock, pair.oi.max, pair.feeExponent, pair.feePerBlockCap, collateralPriceUsd);
|
|
81
106
|
};
|
|
82
107
|
const getPairPendingAccFee = (pairIndex, currentBlock, long, context) => {
|
|
83
108
|
const { accFeeLong, accFeeShort } = getPairPendingAccFees(pairIndex, currentBlock, context);
|
|
84
109
|
return long ? accFeeLong : accFeeShort;
|
|
85
110
|
};
|
|
86
111
|
const getGroupPendingAccFees = (groupIndex, currentBlock, context) => {
|
|
87
|
-
const { groups } = context;
|
|
112
|
+
const { groups, collateralPriceUsd } = context;
|
|
88
113
|
const group = groups[groupIndex];
|
|
89
|
-
return getPendingAccFees(group.accFeeLong, group.accFeeShort, group.oi.long, group.oi.short, group.feePerBlock, currentBlock, group.accLastUpdatedBlock, group.oi.max, group.feeExponent
|
|
114
|
+
return getPendingAccFees(group.accFeeLong, group.accFeeShort, group.oi.long, group.oi.short, group.feePerBlock, currentBlock, group.accLastUpdatedBlock, group.oi.max, group.feeExponent, undefined, // no fee caps for groups
|
|
115
|
+
collateralPriceUsd);
|
|
90
116
|
};
|
|
91
117
|
const getGroupPendingAccFee = (groupIndex, currentBlock, long, context) => {
|
|
92
118
|
const { accFeeLong, accFeeShort } = getGroupPendingAccFees(groupIndex, currentBlock, context);
|
|
@@ -97,14 +123,16 @@ const getPairGroupAccFeesDeltas = (i, pairGroups, initialFees, pairIndex, long,
|
|
|
97
123
|
const beforeTradeOpen = group.block < initialFees.block;
|
|
98
124
|
let deltaGroup, deltaPair;
|
|
99
125
|
if (i == pairGroups.length - 1) {
|
|
100
|
-
const { currentBlock, groups, pairs } = context;
|
|
126
|
+
const { currentBlock, groups, pairs, collateralPriceUsd } = context;
|
|
101
127
|
const openInterest = pairs[pairIndex].oi;
|
|
102
128
|
deltaGroup = getGroupPendingAccFee(group.groupIndex, currentBlock, long, {
|
|
103
129
|
groups,
|
|
130
|
+
collateralPriceUsd,
|
|
104
131
|
});
|
|
105
132
|
deltaPair = getPairPendingAccFee(pairIndex, currentBlock, long, {
|
|
106
133
|
pairs,
|
|
107
134
|
openInterest,
|
|
135
|
+
collateralPriceUsd,
|
|
108
136
|
});
|
|
109
137
|
}
|
|
110
138
|
else {
|
|
@@ -127,8 +155,8 @@ const getPairGroupAccFeesDeltas = (i, pairGroups, initialFees, pairIndex, long,
|
|
|
127
155
|
}
|
|
128
156
|
return { deltaGroup, deltaPair, beforeTradeOpen };
|
|
129
157
|
};
|
|
130
|
-
const getPendingAccFees = (accFeeLong, accFeeShort, oiLong, oiShort, feePerBlock, currentBlock, accLastUpdatedBlock, maxOi, feeExponent, feeCaps // as percentage: eg minP: 0.1 = 10%, maxP: 0.5 = 50%
|
|
131
|
-
) => {
|
|
158
|
+
const getPendingAccFees = (accFeeLong, accFeeShort, oiLong, oiShort, feePerBlock, currentBlock, accLastUpdatedBlock, maxOi, feeExponent, feeCaps, // as percentage: eg minP: 0.1 = 10%, maxP: 0.5 = 50%
|
|
159
|
+
collateralPriceUsd) => {
|
|
132
160
|
const moreShorts = oiLong < oiShort;
|
|
133
161
|
const blockDistance = currentBlock > accLastUpdatedBlock ? currentBlock - accLastUpdatedBlock : 0;
|
|
134
162
|
// If block distance is zero nothing changes
|
|
@@ -140,19 +168,25 @@ const getPendingAccFees = (accFeeLong, accFeeShort, oiLong, oiShort, feePerBlock
|
|
|
140
168
|
deltaShort: 0,
|
|
141
169
|
};
|
|
142
170
|
}
|
|
143
|
-
|
|
144
|
-
|
|
171
|
+
// Convert OI to USD if collateral price is provided (dynamic collateral OI)
|
|
172
|
+
const oiLongUsd = collateralPriceUsd ? oiLong * collateralPriceUsd : oiLong;
|
|
173
|
+
const oiShortUsd = collateralPriceUsd
|
|
174
|
+
? oiShort * collateralPriceUsd
|
|
175
|
+
: oiShort;
|
|
176
|
+
const maxOiUsd = collateralPriceUsd ? maxOi * collateralPriceUsd : maxOi;
|
|
177
|
+
const netOi = Math.abs(oiLongUsd - oiShortUsd);
|
|
178
|
+
// Calculate minimum and maximum effective oi (using USD values if available)
|
|
145
179
|
const { minP, maxP } = getFeePerBlockCaps(feeCaps);
|
|
146
|
-
const minNetOi =
|
|
147
|
-
const maxNetOi =
|
|
180
|
+
const minNetOi = maxOiUsd * minP;
|
|
181
|
+
const maxNetOi = maxOiUsd * maxP;
|
|
148
182
|
// Calculate the minimum acc fee delta (applies to both sides)
|
|
149
183
|
const minDelta = minNetOi > 0
|
|
150
|
-
? getPendingAccFeesDelta(blockDistance, feePerBlock, netOi,
|
|
184
|
+
? getPendingAccFeesDelta(blockDistance, feePerBlock, netOi, maxOiUsd, feeExponent)
|
|
151
185
|
: 0;
|
|
152
186
|
// Calculate the actual acc fee (using capped oi of 100% or less)
|
|
153
187
|
const delta = netOi > minNetOi
|
|
154
188
|
? getPendingAccFeesDelta(blockDistance, feePerBlock, Math.min(netOi, maxNetOi), // if netOi > cap, use cap
|
|
155
|
-
|
|
189
|
+
maxOiUsd, feeExponent)
|
|
156
190
|
: minDelta;
|
|
157
191
|
const [deltaLong, deltaShort] = moreShorts
|
|
158
192
|
? [minDelta, delta]
|
|
@@ -207,3 +241,4 @@ exports.borrowingFeeUtils = {
|
|
|
207
241
|
};
|
|
208
242
|
exports.BorrowingFee = __importStar(require("./types"));
|
|
209
243
|
__exportStar(require("./converter"), exports);
|
|
244
|
+
__exportStar(require("./builder"), exports);
|
|
@@ -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;
|
|
@@ -0,0 +1,75 @@
|
|
|
1
|
+
import type { IFundingFees } from "../../../contracts/types/generated/GNSMultiCollatDiamond";
|
|
2
|
+
import { BorrowingFeeV2 } from ".";
|
|
3
|
+
/**
|
|
4
|
+
* @dev Converts contract BorrowingFeeParams to SDK type
|
|
5
|
+
* @param contractParams Contract borrowing fee params from IFundingFees.BorrowingFeeParams
|
|
6
|
+
* @returns SDK BorrowingFeeParams
|
|
7
|
+
*/
|
|
8
|
+
export declare const convertBorrowingFeeParams: (contractParams: IFundingFees.BorrowingFeeParamsStructOutput) => BorrowingFeeV2.BorrowingFeeParams;
|
|
9
|
+
/**
|
|
10
|
+
* @dev Converts array of contract BorrowingFeeParams to SDK types
|
|
11
|
+
* @param contractParamsArray Array of contract borrowing fee params
|
|
12
|
+
* @returns Array of SDK BorrowingFeeParams
|
|
13
|
+
*/
|
|
14
|
+
export declare const convertBorrowingFeeParamsArray: (contractParamsArray: IFundingFees.BorrowingFeeParamsStructOutput[]) => BorrowingFeeV2.BorrowingFeeParams[];
|
|
15
|
+
/**
|
|
16
|
+
* @dev Converts contract PairBorrowingFeeData to SDK type
|
|
17
|
+
* @param contractData Contract pair borrowing fee data from IFundingFees.PairBorrowingFeeData
|
|
18
|
+
* @returns SDK PairBorrowingFeeData
|
|
19
|
+
*/
|
|
20
|
+
export declare const convertPairBorrowingFeeData: (contractData: IFundingFees.PairBorrowingFeeDataStructOutput) => BorrowingFeeV2.PairBorrowingFeeData;
|
|
21
|
+
/**
|
|
22
|
+
* @dev Converts array of contract PairBorrowingFeeData to SDK types
|
|
23
|
+
* @param contractDataArray Array of contract pair borrowing fee data
|
|
24
|
+
* @returns Array of SDK PairBorrowingFeeData
|
|
25
|
+
*/
|
|
26
|
+
export declare const convertPairBorrowingFeeDataArray: (contractDataArray: IFundingFees.PairBorrowingFeeDataStructOutput[]) => BorrowingFeeV2.PairBorrowingFeeData[];
|
|
27
|
+
/**
|
|
28
|
+
* @dev Converts contract TradeFeesData to SDK TradeInitialAccFees
|
|
29
|
+
* @param contractTradeData Contract trade fees data from IFundingFees.TradeFeesData
|
|
30
|
+
* @returns SDK TradeInitialAccFees
|
|
31
|
+
*/
|
|
32
|
+
export declare const convertTradeInitialAccFees: (contractTradeData: IFundingFees.TradeFeesDataStructOutput) => BorrowingFeeV2.TradeInitialAccFees;
|
|
33
|
+
/**
|
|
34
|
+
* @dev Converts array of contract TradeFeesData to SDK TradeInitialAccFees
|
|
35
|
+
* @param contractTradeDataArray Array of contract trade fees data
|
|
36
|
+
* @returns Array of SDK TradeInitialAccFees
|
|
37
|
+
*/
|
|
38
|
+
export declare const convertTradeInitialAccFeesArray: (contractTradeDataArray: IFundingFees.TradeFeesDataStructOutput[]) => BorrowingFeeV2.TradeInitialAccFees[];
|
|
39
|
+
/**
|
|
40
|
+
* @dev Creates a context object from contract data arrays
|
|
41
|
+
* @param pairIndices Array of pair indices
|
|
42
|
+
* @param borrowingParams Array of borrowing fee params from contract
|
|
43
|
+
* @param borrowingData Array of pair borrowing fee data from contract
|
|
44
|
+
* @param currentTimestamp Optional current timestamp
|
|
45
|
+
* @returns Complete SDK context for borrowing v2 calculations (collateral-scoped)
|
|
46
|
+
*/
|
|
47
|
+
export declare const createBorrowingV2Context: (pairIndices: number[], borrowingParams: IFundingFees.BorrowingFeeParamsStructOutput[], borrowingData: IFundingFees.PairBorrowingFeeDataStructOutput[], currentTimestamp?: number) => BorrowingFeeV2.GetBorrowingFeeV2Context;
|
|
48
|
+
/**
|
|
49
|
+
* @dev Helper function to validate borrowing rate per second
|
|
50
|
+
* @param borrowingRatePerSecondP Borrowing rate per second (normalized float)
|
|
51
|
+
* @returns True if rate is within valid bounds
|
|
52
|
+
*/
|
|
53
|
+
export declare const isValidBorrowingRate: (borrowingRatePerSecondP: number) => boolean;
|
|
54
|
+
/**
|
|
55
|
+
* @dev Helper function to convert borrowing rate to APR percentage
|
|
56
|
+
* @param borrowingRatePerSecondP Borrowing rate per second (normalized float)
|
|
57
|
+
* @returns APR as percentage (e.g., 10.5 for 10.5% APR)
|
|
58
|
+
*/
|
|
59
|
+
export declare const borrowingRateToAPR: (borrowingRatePerSecondP: number) => number;
|
|
60
|
+
/**
|
|
61
|
+
* @dev Helper function to convert APR percentage to borrowing rate per second
|
|
62
|
+
* @param aprPercentage APR as percentage (e.g., 10.5 for 10.5% APR)
|
|
63
|
+
* @returns Borrowing rate per second (normalized float)
|
|
64
|
+
*/
|
|
65
|
+
export declare const aprToBorrowingRate: (aprPercentage: number) => number;
|
|
66
|
+
/**
|
|
67
|
+
* @dev Creates a collateral-scoped context from frontend data structure
|
|
68
|
+
* @param collateralBorrowingData Data structure from frontend (params and data arrays)
|
|
69
|
+
* @param currentTimestamp Optional current timestamp
|
|
70
|
+
* @returns Collateral-scoped borrowing fee v2 context
|
|
71
|
+
*/
|
|
72
|
+
export declare const createCollateralScopedBorrowingContext: (collateralBorrowingData: {
|
|
73
|
+
params: BorrowingFeeV2.BorrowingFeeParams[];
|
|
74
|
+
data: BorrowingFeeV2.PairBorrowingFeeData[];
|
|
75
|
+
}, currentTimestamp?: number) => BorrowingFeeV2.GetBorrowingFeeV2Context;
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.createCollateralScopedBorrowingContext = exports.aprToBorrowingRate = exports.borrowingRateToAPR = exports.isValidBorrowingRate = exports.createBorrowingV2Context = exports.convertTradeInitialAccFeesArray = exports.convertTradeInitialAccFees = exports.convertPairBorrowingFeeDataArray = exports.convertPairBorrowingFeeData = exports.convertBorrowingFeeParamsArray = exports.convertBorrowingFeeParams = void 0;
|
|
4
|
+
const index_1 = require("./index");
|
|
5
|
+
/**
|
|
6
|
+
* @dev Converts contract BorrowingFeeParams to SDK type
|
|
7
|
+
* @param contractParams Contract borrowing fee params from IFundingFees.BorrowingFeeParams
|
|
8
|
+
* @returns SDK BorrowingFeeParams
|
|
9
|
+
*/
|
|
10
|
+
const convertBorrowingFeeParams = (contractParams) => ({
|
|
11
|
+
borrowingRatePerSecondP: contractParams.borrowingRatePerSecondP /
|
|
12
|
+
index_1.BORROWING_V2_PRECISION.RATE_PER_SECOND,
|
|
13
|
+
});
|
|
14
|
+
exports.convertBorrowingFeeParams = convertBorrowingFeeParams;
|
|
15
|
+
/**
|
|
16
|
+
* @dev Converts array of contract BorrowingFeeParams to SDK types
|
|
17
|
+
* @param contractParamsArray Array of contract borrowing fee params
|
|
18
|
+
* @returns Array of SDK BorrowingFeeParams
|
|
19
|
+
*/
|
|
20
|
+
const convertBorrowingFeeParamsArray = (contractParamsArray) => contractParamsArray.map(params => (0, exports.convertBorrowingFeeParams)(params));
|
|
21
|
+
exports.convertBorrowingFeeParamsArray = convertBorrowingFeeParamsArray;
|
|
22
|
+
/**
|
|
23
|
+
* @dev Converts contract PairBorrowingFeeData to SDK type
|
|
24
|
+
* @param contractData Contract pair borrowing fee data from IFundingFees.PairBorrowingFeeData
|
|
25
|
+
* @returns SDK PairBorrowingFeeData
|
|
26
|
+
*/
|
|
27
|
+
const convertPairBorrowingFeeData = (contractData) => ({
|
|
28
|
+
accBorrowingFeeP: parseFloat(contractData.accBorrowingFeeP.toString()) /
|
|
29
|
+
index_1.BORROWING_V2_PRECISION.ACC_FEE,
|
|
30
|
+
lastBorrowingUpdateTs: contractData.lastBorrowingUpdateTs,
|
|
31
|
+
});
|
|
32
|
+
exports.convertPairBorrowingFeeData = convertPairBorrowingFeeData;
|
|
33
|
+
/**
|
|
34
|
+
* @dev Converts array of contract PairBorrowingFeeData to SDK types
|
|
35
|
+
* @param contractDataArray Array of contract pair borrowing fee data
|
|
36
|
+
* @returns Array of SDK PairBorrowingFeeData
|
|
37
|
+
*/
|
|
38
|
+
const convertPairBorrowingFeeDataArray = (contractDataArray) => contractDataArray.map(data => (0, exports.convertPairBorrowingFeeData)(data));
|
|
39
|
+
exports.convertPairBorrowingFeeDataArray = convertPairBorrowingFeeDataArray;
|
|
40
|
+
/**
|
|
41
|
+
* @dev Converts contract TradeFeesData to SDK TradeInitialAccFees
|
|
42
|
+
* @param contractTradeData Contract trade fees data from IFundingFees.TradeFeesData
|
|
43
|
+
* @returns SDK TradeInitialAccFees
|
|
44
|
+
*/
|
|
45
|
+
const convertTradeInitialAccFees = (contractTradeData) => ({
|
|
46
|
+
initialAccBorrowingFeeP: parseFloat(contractTradeData.initialAccBorrowingFeeP.toString()) /
|
|
47
|
+
index_1.BORROWING_V2_PRECISION.ACC_FEE,
|
|
48
|
+
});
|
|
49
|
+
exports.convertTradeInitialAccFees = convertTradeInitialAccFees;
|
|
50
|
+
/**
|
|
51
|
+
* @dev Converts array of contract TradeFeesData to SDK TradeInitialAccFees
|
|
52
|
+
* @param contractTradeDataArray Array of contract trade fees data
|
|
53
|
+
* @returns Array of SDK TradeInitialAccFees
|
|
54
|
+
*/
|
|
55
|
+
const convertTradeInitialAccFeesArray = (contractTradeDataArray) => contractTradeDataArray.map(data => (0, exports.convertTradeInitialAccFees)(data));
|
|
56
|
+
exports.convertTradeInitialAccFeesArray = convertTradeInitialAccFeesArray;
|
|
57
|
+
/**
|
|
58
|
+
* @dev Creates a context object from contract data arrays
|
|
59
|
+
* @param pairIndices Array of pair indices
|
|
60
|
+
* @param borrowingParams Array of borrowing fee params from contract
|
|
61
|
+
* @param borrowingData Array of pair borrowing fee data from contract
|
|
62
|
+
* @param currentTimestamp Optional current timestamp
|
|
63
|
+
* @returns Complete SDK context for borrowing v2 calculations (collateral-scoped)
|
|
64
|
+
*/
|
|
65
|
+
const createBorrowingV2Context = (pairIndices, borrowingParams, borrowingData, currentTimestamp) => {
|
|
66
|
+
const context = {
|
|
67
|
+
currentTimestamp,
|
|
68
|
+
borrowingParams: {},
|
|
69
|
+
borrowingData: {},
|
|
70
|
+
};
|
|
71
|
+
// Build objects indexed by pairIndex
|
|
72
|
+
for (let i = 0; i < pairIndices.length; i++) {
|
|
73
|
+
const pairIndex = pairIndices[i];
|
|
74
|
+
// Store converted data
|
|
75
|
+
context.borrowingParams[pairIndex] = (0, exports.convertBorrowingFeeParams)(borrowingParams[i]);
|
|
76
|
+
context.borrowingData[pairIndex] = (0, exports.convertPairBorrowingFeeData)(borrowingData[i]);
|
|
77
|
+
}
|
|
78
|
+
return context;
|
|
79
|
+
};
|
|
80
|
+
exports.createBorrowingV2Context = createBorrowingV2Context;
|
|
81
|
+
/**
|
|
82
|
+
* @dev Helper function to validate borrowing rate per second
|
|
83
|
+
* @param borrowingRatePerSecondP Borrowing rate per second (normalized float)
|
|
84
|
+
* @returns True if rate is within valid bounds
|
|
85
|
+
*/
|
|
86
|
+
const isValidBorrowingRate = (borrowingRatePerSecondP) => {
|
|
87
|
+
return (borrowingRatePerSecondP >= 0 &&
|
|
88
|
+
borrowingRatePerSecondP <= 317097 / index_1.BORROWING_V2_PRECISION.RATE_PER_SECOND); // Max 1,000% APR
|
|
89
|
+
};
|
|
90
|
+
exports.isValidBorrowingRate = isValidBorrowingRate;
|
|
91
|
+
/**
|
|
92
|
+
* @dev Helper function to convert borrowing rate to APR percentage
|
|
93
|
+
* @param borrowingRatePerSecondP Borrowing rate per second (normalized float)
|
|
94
|
+
* @returns APR as percentage (e.g., 10.5 for 10.5% APR)
|
|
95
|
+
*/
|
|
96
|
+
const borrowingRateToAPR = (borrowingRatePerSecondP) => {
|
|
97
|
+
const SECONDS_PER_YEAR = 365 * 24 * 60 * 60; // 31,536,000
|
|
98
|
+
return borrowingRatePerSecondP * SECONDS_PER_YEAR;
|
|
99
|
+
};
|
|
100
|
+
exports.borrowingRateToAPR = borrowingRateToAPR;
|
|
101
|
+
/**
|
|
102
|
+
* @dev Helper function to convert APR percentage to borrowing rate per second
|
|
103
|
+
* @param aprPercentage APR as percentage (e.g., 10.5 for 10.5% APR)
|
|
104
|
+
* @returns Borrowing rate per second (normalized float)
|
|
105
|
+
*/
|
|
106
|
+
const aprToBorrowingRate = (aprPercentage) => {
|
|
107
|
+
const SECONDS_PER_YEAR = 365 * 24 * 60 * 60; // 31,536,000
|
|
108
|
+
return aprPercentage / SECONDS_PER_YEAR;
|
|
109
|
+
};
|
|
110
|
+
exports.aprToBorrowingRate = aprToBorrowingRate;
|
|
111
|
+
/**
|
|
112
|
+
* @dev Creates a collateral-scoped context from frontend data structure
|
|
113
|
+
* @param collateralBorrowingData Data structure from frontend (params and data arrays)
|
|
114
|
+
* @param currentTimestamp Optional current timestamp
|
|
115
|
+
* @returns Collateral-scoped borrowing fee v2 context
|
|
116
|
+
*/
|
|
117
|
+
const createCollateralScopedBorrowingContext = (collateralBorrowingData, currentTimestamp) => {
|
|
118
|
+
const context = {
|
|
119
|
+
currentTimestamp: currentTimestamp !== null && currentTimestamp !== void 0 ? currentTimestamp : Math.floor(Date.now() / 1000),
|
|
120
|
+
borrowingParams: {},
|
|
121
|
+
borrowingData: {},
|
|
122
|
+
};
|
|
123
|
+
// Map arrays to objects indexed by array position (pairIndex)
|
|
124
|
+
collateralBorrowingData.params.forEach((param, index) => {
|
|
125
|
+
context.borrowingParams[index] = param;
|
|
126
|
+
});
|
|
127
|
+
collateralBorrowingData.data.forEach((data, index) => {
|
|
128
|
+
context.borrowingData[index] = data;
|
|
129
|
+
});
|
|
130
|
+
return context;
|
|
131
|
+
};
|
|
132
|
+
exports.createCollateralScopedBorrowingContext = createCollateralScopedBorrowingContext;
|