@gainsnetwork/sdk 0.0.0-v10.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/README.md +11 -0
- package/lib/constants.d.ts +403 -0
- package/lib/constants.js +436 -0
- package/lib/contracts/addresses.d.ts +3 -0
- package/lib/contracts/addresses.js +31 -0
- package/lib/contracts/addresses.json +127 -0
- package/lib/contracts/index.d.ts +14 -0
- package/lib/contracts/index.js +83 -0
- package/lib/contracts/types/generated/GNSMultiCollatDiamond.d.ts +8716 -0
- package/lib/contracts/types/generated/GNSMultiCollatDiamond.js +2 -0
- package/lib/contracts/types/generated/GToken.d.ts +1917 -0
- package/lib/contracts/types/generated/GToken.js +2 -0
- package/lib/contracts/types/generated/GTokenOpenPnlFeed.d.ts +557 -0
- package/lib/contracts/types/generated/GTokenOpenPnlFeed.js +2 -0
- package/lib/contracts/types/generated/common.d.ts +22 -0
- package/lib/contracts/types/generated/common.js +2 -0
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.d.ts +284 -0
- package/lib/contracts/types/generated/factories/GNSMultiCollatDiamond__factory.js +19330 -0
- package/lib/contracts/types/generated/factories/GTokenOpenPnlFeed__factory.d.ts +59 -0
- package/lib/contracts/types/generated/factories/GTokenOpenPnlFeed__factory.js +765 -0
- package/lib/contracts/types/generated/factories/GToken__factory.d.ts +128 -0
- package/lib/contracts/types/generated/factories/GToken__factory.js +2871 -0
- package/lib/contracts/types/generated/factories/index.d.ts +3 -0
- package/lib/contracts/types/generated/factories/index.js +12 -0
- package/lib/contracts/types/generated/index.d.ts +7 -0
- package/lib/contracts/types/generated/index.js +33 -0
- package/lib/contracts/types/index.d.ts +32 -0
- package/lib/contracts/types/index.js +25 -0
- package/lib/contracts/utils/borrowingFees.d.ts +9 -0
- package/lib/contracts/utils/borrowingFees.js +54 -0
- package/lib/contracts/utils/index.d.ts +3 -0
- package/lib/contracts/utils/index.js +19 -0
- package/lib/contracts/utils/openTrades.d.ts +10 -0
- package/lib/contracts/utils/openTrades.js +126 -0
- package/lib/contracts/utils/pairs.d.ts +7 -0
- package/lib/contracts/utils/pairs.js +478 -0
- package/lib/index.d.ts +7 -0
- package/lib/index.js +24 -0
- package/lib/markets/commodities.d.ts +1 -0
- package/lib/markets/commodities.js +33 -0
- package/lib/markets/crypto.d.ts +1 -0
- package/lib/markets/crypto.js +6 -0
- package/lib/markets/forex.d.ts +3 -0
- package/lib/markets/forex.js +45 -0
- package/lib/markets/index.d.ts +5 -0
- package/lib/markets/index.js +21 -0
- package/lib/markets/indices.d.ts +1 -0
- package/lib/markets/indices.js +6 -0
- package/lib/markets/stocks.d.ts +3 -0
- package/lib/markets/stocks.js +58 -0
- package/lib/trade/fees/borrowing/converter.d.ts +17 -0
- package/lib/trade/fees/borrowing/converter.js +42 -0
- package/lib/trade/fees/borrowing/index.d.ts +58 -0
- package/lib/trade/fees/borrowing/index.js +209 -0
- package/lib/trade/fees/borrowing/types.d.ts +36 -0
- package/lib/trade/fees/borrowing/types.js +2 -0
- package/lib/trade/fees/index.d.ts +4 -0
- package/lib/trade/fees/index.js +34 -0
- package/lib/trade/fees/tiers/index.d.ts +12 -0
- package/lib/trade/fees/tiers/index.js +54 -0
- package/lib/trade/fees/tiers/types.d.ts +15 -0
- package/lib/trade/fees/tiers/types.js +8 -0
- package/lib/trade/index.d.ts +7 -0
- package/lib/trade/index.js +23 -0
- package/lib/trade/liquidation.d.ts +12 -0
- package/lib/trade/liquidation.js +55 -0
- package/lib/trade/oiWindows.d.ts +3 -0
- package/lib/trade/oiWindows.js +20 -0
- package/lib/trade/pnl.d.ts +10 -0
- package/lib/trade/pnl.js +33 -0
- package/lib/trade/spread.d.ts +18 -0
- package/lib/trade/spread.js +108 -0
- package/lib/trade/types.d.ts +598 -0
- package/lib/trade/types.js +402 -0
- package/lib/utils/index.d.ts +1 -0
- package/lib/utils/index.js +17 -0
- package/lib/utils/packing.d.ts +2 -0
- package/lib/utils/packing.js +39 -0
- package/lib/vault/index.d.ts +8 -0
- package/lib/vault/index.js +10 -0
- package/package.json +105 -0
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getUSMarketsNow = exports.isStocksOpen = void 0;
|
|
4
|
+
/* eslint-disable @typescript-eslint/no-unsafe-call */
|
|
5
|
+
/* eslint-disable @typescript-eslint/no-unsafe-member-access */
|
|
6
|
+
// @ts-ignore-file
|
|
7
|
+
const luxon_1 = require("luxon");
|
|
8
|
+
const STOCKS_MARKETS_TIME_ZONE_IANA = luxon_1.IANAZone.create("America/New_York");
|
|
9
|
+
const isStocksOpen = (dateToCheck) => {
|
|
10
|
+
const now = luxon_1.DateTime.fromJSDate(dateToCheck).setZone(STOCKS_MARKETS_TIME_ZONE_IANA);
|
|
11
|
+
const weekday = now.weekday;
|
|
12
|
+
const hour = now.hour;
|
|
13
|
+
const dayOfMonth = now.day;
|
|
14
|
+
const month = now.month;
|
|
15
|
+
const minute = now.minute;
|
|
16
|
+
const isClosed =
|
|
17
|
+
// New Year's Day 2026
|
|
18
|
+
(month === 1 && dayOfMonth === 1) ||
|
|
19
|
+
// Martin Luther King, Jr. Day 2026
|
|
20
|
+
(month === 1 && dayOfMonth === 19) ||
|
|
21
|
+
// Presidents' Day 2026
|
|
22
|
+
(month === 2 && dayOfMonth === 16) ||
|
|
23
|
+
// Good Friday 2026
|
|
24
|
+
(month === 4 && dayOfMonth === 3) ||
|
|
25
|
+
// Memorial Day 2025
|
|
26
|
+
(month === 5 && dayOfMonth === 26) ||
|
|
27
|
+
// Juneteenth National Independence Day 2025
|
|
28
|
+
(month === 6 && dayOfMonth === 19) ||
|
|
29
|
+
// Independence Day 2025
|
|
30
|
+
(month === 7 && dayOfMonth === 4) ||
|
|
31
|
+
// Labor Day 2025
|
|
32
|
+
(month === 9 && dayOfMonth === 1) ||
|
|
33
|
+
// Thanksgiving Day 2025
|
|
34
|
+
(month === 11 && dayOfMonth === 27) ||
|
|
35
|
+
// Black Friday 2025 (closes early at 1PM)
|
|
36
|
+
(month === 11 && dayOfMonth === 28 && hour >= 13) ||
|
|
37
|
+
// Christmas Eve 2025 (closes early at 1PM)
|
|
38
|
+
(month === 12 && dayOfMonth === 24 && hour >= 13) ||
|
|
39
|
+
// Christmas Day 2025
|
|
40
|
+
(month === 12 && dayOfMonth === 25) ||
|
|
41
|
+
// Day before Independence Day 2025 (closes early at 1PM)
|
|
42
|
+
(month === 7 && dayOfMonth === 3 && hour >= 13) ||
|
|
43
|
+
// Saturday
|
|
44
|
+
weekday === 6 ||
|
|
45
|
+
// Sunday
|
|
46
|
+
weekday === 7 ||
|
|
47
|
+
// Mo-Fr Daily Opening
|
|
48
|
+
hour < 9 ||
|
|
49
|
+
(hour === 9 && minute < 30) ||
|
|
50
|
+
// Mo-Fr Daily Closing
|
|
51
|
+
hour >= 16;
|
|
52
|
+
return !isClosed;
|
|
53
|
+
};
|
|
54
|
+
exports.isStocksOpen = isStocksOpen;
|
|
55
|
+
const getUSMarketsNow = () => {
|
|
56
|
+
return luxon_1.DateTime.now().setZone(STOCKS_MARKETS_TIME_ZONE_IANA);
|
|
57
|
+
};
|
|
58
|
+
exports.getUSMarketsNow = getUSMarketsNow;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { IBorrowingFees } from "../../../contracts/types/generated/GNSMultiCollatDiamond";
|
|
2
|
+
import { BorrowingFee } from ".";
|
|
3
|
+
export declare const convertPairGroupBorrowingFee: (pairGroup: IBorrowingFees.BorrowingPairGroupStructOutput) => BorrowingFee.PairGroup;
|
|
4
|
+
export declare const convertPairBorrowingFee: (pair: IBorrowingFees.BorrowingDataStructOutput, pairOi: IBorrowingFees.OpenInterestStructOutput, pairGroup: IBorrowingFees.BorrowingPairGroupStructOutput[], feeCap: IBorrowingFees.BorrowingFeePerBlockCapStructOutput) => BorrowingFee.Pair;
|
|
5
|
+
export declare const convertPairBorrowingFees: ([pairs, pairOi, pairGroups, feeCaps]: [
|
|
6
|
+
IBorrowingFees.BorrowingDataStructOutput[],
|
|
7
|
+
IBorrowingFees.OpenInterestStructOutput[],
|
|
8
|
+
IBorrowingFees.BorrowingPairGroupStructOutput[][],
|
|
9
|
+
IBorrowingFees.BorrowingFeePerBlockCapStructOutput[]
|
|
10
|
+
]) => BorrowingFee.Pair[];
|
|
11
|
+
export declare const convertGroupBorrowingFee: (group: IBorrowingFees.BorrowingDataStructOutput, groupOi: IBorrowingFees.OpenInterestStructOutput) => BorrowingFee.Group;
|
|
12
|
+
export declare const convertGroupBorrowingData: (obj: IBorrowingFees.BorrowingDataStructOutput, oi: IBorrowingFees.OpenInterestStructOutput) => BorrowingFee.BorrowingData & BorrowingFee.BorrowingOi;
|
|
13
|
+
export declare const convertGroupBorrowingFees: ([groups, groupOis]: [
|
|
14
|
+
IBorrowingFees.BorrowingDataStructOutput[],
|
|
15
|
+
IBorrowingFees.OpenInterestStructOutput[]
|
|
16
|
+
]) => BorrowingFee.Group[];
|
|
17
|
+
export declare const convertFeePerBlockCap: (feeCap: IBorrowingFees.BorrowingFeePerBlockCapStructOutput) => BorrowingFee.BorrowingFeePerBlockCap;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.convertFeePerBlockCap = exports.convertGroupBorrowingFees = exports.convertGroupBorrowingData = exports.convertGroupBorrowingFee = exports.convertPairBorrowingFees = exports.convertPairBorrowingFee = exports.convertPairGroupBorrowingFee = void 0;
|
|
4
|
+
const borrowingFees_1 = require("../../../contracts/utils/borrowingFees");
|
|
5
|
+
const convertPairGroupBorrowingFee = (pairGroup) => ({
|
|
6
|
+
groupIndex: pairGroup.groupIndex,
|
|
7
|
+
name: (0, borrowingFees_1.getBorrowingGroupName)(pairGroup.groupIndex),
|
|
8
|
+
initialAccFeeLong: parseFloat(pairGroup.initialAccFeeLong.toString()) / 1e10,
|
|
9
|
+
initialAccFeeShort: parseFloat(pairGroup.initialAccFeeShort.toString()) / 1e10,
|
|
10
|
+
prevGroupAccFeeLong: parseFloat(pairGroup.prevGroupAccFeeLong.toString()) / 1e10,
|
|
11
|
+
prevGroupAccFeeShort: parseFloat(pairGroup.prevGroupAccFeeShort.toString()) / 1e10,
|
|
12
|
+
pairAccFeeLong: parseFloat(pairGroup.pairAccFeeLong.toString()) / 1e10,
|
|
13
|
+
pairAccFeeShort: parseFloat(pairGroup.pairAccFeeShort.toString()) / 1e10,
|
|
14
|
+
block: pairGroup.block,
|
|
15
|
+
});
|
|
16
|
+
exports.convertPairGroupBorrowingFee = convertPairGroupBorrowingFee;
|
|
17
|
+
const convertPairBorrowingFee = (pair, pairOi, pairGroup, feeCap) => (Object.assign(Object.assign({}, (0, exports.convertGroupBorrowingData)(pair, pairOi)), { groups: pairGroup.map(value => (0, exports.convertPairGroupBorrowingFee)(value)), feePerBlockCap: (0, exports.convertFeePerBlockCap)(feeCap) }));
|
|
18
|
+
exports.convertPairBorrowingFee = convertPairBorrowingFee;
|
|
19
|
+
const convertPairBorrowingFees = ([pairs, pairOi, pairGroups, feeCaps]) => pairs.map((value, ix) => (0, exports.convertPairBorrowingFee)(value, pairOi[ix], pairGroups[ix], feeCaps[ix]));
|
|
20
|
+
exports.convertPairBorrowingFees = convertPairBorrowingFees;
|
|
21
|
+
const convertGroupBorrowingFee = (group, groupOi) => (0, exports.convertGroupBorrowingData)(group, groupOi);
|
|
22
|
+
exports.convertGroupBorrowingFee = convertGroupBorrowingFee;
|
|
23
|
+
const convertGroupBorrowingData = (obj, oi) => ({
|
|
24
|
+
oi: {
|
|
25
|
+
long: parseFloat(oi.long.toString()) / 1e10,
|
|
26
|
+
short: parseFloat(oi.short.toString()) / 1e10,
|
|
27
|
+
max: parseFloat(oi.max.toString()) / 1e10,
|
|
28
|
+
},
|
|
29
|
+
feePerBlock: obj.feePerBlock / 1e10,
|
|
30
|
+
accFeeLong: parseFloat(obj.accFeeLong.toString()) / 1e10,
|
|
31
|
+
accFeeShort: parseFloat(obj.accFeeShort.toString()) / 1e10,
|
|
32
|
+
accLastUpdatedBlock: obj.accLastUpdatedBlock,
|
|
33
|
+
feeExponent: obj.feeExponent,
|
|
34
|
+
});
|
|
35
|
+
exports.convertGroupBorrowingData = convertGroupBorrowingData;
|
|
36
|
+
const convertGroupBorrowingFees = ([groups, groupOis]) => groups.map((value, ix) => (0, exports.convertGroupBorrowingFee)(value, groupOis[ix]));
|
|
37
|
+
exports.convertGroupBorrowingFees = convertGroupBorrowingFees;
|
|
38
|
+
const convertFeePerBlockCap = (feeCap) => ({
|
|
39
|
+
minP: feeCap.minP ? parseFloat(feeCap.minP.toString()) / 1e3 / 100 : 0,
|
|
40
|
+
maxP: feeCap.maxP ? parseFloat(feeCap.maxP.toString()) / 1e3 / 100 : 1,
|
|
41
|
+
});
|
|
42
|
+
exports.convertFeePerBlockCap = convertFeePerBlockCap;
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { OpenInterest, PairIndex } from "../../types";
|
|
2
|
+
import * as BorrowingFee from "./types";
|
|
3
|
+
export type GetBorrowingFeeContext = {
|
|
4
|
+
currentBlock: number;
|
|
5
|
+
groups: BorrowingFee.Group[];
|
|
6
|
+
pairs: BorrowingFee.Pair[];
|
|
7
|
+
};
|
|
8
|
+
export declare const getBorrowingFee: (posDai: number, pairIndex: PairIndex, long: boolean, initialAccFees: BorrowingFee.InitialAccFees, context: GetBorrowingFeeContext) => number;
|
|
9
|
+
export declare const withinMaxGroupOi: (pairIndex: PairIndex, long: boolean, positionSizeCollateral: number, context: {
|
|
10
|
+
groups: BorrowingFee.Group[];
|
|
11
|
+
pairs: BorrowingFee.Pair[];
|
|
12
|
+
}) => boolean;
|
|
13
|
+
export declare const borrowingFeeUtils: {
|
|
14
|
+
getPairGroupAccFeesDeltas: (i: number, pairGroups: BorrowingFee.PairGroup[], initialFees: BorrowingFee.InitialAccFees, pairIndex: PairIndex, long: boolean, context: GetBorrowingFeeContext) => {
|
|
15
|
+
deltaGroup: number;
|
|
16
|
+
deltaPair: number;
|
|
17
|
+
beforeTradeOpen: boolean;
|
|
18
|
+
};
|
|
19
|
+
getPairPendingAccFees: (pairIndex: PairIndex, currentBlock: number, context: {
|
|
20
|
+
pairs: BorrowingFee.Pair[];
|
|
21
|
+
openInterest: OpenInterest;
|
|
22
|
+
}) => {
|
|
23
|
+
accFeeLong: number;
|
|
24
|
+
accFeeShort: number;
|
|
25
|
+
deltaLong: number;
|
|
26
|
+
deltaShort: number;
|
|
27
|
+
};
|
|
28
|
+
getPairPendingAccFee: (pairIndex: PairIndex, currentBlock: number, long: boolean, context: {
|
|
29
|
+
pairs: BorrowingFee.Pair[];
|
|
30
|
+
openInterest: OpenInterest;
|
|
31
|
+
}) => number;
|
|
32
|
+
getGroupPendingAccFees: (groupIndex: number, currentBlock: number, context: {
|
|
33
|
+
groups: BorrowingFee.Group[];
|
|
34
|
+
}) => {
|
|
35
|
+
accFeeLong: number;
|
|
36
|
+
accFeeShort: number;
|
|
37
|
+
deltaLong: number;
|
|
38
|
+
deltaShort: number;
|
|
39
|
+
};
|
|
40
|
+
getGroupPendingAccFee: (groupIndex: number, currentBlock: number, long: boolean, context: {
|
|
41
|
+
groups: BorrowingFee.Group[];
|
|
42
|
+
}) => number;
|
|
43
|
+
getPendingAccFees: (accFeeLong: number, accFeeShort: number, oiLong: number, oiShort: number, feePerBlock: number, currentBlock: number, accLastUpdatedBlock: number, maxOi: number, feeExponent: number, feeCaps?: BorrowingFee.BorrowingFeePerBlockCap) => {
|
|
44
|
+
accFeeLong: number;
|
|
45
|
+
accFeeShort: number;
|
|
46
|
+
deltaLong: number;
|
|
47
|
+
deltaShort: number;
|
|
48
|
+
};
|
|
49
|
+
getActiveFeePerBlock: (pair: BorrowingFee.Pair, group: BorrowingFee.Group | undefined) => number;
|
|
50
|
+
getBorrowingDataActiveFeePerBlock: (val: BorrowingFee.Pair | BorrowingFee.Group) => number;
|
|
51
|
+
getPairGroupIndex: (pairIndex: PairIndex, context: {
|
|
52
|
+
pairs: BorrowingFee.Pair[];
|
|
53
|
+
}) => number;
|
|
54
|
+
getPendingAccFeesDelta: (blockDistance: number, feePerBlock: number, netOi: number, maxOi: number, feeExponent: number) => number;
|
|
55
|
+
getFeePerBlockCaps: (cap?: BorrowingFee.BorrowingFeePerBlockCap) => BorrowingFee.BorrowingFeePerBlockCap;
|
|
56
|
+
};
|
|
57
|
+
export * as BorrowingFee from "./types";
|
|
58
|
+
export * from "./converter";
|
|
@@ -0,0 +1,209 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
25
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
26
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
27
|
+
};
|
|
28
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
29
|
+
exports.BorrowingFee = exports.borrowingFeeUtils = exports.withinMaxGroupOi = exports.getBorrowingFee = void 0;
|
|
30
|
+
const getBorrowingFee = (posDai, pairIndex, long, initialAccFees, context) => {
|
|
31
|
+
if (!context.groups || !context.pairs || !context.pairs[pairIndex]) {
|
|
32
|
+
return 0;
|
|
33
|
+
}
|
|
34
|
+
const { pairs } = context;
|
|
35
|
+
const pairGroups = pairs[pairIndex].groups;
|
|
36
|
+
const firstPairGroup = (pairGroups === null || pairGroups === void 0 ? void 0 : pairGroups.length) > 0 ? pairGroups[0] : undefined;
|
|
37
|
+
let fee = 0;
|
|
38
|
+
if (!firstPairGroup || firstPairGroup.block > initialAccFees.block) {
|
|
39
|
+
const openInterest = pairs[pairIndex].oi;
|
|
40
|
+
fee =
|
|
41
|
+
(!firstPairGroup
|
|
42
|
+
? getPairPendingAccFee(pairIndex, context.currentBlock, long, {
|
|
43
|
+
pairs,
|
|
44
|
+
openInterest,
|
|
45
|
+
})
|
|
46
|
+
: long
|
|
47
|
+
? firstPairGroup.pairAccFeeLong
|
|
48
|
+
: firstPairGroup.pairAccFeeShort) - initialAccFees.accPairFee;
|
|
49
|
+
}
|
|
50
|
+
for (let i = pairGroups.length; i > 0; i--) {
|
|
51
|
+
const { deltaGroup, deltaPair, beforeTradeOpen } = getPairGroupAccFeesDeltas(i - 1, pairGroups, initialAccFees, pairIndex, long, context);
|
|
52
|
+
fee += Math.max(deltaGroup, deltaPair);
|
|
53
|
+
if (beforeTradeOpen) {
|
|
54
|
+
break;
|
|
55
|
+
}
|
|
56
|
+
}
|
|
57
|
+
return (posDai * fee) / 100;
|
|
58
|
+
};
|
|
59
|
+
exports.getBorrowingFee = getBorrowingFee;
|
|
60
|
+
const withinMaxGroupOi = (pairIndex, long, positionSizeCollateral, context) => {
|
|
61
|
+
const { groups, pairs } = context;
|
|
62
|
+
if (!groups || !pairs) {
|
|
63
|
+
return false;
|
|
64
|
+
}
|
|
65
|
+
const g = groups[getPairGroupIndex(pairIndex, { pairs })].oi;
|
|
66
|
+
return (g.max == 0 || (long ? g.long : g.short) + positionSizeCollateral <= g.max);
|
|
67
|
+
};
|
|
68
|
+
exports.withinMaxGroupOi = withinMaxGroupOi;
|
|
69
|
+
const getPairGroupIndex = (pairIndex, context) => {
|
|
70
|
+
const { pairs } = context;
|
|
71
|
+
if (!pairs[pairIndex]) {
|
|
72
|
+
return 0;
|
|
73
|
+
}
|
|
74
|
+
const pairGroups = pairs[pairIndex].groups;
|
|
75
|
+
return pairGroups.length == 0 ? 0 : pairGroups[0].groupIndex;
|
|
76
|
+
};
|
|
77
|
+
const getPairPendingAccFees = (pairIndex, currentBlock, context) => {
|
|
78
|
+
const { pairs, openInterest: { long, short }, } = context;
|
|
79
|
+
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);
|
|
81
|
+
};
|
|
82
|
+
const getPairPendingAccFee = (pairIndex, currentBlock, long, context) => {
|
|
83
|
+
const { accFeeLong, accFeeShort } = getPairPendingAccFees(pairIndex, currentBlock, context);
|
|
84
|
+
return long ? accFeeLong : accFeeShort;
|
|
85
|
+
};
|
|
86
|
+
const getGroupPendingAccFees = (groupIndex, currentBlock, context) => {
|
|
87
|
+
const { groups } = context;
|
|
88
|
+
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);
|
|
90
|
+
};
|
|
91
|
+
const getGroupPendingAccFee = (groupIndex, currentBlock, long, context) => {
|
|
92
|
+
const { accFeeLong, accFeeShort } = getGroupPendingAccFees(groupIndex, currentBlock, context);
|
|
93
|
+
return long ? accFeeLong : accFeeShort;
|
|
94
|
+
};
|
|
95
|
+
const getPairGroupAccFeesDeltas = (i, pairGroups, initialFees, pairIndex, long, context) => {
|
|
96
|
+
const group = pairGroups[i];
|
|
97
|
+
const beforeTradeOpen = group.block < initialFees.block;
|
|
98
|
+
let deltaGroup, deltaPair;
|
|
99
|
+
if (i == pairGroups.length - 1) {
|
|
100
|
+
const { currentBlock, groups, pairs } = context;
|
|
101
|
+
const openInterest = pairs[pairIndex].oi;
|
|
102
|
+
deltaGroup = getGroupPendingAccFee(group.groupIndex, currentBlock, long, {
|
|
103
|
+
groups,
|
|
104
|
+
});
|
|
105
|
+
deltaPair = getPairPendingAccFee(pairIndex, currentBlock, long, {
|
|
106
|
+
pairs,
|
|
107
|
+
openInterest,
|
|
108
|
+
});
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
const nextGroup = pairGroups[i + 1];
|
|
112
|
+
if (beforeTradeOpen && nextGroup.block <= initialFees.block) {
|
|
113
|
+
return { deltaGroup: 0, deltaPair: 0, beforeTradeOpen };
|
|
114
|
+
}
|
|
115
|
+
deltaGroup = long
|
|
116
|
+
? nextGroup.prevGroupAccFeeLong
|
|
117
|
+
: nextGroup.prevGroupAccFeeShort;
|
|
118
|
+
deltaPair = long ? nextGroup.pairAccFeeLong : nextGroup.pairAccFeeShort;
|
|
119
|
+
}
|
|
120
|
+
if (beforeTradeOpen) {
|
|
121
|
+
deltaGroup -= initialFees.accGroupFee;
|
|
122
|
+
deltaPair -= initialFees.accPairFee;
|
|
123
|
+
}
|
|
124
|
+
else {
|
|
125
|
+
deltaGroup -= long ? group.initialAccFeeLong : group.initialAccFeeShort;
|
|
126
|
+
deltaPair -= long ? group.pairAccFeeLong : group.pairAccFeeShort;
|
|
127
|
+
}
|
|
128
|
+
return { deltaGroup, deltaPair, beforeTradeOpen };
|
|
129
|
+
};
|
|
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
|
+
) => {
|
|
132
|
+
const moreShorts = oiLong < oiShort;
|
|
133
|
+
const blockDistance = currentBlock > accLastUpdatedBlock ? currentBlock - accLastUpdatedBlock : 0;
|
|
134
|
+
// If block distance is zero nothing changes
|
|
135
|
+
if (blockDistance === 0) {
|
|
136
|
+
return {
|
|
137
|
+
accFeeLong,
|
|
138
|
+
accFeeShort,
|
|
139
|
+
deltaLong: 0,
|
|
140
|
+
deltaShort: 0,
|
|
141
|
+
};
|
|
142
|
+
}
|
|
143
|
+
const netOi = Math.abs(oiLong - oiShort);
|
|
144
|
+
// Calculate minimum and maximum effective oi
|
|
145
|
+
const { minP, maxP } = getFeePerBlockCaps(feeCaps);
|
|
146
|
+
const minNetOi = maxOi * minP;
|
|
147
|
+
const maxNetOi = maxOi * maxP;
|
|
148
|
+
// Calculate the minimum acc fee delta (applies to both sides)
|
|
149
|
+
const minDelta = minNetOi > 0
|
|
150
|
+
? getPendingAccFeesDelta(blockDistance, feePerBlock, netOi, maxOi, feeExponent)
|
|
151
|
+
: 0;
|
|
152
|
+
// Calculate the actual acc fee (using capped oi of 100% or less)
|
|
153
|
+
const delta = netOi > minNetOi
|
|
154
|
+
? getPendingAccFeesDelta(blockDistance, feePerBlock, Math.min(netOi, maxNetOi), // if netOi > cap, use cap
|
|
155
|
+
maxOi, feeExponent)
|
|
156
|
+
: minDelta;
|
|
157
|
+
const [deltaLong, deltaShort] = moreShorts
|
|
158
|
+
? [minDelta, delta]
|
|
159
|
+
: [delta, minDelta];
|
|
160
|
+
return {
|
|
161
|
+
accFeeLong: accFeeLong + deltaLong,
|
|
162
|
+
accFeeShort: accFeeShort + deltaShort,
|
|
163
|
+
deltaLong,
|
|
164
|
+
deltaShort,
|
|
165
|
+
};
|
|
166
|
+
};
|
|
167
|
+
const getPendingAccFeesDelta = (blockDistance, feePerBlock, netOi, maxOi, feeExponent) => {
|
|
168
|
+
return maxOi > 0 && feeExponent > 0
|
|
169
|
+
? feePerBlock * blockDistance * Math.pow((netOi / maxOi), feeExponent)
|
|
170
|
+
: 0;
|
|
171
|
+
};
|
|
172
|
+
const getFeePerBlockCaps = (cap) => {
|
|
173
|
+
return {
|
|
174
|
+
minP: (cap === null || cap === void 0 ? void 0 : cap.minP) || 0,
|
|
175
|
+
maxP: (cap === null || cap === void 0 ? void 0 : cap.maxP) && cap.maxP > 0 ? cap.maxP : 1,
|
|
176
|
+
};
|
|
177
|
+
};
|
|
178
|
+
const getBorrowingDataActiveFeePerBlock = (val) => {
|
|
179
|
+
const { long, short, max } = val.oi;
|
|
180
|
+
const { minP, maxP } = getFeePerBlockCaps("feePerBlockCap" in val ? val.feePerBlockCap : undefined);
|
|
181
|
+
// Calculate the effective open interest
|
|
182
|
+
// If minP > 0 then netOi has to be at least minP * maxOi
|
|
183
|
+
// If maxP > 0 then netOi cannot be more than maxP * maxOi
|
|
184
|
+
const effectiveOi = Math.min(Math.max(Math.abs(long - short), max * minP), max * maxP);
|
|
185
|
+
return val.feePerBlock * Math.pow((effectiveOi / max), val.feeExponent);
|
|
186
|
+
};
|
|
187
|
+
const getActiveFeePerBlock = (pair, group) => {
|
|
188
|
+
const pairFeePerBlock = getBorrowingDataActiveFeePerBlock(pair);
|
|
189
|
+
if (!group) {
|
|
190
|
+
return pairFeePerBlock;
|
|
191
|
+
}
|
|
192
|
+
const groupFeePerBlock = getBorrowingDataActiveFeePerBlock(group);
|
|
193
|
+
return Math.max(pairFeePerBlock, groupFeePerBlock);
|
|
194
|
+
};
|
|
195
|
+
exports.borrowingFeeUtils = {
|
|
196
|
+
getPairGroupAccFeesDeltas,
|
|
197
|
+
getPairPendingAccFees,
|
|
198
|
+
getPairPendingAccFee,
|
|
199
|
+
getGroupPendingAccFees,
|
|
200
|
+
getGroupPendingAccFee,
|
|
201
|
+
getPendingAccFees,
|
|
202
|
+
getActiveFeePerBlock,
|
|
203
|
+
getBorrowingDataActiveFeePerBlock,
|
|
204
|
+
getPairGroupIndex,
|
|
205
|
+
getPendingAccFeesDelta,
|
|
206
|
+
getFeePerBlockCaps,
|
|
207
|
+
};
|
|
208
|
+
exports.BorrowingFee = __importStar(require("./types"));
|
|
209
|
+
__exportStar(require("./converter"), exports);
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { OpenInterest } from "../../types";
|
|
2
|
+
export type BorrowingData = {
|
|
3
|
+
feePerBlock: number;
|
|
4
|
+
accFeeLong: number;
|
|
5
|
+
accFeeShort: number;
|
|
6
|
+
accLastUpdatedBlock: number;
|
|
7
|
+
feeExponent: number;
|
|
8
|
+
};
|
|
9
|
+
export type PairGroup = {
|
|
10
|
+
groupIndex: number;
|
|
11
|
+
name?: string;
|
|
12
|
+
block: number;
|
|
13
|
+
initialAccFeeLong: number;
|
|
14
|
+
initialAccFeeShort: number;
|
|
15
|
+
prevGroupAccFeeLong: number;
|
|
16
|
+
prevGroupAccFeeShort: number;
|
|
17
|
+
pairAccFeeLong: number;
|
|
18
|
+
pairAccFeeShort: number;
|
|
19
|
+
};
|
|
20
|
+
export type BorrowingOi = {
|
|
21
|
+
oi: OpenInterest;
|
|
22
|
+
};
|
|
23
|
+
export type BorrowingFeePerBlockCap = {
|
|
24
|
+
minP: number;
|
|
25
|
+
maxP: number;
|
|
26
|
+
};
|
|
27
|
+
export type Pair = BorrowingData & BorrowingOi & {
|
|
28
|
+
groups: PairGroup[];
|
|
29
|
+
feePerBlockCap?: BorrowingFeePerBlockCap;
|
|
30
|
+
};
|
|
31
|
+
export type Group = BorrowingData & BorrowingOi;
|
|
32
|
+
export type InitialAccFees = {
|
|
33
|
+
accPairFee: number;
|
|
34
|
+
accGroupFee: number;
|
|
35
|
+
block: number;
|
|
36
|
+
};
|
|
@@ -0,0 +1,4 @@
|
|
|
1
|
+
import { Fee, PairIndex } from "../types";
|
|
2
|
+
export declare const getClosingFee: (posDai: number, leverage: number, pairIndex: PairIndex, pairFee: Fee | undefined, collateralPriceUsd?: number | undefined, feeMultiplier?: number | undefined) => number;
|
|
3
|
+
export * from "./borrowing";
|
|
4
|
+
export * from "./tiers";
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.getClosingFee = void 0;
|
|
18
|
+
const getClosingFee = (posDai, leverage, pairIndex, pairFee, collateralPriceUsd = 0, feeMultiplier = 1) => {
|
|
19
|
+
if (posDai === undefined ||
|
|
20
|
+
leverage === undefined ||
|
|
21
|
+
pairIndex === undefined ||
|
|
22
|
+
pairFee === undefined) {
|
|
23
|
+
return 0;
|
|
24
|
+
}
|
|
25
|
+
const { totalPositionSizeFeeP, minPositionSizeUsd } = pairFee;
|
|
26
|
+
return (totalPositionSizeFeeP *
|
|
27
|
+
feeMultiplier *
|
|
28
|
+
Math.max(collateralPriceUsd && collateralPriceUsd > 0
|
|
29
|
+
? minPositionSizeUsd / collateralPriceUsd
|
|
30
|
+
: 0, posDai * leverage));
|
|
31
|
+
};
|
|
32
|
+
exports.getClosingFee = getClosingFee;
|
|
33
|
+
__exportStar(require("./borrowing"), exports);
|
|
34
|
+
__exportStar(require("./tiers"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { FeeTiers, TraderFeeTiers } from "../../types";
|
|
2
|
+
import { FeeTier } from "./types";
|
|
3
|
+
export declare const TRAILING_PERIOD_DAYS = 30;
|
|
4
|
+
export declare const FEE_MULTIPLIER_SCALE = 1;
|
|
5
|
+
export declare const MAX_FEE_TIERS = 8;
|
|
6
|
+
export declare const getCurrentDay: () => number;
|
|
7
|
+
export declare const getFeeTiersCount: (feeTiers: FeeTier[]) => number;
|
|
8
|
+
export declare const getFeeMultiplier: (trailingPoints: number, tiers: FeeTier[]) => number;
|
|
9
|
+
export declare const computeFeeMultiplier: (feeTiers: FeeTiers, traderFeeTiers: TraderFeeTiers) => {
|
|
10
|
+
feeMultiplier: number;
|
|
11
|
+
trailingPoints: number;
|
|
12
|
+
};
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.computeFeeMultiplier = exports.getFeeMultiplier = exports.getFeeTiersCount = exports.getCurrentDay = exports.MAX_FEE_TIERS = exports.FEE_MULTIPLIER_SCALE = exports.TRAILING_PERIOD_DAYS = void 0;
|
|
4
|
+
const types_1 = require("./types");
|
|
5
|
+
exports.TRAILING_PERIOD_DAYS = 30;
|
|
6
|
+
exports.FEE_MULTIPLIER_SCALE = 1;
|
|
7
|
+
exports.MAX_FEE_TIERS = 8;
|
|
8
|
+
const getCurrentDay = () => Math.floor(Date.now() / 1000 / 60 / 60 / 24);
|
|
9
|
+
exports.getCurrentDay = getCurrentDay;
|
|
10
|
+
const getFeeTiersCount = (feeTiers) => {
|
|
11
|
+
var _a;
|
|
12
|
+
for (let i = exports.MAX_FEE_TIERS; i > 0; --i) {
|
|
13
|
+
if (((_a = feeTiers[i - 1]) === null || _a === void 0 ? void 0 : _a.feeMultiplier) > 0) {
|
|
14
|
+
return i;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
17
|
+
return 0;
|
|
18
|
+
};
|
|
19
|
+
exports.getFeeTiersCount = getFeeTiersCount;
|
|
20
|
+
const getFeeMultiplier = (trailingPoints, tiers) => {
|
|
21
|
+
let feeMultiplier = exports.FEE_MULTIPLIER_SCALE;
|
|
22
|
+
for (let i = (0, exports.getFeeTiersCount)(tiers); i > 0; --i) {
|
|
23
|
+
const feeTier = tiers[i - 1];
|
|
24
|
+
if (trailingPoints >= feeTier.pointsThreshold) {
|
|
25
|
+
feeMultiplier = feeTier.feeMultiplier;
|
|
26
|
+
break;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return feeMultiplier;
|
|
30
|
+
};
|
|
31
|
+
exports.getFeeMultiplier = getFeeMultiplier;
|
|
32
|
+
const computeFeeMultiplier = (feeTiers, traderFeeTiers) => {
|
|
33
|
+
const { currentDay, tiers } = feeTiers;
|
|
34
|
+
const { traderInfo, expiredPoints, lastDayUpdatedPoints, traderEnrollment } = traderFeeTiers;
|
|
35
|
+
const { lastDayUpdated, trailingPoints } = traderInfo;
|
|
36
|
+
let curTrailingPoints = trailingPoints;
|
|
37
|
+
if (currentDay > lastDayUpdated) {
|
|
38
|
+
curTrailingPoints = 0;
|
|
39
|
+
const earliestActiveDay = currentDay - exports.TRAILING_PERIOD_DAYS;
|
|
40
|
+
if (lastDayUpdated >= earliestActiveDay) {
|
|
41
|
+
curTrailingPoints = trailingPoints + lastDayUpdatedPoints;
|
|
42
|
+
const expiredTrailingPoints = expiredPoints.reduce((acc, points) => acc + points, 0);
|
|
43
|
+
curTrailingPoints -= expiredTrailingPoints;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const feeMultiplier = traderEnrollment.status === types_1.TraderEnrollmentStatus.EXCLUDED
|
|
47
|
+
? exports.FEE_MULTIPLIER_SCALE
|
|
48
|
+
: (0, exports.getFeeMultiplier)(curTrailingPoints, tiers);
|
|
49
|
+
return {
|
|
50
|
+
feeMultiplier,
|
|
51
|
+
trailingPoints: curTrailingPoints,
|
|
52
|
+
};
|
|
53
|
+
};
|
|
54
|
+
exports.computeFeeMultiplier = computeFeeMultiplier;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
export type FeeTier = {
|
|
2
|
+
feeMultiplier: number;
|
|
3
|
+
pointsThreshold: number;
|
|
4
|
+
};
|
|
5
|
+
export type TraderInfo = {
|
|
6
|
+
lastDayUpdated: number;
|
|
7
|
+
trailingPoints: number;
|
|
8
|
+
};
|
|
9
|
+
export declare enum TraderEnrollmentStatus {
|
|
10
|
+
ENROLLED = 0,
|
|
11
|
+
EXCLUDED = 1
|
|
12
|
+
}
|
|
13
|
+
export type TraderEnrollment = {
|
|
14
|
+
status: TraderEnrollmentStatus;
|
|
15
|
+
};
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TraderEnrollmentStatus = void 0;
|
|
4
|
+
var TraderEnrollmentStatus;
|
|
5
|
+
(function (TraderEnrollmentStatus) {
|
|
6
|
+
TraderEnrollmentStatus[TraderEnrollmentStatus["ENROLLED"] = 0] = "ENROLLED";
|
|
7
|
+
TraderEnrollmentStatus[TraderEnrollmentStatus["EXCLUDED"] = 1] = "EXCLUDED";
|
|
8
|
+
})(TraderEnrollmentStatus = exports.TraderEnrollmentStatus || (exports.TraderEnrollmentStatus = {}));
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
__exportStar(require("./fees"), exports);
|
|
18
|
+
__exportStar(require("./pnl"), exports);
|
|
19
|
+
__exportStar(require("./spread"), exports);
|
|
20
|
+
__exportStar(require("./liquidation"), exports);
|
|
21
|
+
__exportStar(require("./fees"), exports);
|
|
22
|
+
__exportStar(require("./types"), exports);
|
|
23
|
+
__exportStar(require("./oiWindows"), exports);
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { GetBorrowingFeeContext, BorrowingFee } from "./fees";
|
|
2
|
+
import { Fee, LiquidationParams, Trade, UserPriceImpact } from "./types";
|
|
3
|
+
import { ContractsVersion } from "../contracts/types";
|
|
4
|
+
export type GetLiquidationPriceContext = GetBorrowingFeeContext & {
|
|
5
|
+
liquidationParams: LiquidationParams | undefined;
|
|
6
|
+
pairSpreadP: number | undefined;
|
|
7
|
+
collateralPriceUsd: number | undefined;
|
|
8
|
+
contractsVersion: ContractsVersion | undefined;
|
|
9
|
+
userPriceImpact?: UserPriceImpact | undefined;
|
|
10
|
+
};
|
|
11
|
+
export declare const getLiquidationPrice: (trade: Trade, fee: Fee, initialAccFees: BorrowingFee.InitialAccFees, context: GetLiquidationPriceContext) => number;
|
|
12
|
+
export declare const getLiqPnlThresholdP: (liquidationParams: LiquidationParams | undefined, leverage: number | undefined) => number;
|