@gainsnetwork/sdk 1.0.0-rc3 → 1.0.0-rc5

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.
@@ -28,6 +28,7 @@ const buildBorrowingV1Context = (globalTradingVariables, collateralIndex, curren
28
28
  pairs,
29
29
  groups,
30
30
  collateralPriceUsd: ((_a = collateral.prices) === null || _a === void 0 ? void 0 : _a.collateralPriceUsd) || 1,
31
+ pairOis: collateral.pairOis,
31
32
  };
32
33
  };
33
34
  exports.buildBorrowingV1Context = buildBorrowingV1Context;
@@ -1,3 +1,4 @@
1
+ import { UnifiedPairOi } from "../../..";
1
2
  import { OpenInterest, PairIndex } from "../../types";
2
3
  import * as BorrowingFee from "./types";
3
4
  export type GetBorrowingFeeContext = {
@@ -5,6 +6,7 @@ export type GetBorrowingFeeContext = {
5
6
  groups: BorrowingFee.Group[];
6
7
  pairs: BorrowingFee.Pair[];
7
8
  collateralPriceUsd: number;
9
+ pairOis: UnifiedPairOi[];
8
10
  };
9
11
  /**
10
12
  * @dev Calculates borrowing fees using v1 model (block-based with groups)
@@ -17,7 +19,7 @@ export type GetBorrowingFeeContext = {
17
19
  * @param context Context with current block, fee data, and collateral price
18
20
  * @returns Borrowing fee in collateral tokens
19
21
  */
20
- export declare const getBorrowingFee: (posDai: number, pairIndex: PairIndex | undefined, long: boolean, initialAccFees: BorrowingFee.InitialAccFees, context: GetBorrowingFeeContext) => number;
22
+ export declare const getBorrowingFee: (posDai: number, pairIndex: PairIndex | undefined, long: boolean, initialAccFees: BorrowingFee.InitialAccFees, currentPairPrice: number, context: GetBorrowingFeeContext) => number;
21
23
  /**
22
24
  * @dev This function uses static OI which doesn't reflect current market values
23
25
  * @dev The v10 contracts use dynamic OI (beforeV10 + afterV10Token * currentPrice)
@@ -27,7 +29,7 @@ export declare const withinMaxGroupOi: (pairIndex: PairIndex, long: boolean, pos
27
29
  pairs: BorrowingFee.Pair[];
28
30
  }) => boolean;
29
31
  export declare const borrowingFeeUtils: {
30
- getPairGroupAccFeesDeltas: (i: number, pairGroups: BorrowingFee.PairGroup[], initialFees: BorrowingFee.InitialAccFees, pairIndex: PairIndex, long: boolean, context: GetBorrowingFeeContext) => {
32
+ getPairGroupAccFeesDeltas: (i: number, pairGroups: BorrowingFee.PairGroup[], initialFees: BorrowingFee.InitialAccFees, pairIndex: PairIndex, long: boolean, currentPairPrice: number, context: GetBorrowingFeeContext) => {
31
33
  deltaGroup: number;
32
34
  deltaPair: number;
33
35
  beforeTradeOpen: boolean;
@@ -27,6 +27,7 @@ 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
+ const __1 = require("../../..");
30
31
  /**
31
32
  * @dev Calculates borrowing fees using v1 model (block-based with groups)
32
33
  * @dev Still actively used by markets that haven't migrated to v2
@@ -38,7 +39,7 @@ exports.BorrowingFee = exports.borrowingFeeUtils = exports.withinMaxGroupOi = ex
38
39
  * @param context Context with current block, fee data, and collateral price
39
40
  * @returns Borrowing fee in collateral tokens
40
41
  */
41
- const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
42
+ const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, currentPairPrice, context) => {
42
43
  if (pairIndex === undefined) {
43
44
  throw new Error("pairIndex is required for borrowing fee calculations");
44
45
  }
@@ -50,12 +51,19 @@ const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
50
51
  const firstPairGroup = (pairGroups === null || pairGroups === void 0 ? void 0 : pairGroups.length) > 0 ? pairGroups[0] : undefined;
51
52
  let fee = 0;
52
53
  if (!firstPairGroup || firstPairGroup.block > initialAccFees.block) {
53
- const openInterest = pairs[pairIndex].oi;
54
+ const openInterest = (0, __1.getPairTotalOisDynamicCollateral)(pairIndex, {
55
+ pairOis: context.pairOis,
56
+ currentPairPrice,
57
+ });
54
58
  fee =
55
59
  (!firstPairGroup
56
60
  ? getPairPendingAccFee(pairIndex, context.currentBlock, long, {
57
61
  pairs,
58
- openInterest,
62
+ openInterest: {
63
+ long: openInterest.long,
64
+ short: openInterest.short,
65
+ max: context.pairOis[pairIndex].maxCollateral,
66
+ },
59
67
  collateralPriceUsd: context.collateralPriceUsd,
60
68
  })
61
69
  : long
@@ -63,11 +71,12 @@ const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
63
71
  : firstPairGroup.pairAccFeeShort) - initialAccFees.accPairFee;
64
72
  }
65
73
  for (let i = pairGroups.length; i > 0; i--) {
66
- const { deltaGroup, deltaPair, beforeTradeOpen } = getPairGroupAccFeesDeltas(i - 1, pairGroups, initialAccFees, pairIndex, long, {
74
+ const { deltaGroup, deltaPair, beforeTradeOpen } = getPairGroupAccFeesDeltas(i - 1, pairGroups, initialAccFees, pairIndex, long, currentPairPrice, {
67
75
  currentBlock: context.currentBlock,
68
76
  groups,
69
77
  pairs,
70
78
  collateralPriceUsd: context.collateralPriceUsd,
79
+ pairOis: context.pairOis,
71
80
  });
72
81
  fee += Math.max(deltaGroup, deltaPair);
73
82
  if (beforeTradeOpen) {
@@ -101,7 +110,7 @@ const getPairGroupIndex = (pairIndex, context) => {
101
110
  const getPairPendingAccFees = (pairIndex, currentBlock, context) => {
102
111
  const { pairs, openInterest: { long, short }, collateralPriceUsd, } = context;
103
112
  const pair = pairs[pairIndex];
104
- return getPendingAccFees(pair.accFeeLong, pair.accFeeShort, long, short, pair.feePerBlock, currentBlock, pair.accLastUpdatedBlock, pair.oi.max, pair.feeExponent, pair.feePerBlockCap, collateralPriceUsd);
113
+ return getPendingAccFees(pair.accFeeLong, pair.accFeeShort, long, short, pair.feePerBlock, currentBlock, pair.accLastUpdatedBlock, context.openInterest.max, pair.feeExponent, pair.feePerBlockCap, collateralPriceUsd);
105
114
  };
106
115
  const getPairPendingAccFee = (pairIndex, currentBlock, long, context) => {
107
116
  const { accFeeLong, accFeeShort } = getPairPendingAccFees(pairIndex, currentBlock, context);
@@ -117,20 +126,27 @@ const getGroupPendingAccFee = (groupIndex, currentBlock, long, context) => {
117
126
  const { accFeeLong, accFeeShort } = getGroupPendingAccFees(groupIndex, currentBlock, context);
118
127
  return long ? accFeeLong : accFeeShort;
119
128
  };
120
- const getPairGroupAccFeesDeltas = (i, pairGroups, initialFees, pairIndex, long, context) => {
129
+ const getPairGroupAccFeesDeltas = (i, pairGroups, initialFees, pairIndex, long, currentPairPrice, context) => {
121
130
  const group = pairGroups[i];
122
131
  const beforeTradeOpen = group.block < initialFees.block;
123
132
  let deltaGroup, deltaPair;
124
133
  if (i == pairGroups.length - 1) {
125
134
  const { currentBlock, groups, pairs, collateralPriceUsd } = context;
126
- const openInterest = pairs[pairIndex].oi;
135
+ const openInterest = (0, __1.getPairTotalOisDynamicCollateral)(pairIndex, {
136
+ pairOis: context.pairOis,
137
+ currentPairPrice,
138
+ });
127
139
  deltaGroup = getGroupPendingAccFee(group.groupIndex, currentBlock, long, {
128
140
  groups,
129
141
  collateralPriceUsd,
130
142
  });
131
143
  deltaPair = getPairPendingAccFee(pairIndex, currentBlock, long, {
132
144
  pairs,
133
- openInterest,
145
+ openInterest: {
146
+ long: openInterest.long,
147
+ short: openInterest.short,
148
+ max: context.pairOis[pairIndex].maxCollateral,
149
+ },
134
150
  collateralPriceUsd,
135
151
  });
136
152
  }
@@ -180,7 +196,7 @@ collateralPriceUsd) => {
180
196
  const maxNetOi = maxOiUsd * maxP;
181
197
  // Calculate the minimum acc fee delta (applies to both sides)
182
198
  const minDelta = minNetOi > 0
183
- ? getPendingAccFeesDelta(blockDistance, feePerBlock, netOi, maxOiUsd, feeExponent)
199
+ ? getPendingAccFeesDelta(blockDistance, feePerBlock, minNetOi, maxOiUsd, feeExponent)
184
200
  : 0;
185
201
  // Calculate the actual acc fee (using capped oi of 100% or less)
186
202
  const delta = netOi > minNetOi
@@ -137,7 +137,7 @@ const getTradePendingHoldingFeesCollateral = (trade, tradeInfo, tradeFeesData, c
137
137
  // Calculate v1 borrowing fees (some markets use v1 indefinitely)
138
138
  let borrowingFeeCollateral_old = 0;
139
139
  if (context.borrowingV1 && context.initialAccFees) {
140
- borrowingFeeCollateral_old = (0, borrowing_1.getBorrowingFee)(positionSizeCollateral, trade.pairIndex, trade.long, context.initialAccFees, context.borrowingV1);
140
+ borrowingFeeCollateral_old = (0, borrowing_1.getBorrowingFee)(positionSizeCollateral, trade.pairIndex, trade.long, context.initialAccFees, currentPairPrice, context.borrowingV1);
141
141
  }
142
142
  return {
143
143
  fundingFeeCollateral,
@@ -6,6 +6,7 @@ import { Trade, TradeInfo, LiquidationParams, Fee, GlobalTradeFeeParams, TradeFe
6
6
  import { ComprehensivePnlResult, GetComprehensivePnlContext } from "./types";
7
7
  import { BorrowingFee } from "../fees/borrowing";
8
8
  import { ContractsVersion } from "../../contracts/types";
9
+ import { UnifiedPairOi } from "src/markets";
9
10
  /**
10
11
  * @dev Gets trade realized PnL components from TradeFeesData
11
12
  * @dev Mirrors contract's getTradeRealizedPnlCollateral function
@@ -60,6 +61,7 @@ export type GetPnlContext = {
60
61
  fee: Fee;
61
62
  globalTradeFeeParams: GlobalTradeFeeParams;
62
63
  traderFeeMultiplier?: number;
64
+ pairOis: UnifiedPairOi[];
63
65
  };
64
66
  /**
65
67
  * @dev Legacy PnL calculation function
@@ -206,11 +206,12 @@ const getPnl = (price, trade, _tradeInfo, initialAccFees, liquidationParams, use
206
206
  context.groups &&
207
207
  context.currentBlock !== undefined &&
208
208
  context.collateralPriceUsd !== undefined) {
209
- pnlCollat -= (0, borrowing_1.getBorrowingFee)(posCollat * trade.leverage, trade.pairIndex, trade.long, initialAccFees, {
209
+ pnlCollat -= (0, borrowing_1.getBorrowingFee)(posCollat * trade.leverage, trade.pairIndex, trade.long, initialAccFees, price, {
210
210
  currentBlock: context.currentBlock,
211
211
  groups: context.groups,
212
212
  pairs: context.pairs,
213
213
  collateralPriceUsd: context.collateralPriceUsd,
214
+ pairOis: context.pairOis,
214
215
  });
215
216
  }
216
217
  let pnlPercentage = (pnlCollat / posCollat) * 100;
@@ -56,6 +56,7 @@ const getTradeOpeningPriceImpact = (input, context) => {
56
56
  return {
57
57
  priceAfterImpact,
58
58
  percentProfitP,
59
+ fixedSpreadP: spreadP,
59
60
  cumulVolPriceImpactP,
60
61
  baseSkewPriceImpactP: skewPriceImpactObject.basePriceImpactP,
61
62
  tradeSkewPriceImpactP: skewPriceImpactObject.tradePriceImpactP,
@@ -35,6 +35,7 @@ export type TradeOpeningPriceImpactContext = {
35
35
  export type TradeOpeningPriceImpactResult = {
36
36
  priceAfterImpact: number;
37
37
  percentProfitP: number;
38
+ fixedSpreadP: number;
38
39
  cumulVolPriceImpactP: number;
39
40
  baseSkewPriceImpactP: number;
40
41
  tradeSkewPriceImpactP: number;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@gainsnetwork/sdk",
3
- "version": "1.0.0-rc3",
3
+ "version": "1.0.0-rc5",
4
4
  "description": "Gains Network SDK",
5
5
  "main": "./lib/index.js",
6
6
  "files": [