@gainsnetwork/sdk 1.1.0 → 1.1.1-rc1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/contracts/addresses.d.ts +1 -0
- package/lib/contracts/addresses.js +9 -1
- package/lib/contracts/addresses.json +29 -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/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/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/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/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/markets/oi/fetcher.d.ts +58 -0
- package/lib/markets/oi/fetcher.js +181 -0
- package/lib/markets/oi/validation.d.ts +80 -0
- package/lib/markets/oi/validation.js +172 -0
- package/lib/markets/price/signedPrices.d.ts +36 -0
- package/lib/markets/price/signedPrices.js +181 -0
- package/lib/pricing/depthBands/converter.d.ts +65 -0
- package/lib/pricing/depthBands/converter.js +155 -0
- package/lib/pricing/depthBands/decoder.d.ts +32 -0
- package/lib/pricing/depthBands/decoder.js +109 -0
- package/lib/pricing/depthBands/encoder.d.ts +19 -0
- package/lib/pricing/depthBands/encoder.js +105 -0
- package/lib/pricing/depthBands/index.d.ts +8 -0
- package/lib/pricing/depthBands/index.js +26 -0
- package/lib/pricing/depthBands/types.d.ts +49 -0
- package/lib/pricing/depthBands/types.js +10 -0
- package/lib/pricing/depthBands/validator.d.ts +22 -0
- package/lib/pricing/depthBands/validator.js +113 -0
- package/lib/pricing/depthBands.d.ts +39 -0
- package/lib/pricing/depthBands.js +92 -0
- package/lib/pricing/index.d.ts +4 -0
- package/lib/pricing/index.js +20 -0
- package/lib/trade/effectiveLeverage/builder.d.ts +23 -0
- package/lib/trade/effectiveLeverage/builder.js +30 -0
- package/lib/trade/fees/holdingFees/index.d.ts +46 -0
- package/lib/trade/fees/holdingFees/index.js +105 -0
- package/lib/trade/fees/holdingFees/types.d.ts +23 -0
- package/lib/trade/fees/holdingFees/types.js +5 -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/priceImpact/cumulVol/types.d.ts +11 -0
- package/lib/trade/priceImpact/cumulVol/types.js +2 -0
- package/package.json +1 -1
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Depth bands 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
|
+
// Types
|
|
21
|
+
__exportStar(require("./types"), exports);
|
|
22
|
+
// Core functions
|
|
23
|
+
__exportStar(require("./encoder"), exports);
|
|
24
|
+
__exportStar(require("./decoder"), exports);
|
|
25
|
+
__exportStar(require("./validator"), exports);
|
|
26
|
+
__exportStar(require("./converter"), exports);
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Depth bands types for price impact calculations
|
|
3
|
+
*/
|
|
4
|
+
export declare const DEPTH_BANDS_COUNT = 30;
|
|
5
|
+
export declare const DEPTH_BANDS_PER_SLOT1 = 14;
|
|
6
|
+
export declare const HUNDRED_P_BPS = 10000;
|
|
7
|
+
/**
|
|
8
|
+
* @dev Encoded depth bands for a single direction
|
|
9
|
+
*/
|
|
10
|
+
export interface EncodedDepthBands {
|
|
11
|
+
slot1: bigint;
|
|
12
|
+
slot2: bigint;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* @dev Encoded depth bands for both directions
|
|
16
|
+
*/
|
|
17
|
+
export interface PairDepthBands {
|
|
18
|
+
aboveSlot1: bigint;
|
|
19
|
+
aboveSlot2: bigint;
|
|
20
|
+
belowSlot1: bigint;
|
|
21
|
+
belowSlot2: bigint;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* @dev Decoded depth bands data
|
|
25
|
+
*/
|
|
26
|
+
export interface DecodedDepthBands {
|
|
27
|
+
totalDepthUsd: number;
|
|
28
|
+
bands: number[];
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* @dev Input for encoding depth bands
|
|
32
|
+
*/
|
|
33
|
+
export interface DepthBandsInput {
|
|
34
|
+
totalDepthUsd: number;
|
|
35
|
+
cumulativeDepthUsd: number[];
|
|
36
|
+
}
|
|
37
|
+
/**
|
|
38
|
+
* @dev Depth bands mapping for all pairs
|
|
39
|
+
*/
|
|
40
|
+
export interface DepthBandsMapping {
|
|
41
|
+
slot1: bigint;
|
|
42
|
+
slot2: bigint;
|
|
43
|
+
}
|
|
44
|
+
/**
|
|
45
|
+
* @dev Decoded depth bands mapping
|
|
46
|
+
*/
|
|
47
|
+
export interface DecodedDepthBandsMapping {
|
|
48
|
+
bands: number[];
|
|
49
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Depth bands types for price impact calculations
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.HUNDRED_P_BPS = exports.DEPTH_BANDS_PER_SLOT1 = exports.DEPTH_BANDS_COUNT = void 0;
|
|
7
|
+
// Constants matching smart contract
|
|
8
|
+
exports.DEPTH_BANDS_COUNT = 30;
|
|
9
|
+
exports.DEPTH_BANDS_PER_SLOT1 = 14;
|
|
10
|
+
exports.HUNDRED_P_BPS = 10000;
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Depth bands validation functions
|
|
3
|
+
*/
|
|
4
|
+
import { type DecodedDepthBands, type DepthBandsInput } from "./types";
|
|
5
|
+
/**
|
|
6
|
+
* Validate that depth bands are properly formed
|
|
7
|
+
* @param bands Decoded depth bands to validate
|
|
8
|
+
* @throws Error if bands are invalid
|
|
9
|
+
*/
|
|
10
|
+
export declare function validateDepthBands(bands: DecodedDepthBands): void;
|
|
11
|
+
/**
|
|
12
|
+
* Validate depth bands input before encoding
|
|
13
|
+
* @param input Depth bands input to validate
|
|
14
|
+
* @throws Error if input is invalid
|
|
15
|
+
*/
|
|
16
|
+
export declare function validateDepthBandsInput(input: DepthBandsInput): void;
|
|
17
|
+
/**
|
|
18
|
+
* Validate depth bands mapping
|
|
19
|
+
* @param bands Array of band offset values in ppm
|
|
20
|
+
* @throws Error if mapping is invalid
|
|
21
|
+
*/
|
|
22
|
+
export declare function validateDepthBandsMapping(bands: number[]): void;
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Depth bands validation functions
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.validateDepthBandsMapping = exports.validateDepthBandsInput = exports.validateDepthBands = void 0;
|
|
7
|
+
const types_1 = require("./types");
|
|
8
|
+
/**
|
|
9
|
+
* Validate that depth bands are properly formed
|
|
10
|
+
* @param bands Decoded depth bands to validate
|
|
11
|
+
* @throws Error if bands are invalid
|
|
12
|
+
*/
|
|
13
|
+
function validateDepthBands(bands) {
|
|
14
|
+
const { totalDepthUsd, bands: bandValues } = bands;
|
|
15
|
+
// Check band count
|
|
16
|
+
if (bandValues.length !== types_1.DEPTH_BANDS_COUNT) {
|
|
17
|
+
throw new Error(`Invalid band count: expected ${types_1.DEPTH_BANDS_COUNT}, got ${bandValues.length}`);
|
|
18
|
+
}
|
|
19
|
+
// Check totalDepthUsd is non-negative
|
|
20
|
+
if (totalDepthUsd < 0) {
|
|
21
|
+
throw new Error(`Total depth USD cannot be negative: ${totalDepthUsd}`);
|
|
22
|
+
}
|
|
23
|
+
// Check bands are monotonically increasing
|
|
24
|
+
let prevBand = 0;
|
|
25
|
+
for (let i = 0; i < bandValues.length; i++) {
|
|
26
|
+
const band = bandValues[i];
|
|
27
|
+
if (band < 0) {
|
|
28
|
+
throw new Error(`Band ${i} cannot be negative: ${band}`);
|
|
29
|
+
}
|
|
30
|
+
if (band < prevBand) {
|
|
31
|
+
throw new Error(`Band ${i} (${band}) is less than previous band (${prevBand})`);
|
|
32
|
+
}
|
|
33
|
+
if (band > types_1.HUNDRED_P_BPS) {
|
|
34
|
+
throw new Error(`Band ${i} exceeds 100%: ${band} bps > ${types_1.HUNDRED_P_BPS} bps`);
|
|
35
|
+
}
|
|
36
|
+
prevBand = band;
|
|
37
|
+
}
|
|
38
|
+
// Check that last band reaches 100% (with tolerance for rounding)
|
|
39
|
+
const lastBand = bandValues[bandValues.length - 1];
|
|
40
|
+
if (totalDepthUsd > 0 && Math.abs(lastBand - types_1.HUNDRED_P_BPS) > 10) {
|
|
41
|
+
throw new Error(`Last band does not reach 100%: ${lastBand} bps (expected ~${types_1.HUNDRED_P_BPS} bps)`);
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.validateDepthBands = validateDepthBands;
|
|
45
|
+
/**
|
|
46
|
+
* Validate depth bands input before encoding
|
|
47
|
+
* @param input Depth bands input to validate
|
|
48
|
+
* @throws Error if input is invalid
|
|
49
|
+
*/
|
|
50
|
+
function validateDepthBandsInput(input) {
|
|
51
|
+
const { totalDepthUsd, cumulativeDepthUsd } = input;
|
|
52
|
+
// Check array length
|
|
53
|
+
if (cumulativeDepthUsd.length !== types_1.DEPTH_BANDS_COUNT) {
|
|
54
|
+
throw new Error(`Invalid cumulative depth array length: expected ${types_1.DEPTH_BANDS_COUNT}, got ${cumulativeDepthUsd.length}`);
|
|
55
|
+
}
|
|
56
|
+
// Check totalDepthUsd
|
|
57
|
+
if (totalDepthUsd < 0) {
|
|
58
|
+
throw new Error(`Total depth USD cannot be negative: ${totalDepthUsd}`);
|
|
59
|
+
}
|
|
60
|
+
const maxUint32 = Math.pow(2, 32) - 1;
|
|
61
|
+
if (totalDepthUsd > maxUint32) {
|
|
62
|
+
throw new Error(`Total depth USD exceeds uint32 max: ${totalDepthUsd} > ${maxUint32}`);
|
|
63
|
+
}
|
|
64
|
+
// Check cumulative values are monotonically increasing
|
|
65
|
+
let prevValue = 0;
|
|
66
|
+
for (let i = 0; i < cumulativeDepthUsd.length; i++) {
|
|
67
|
+
const value = cumulativeDepthUsd[i];
|
|
68
|
+
if (value < 0) {
|
|
69
|
+
throw new Error(`Cumulative depth at band ${i} cannot be negative: ${value}`);
|
|
70
|
+
}
|
|
71
|
+
if (value < prevValue) {
|
|
72
|
+
throw new Error(`Cumulative depth at band ${i} (${value}) is less than previous (${prevValue})`);
|
|
73
|
+
}
|
|
74
|
+
if (value > totalDepthUsd) {
|
|
75
|
+
throw new Error(`Cumulative depth at band ${i} (${value}) exceeds total depth (${totalDepthUsd})`);
|
|
76
|
+
}
|
|
77
|
+
prevValue = value;
|
|
78
|
+
}
|
|
79
|
+
// Check that last cumulative value equals total depth (with tolerance)
|
|
80
|
+
const lastValue = cumulativeDepthUsd[cumulativeDepthUsd.length - 1];
|
|
81
|
+
if (totalDepthUsd > 0 && Math.abs(lastValue - totalDepthUsd) > 1) {
|
|
82
|
+
throw new Error(`Last cumulative depth (${lastValue}) does not match total depth (${totalDepthUsd})`);
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
exports.validateDepthBandsInput = validateDepthBandsInput;
|
|
86
|
+
/**
|
|
87
|
+
* Validate depth bands mapping
|
|
88
|
+
* @param bands Array of band offset values in ppm
|
|
89
|
+
* @throws Error if mapping is invalid
|
|
90
|
+
*/
|
|
91
|
+
function validateDepthBandsMapping(bands) {
|
|
92
|
+
// Check array length
|
|
93
|
+
if (bands.length !== types_1.DEPTH_BANDS_COUNT) {
|
|
94
|
+
throw new Error(`Invalid band count: expected ${types_1.DEPTH_BANDS_COUNT}, got ${bands.length}`);
|
|
95
|
+
}
|
|
96
|
+
// Check bands are monotonically increasing and within range
|
|
97
|
+
let prevBand = 0;
|
|
98
|
+
const maxUint16 = Math.pow(2, 16) - 1;
|
|
99
|
+
for (let i = 0; i < bands.length; i++) {
|
|
100
|
+
const band = bands[i];
|
|
101
|
+
if (band < 0) {
|
|
102
|
+
throw new Error(`Band ${i} cannot be negative: ${band}`);
|
|
103
|
+
}
|
|
104
|
+
if (band > maxUint16) {
|
|
105
|
+
throw new Error(`Band ${i} exceeds uint16 max: ${band} > ${maxUint16}`);
|
|
106
|
+
}
|
|
107
|
+
if (band < prevBand) {
|
|
108
|
+
throw new Error(`Band ${i} (${band} ppm) is less than previous band (${prevBand} ppm)`);
|
|
109
|
+
}
|
|
110
|
+
prevBand = band;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
exports.validateDepthBandsMapping = validateDepthBandsMapping;
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Depth bands encoding/decoding functions
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Encode depth bands data into two uint256 slots
|
|
6
|
+
* @param totalDepthUsd Total depth in USD (must fit in uint32)
|
|
7
|
+
* @param bandPercentagesBps Array of 30 band percentages in basis points
|
|
8
|
+
* @returns Two slots as bigints
|
|
9
|
+
*/
|
|
10
|
+
export declare function encodeDepthBands(totalDepthUsd: number, bandPercentagesBps: number[]): {
|
|
11
|
+
slot1: bigint;
|
|
12
|
+
slot2: bigint;
|
|
13
|
+
};
|
|
14
|
+
/**
|
|
15
|
+
* Decode depth bands from two uint256 slots
|
|
16
|
+
* @param slot1 First slot containing totalDepthUsd and bands 0-13
|
|
17
|
+
* @param slot2 Second slot containing bands 14-29
|
|
18
|
+
* @returns Total depth and array of band percentages
|
|
19
|
+
*/
|
|
20
|
+
export declare function decodeDepthBands(slot1: bigint, slot2: bigint): {
|
|
21
|
+
totalDepthUsd: number;
|
|
22
|
+
bands: number[];
|
|
23
|
+
};
|
|
24
|
+
/**
|
|
25
|
+
* Encode depth bands mapping (global offsets for all pairs)
|
|
26
|
+
* @param bands Array of 30 band offset values in ppm
|
|
27
|
+
* @returns Two slots as bigints
|
|
28
|
+
*/
|
|
29
|
+
export declare function encodeDepthBandsMapping(bands: number[]): {
|
|
30
|
+
slot1: bigint;
|
|
31
|
+
slot2: bigint;
|
|
32
|
+
};
|
|
33
|
+
/**
|
|
34
|
+
* Decode depth bands mapping from two uint256 slots
|
|
35
|
+
* @param slot1 First slot containing bands 0-13
|
|
36
|
+
* @param slot2 Second slot containing bands 14-29
|
|
37
|
+
* @returns Array of band offset values in ppm
|
|
38
|
+
*/
|
|
39
|
+
export declare function decodeDepthBandsMapping(slot1: bigint, slot2: bigint): number[];
|
|
@@ -0,0 +1,92 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Depth bands encoding/decoding functions
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.decodeDepthBandsMapping = exports.encodeDepthBandsMapping = exports.decodeDepthBands = exports.encodeDepthBands = void 0;
|
|
7
|
+
const DEPTH_BANDS_COUNT = 30;
|
|
8
|
+
const DEPTH_BANDS_PER_SLOT1 = 14;
|
|
9
|
+
/**
|
|
10
|
+
* Encode depth bands data into two uint256 slots
|
|
11
|
+
* @param totalDepthUsd Total depth in USD (must fit in uint32)
|
|
12
|
+
* @param bandPercentagesBps Array of 30 band percentages in basis points
|
|
13
|
+
* @returns Two slots as bigints
|
|
14
|
+
*/
|
|
15
|
+
function encodeDepthBands(totalDepthUsd, bandPercentagesBps) {
|
|
16
|
+
// Pack slot1: totalDepthUsd (32 bits) + bands 0-13 (14 * 16 bits)
|
|
17
|
+
let slot1 = BigInt(totalDepthUsd);
|
|
18
|
+
for (let i = 0; i < DEPTH_BANDS_PER_SLOT1; i++) {
|
|
19
|
+
const shift = 32 + i * 16;
|
|
20
|
+
slot1 |= BigInt(bandPercentagesBps[i]) << BigInt(shift);
|
|
21
|
+
}
|
|
22
|
+
// Pack slot2: bands 14-29 (16 * 16 bits)
|
|
23
|
+
let slot2 = BigInt(0);
|
|
24
|
+
for (let i = DEPTH_BANDS_PER_SLOT1; i < DEPTH_BANDS_COUNT; i++) {
|
|
25
|
+
const shift = (i - DEPTH_BANDS_PER_SLOT1) * 16;
|
|
26
|
+
slot2 |= BigInt(bandPercentagesBps[i]) << BigInt(shift);
|
|
27
|
+
}
|
|
28
|
+
return { slot1, slot2 };
|
|
29
|
+
}
|
|
30
|
+
exports.encodeDepthBands = encodeDepthBands;
|
|
31
|
+
/**
|
|
32
|
+
* Decode depth bands from two uint256 slots
|
|
33
|
+
* @param slot1 First slot containing totalDepthUsd and bands 0-13
|
|
34
|
+
* @param slot2 Second slot containing bands 14-29
|
|
35
|
+
* @returns Total depth and array of band percentages
|
|
36
|
+
*/
|
|
37
|
+
function decodeDepthBands(slot1, slot2) {
|
|
38
|
+
const totalDepthUsd = Number(slot1 & BigInt(0xffffffff));
|
|
39
|
+
const bands = [];
|
|
40
|
+
// Extract bands 0-13 from slot1
|
|
41
|
+
for (let i = 0; i < DEPTH_BANDS_PER_SLOT1; i++) {
|
|
42
|
+
const shift = 32 + i * 16;
|
|
43
|
+
bands.push(Number((slot1 >> BigInt(shift)) & BigInt(0xffff)));
|
|
44
|
+
}
|
|
45
|
+
// Extract bands 14-29 from slot2
|
|
46
|
+
for (let i = DEPTH_BANDS_PER_SLOT1; i < DEPTH_BANDS_COUNT; i++) {
|
|
47
|
+
const shift = (i - DEPTH_BANDS_PER_SLOT1) * 16;
|
|
48
|
+
bands.push(Number((slot2 >> BigInt(shift)) & BigInt(0xffff)));
|
|
49
|
+
}
|
|
50
|
+
return { totalDepthUsd, bands };
|
|
51
|
+
}
|
|
52
|
+
exports.decodeDepthBands = decodeDepthBands;
|
|
53
|
+
/**
|
|
54
|
+
* Encode depth bands mapping (global offsets for all pairs)
|
|
55
|
+
* @param bands Array of 30 band offset values in ppm
|
|
56
|
+
* @returns Two slots as bigints
|
|
57
|
+
*/
|
|
58
|
+
function encodeDepthBandsMapping(bands) {
|
|
59
|
+
// Pack slot1: bands 0-13 (14 * 16 bits)
|
|
60
|
+
let slot1 = BigInt(0);
|
|
61
|
+
for (let i = 0; i < DEPTH_BANDS_PER_SLOT1; i++) {
|
|
62
|
+
slot1 |= BigInt(bands[i]) << BigInt(i * 16);
|
|
63
|
+
}
|
|
64
|
+
// Pack slot2: bands 14-29 (16 * 16 bits)
|
|
65
|
+
let slot2 = BigInt(0);
|
|
66
|
+
for (let i = DEPTH_BANDS_PER_SLOT1; i < DEPTH_BANDS_COUNT; i++) {
|
|
67
|
+
const shift = (i - DEPTH_BANDS_PER_SLOT1) * 16;
|
|
68
|
+
slot2 |= BigInt(bands[i]) << BigInt(shift);
|
|
69
|
+
}
|
|
70
|
+
return { slot1, slot2 };
|
|
71
|
+
}
|
|
72
|
+
exports.encodeDepthBandsMapping = encodeDepthBandsMapping;
|
|
73
|
+
/**
|
|
74
|
+
* Decode depth bands mapping from two uint256 slots
|
|
75
|
+
* @param slot1 First slot containing bands 0-13
|
|
76
|
+
* @param slot2 Second slot containing bands 14-29
|
|
77
|
+
* @returns Array of band offset values in ppm
|
|
78
|
+
*/
|
|
79
|
+
function decodeDepthBandsMapping(slot1, slot2) {
|
|
80
|
+
const bands = [];
|
|
81
|
+
// Extract bands 0-13 from slot1
|
|
82
|
+
for (let i = 0; i < DEPTH_BANDS_PER_SLOT1; i++) {
|
|
83
|
+
bands.push(Number((slot1 >> BigInt(i * 16)) & BigInt(0xffff)));
|
|
84
|
+
}
|
|
85
|
+
// Extract bands 14-29 from slot2
|
|
86
|
+
for (let i = DEPTH_BANDS_PER_SLOT1; i < DEPTH_BANDS_COUNT; i++) {
|
|
87
|
+
const shift = (i - DEPTH_BANDS_PER_SLOT1) * 16;
|
|
88
|
+
bands.push(Number((slot2 >> BigInt(shift)) & BigInt(0xffff)));
|
|
89
|
+
}
|
|
90
|
+
return bands;
|
|
91
|
+
}
|
|
92
|
+
exports.decodeDepthBandsMapping = decodeDepthBandsMapping;
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Pricing 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("./depthBands"), exports);
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { GlobalTradingVariablesType } from "../../backend/tradingVariables/types";
|
|
2
|
+
import { TradeInfo } from "..";
|
|
3
|
+
import { TradeEffectiveLeverageContext } from "./types";
|
|
4
|
+
/**
|
|
5
|
+
* @dev Builds a complete context for effective leverage calculations
|
|
6
|
+
* @dev Uses the closing price impact context builder as a sub-context
|
|
7
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
8
|
+
* @param collateralIndex The collateral index (1-based)
|
|
9
|
+
* @param pairIndex The pair index
|
|
10
|
+
* @param tradeInfo Trade information including createdBlock
|
|
11
|
+
* @param additionalParams Additional parameters for price impact calculations
|
|
12
|
+
* @returns Complete context ready for getTradeNewEffectiveLeverage
|
|
13
|
+
*/
|
|
14
|
+
export declare const buildTradeEffectiveLeverageContext: (globalTradingVariables: GlobalTradingVariablesType, collateralIndex: number, pairIndex: number, tradeInfo: TradeInfo, additionalParams: {
|
|
15
|
+
currentBlock: number;
|
|
16
|
+
contractsVersion?: number;
|
|
17
|
+
isPnlPositive?: boolean;
|
|
18
|
+
userPriceImpact?: {
|
|
19
|
+
cumulVolPriceImpactMultiplier: number;
|
|
20
|
+
fixedSpreadP: number;
|
|
21
|
+
};
|
|
22
|
+
protectionCloseFactorWhitelist?: boolean;
|
|
23
|
+
}) => TradeEffectiveLeverageContext | undefined;
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.buildTradeEffectiveLeverageContext = void 0;
|
|
4
|
+
const __1 = require("..");
|
|
5
|
+
/**
|
|
6
|
+
* @dev Builds a complete context for effective leverage calculations
|
|
7
|
+
* @dev Uses the closing price impact context builder as a sub-context
|
|
8
|
+
* @param globalTradingVariables The transformed global trading variables from backend
|
|
9
|
+
* @param collateralIndex The collateral index (1-based)
|
|
10
|
+
* @param pairIndex The pair index
|
|
11
|
+
* @param tradeInfo Trade information including createdBlock
|
|
12
|
+
* @param additionalParams Additional parameters for price impact calculations
|
|
13
|
+
* @returns Complete context ready for getTradeNewEffectiveLeverage
|
|
14
|
+
*/
|
|
15
|
+
const buildTradeEffectiveLeverageContext = (globalTradingVariables, collateralIndex, pairIndex, tradeInfo, additionalParams) => {
|
|
16
|
+
var _a;
|
|
17
|
+
// Build the closing price impact context which we'll use for PnL calculations
|
|
18
|
+
const closingPriceImpactContext = (0, __1.buildTradeClosingPriceImpactContext)(globalTradingVariables, collateralIndex, pairIndex, tradeInfo, additionalParams);
|
|
19
|
+
if (!closingPriceImpactContext) {
|
|
20
|
+
return undefined;
|
|
21
|
+
}
|
|
22
|
+
// Extract base spread from pairs data
|
|
23
|
+
const pairs = globalTradingVariables.pairs;
|
|
24
|
+
const baseSpreadP = ((_a = pairs === null || pairs === void 0 ? void 0 : pairs[pairIndex]) === null || _a === void 0 ? void 0 : _a.spreadP) || 0;
|
|
25
|
+
return {
|
|
26
|
+
closingPriceImpactContext,
|
|
27
|
+
baseSpreadP,
|
|
28
|
+
};
|
|
29
|
+
};
|
|
30
|
+
exports.buildTradeEffectiveLeverageContext = buildTradeEffectiveLeverageContext;
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Holding fees calculation utilities for v10+ markets
|
|
3
|
+
* @dev Combines funding fees and borrowing v2 fees
|
|
4
|
+
*/
|
|
5
|
+
import { FundingFeeParams, PairFundingFeeData, PairOiAfterV10 } from "../fundingFees/types";
|
|
6
|
+
import { BorrowingFeeParams, PairBorrowingFeeData } from "../borrowingV2/types";
|
|
7
|
+
export type HoldingFeeRates = {
|
|
8
|
+
longHourlyRate: number;
|
|
9
|
+
shortHourlyRate: number;
|
|
10
|
+
fundingFeeLongHourlyRate: number;
|
|
11
|
+
fundingFeeShortHourlyRate: number;
|
|
12
|
+
borrowingFeeHourlyRate: number;
|
|
13
|
+
currentFundingRatePerSecondP: number;
|
|
14
|
+
currentBorrowingRatePerSecondP: number;
|
|
15
|
+
};
|
|
16
|
+
export type GetPairHoldingFeeRatesInput = {
|
|
17
|
+
fundingParams: FundingFeeParams;
|
|
18
|
+
fundingData: PairFundingFeeData;
|
|
19
|
+
pairOiToken: PairOiAfterV10;
|
|
20
|
+
netExposureToken: number;
|
|
21
|
+
netExposureUsd: number;
|
|
22
|
+
borrowingParams: BorrowingFeeParams | null;
|
|
23
|
+
borrowingData: PairBorrowingFeeData | null;
|
|
24
|
+
currentPairPrice: number;
|
|
25
|
+
currentTimestamp: number;
|
|
26
|
+
};
|
|
27
|
+
/**
|
|
28
|
+
* @dev Calculates current holding fee rates per hour for display
|
|
29
|
+
* @param input Input parameters for calculation
|
|
30
|
+
* @returns Holding fee rates per hour with breakdown
|
|
31
|
+
*/
|
|
32
|
+
export declare const getPairHoldingFeeRates: (input: GetPairHoldingFeeRatesInput) => HoldingFeeRates;
|
|
33
|
+
/**
|
|
34
|
+
* @dev Converts a per-second rate to annual percentage rate (APR)
|
|
35
|
+
* @param ratePerSecond Rate per second
|
|
36
|
+
* @returns Annual percentage rate
|
|
37
|
+
*/
|
|
38
|
+
export declare const convertRatePerSecondToAPR: (ratePerSecond: number) => number;
|
|
39
|
+
/**
|
|
40
|
+
* @dev Formats a holding fee rate for display
|
|
41
|
+
* @param rate Hourly rate (can be negative)
|
|
42
|
+
* @param decimals Number of decimal places
|
|
43
|
+
* @returns Formatted string with sign
|
|
44
|
+
*/
|
|
45
|
+
export declare const formatHoldingFeeRate: (rate: number, decimals?: number) => string;
|
|
46
|
+
export * as HoldingFees from "./types";
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* @dev Holding fees calculation utilities for v10+ markets
|
|
4
|
+
* @dev Combines funding fees and borrowing v2 fees
|
|
5
|
+
*/
|
|
6
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
7
|
+
if (k2 === undefined) k2 = k;
|
|
8
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
9
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
10
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
11
|
+
}
|
|
12
|
+
Object.defineProperty(o, k2, desc);
|
|
13
|
+
}) : (function(o, m, k, k2) {
|
|
14
|
+
if (k2 === undefined) k2 = k;
|
|
15
|
+
o[k2] = m[k];
|
|
16
|
+
}));
|
|
17
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
18
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
19
|
+
}) : function(o, v) {
|
|
20
|
+
o["default"] = v;
|
|
21
|
+
});
|
|
22
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
23
|
+
if (mod && mod.__esModule) return mod;
|
|
24
|
+
var result = {};
|
|
25
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
26
|
+
__setModuleDefault(result, mod);
|
|
27
|
+
return result;
|
|
28
|
+
};
|
|
29
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
30
|
+
exports.HoldingFees = exports.formatHoldingFeeRate = exports.convertRatePerSecondToAPR = exports.getPairHoldingFeeRates = void 0;
|
|
31
|
+
const fundingFees_1 = require("../fundingFees");
|
|
32
|
+
const SECONDS_PER_HOUR = 3600;
|
|
33
|
+
const SECONDS_PER_YEAR = 365 * 24 * 60 * 60;
|
|
34
|
+
const PERCENTAGE_PRECISION = 100;
|
|
35
|
+
/**
|
|
36
|
+
* @dev Calculates current holding fee rates per hour for display
|
|
37
|
+
* @param input Input parameters for calculation
|
|
38
|
+
* @returns Holding fee rates per hour with breakdown
|
|
39
|
+
*/
|
|
40
|
+
const getPairHoldingFeeRates = (input) => {
|
|
41
|
+
const { fundingParams, fundingData, pairOiToken, netExposureToken, netExposureUsd, borrowingParams, borrowingData, currentPairPrice, currentTimestamp, } = input;
|
|
42
|
+
// Calculate funding fee rates
|
|
43
|
+
let fundingFeeLongHourlyRate = 0;
|
|
44
|
+
let fundingFeeShortHourlyRate = 0;
|
|
45
|
+
let currentFundingRatePerSecondP = 0;
|
|
46
|
+
if (fundingParams.fundingFeesEnabled) {
|
|
47
|
+
// Get current funding rate
|
|
48
|
+
const pendingFunding = (0, fundingFees_1.getPairPendingAccFundingFees)(fundingParams, fundingData, currentPairPrice, pairOiToken, netExposureToken, netExposureUsd, currentTimestamp);
|
|
49
|
+
currentFundingRatePerSecondP = pendingFunding.currentFundingRatePerSecondP;
|
|
50
|
+
// Get APR multipliers
|
|
51
|
+
const { longAprMultiplier, shortAprMultiplier } = (0, fundingFees_1.getLongShortAprMultiplier)(currentFundingRatePerSecondP, pairOiToken.oiLongToken, pairOiToken.oiShortToken, fundingParams.aprMultiplierEnabled);
|
|
52
|
+
// Calculate hourly rates
|
|
53
|
+
// Funding rate * seconds per hour * current price * APR multiplier / 100
|
|
54
|
+
const baseHourlyRate = (currentFundingRatePerSecondP * SECONDS_PER_HOUR * currentPairPrice) /
|
|
55
|
+
PERCENTAGE_PRECISION;
|
|
56
|
+
// Long side pays when rate is positive, earns when negative
|
|
57
|
+
fundingFeeLongHourlyRate = baseHourlyRate * longAprMultiplier;
|
|
58
|
+
// Short side is opposite
|
|
59
|
+
fundingFeeShortHourlyRate = -baseHourlyRate * shortAprMultiplier;
|
|
60
|
+
}
|
|
61
|
+
// Calculate borrowing v2 rates
|
|
62
|
+
let borrowingFeeHourlyRate = 0;
|
|
63
|
+
let currentBorrowingRatePerSecondP = 0;
|
|
64
|
+
if (borrowingParams && borrowingData) {
|
|
65
|
+
currentBorrowingRatePerSecondP = borrowingParams.borrowingRatePerSecondP;
|
|
66
|
+
// Borrowing rate * seconds per hour * current price / 100
|
|
67
|
+
borrowingFeeHourlyRate =
|
|
68
|
+
(currentBorrowingRatePerSecondP * SECONDS_PER_HOUR * currentPairPrice) /
|
|
69
|
+
PERCENTAGE_PRECISION;
|
|
70
|
+
}
|
|
71
|
+
// Total holding fees (funding can be negative/positive, borrowing always positive cost)
|
|
72
|
+
const longHourlyRate = fundingFeeLongHourlyRate + borrowingFeeHourlyRate;
|
|
73
|
+
const shortHourlyRate = fundingFeeShortHourlyRate + borrowingFeeHourlyRate;
|
|
74
|
+
return {
|
|
75
|
+
longHourlyRate,
|
|
76
|
+
shortHourlyRate,
|
|
77
|
+
fundingFeeLongHourlyRate,
|
|
78
|
+
fundingFeeShortHourlyRate,
|
|
79
|
+
borrowingFeeHourlyRate,
|
|
80
|
+
currentFundingRatePerSecondP,
|
|
81
|
+
currentBorrowingRatePerSecondP,
|
|
82
|
+
};
|
|
83
|
+
};
|
|
84
|
+
exports.getPairHoldingFeeRates = getPairHoldingFeeRates;
|
|
85
|
+
/**
|
|
86
|
+
* @dev Converts a per-second rate to annual percentage rate (APR)
|
|
87
|
+
* @param ratePerSecond Rate per second
|
|
88
|
+
* @returns Annual percentage rate
|
|
89
|
+
*/
|
|
90
|
+
const convertRatePerSecondToAPR = (ratePerSecond) => {
|
|
91
|
+
return ratePerSecond * SECONDS_PER_YEAR * PERCENTAGE_PRECISION;
|
|
92
|
+
};
|
|
93
|
+
exports.convertRatePerSecondToAPR = convertRatePerSecondToAPR;
|
|
94
|
+
/**
|
|
95
|
+
* @dev Formats a holding fee rate for display
|
|
96
|
+
* @param rate Hourly rate (can be negative)
|
|
97
|
+
* @param decimals Number of decimal places
|
|
98
|
+
* @returns Formatted string with sign
|
|
99
|
+
*/
|
|
100
|
+
const formatHoldingFeeRate = (rate, decimals = 4) => {
|
|
101
|
+
const sign = rate > 0 ? "+" : "";
|
|
102
|
+
return `${sign}${rate.toFixed(decimals)}%`;
|
|
103
|
+
};
|
|
104
|
+
exports.formatHoldingFeeRate = formatHoldingFeeRate;
|
|
105
|
+
exports.HoldingFees = __importStar(require("./types"));
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Type definitions for holding fees (funding + borrowing v2)
|
|
3
|
+
*/
|
|
4
|
+
export interface HoldingFeeRates {
|
|
5
|
+
longHourlyRate: number;
|
|
6
|
+
shortHourlyRate: number;
|
|
7
|
+
fundingFeeLongHourlyRate: number;
|
|
8
|
+
fundingFeeShortHourlyRate: number;
|
|
9
|
+
borrowingFeeHourlyRate: number;
|
|
10
|
+
currentFundingRatePerSecondP: number;
|
|
11
|
+
currentBorrowingRatePerSecondP: number;
|
|
12
|
+
}
|
|
13
|
+
export interface GetPairHoldingFeeRatesInput {
|
|
14
|
+
fundingParams: import("../fundingFees/types").FundingFeeParams;
|
|
15
|
+
fundingData: import("../fundingFees/types").PairFundingFeeData;
|
|
16
|
+
pairOiToken: import("../fundingFees/types").PairOiAfterV10;
|
|
17
|
+
netExposureToken: number;
|
|
18
|
+
netExposureUsd: number;
|
|
19
|
+
borrowingParams: import("../borrowingV2/types").BorrowingFeeParams | null;
|
|
20
|
+
borrowingData: import("../borrowingV2/types").PairBorrowingFeeData | null;
|
|
21
|
+
currentPairPrice: number;
|
|
22
|
+
currentTimestamp: number;
|
|
23
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @dev Holding fees calculation for structured contexts
|
|
3
|
+
*/
|
|
4
|
+
import { Trade, TradeInfo, TradeFeesData } from "../../types";
|
|
5
|
+
import { TradeHoldingFees } from "./types";
|
|
6
|
+
import { ContractsVersion } from "../../../contracts/types";
|
|
7
|
+
import type { BorrowingV1SubContext, BorrowingV2SubContext, FundingFeesSubContext } from "../../pnl";
|
|
8
|
+
/**
|
|
9
|
+
* @dev Context for holding fees calculation with structured sub-contexts
|
|
10
|
+
*/
|
|
11
|
+
export type GetStructuredHoldingFeesContext = {
|
|
12
|
+
contractsVersion: ContractsVersion;
|
|
13
|
+
currentTimestamp: number;
|
|
14
|
+
collateralPriceUsd: number;
|
|
15
|
+
borrowingV1?: BorrowingV1SubContext;
|
|
16
|
+
borrowingV2?: BorrowingV2SubContext;
|
|
17
|
+
funding?: FundingFeesSubContext;
|
|
18
|
+
};
|
|
19
|
+
/**
|
|
20
|
+
* @dev Calculates total holding fees using structured context
|
|
21
|
+
* @param trade The trade to calculate fees for
|
|
22
|
+
* @param tradeInfo Trade info containing contracts version
|
|
23
|
+
* @param tradeFeesData Trade fees data containing initial acc fees
|
|
24
|
+
* @param currentPairPrice Current pair price
|
|
25
|
+
* @param context Structured context with sub-contexts for each fee type
|
|
26
|
+
* @returns Object containing all holding fee components
|
|
27
|
+
*/
|
|
28
|
+
export declare const getTradePendingHoldingFeesCollateralStructured: (trade: Trade, tradeInfo: TradeInfo, tradeFeesData: TradeFeesData, currentPairPrice: number, context: GetStructuredHoldingFeesContext) => TradeHoldingFees;
|