@megatao/sdk 1.1.0
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/.env.example +37 -0
- package/CHANGELOG.md +19 -0
- package/README.md +199 -0
- package/bin/alf +4 -0
- package/cli/README.md +198 -0
- package/cli/TEST_MANUAL.md +577 -0
- package/cli/commands/account.ts +545 -0
- package/cli/commands/funding.ts +481 -0
- package/cli/commands/liquidation.ts +523 -0
- package/cli/commands/market.ts +590 -0
- package/cli/commands/orders.ts +395 -0
- package/cli/commands/position.ts +1085 -0
- package/cli/commands/shared/positionUtils.ts +239 -0
- package/cli/commands/trading.ts +483 -0
- package/cli/commands/utils.ts +281 -0
- package/cli/commands/vault.ts +522 -0
- package/cli/index.ts +169 -0
- package/cli/interactive.ts +530 -0
- package/cli/utils/client.ts +457 -0
- package/cli/utils/config.ts +226 -0
- package/cli/utils/display.ts +258 -0
- package/cli/utils/index.ts +10 -0
- package/cli/utils/prompts.ts +364 -0
- package/config.example.json +23 -0
- package/dist/AlphaFuturesClient.d.ts +36 -0
- package/dist/AlphaFuturesClient.d.ts.map +1 -0
- package/dist/AlphaFuturesClient.js +116 -0
- package/dist/AlphaFuturesClient.js.map +1 -0
- package/dist/abi/Alpha.json +5987 -0
- package/dist/abi/abis.d.ts +319 -0
- package/dist/abi/abis.d.ts.map +1 -0
- package/dist/abi/abis.js +128 -0
- package/dist/abi/abis.js.map +1 -0
- package/dist/abi/index.d.ts +11 -0
- package/dist/abi/index.d.ts.map +1 -0
- package/dist/abi/index.js +15 -0
- package/dist/abi/index.js.map +1 -0
- package/dist/config/contracts.config.d.ts +70 -0
- package/dist/config/contracts.config.d.ts.map +1 -0
- package/dist/config/contracts.config.js +137 -0
- package/dist/config/contracts.config.js.map +1 -0
- package/dist/config/environments/alpha.config.d.ts +17 -0
- package/dist/config/environments/alpha.config.d.ts.map +1 -0
- package/dist/config/environments/alpha.config.js +140 -0
- package/dist/config/environments/alpha.config.js.map +1 -0
- package/dist/config/environments/beta.config.d.ts +16 -0
- package/dist/config/environments/beta.config.d.ts.map +1 -0
- package/dist/config/environments/beta.config.js +131 -0
- package/dist/config/environments/beta.config.js.map +1 -0
- package/dist/config/environments/dev.config.d.ts +13 -0
- package/dist/config/environments/dev.config.d.ts.map +1 -0
- package/dist/config/environments/dev.config.js +123 -0
- package/dist/config/environments/dev.config.js.map +1 -0
- package/dist/config/environments/index.d.ts +48 -0
- package/dist/config/environments/index.d.ts.map +1 -0
- package/dist/config/environments/index.js +81 -0
- package/dist/config/environments/index.js.map +1 -0
- package/dist/config/environments/localhost.config.d.ts +16 -0
- package/dist/config/environments/localhost.config.d.ts.map +1 -0
- package/dist/config/environments/localhost.config.js +152 -0
- package/dist/config/environments/localhost.config.js.map +1 -0
- package/dist/config/environments/prod.config.d.ts +20 -0
- package/dist/config/environments/prod.config.d.ts.map +1 -0
- package/dist/config/environments/prod.config.js +143 -0
- package/dist/config/environments/prod.config.js.map +1 -0
- package/dist/config/index.d.ts +7 -0
- package/dist/config/index.d.ts.map +1 -0
- package/dist/config/index.js +41 -0
- package/dist/config/index.js.map +1 -0
- package/dist/constants/assets.d.ts +76 -0
- package/dist/constants/assets.d.ts.map +1 -0
- package/dist/constants/assets.js +277 -0
- package/dist/constants/assets.js.map +1 -0
- package/dist/constants/contracts.d.ts +41 -0
- package/dist/constants/contracts.d.ts.map +1 -0
- package/dist/constants/contracts.js +57 -0
- package/dist/constants/contracts.js.map +1 -0
- package/dist/constants/index.d.ts +36 -0
- package/dist/constants/index.d.ts.map +1 -0
- package/dist/constants/index.js +75 -0
- package/dist/constants/index.js.map +1 -0
- package/dist/constants/networks.d.ts +32 -0
- package/dist/constants/networks.d.ts.map +1 -0
- package/dist/constants/networks.js +174 -0
- package/dist/constants/networks.js.map +1 -0
- package/dist/contracts/index.d.ts +5 -0
- package/dist/contracts/index.d.ts.map +1 -0
- package/dist/contracts/index.js +21 -0
- package/dist/contracts/index.js.map +1 -0
- package/dist/contracts/viem/AlphaViem.d.ts +518 -0
- package/dist/contracts/viem/AlphaViem.d.ts.map +1 -0
- package/dist/contracts/viem/AlphaViem.js +1287 -0
- package/dist/contracts/viem/AlphaViem.js.map +1 -0
- package/dist/contracts/viem/PriceOracleViem.d.ts +71 -0
- package/dist/contracts/viem/PriceOracleViem.d.ts.map +1 -0
- package/dist/contracts/viem/PriceOracleViem.js +212 -0
- package/dist/contracts/viem/PriceOracleViem.js.map +1 -0
- package/dist/contracts/viem/index.d.ts +9 -0
- package/dist/contracts/viem/index.d.ts.map +1 -0
- package/dist/contracts/viem/index.js +17 -0
- package/dist/contracts/viem/index.js.map +1 -0
- package/dist/errors/index.d.ts +44 -0
- package/dist/errors/index.d.ts.map +1 -0
- package/dist/errors/index.js +83 -0
- package/dist/errors/index.js.map +1 -0
- package/dist/index.d.ts +19 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +60 -0
- package/dist/index.js.map +1 -0
- package/dist/types/alpha.d.ts +299 -0
- package/dist/types/alpha.d.ts.map +1 -0
- package/dist/types/alpha.js +6 -0
- package/dist/types/alpha.js.map +1 -0
- package/dist/types/client.d.ts +24 -0
- package/dist/types/client.d.ts.map +1 -0
- package/dist/types/client.js +13 -0
- package/dist/types/client.js.map +1 -0
- package/dist/types/contracts.d.ts +48 -0
- package/dist/types/contracts.d.ts.map +1 -0
- package/dist/types/contracts.js +6 -0
- package/dist/types/contracts.js.map +1 -0
- package/dist/types/funding.d.ts +27 -0
- package/dist/types/funding.d.ts.map +1 -0
- package/dist/types/funding.js +6 -0
- package/dist/types/funding.js.map +1 -0
- package/dist/types/index.d.ts +92 -0
- package/dist/types/index.d.ts.map +1 -0
- package/dist/types/index.js +47 -0
- package/dist/types/index.js.map +1 -0
- package/dist/types/liquidation.d.ts +20 -0
- package/dist/types/liquidation.d.ts.map +1 -0
- package/dist/types/liquidation.js +6 -0
- package/dist/types/liquidation.js.map +1 -0
- package/dist/types/margin.d.ts +29 -0
- package/dist/types/margin.d.ts.map +1 -0
- package/dist/types/margin.js +6 -0
- package/dist/types/margin.js.map +1 -0
- package/dist/types/oracle.d.ts +21 -0
- package/dist/types/oracle.d.ts.map +1 -0
- package/dist/types/oracle.js +6 -0
- package/dist/types/oracle.js.map +1 -0
- package/dist/types/positions.d.ts +43 -0
- package/dist/types/positions.d.ts.map +1 -0
- package/dist/types/positions.js +13 -0
- package/dist/types/positions.js.map +1 -0
- package/dist/utils/calculations.d.ts +84 -0
- package/dist/utils/calculations.d.ts.map +1 -0
- package/dist/utils/calculations.js +155 -0
- package/dist/utils/calculations.js.map +1 -0
- package/dist/utils/errors.d.ts +24 -0
- package/dist/utils/errors.d.ts.map +1 -0
- package/dist/utils/errors.js +129 -0
- package/dist/utils/errors.js.map +1 -0
- package/dist/utils/events.d.ts +40 -0
- package/dist/utils/events.d.ts.map +1 -0
- package/dist/utils/events.js +73 -0
- package/dist/utils/events.js.map +1 -0
- package/dist/utils/format.d.ts +40 -0
- package/dist/utils/format.d.ts.map +1 -0
- package/dist/utils/format.js +86 -0
- package/dist/utils/format.js.map +1 -0
- package/dist/utils/index.d.ts +10 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js +26 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/network.d.ts +52 -0
- package/dist/utils/network.d.ts.map +1 -0
- package/dist/utils/network.js +192 -0
- package/dist/utils/network.js.map +1 -0
- package/dist/utils/positionCalculations.d.ts +145 -0
- package/dist/utils/positionCalculations.d.ts.map +1 -0
- package/dist/utils/positionCalculations.js +278 -0
- package/dist/utils/positionCalculations.js.map +1 -0
- package/dist/utils/validation.d.ts +28 -0
- package/dist/utils/validation.d.ts.map +1 -0
- package/dist/utils/validation.js +68 -0
- package/dist/utils/validation.js.map +1 -0
- package/docs/README.md +40 -0
- package/docs/api/API.md +831 -0
- package/docs/guides/GETTING_STARTED.md +316 -0
- package/docs/guides/TRADING_GUIDE.md +677 -0
- package/docs/integration/INTEGRATION_GUIDE.md +1679 -0
- package/docs/integration/VIEM_INTEGRATION.md +294 -0
- package/docs/reference/CLI_QUICK_REFERENCE.md +197 -0
- package/docs/reference/TROUBLESHOOTING.md +922 -0
- package/package.json +113 -0
- package/src/AlphaFuturesClient.ts +158 -0
- package/src/abi/.gitkeep +1 -0
- package/src/abi/Alpha.json +5987 -0
- package/src/abi/README.md +99 -0
- package/src/abi/abis.ts +131 -0
- package/src/abi/index.ts +13 -0
- package/src/config/contracts.config.ts +186 -0
- package/src/config/environments/alpha.config.ts +139 -0
- package/src/config/environments/beta.config.ts +130 -0
- package/src/config/environments/dev.config.ts +122 -0
- package/src/config/environments/index.ts +87 -0
- package/src/config/environments/localhost.config.ts +153 -0
- package/src/config/environments/prod.config.ts +142 -0
- package/src/config/index.ts +29 -0
- package/src/constants/assets.ts +299 -0
- package/src/constants/contracts.ts +64 -0
- package/src/constants/index.ts +69 -0
- package/src/constants/networks.ts +182 -0
- package/src/contracts/index.ts +5 -0
- package/src/contracts/viem/AlphaViem.ts +1615 -0
- package/src/contracts/viem/PriceOracleViem.ts +272 -0
- package/src/contracts/viem/index.ts +11 -0
- package/src/errors/index.ts +87 -0
- package/src/index.ts +59 -0
- package/src/types/VIEM_TYPES_README.md +70 -0
- package/src/types/alpha.ts +358 -0
- package/src/types/client.ts +27 -0
- package/src/types/contracts.ts +74 -0
- package/src/types/funding.ts +31 -0
- package/src/types/index.ts +108 -0
- package/src/types/liquidation.ts +23 -0
- package/src/types/margin.ts +34 -0
- package/src/types/oracle.ts +24 -0
- package/src/types/positions.ts +48 -0
- package/src/utils/calculations.ts +175 -0
- package/src/utils/errors.ts +147 -0
- package/src/utils/events.ts +98 -0
- package/src/utils/format.ts +84 -0
- package/src/utils/index.ts +10 -0
- package/src/utils/network.ts +212 -0
- package/src/utils/positionCalculations.ts +317 -0
- package/src/utils/validation.ts +76 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Margin-related type definitions
|
|
3
|
+
*/
|
|
4
|
+
import { Address } from 'viem';
|
|
5
|
+
export interface MarginAccount {
|
|
6
|
+
balance: bigint;
|
|
7
|
+
lockedMargin: bigint;
|
|
8
|
+
availableBalance: bigint;
|
|
9
|
+
totalPositions: number;
|
|
10
|
+
}
|
|
11
|
+
export interface MarginRequirement {
|
|
12
|
+
initialMargin: bigint;
|
|
13
|
+
maintenanceMargin: bigint;
|
|
14
|
+
currentMargin: bigint;
|
|
15
|
+
marginRatio: bigint;
|
|
16
|
+
}
|
|
17
|
+
export interface DepositParams {
|
|
18
|
+
amount: bigint;
|
|
19
|
+
}
|
|
20
|
+
export interface WithdrawParams {
|
|
21
|
+
amount: bigint;
|
|
22
|
+
}
|
|
23
|
+
export interface MarginUpdateEvent {
|
|
24
|
+
user: Address;
|
|
25
|
+
amount: bigint;
|
|
26
|
+
isDeposit: boolean;
|
|
27
|
+
timestamp: bigint;
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=margin.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"margin.d.ts","sourceRoot":"","sources":["../../src/types/margin.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAE/B,MAAM,WAAW,aAAa;IAC5B,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,CAAC;IACrB,gBAAgB,EAAE,MAAM,CAAC;IACzB,cAAc,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,iBAAiB;IAChC,aAAa,EAAE,MAAM,CAAC;IACtB,iBAAiB,EAAE,MAAM,CAAC;IAC1B,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,aAAa;IAC5B,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,cAAc;IAC7B,MAAM,EAAE,MAAM,CAAC;CAChB;AAED,MAAM,WAAW,iBAAiB;IAChC,IAAI,EAAE,OAAO,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,SAAS,EAAE,OAAO,CAAC;IACnB,SAAS,EAAE,MAAM,CAAC;CACnB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"margin.js","sourceRoot":"","sources":["../../src/types/margin.ts"],"names":[],"mappings":";AAAA;;GAEG"}
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Oracle-related type definitions
|
|
3
|
+
*/
|
|
4
|
+
import { Address } from 'viem';
|
|
5
|
+
export interface PriceData {
|
|
6
|
+
asset: Address;
|
|
7
|
+
price: bigint;
|
|
8
|
+
timestamp: bigint;
|
|
9
|
+
confidence?: bigint;
|
|
10
|
+
}
|
|
11
|
+
export interface OracleConfig {
|
|
12
|
+
heartbeat: bigint;
|
|
13
|
+
deviation: bigint;
|
|
14
|
+
decimals: number;
|
|
15
|
+
}
|
|
16
|
+
export interface PriceFeed {
|
|
17
|
+
asset: Address;
|
|
18
|
+
oracle: Address;
|
|
19
|
+
isActive: boolean;
|
|
20
|
+
}
|
|
21
|
+
//# sourceMappingURL=oracle.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oracle.d.ts","sourceRoot":"","sources":["../../src/types/oracle.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAE/B,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,OAAO,CAAC;IACf,KAAK,EAAE,MAAM,CAAC;IACd,SAAS,EAAE,MAAM,CAAC;IAClB,UAAU,CAAC,EAAE,MAAM,CAAC;CACrB;AAED,MAAM,WAAW,YAAY;IAC3B,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,SAAS;IACxB,KAAK,EAAE,OAAO,CAAC;IACf,MAAM,EAAE,OAAO,CAAC;IAChB,QAAQ,EAAE,OAAO,CAAC;CACnB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"oracle.js","sourceRoot":"","sources":["../../src/types/oracle.ts"],"names":[],"mappings":";AAAA;;GAEG"}
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Position-related type definitions
|
|
3
|
+
*/
|
|
4
|
+
import { Address } from 'viem';
|
|
5
|
+
import type { Bytes16 } from './alpha';
|
|
6
|
+
export interface Position {
|
|
7
|
+
id: Bytes16;
|
|
8
|
+
trader: Address;
|
|
9
|
+
asset: Address;
|
|
10
|
+
isLong: boolean;
|
|
11
|
+
notionalValue: bigint;
|
|
12
|
+
entryPrice: bigint;
|
|
13
|
+
margin: bigint;
|
|
14
|
+
lastFundingIndex: bigint;
|
|
15
|
+
timestamp: bigint;
|
|
16
|
+
isActive: boolean;
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Parameters for opening a new position (margin-based approach)
|
|
20
|
+
* Users specify margin amount and desired leverage
|
|
21
|
+
*/
|
|
22
|
+
export interface PositionParams {
|
|
23
|
+
asset: Address;
|
|
24
|
+
isLong: boolean;
|
|
25
|
+
margin: bigint;
|
|
26
|
+
leverage: bigint;
|
|
27
|
+
}
|
|
28
|
+
export interface ClosePositionParams {
|
|
29
|
+
positionId: Bytes16;
|
|
30
|
+
notionalValue?: bigint;
|
|
31
|
+
}
|
|
32
|
+
export interface PositionInfo {
|
|
33
|
+
position: Position;
|
|
34
|
+
unrealizedPnL: bigint;
|
|
35
|
+
marginRatio: bigint;
|
|
36
|
+
liquidationPrice: bigint;
|
|
37
|
+
}
|
|
38
|
+
export declare enum PositionStatus {
|
|
39
|
+
ACTIVE = "ACTIVE",
|
|
40
|
+
CLOSED = "CLOSED",
|
|
41
|
+
LIQUIDATED = "LIQUIDATED"
|
|
42
|
+
}
|
|
43
|
+
//# sourceMappingURL=positions.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"positions.d.ts","sourceRoot":"","sources":["../../src/types/positions.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO,EAAE,OAAO,EAAE,MAAM,MAAM,CAAC;AAC/B,OAAO,KAAK,EAAE,OAAO,EAAE,MAAM,SAAS,CAAC;AAEvC,MAAM,WAAW,QAAQ;IACvB,EAAE,EAAE,OAAO,CAAC;IACZ,MAAM,EAAE,OAAO,CAAC;IAChB,KAAK,EAAE,OAAO,CAAC;IACf,MAAM,EAAE,OAAO,CAAC;IAChB,aAAa,EAAE,MAAM,CAAC;IACtB,UAAU,EAAE,MAAM,CAAC;IACnB,MAAM,EAAE,MAAM,CAAC;IACf,gBAAgB,EAAE,MAAM,CAAC;IACzB,SAAS,EAAE,MAAM,CAAC;IAClB,QAAQ,EAAE,OAAO,CAAC;CACnB;AAED;;;GAGG;AACH,MAAM,WAAW,cAAc;IAC7B,KAAK,EAAE,OAAO,CAAC;IACf,MAAM,EAAE,OAAO,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,EAAE,MAAM,CAAC;CAClB;AAED,MAAM,WAAW,mBAAmB;IAClC,UAAU,EAAE,OAAO,CAAC;IACpB,aAAa,CAAC,EAAE,MAAM,CAAC;CACxB;AAED,MAAM,WAAW,YAAY;IAC3B,QAAQ,EAAE,QAAQ,CAAC;IACnB,aAAa,EAAE,MAAM,CAAC;IACtB,WAAW,EAAE,MAAM,CAAC;IACpB,gBAAgB,EAAE,MAAM,CAAC;CAC1B;AAED,oBAAY,cAAc;IACxB,MAAM,WAAW;IACjB,MAAM,WAAW;IACjB,UAAU,eAAe;CAC1B"}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Position-related type definitions
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.PositionStatus = void 0;
|
|
7
|
+
var PositionStatus;
|
|
8
|
+
(function (PositionStatus) {
|
|
9
|
+
PositionStatus["ACTIVE"] = "ACTIVE";
|
|
10
|
+
PositionStatus["CLOSED"] = "CLOSED";
|
|
11
|
+
PositionStatus["LIQUIDATED"] = "LIQUIDATED";
|
|
12
|
+
})(PositionStatus || (exports.PositionStatus = PositionStatus = {}));
|
|
13
|
+
//# sourceMappingURL=positions.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"positions.js","sourceRoot":"","sources":["../../src/types/positions.ts"],"names":[],"mappings":";AAAA;;GAEG;;;AAyCH,IAAY,cAIX;AAJD,WAAY,cAAc;IACxB,mCAAiB,CAAA;IACjB,mCAAiB,CAAA;IACjB,2CAAyB,CAAA;AAC3B,CAAC,EAJW,cAAc,8BAAd,cAAc,QAIzB"}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Calculation utilities for Alpha Futures
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Calculate initial margin required for a position
|
|
6
|
+
* @param positionSize - The size of the position
|
|
7
|
+
* @returns The initial margin required (33.33% for 3x leverage)
|
|
8
|
+
*/
|
|
9
|
+
export declare function calculateInitialMargin(positionSize: bigint): bigint;
|
|
10
|
+
/**
|
|
11
|
+
* Calculate maintenance margin required for a position
|
|
12
|
+
* @param positionSize - The size of the position
|
|
13
|
+
* @returns The maintenance margin required (20%)
|
|
14
|
+
*/
|
|
15
|
+
export declare function calculateMaintenanceMargin(positionSize: bigint): bigint;
|
|
16
|
+
/**
|
|
17
|
+
* Calculate leverage from position size and margin
|
|
18
|
+
* @param positionSize - The size of the position
|
|
19
|
+
* @param margin - The margin amount
|
|
20
|
+
* @returns The leverage as a bigint
|
|
21
|
+
*/
|
|
22
|
+
export declare function calculateLeverage(positionSize: bigint, margin: bigint): bigint;
|
|
23
|
+
/**
|
|
24
|
+
* Calculate position size from margin and leverage
|
|
25
|
+
* @param margin - The margin amount
|
|
26
|
+
* @param leverage - The leverage to use
|
|
27
|
+
* @returns The position size
|
|
28
|
+
*/
|
|
29
|
+
export declare function calculatePositionSize(margin: bigint, leverage: bigint): bigint;
|
|
30
|
+
/**
|
|
31
|
+
* Calculate unrealized PnL for a position
|
|
32
|
+
* @param isLong - Whether the position is long
|
|
33
|
+
* @param size - The position size
|
|
34
|
+
* @param entryPrice - The entry price
|
|
35
|
+
* @param currentPrice - The current price
|
|
36
|
+
* @returns The unrealized PnL (positive for profit, negative for loss)
|
|
37
|
+
*/
|
|
38
|
+
export declare function calculateUnrealizedPnL(isLong: boolean, size: bigint, entryPrice: bigint, currentPrice: bigint): bigint;
|
|
39
|
+
/**
|
|
40
|
+
* Calculate margin ratio in basis points
|
|
41
|
+
* @param margin - The current margin
|
|
42
|
+
* @param unrealizedPnL - The unrealized PnL
|
|
43
|
+
* @param positionSize - The position size
|
|
44
|
+
* @returns The margin ratio in basis points
|
|
45
|
+
*/
|
|
46
|
+
export declare function calculateMarginRatio(margin: bigint, unrealizedPnL: bigint, positionSize: bigint): bigint;
|
|
47
|
+
/**
|
|
48
|
+
* Calculate liquidation price for a position
|
|
49
|
+
* @param isLong - Whether the position is long
|
|
50
|
+
* @param entryPrice - The entry price
|
|
51
|
+
* @param margin - The margin amount
|
|
52
|
+
* @param positionSize - The position size
|
|
53
|
+
* @returns The liquidation price
|
|
54
|
+
*/
|
|
55
|
+
export declare function calculateLiquidationPrice(isLong: boolean, entryPrice: bigint, margin: bigint, positionSize: bigint): bigint;
|
|
56
|
+
/**
|
|
57
|
+
* Calculate trading fee (0.1% of position size)
|
|
58
|
+
* @param positionSize - The position size
|
|
59
|
+
* @returns The trading fee
|
|
60
|
+
*/
|
|
61
|
+
export declare function calculateTradingFee(positionSize: bigint): bigint;
|
|
62
|
+
/**
|
|
63
|
+
* Calculate liquidation bonus (5% of remaining margin)
|
|
64
|
+
* @param remainingMargin - The remaining margin after liquidation
|
|
65
|
+
* @returns The liquidation bonus
|
|
66
|
+
*/
|
|
67
|
+
export declare function calculateLiquidationBonus(remainingMargin: bigint): bigint;
|
|
68
|
+
/**
|
|
69
|
+
* Calculate funding payment
|
|
70
|
+
* @param positionSize - The position size
|
|
71
|
+
* @param fundingRate - The funding rate in basis points
|
|
72
|
+
* @param isLong - Whether the position is long
|
|
73
|
+
* @returns The funding payment (positive means payment required, negative means payment received)
|
|
74
|
+
*/
|
|
75
|
+
export declare function calculateFundingPayment(positionSize: bigint, fundingRate: bigint, isLong: boolean): bigint;
|
|
76
|
+
/**
|
|
77
|
+
* Calculate price impact in basis points
|
|
78
|
+
* @param orderSize - The size of the order
|
|
79
|
+
* @param liquidity - The available liquidity
|
|
80
|
+
* @param impactMultiplier - The impact multiplier (default 100)
|
|
81
|
+
* @returns The price impact in basis points
|
|
82
|
+
*/
|
|
83
|
+
export declare function calculatePriceImpact(orderSize: bigint, liquidity: bigint, impactMultiplier?: bigint): bigint;
|
|
84
|
+
//# sourceMappingURL=calculations.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calculations.d.ts","sourceRoot":"","sources":["../../src/utils/calculations.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH;;;;GAIG;AACH,wBAAgB,sBAAsB,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAGnE;AAED;;;;GAIG;AACH,wBAAgB,0BAA0B,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAGvE;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,YAAY,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,GAAG,MAAM,CAG9E;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,GAAG,MAAM,CAE9E;AAED;;;;;;;GAOG;AACH,wBAAgB,sBAAsB,CACpC,MAAM,EAAE,OAAO,EACf,IAAI,EAAE,MAAM,EACZ,UAAU,EAAE,MAAM,EAClB,YAAY,EAAE,MAAM,GACnB,MAAM,CAOR;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAClC,MAAM,EAAE,MAAM,EACd,aAAa,EAAE,MAAM,EACrB,YAAY,EAAE,MAAM,GACnB,MAAM,CAKR;AAED;;;;;;;GAOG;AACH,wBAAgB,yBAAyB,CACvC,MAAM,EAAE,OAAO,EACf,UAAU,EAAE,MAAM,EAClB,MAAM,EAAE,MAAM,EACd,YAAY,EAAE,MAAM,GACnB,MAAM,CAcR;AAED;;;;GAIG;AACH,wBAAgB,mBAAmB,CAAC,YAAY,EAAE,MAAM,GAAG,MAAM,CAEhE;AAED;;;;GAIG;AACH,wBAAgB,yBAAyB,CAAC,eAAe,EAAE,MAAM,GAAG,MAAM,CAEzE;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CACrC,YAAY,EAAE,MAAM,EACpB,WAAW,EAAE,MAAM,EACnB,MAAM,EAAE,OAAO,GACd,MAAM,CAUR;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAClC,SAAS,EAAE,MAAM,EACjB,SAAS,EAAE,MAAM,EACjB,gBAAgB,GAAE,MAAa,GAC9B,MAAM,CAMR"}
|
|
@@ -0,0 +1,155 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Calculation utilities for Alpha Futures
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.calculateInitialMargin = calculateInitialMargin;
|
|
7
|
+
exports.calculateMaintenanceMargin = calculateMaintenanceMargin;
|
|
8
|
+
exports.calculateLeverage = calculateLeverage;
|
|
9
|
+
exports.calculatePositionSize = calculatePositionSize;
|
|
10
|
+
exports.calculateUnrealizedPnL = calculateUnrealizedPnL;
|
|
11
|
+
exports.calculateMarginRatio = calculateMarginRatio;
|
|
12
|
+
exports.calculateLiquidationPrice = calculateLiquidationPrice;
|
|
13
|
+
exports.calculateTradingFee = calculateTradingFee;
|
|
14
|
+
exports.calculateLiquidationBonus = calculateLiquidationBonus;
|
|
15
|
+
exports.calculateFundingPayment = calculateFundingPayment;
|
|
16
|
+
exports.calculatePriceImpact = calculatePriceImpact;
|
|
17
|
+
const constants_1 = require("../constants");
|
|
18
|
+
/**
|
|
19
|
+
* Calculate initial margin required for a position
|
|
20
|
+
* @param positionSize - The size of the position
|
|
21
|
+
* @returns The initial margin required (33.33% for 3x leverage)
|
|
22
|
+
*/
|
|
23
|
+
function calculateInitialMargin(positionSize) {
|
|
24
|
+
// For 3x leverage, initial margin is 33.33%
|
|
25
|
+
return (positionSize * 3333n) / 10000n;
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Calculate maintenance margin required for a position
|
|
29
|
+
* @param positionSize - The size of the position
|
|
30
|
+
* @returns The maintenance margin required (20%)
|
|
31
|
+
*/
|
|
32
|
+
function calculateMaintenanceMargin(positionSize) {
|
|
33
|
+
// Maintenance margin is 20%
|
|
34
|
+
return (positionSize * 2000n) / 10000n;
|
|
35
|
+
}
|
|
36
|
+
/**
|
|
37
|
+
* Calculate leverage from position size and margin
|
|
38
|
+
* @param positionSize - The size of the position
|
|
39
|
+
* @param margin - The margin amount
|
|
40
|
+
* @returns The leverage as a bigint
|
|
41
|
+
*/
|
|
42
|
+
function calculateLeverage(positionSize, margin) {
|
|
43
|
+
if (margin === 0n)
|
|
44
|
+
return 0n;
|
|
45
|
+
return positionSize / margin;
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Calculate position size from margin and leverage
|
|
49
|
+
* @param margin - The margin amount
|
|
50
|
+
* @param leverage - The leverage to use
|
|
51
|
+
* @returns The position size
|
|
52
|
+
*/
|
|
53
|
+
function calculatePositionSize(margin, leverage) {
|
|
54
|
+
return margin * leverage;
|
|
55
|
+
}
|
|
56
|
+
/**
|
|
57
|
+
* Calculate unrealized PnL for a position
|
|
58
|
+
* @param isLong - Whether the position is long
|
|
59
|
+
* @param size - The position size
|
|
60
|
+
* @param entryPrice - The entry price
|
|
61
|
+
* @param currentPrice - The current price
|
|
62
|
+
* @returns The unrealized PnL (positive for profit, negative for loss)
|
|
63
|
+
*/
|
|
64
|
+
function calculateUnrealizedPnL(isLong, size, entryPrice, currentPrice) {
|
|
65
|
+
if (entryPrice === 0n)
|
|
66
|
+
return 0n;
|
|
67
|
+
const priceDiff = currentPrice - entryPrice;
|
|
68
|
+
const pnl = (size * priceDiff) / entryPrice;
|
|
69
|
+
return isLong ? pnl : -pnl;
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Calculate margin ratio in basis points
|
|
73
|
+
* @param margin - The current margin
|
|
74
|
+
* @param unrealizedPnL - The unrealized PnL
|
|
75
|
+
* @param positionSize - The position size
|
|
76
|
+
* @returns The margin ratio in basis points
|
|
77
|
+
*/
|
|
78
|
+
function calculateMarginRatio(margin, unrealizedPnL, positionSize) {
|
|
79
|
+
if (positionSize === 0n)
|
|
80
|
+
return 0n;
|
|
81
|
+
const adjustedMargin = margin + unrealizedPnL;
|
|
82
|
+
return (adjustedMargin * 10000n) / positionSize;
|
|
83
|
+
}
|
|
84
|
+
/**
|
|
85
|
+
* Calculate liquidation price for a position
|
|
86
|
+
* @param isLong - Whether the position is long
|
|
87
|
+
* @param entryPrice - The entry price
|
|
88
|
+
* @param margin - The margin amount
|
|
89
|
+
* @param positionSize - The position size
|
|
90
|
+
* @returns The liquidation price
|
|
91
|
+
*/
|
|
92
|
+
function calculateLiquidationPrice(isLong, entryPrice, margin, positionSize) {
|
|
93
|
+
if (positionSize === 0n)
|
|
94
|
+
return 0n;
|
|
95
|
+
// Liquidation occurs at 20% margin ratio
|
|
96
|
+
const maintenanceMargin = calculateMaintenanceMargin(positionSize);
|
|
97
|
+
const marginDiff = margin - maintenanceMargin;
|
|
98
|
+
if (isLong) {
|
|
99
|
+
// Long position liquidates when price drops
|
|
100
|
+
return entryPrice - (marginDiff * entryPrice) / positionSize;
|
|
101
|
+
}
|
|
102
|
+
else {
|
|
103
|
+
// Short position liquidates when price rises
|
|
104
|
+
return entryPrice + (marginDiff * entryPrice) / positionSize;
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
/**
|
|
108
|
+
* Calculate trading fee (0.1% of position size)
|
|
109
|
+
* @param positionSize - The position size
|
|
110
|
+
* @returns The trading fee
|
|
111
|
+
*/
|
|
112
|
+
function calculateTradingFee(positionSize) {
|
|
113
|
+
return (positionSize * 10n) / 10000n; // 0.1%
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Calculate liquidation bonus (5% of remaining margin)
|
|
117
|
+
* @param remainingMargin - The remaining margin after liquidation
|
|
118
|
+
* @returns The liquidation bonus
|
|
119
|
+
*/
|
|
120
|
+
function calculateLiquidationBonus(remainingMargin) {
|
|
121
|
+
return (remainingMargin * 500n) / 10000n; // 5%
|
|
122
|
+
}
|
|
123
|
+
/**
|
|
124
|
+
* Calculate funding payment
|
|
125
|
+
* @param positionSize - The position size
|
|
126
|
+
* @param fundingRate - The funding rate in basis points
|
|
127
|
+
* @param isLong - Whether the position is long
|
|
128
|
+
* @returns The funding payment (positive means payment required, negative means payment received)
|
|
129
|
+
*/
|
|
130
|
+
function calculateFundingPayment(positionSize, fundingRate, isLong) {
|
|
131
|
+
const payment = (positionSize * fundingRate) / 10000n;
|
|
132
|
+
// If funding rate is positive, longs pay shorts
|
|
133
|
+
// If funding rate is negative, shorts pay longs
|
|
134
|
+
if (fundingRate > 0n) {
|
|
135
|
+
return isLong ? payment : -payment;
|
|
136
|
+
}
|
|
137
|
+
else {
|
|
138
|
+
return isLong ? payment : -payment;
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
/**
|
|
142
|
+
* Calculate price impact in basis points
|
|
143
|
+
* @param orderSize - The size of the order
|
|
144
|
+
* @param liquidity - The available liquidity
|
|
145
|
+
* @param impactMultiplier - The impact multiplier (default 100)
|
|
146
|
+
* @returns The price impact in basis points
|
|
147
|
+
*/
|
|
148
|
+
function calculatePriceImpact(orderSize, liquidity, impactMultiplier = 100n) {
|
|
149
|
+
if (liquidity === 0n)
|
|
150
|
+
return 0n;
|
|
151
|
+
// Price impact = (orderSize / liquidity) * BASIS_POINTS * impactMultiplier / 100
|
|
152
|
+
// The impactMultiplier is divided by 100 as it's expressed as a percentage (100 = 1x)
|
|
153
|
+
return (orderSize * constants_1.BASIS_POINTS * impactMultiplier) / (liquidity * 100n);
|
|
154
|
+
}
|
|
155
|
+
//# sourceMappingURL=calculations.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"calculations.js","sourceRoot":"","sources":["../../src/utils/calculations.ts"],"names":[],"mappings":";AAAA;;GAEG;;AASH,wDAGC;AAOD,gEAGC;AAQD,8CAGC;AAQD,sDAEC;AAUD,wDAYC;AASD,oDASC;AAUD,8DAmBC;AAOD,kDAEC;AAOD,8DAEC;AASD,0DAcC;AASD,oDAUC;AA1KD,4CAA4C;AAE5C;;;;GAIG;AACH,SAAgB,sBAAsB,CAAC,YAAoB;IACzD,4CAA4C;IAC5C,OAAO,CAAC,YAAY,GAAG,KAAK,CAAC,GAAG,MAAM,CAAC;AACzC,CAAC;AAED;;;;GAIG;AACH,SAAgB,0BAA0B,CAAC,YAAoB;IAC7D,4BAA4B;IAC5B,OAAO,CAAC,YAAY,GAAG,KAAK,CAAC,GAAG,MAAM,CAAC;AACzC,CAAC;AAED;;;;;GAKG;AACH,SAAgB,iBAAiB,CAAC,YAAoB,EAAE,MAAc;IACpE,IAAI,MAAM,KAAK,EAAE;QAAE,OAAO,EAAE,CAAC;IAC7B,OAAO,YAAY,GAAG,MAAM,CAAC;AAC/B,CAAC;AAED;;;;;GAKG;AACH,SAAgB,qBAAqB,CAAC,MAAc,EAAE,QAAgB;IACpE,OAAO,MAAM,GAAG,QAAQ,CAAC;AAC3B,CAAC;AAED;;;;;;;GAOG;AACH,SAAgB,sBAAsB,CACpC,MAAe,EACf,IAAY,EACZ,UAAkB,EAClB,YAAoB;IAEpB,IAAI,UAAU,KAAK,EAAE;QAAE,OAAO,EAAE,CAAC;IAEjC,MAAM,SAAS,GAAG,YAAY,GAAG,UAAU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ACH,SAAgB,uBAAuB,CACrC,YAAoB,EACpB,WAAmB,EACnB,MAAe;IAEf,MAAM,OAAO,GAAG,CAAC,YAAY,GAAG,WAAW,CAAC,GAAG,MAAM,CAAC;IAEtD,gDAAgD;IAChD,gDAAgD;IAChD,IAAI,WAAW,GAAG,EAAE,EAAE,CAAC;QACrB,OAAO,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IACrC,CAAC;SAAM,CAAC;QACN,OAAO,MAAM,CAAC,CAAC,CAAC,OAAO,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC;IACrC,CAAC;AACH,CAAC;AAED;;;;;;GAMG;AACH,SAAgB,oBAAoB,CAClC,SAAiB,EACjB,SAAiB,EACjB,mBAA2B,IAAI;IAE/B,IAAI,SAAS,KAAK,EAAE;QAAE,OAAO,EAAE,CAAC;IAEhC,iFAAiF;IACjF,sFAAsF;IACtF,OAAO,CAAC,SAAS,GAAG,wBAAY,GAAG,gBAAgB,CAAC,GAAG,CAAC,SAAS,GAAG,IAAI,CAAC,CAAC;AAC5E,CAAC"}
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Error handling utilities
|
|
3
|
+
*/
|
|
4
|
+
/**
|
|
5
|
+
* Parse transaction error and return user-friendly message
|
|
6
|
+
*/
|
|
7
|
+
export declare function parseTransactionError(error: unknown): string;
|
|
8
|
+
/**
|
|
9
|
+
* Check if error is network-related
|
|
10
|
+
*/
|
|
11
|
+
export declare function isNetworkError(error: unknown): boolean;
|
|
12
|
+
/**
|
|
13
|
+
* Extract error message from various error types
|
|
14
|
+
*/
|
|
15
|
+
export declare function getErrorMessage(error: unknown): string;
|
|
16
|
+
/**
|
|
17
|
+
* Check if error is retryable
|
|
18
|
+
*/
|
|
19
|
+
export declare function isRetryableError(error: unknown): boolean;
|
|
20
|
+
/**
|
|
21
|
+
* Format error for logging
|
|
22
|
+
*/
|
|
23
|
+
export declare function formatError(error: unknown): Record<string, unknown>;
|
|
24
|
+
//# sourceMappingURL=errors.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.d.ts","sourceRoot":"","sources":["../../src/utils/errors.ts"],"names":[],"mappings":"AAAA;;GAEG;AAIH;;GAEG;AACH,wBAAgB,qBAAqB,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAuC5D;AAED;;GAEG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAgBtD;AAED;;GAEG;AACH,wBAAgB,eAAe,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CActD;AAED;;GAEG;AACH,wBAAgB,gBAAgB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAkBxD;AAED;;GAEG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CA8BnE"}
|
|
@@ -0,0 +1,129 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* Error handling utilities
|
|
4
|
+
*/
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.parseTransactionError = parseTransactionError;
|
|
7
|
+
exports.isNetworkError = isNetworkError;
|
|
8
|
+
exports.getErrorMessage = getErrorMessage;
|
|
9
|
+
exports.isRetryableError = isRetryableError;
|
|
10
|
+
exports.formatError = formatError;
|
|
11
|
+
// Error handling utilities
|
|
12
|
+
/**
|
|
13
|
+
* Parse transaction error and return user-friendly message
|
|
14
|
+
*/
|
|
15
|
+
function parseTransactionError(error) {
|
|
16
|
+
const errorMessage = error?.message || error?.toString?.() || 'Unknown error';
|
|
17
|
+
// Check for revert reason
|
|
18
|
+
if (errorMessage.includes('execution reverted')) {
|
|
19
|
+
const match = errorMessage.match(/execution reverted: (.+?)(?:\n|$)/);
|
|
20
|
+
if (match) {
|
|
21
|
+
return match[1];
|
|
22
|
+
}
|
|
23
|
+
return 'Transaction execution reverted';
|
|
24
|
+
}
|
|
25
|
+
// Check for network errors
|
|
26
|
+
if (isNetworkError(error)) {
|
|
27
|
+
return 'Network error: Please check your connection and try again';
|
|
28
|
+
}
|
|
29
|
+
// Check for user rejection
|
|
30
|
+
if (errorMessage.includes('user rejected') || errorMessage.includes('User rejected')) {
|
|
31
|
+
return 'User rejected the transaction';
|
|
32
|
+
}
|
|
33
|
+
// Check for insufficient funds
|
|
34
|
+
if (errorMessage.includes('insufficient funds')) {
|
|
35
|
+
return 'Insufficient funds for transaction';
|
|
36
|
+
}
|
|
37
|
+
// Check for nonce errors
|
|
38
|
+
if (errorMessage.includes('nonce too low')) {
|
|
39
|
+
return 'Transaction nonce too low';
|
|
40
|
+
}
|
|
41
|
+
// Check for gas errors
|
|
42
|
+
if (errorMessage.includes('out of gas') || errorMessage.includes('gas required exceeds')) {
|
|
43
|
+
return 'Transaction ran out of gas';
|
|
44
|
+
}
|
|
45
|
+
// Return original message if no specific handling
|
|
46
|
+
return errorMessage;
|
|
47
|
+
}
|
|
48
|
+
/**
|
|
49
|
+
* Check if error is network-related
|
|
50
|
+
*/
|
|
51
|
+
function isNetworkError(error) {
|
|
52
|
+
const errorMessage = (error?.message || error?.toString?.() || '').toLowerCase();
|
|
53
|
+
const networkErrorPatterns = [
|
|
54
|
+
'network',
|
|
55
|
+
'timeout',
|
|
56
|
+
'connection',
|
|
57
|
+
'econnrefused',
|
|
58
|
+
'etimedout',
|
|
59
|
+
'enotfound',
|
|
60
|
+
'econnreset',
|
|
61
|
+
'socket hang up',
|
|
62
|
+
'fetch failed',
|
|
63
|
+
];
|
|
64
|
+
return networkErrorPatterns.some((pattern) => errorMessage.includes(pattern));
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Extract error message from various error types
|
|
68
|
+
*/
|
|
69
|
+
function getErrorMessage(error) {
|
|
70
|
+
if (error instanceof Error) {
|
|
71
|
+
return error.message;
|
|
72
|
+
}
|
|
73
|
+
if (typeof error === 'string') {
|
|
74
|
+
return error;
|
|
75
|
+
}
|
|
76
|
+
if (error && typeof error === 'object' && 'message' in error) {
|
|
77
|
+
return String(error.message);
|
|
78
|
+
}
|
|
79
|
+
return 'Unknown error';
|
|
80
|
+
}
|
|
81
|
+
/**
|
|
82
|
+
* Check if error is retryable
|
|
83
|
+
*/
|
|
84
|
+
function isRetryableError(error) {
|
|
85
|
+
// Network errors are usually retryable
|
|
86
|
+
if (isNetworkError(error)) {
|
|
87
|
+
return true;
|
|
88
|
+
}
|
|
89
|
+
const errorMessage = (error?.message || '').toLowerCase();
|
|
90
|
+
// Specific retryable conditions
|
|
91
|
+
const retryablePatterns = [
|
|
92
|
+
'nonce too low',
|
|
93
|
+
'replacement transaction underpriced',
|
|
94
|
+
'transaction underpriced',
|
|
95
|
+
'known transaction',
|
|
96
|
+
'already known',
|
|
97
|
+
];
|
|
98
|
+
return retryablePatterns.some((pattern) => errorMessage.includes(pattern));
|
|
99
|
+
}
|
|
100
|
+
/**
|
|
101
|
+
* Format error for logging
|
|
102
|
+
*/
|
|
103
|
+
function formatError(error) {
|
|
104
|
+
const formatted = {
|
|
105
|
+
message: getErrorMessage(error),
|
|
106
|
+
timestamp: new Date().toISOString(),
|
|
107
|
+
};
|
|
108
|
+
if (error instanceof Error) {
|
|
109
|
+
formatted.name = error.name;
|
|
110
|
+
formatted.stack = error.stack;
|
|
111
|
+
}
|
|
112
|
+
const errorObj = error;
|
|
113
|
+
if (errorObj?.code) {
|
|
114
|
+
formatted.code = errorObj.code;
|
|
115
|
+
}
|
|
116
|
+
if (errorObj?.reason) {
|
|
117
|
+
formatted.reason = errorObj.reason;
|
|
118
|
+
}
|
|
119
|
+
if (errorObj?.transaction) {
|
|
120
|
+
formatted.transaction = {
|
|
121
|
+
to: errorObj.transaction.to,
|
|
122
|
+
from: errorObj.transaction.from,
|
|
123
|
+
value: errorObj.transaction.value?.toString(),
|
|
124
|
+
data: errorObj.transaction.data?.slice(0, 100) + '...', // Truncate long data
|
|
125
|
+
};
|
|
126
|
+
}
|
|
127
|
+
return formatted;
|
|
128
|
+
}
|
|
129
|
+
//# sourceMappingURL=errors.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"errors.js","sourceRoot":"","sources":["../../src/utils/errors.ts"],"names":[],"mappings":";AAAA;;GAEG;;AAOH,sDAuCC;AAKD,wCAgBC;AAKD,0CAcC;AAKD,4CAkBC;AAKD,kCA8BC;AA9ID,2BAA2B;AAE3B;;GAEG;AACH,SAAgB,qBAAqB,CAAC,KAAc;IAClD,MAAM,YAAY,GAAI,KAAa,EAAE,OAAO,IAAI,KAAK,EAAE,QAAQ,EAAE,EAAE,IAAI,eAAe,CAAC;IAEvF,0BAA0B;IAC1B,IAAI,YAAY,CAAC,QAAQ,CAAC,oBAAoB,CAAC,EAAE,CAAC;QAChD,MAAM,KAAK,GAAG,YAAY,CAAC,KAAK,CAAC,mCAAmC,CAAC,CAAC;QACtE,IAAI,KAAK,EAAE,CAAC;YACV,OAAO,KAAK,CAAC,CAAC,CAAC,CAAC;QAClB,CAAC;QACD,OAAO,gCAAgC,CAAC;IAC1C,CAAC;IAED,2BAA2B;IAC3B,IAAI,cAAc,CAAC,KAAK,CAAC,EAAE,CAAC;QAC1B,OAAO,2DAA2D,CAAC;IACrE,CAAC;IAED,2BAA2B;IAC3B,IAAI,YAAY,CAAC,QAAQ,CAAC,eAAe,CAAC,IAAI,YAAY,CAAC,QAAQ,CAAC,eAAe,CAAC,EAAE,CAAC;QACrF,OAAO,+BAA+B,CAAC;IACzC,CAAC;IAED,+BAA+B;IAC/B,IAAI,YAAY,CAAC,QAAQ,CAAC,oBAAoB,CAAC,EAAE,CAAC;QAChD,OAAO,oCAAoC,CAAC;IAC9C,CAAC;IAED,yBAAyB;IACzB,IAAI,YAAY,CAAC,QAAQ,CAAC,eAAe,CAAC,EAAE,CAAC;QAC3C,OAAO,2BAA2B,CAAC;IACrC,CAAC;IAED,uBAAuB;IACvB,IAAI,YAAY,CAA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BAAqB;SAC9E,CAAC;IACJ,CAAC;IAED,OAAO,SAAS,CAAC;AACnB,CAAC"}
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Event parsing and filtering utilities for Viem
|
|
3
|
+
*/
|
|
4
|
+
import { Log, type Abi, type AbiEvent } from 'viem';
|
|
5
|
+
/**
|
|
6
|
+
* Decoded event log type
|
|
7
|
+
*/
|
|
8
|
+
export interface DecodedEventLog {
|
|
9
|
+
eventName: string;
|
|
10
|
+
args: Record<string, unknown> | readonly unknown[] | undefined;
|
|
11
|
+
address: string;
|
|
12
|
+
blockHash: string;
|
|
13
|
+
blockNumber: bigint;
|
|
14
|
+
data: string;
|
|
15
|
+
logIndex: number;
|
|
16
|
+
removed: boolean;
|
|
17
|
+
topics: readonly string[];
|
|
18
|
+
transactionHash: string;
|
|
19
|
+
transactionIndex: number;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Parse event logs using ABI
|
|
23
|
+
*/
|
|
24
|
+
export declare function parseEventLogs(logs: Log[], abi: Abi): DecodedEventLog[];
|
|
25
|
+
/**
|
|
26
|
+
* Filter events by name
|
|
27
|
+
*/
|
|
28
|
+
export declare function filterEventsByName(events: DecodedEventLog[], eventName: string): DecodedEventLog[];
|
|
29
|
+
/**
|
|
30
|
+
* Get event from ABI
|
|
31
|
+
*/
|
|
32
|
+
export declare function getEventFromAbi(abi: Abi, eventName: string): AbiEvent | undefined;
|
|
33
|
+
/**
|
|
34
|
+
* Create event filter topics
|
|
35
|
+
*/
|
|
36
|
+
export declare function createEventFilter(abi: Abi, eventName: string, _args?: Record<string, unknown>): {
|
|
37
|
+
address?: string;
|
|
38
|
+
topics?: string[];
|
|
39
|
+
};
|
|
40
|
+
//# sourceMappingURL=events.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"events.d.ts","sourceRoot":"","sources":["../../src/utils/events.ts"],"names":[],"mappings":"AAAA;;GAEG;AAEH,OAAO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