@metamask-previews/perps-controller 24.1.0-preview-2da314d6d → 24.1.0-preview-11e30b2
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/CHANGELOG.md +18 -0
- package/dist/PerpsController-method-action-types.d.ts +7 -5
- package/dist/PerpsController-method-action-types.d.ts.map +1 -1
- package/dist/PerpsController-method-action-types.js.map +1 -1
- package/dist/PerpsController.d.ts +7 -5
- package/dist/PerpsController.d.ts.map +1 -1
- package/dist/PerpsController.js +7 -5
- package/dist/PerpsController.js.map +1 -1
- package/dist/errors.d.ts +13 -2
- package/dist/errors.d.ts.map +1 -1
- package/dist/errors.js +2 -2
- package/dist/errors.js.map +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js.map +1 -1
- package/dist/perpsErrorCodes.d.ts +3 -0
- package/dist/perpsErrorCodes.d.ts.map +1 -1
- package/dist/perpsErrorCodes.js +4 -0
- package/dist/perpsErrorCodes.js.map +1 -1
- package/dist/providers/AggregatedPerpsProvider.d.ts.map +1 -1
- package/dist/providers/AggregatedPerpsProvider.js +6 -5
- package/dist/providers/AggregatedPerpsProvider.js.map +1 -1
- package/dist/providers/HyperLiquidProvider.d.ts.map +1 -1
- package/dist/providers/HyperLiquidProvider.js +121 -165
- package/dist/providers/HyperLiquidProvider.js.map +1 -1
- package/dist/services/TradingService.d.ts.map +1 -1
- package/dist/services/TradingService.js +19 -5
- package/dist/services/TradingService.js.map +1 -1
- package/dist/types/index.d.ts +36 -1
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js.map +1 -1
- package/dist/utils/errorContext.d.ts +33 -0
- package/dist/utils/errorContext.d.ts.map +1 -1
- package/dist/utils/errorContext.js +41 -0
- package/dist/utils/errorContext.js.map +1 -1
- package/dist/utils/errorUtils.d.ts +18 -0
- package/dist/utils/errorUtils.d.ts.map +1 -1
- package/dist/utils/errorUtils.js +133 -1
- package/dist/utils/errorUtils.js.map +1 -1
- package/dist/utils/hyperLiquidValidation.d.ts +9 -2
- package/dist/utils/hyperLiquidValidation.d.ts.map +1 -1
- package/dist/utils/hyperLiquidValidation.js +18 -17
- package/dist/utils/hyperLiquidValidation.js.map +1 -1
- package/docs/architecture.md +4 -2
- package/package.json +1 -1
|
@@ -50,6 +50,39 @@ export declare function markProviderErrorReported<Result extends object>(result:
|
|
|
50
50
|
* @returns Whether the provider owns reporting for the failure.
|
|
51
51
|
*/
|
|
52
52
|
export declare function wasProviderErrorReported(result: object): boolean;
|
|
53
|
+
declare const PROVIDER_ERROR_CAUSE: unique symbol;
|
|
54
|
+
type ProviderErrorCause = {
|
|
55
|
+
[PROVIDER_ERROR_CAUSE]?: Error;
|
|
56
|
+
};
|
|
57
|
+
/**
|
|
58
|
+
* Attach the normalized error behind a failed provider result.
|
|
59
|
+
*
|
|
60
|
+
* Results carry only stable codes, so a service that owns reporting would
|
|
61
|
+
* otherwise rebuild an error from `result.error` and lose the original
|
|
62
|
+
* failure. The property is non-enumerable so the result's observable shape is
|
|
63
|
+
* unchanged; a wrapper that spreads a result must carry it over with
|
|
64
|
+
* `carryProviderErrorCause`.
|
|
65
|
+
*
|
|
66
|
+
* @param result - Provider result to annotate.
|
|
67
|
+
* @param error - Normalized error whose `cause` holds the original failure.
|
|
68
|
+
* @returns The same result with internal reporting metadata.
|
|
69
|
+
*/
|
|
70
|
+
export declare function attachProviderErrorCause<Result extends object>(result: Result, error: Error): Result & ProviderErrorCause;
|
|
71
|
+
/**
|
|
72
|
+
* Copy an attached provider error from one result onto a derived result.
|
|
73
|
+
*
|
|
74
|
+
* @param source - Result that may carry an attached error.
|
|
75
|
+
* @param target - Result derived from `source`, typically by spreading it.
|
|
76
|
+
* @returns `target`, annotated with the source's error when it had one.
|
|
77
|
+
*/
|
|
78
|
+
export declare function carryProviderErrorCause<Result extends object>(source: object, target: Result): Result;
|
|
79
|
+
/**
|
|
80
|
+
* Read the normalized error a provider attached to a failed result.
|
|
81
|
+
*
|
|
82
|
+
* @param result - Provider result returned to a service.
|
|
83
|
+
* @returns The attached error, or undefined when the provider attached none.
|
|
84
|
+
*/
|
|
85
|
+
export declare function getProviderErrorCause(result: object): Error | undefined;
|
|
53
86
|
/**
|
|
54
87
|
* Build a Perps logger payload with bounded, searchable Sentry tags.
|
|
55
88
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errorContext.d.ts","sourceRoot":"","sources":["../../src/utils/errorContext.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,mBAAmB,CAAC;AAErD,eAAO,MAAM,qBAAqB;aAChC,oBAAoB,EAAE,uBAAuB;aAC7C,eAAe,EAAE,kBAAkB;aACnC,kBAAkB,EAAE,qBAAqB;aACzC,mBAAmB,EAAE,sBAAsB;CACnC,CAAC;AAEX,MAAM,MAAM,mBAAmB,GAC7B,CAAC,OAAO,qBAAqB,CAAC,CAAC,MAAM,OAAO,qBAAqB,CAAC,CAAC;AAErE,eAAO,MAAM,kBAAkB;aAC7B,oBAAoB,EAAE,uBAAuB;aAC7C,gBAAgB,EAAE,mBAAmB;aACrC,mBAAmB,EAAE,sBAAsB;aAC3C,UAAU,EAAE,aAAa;aACzB,SAAS,EAAE,YAAY;aACvB,WAAW,EAAE,cAAc;aAC3B,aAAa,EAAE,gBAAgB;aAC/B,kBAAkB,EAAE,sBAAsB;aAC1C,YAAY,EAAE,eAAe;aAC7B,YAAY,EAAE,eAAe;CACrB,CAAC;AAEX,MAAM,MAAM,gBAAgB,GAC1B,CAAC,OAAO,kBAAkB,CAAC,CAAC,MAAM,OAAO,kBAAkB,CAAC,CAAC;AAE/D,eAAO,MAAM,qBAAqB;aAChC,iBAAiB,EAAE,wBAAwB;CACnC,CAAC;AAEX,MAAM,MAAM,mBAAmB,GAC7B,CAAC,OAAO,qBAAqB,CAAC,CAAC,MAAM,OAAO,qBAAqB,CAAC,CAAC;AAErE,MAAM,MAAM,cAAc,GAAG;IAC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M,EAAE,MAAM,GAAG,OAAO,CAEhE;AAED,QAAA,MAAM,oBAAoB,eAA+B,CAAC;AAE1D,KAAK,kBAAkB,GAAG;IACxB,CAAC,oBAAoB,CAAC,CAAC,EAAE,KAAK,CAAC;CAChC,CAAC;AAEF;;;;;;;;;;;;GAYG;AACH,wBAAgB,wBAAwB,CAAC,MAAM,SAAS,MAAM,EAC5D,MAAM,EAAE,MAAM,EACd,KAAK,EAAE,KAAK,GACX,MAAM,GAAG,kBAAkB,CAM7B;AAED;;;;;;GAMG;AACH,wBAAgB,uBAAuB,CAAC,MAAM,SAAS,MAAM,EAC3D,MAAM,EAAE,MAAM,EACd,MAAM,EAAE,MAAM,GACb,MAAM,CAGR;AAED;;;;;GAKG;AACH,wBAAgB,qBAAqB,CAAC,MAAM,EAAE,MAAM,GAAG,KAAK,GAAG,SAAS,CAEvE;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,uBAAuB,CAAC,OAAO,EAAE;IAC/C,WAAW,EAAE,MAAM,CAAC;IACpB,MAAM,EAAE,MAAM,CAAC;IACf,QAAQ,CAAC,EAAE,MAAM,CAAC;IAClB,OAAO,CAAC,EAAE,SAAS,GAAG,SAAS,CAAC;IAChC,SAAS,CAAC,EAAE,cAAc,CAAC;IAC3B,IAAI,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;CAChC,GAAG,kBAAkB,CAgCrB"}
|
|
@@ -42,6 +42,47 @@ export function markProviderErrorReported(result) {
|
|
|
42
42
|
export function wasProviderErrorReported(result) {
|
|
43
43
|
return result[PROVIDER_ERROR_REPORTED] === true;
|
|
44
44
|
}
|
|
45
|
+
const PROVIDER_ERROR_CAUSE = Symbol('providerErrorCause');
|
|
46
|
+
/**
|
|
47
|
+
* Attach the normalized error behind a failed provider result.
|
|
48
|
+
*
|
|
49
|
+
* Results carry only stable codes, so a service that owns reporting would
|
|
50
|
+
* otherwise rebuild an error from `result.error` and lose the original
|
|
51
|
+
* failure. The property is non-enumerable so the result's observable shape is
|
|
52
|
+
* unchanged; a wrapper that spreads a result must carry it over with
|
|
53
|
+
* `carryProviderErrorCause`.
|
|
54
|
+
*
|
|
55
|
+
* @param result - Provider result to annotate.
|
|
56
|
+
* @param error - Normalized error whose `cause` holds the original failure.
|
|
57
|
+
* @returns The same result with internal reporting metadata.
|
|
58
|
+
*/
|
|
59
|
+
export function attachProviderErrorCause(result, error) {
|
|
60
|
+
return Object.defineProperty(result, PROVIDER_ERROR_CAUSE, {
|
|
61
|
+
value: error,
|
|
62
|
+
enumerable: false,
|
|
63
|
+
configurable: true,
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* Copy an attached provider error from one result onto a derived result.
|
|
68
|
+
*
|
|
69
|
+
* @param source - Result that may carry an attached error.
|
|
70
|
+
* @param target - Result derived from `source`, typically by spreading it.
|
|
71
|
+
* @returns `target`, annotated with the source's error when it had one.
|
|
72
|
+
*/
|
|
73
|
+
export function carryProviderErrorCause(source, target) {
|
|
74
|
+
const error = getProviderErrorCause(source);
|
|
75
|
+
return error ? attachProviderErrorCause(target, error) : target;
|
|
76
|
+
}
|
|
77
|
+
/**
|
|
78
|
+
* Read the normalized error a provider attached to a failed result.
|
|
79
|
+
*
|
|
80
|
+
* @param result - Provider result returned to a service.
|
|
81
|
+
* @returns The attached error, or undefined when the provider attached none.
|
|
82
|
+
*/
|
|
83
|
+
export function getProviderErrorCause(result) {
|
|
84
|
+
return result[PROVIDER_ERROR_CAUSE];
|
|
85
|
+
}
|
|
45
86
|
/**
|
|
46
87
|
* Build a Perps logger payload with bounded, searchable Sentry tags.
|
|
47
88
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errorContext.js","sourceRoot":"","sources":["../../src/utils/errorContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAG9D,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACnC,oBAAoB,E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I,OAAO,KAAK,SAAS,EAAE,CAAC;QAC1B,IAAI,CAAC,OAAO,GAAG,OAAO,CAAC;IACzB,CAAC;IACD,IAAI,SAAS,KAAK,SAAS,EAAE,CAAC;QAC5B,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC,SAAS,CAAC;QACrC,IAAI,SAAS,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;YACnC,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC;QACjC,CAAC;QACD,IAAI,SAAS,CAAC,SAAS,KAAK,SAAS,EAAE,CAAC;YACtC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC,SAAS,CAAC;QACvC,CAAC;IACH,CAAC;IAED,OAAO;QACL,IAAI;QACJ,OAAO,EAAE;YACP,IAAI,EAAE,WAAW;YACjB,IAAI,EAAE;gBACJ,MAAM;gBACN,GAAG,IAAI;aACR;SACF;KACF,CAAC;AACJ,CAAC","sourcesContent":["import { PERPS_CONSTANTS } from '../constants/perpsConfig.js';\nimport type { PerpsLogger } from '../types/index.js';\n\nexport const PERPS_ERROR_OPERATION = {\n ConnectionManagement: 'connection_management',\n OrderManagement: 'order_management',\n PositionManagement: 'position_management',\n FinancialOperations: 'financial_operations',\n} as const;\n\nexport type PerpsErrorOperation =\n (typeof PERPS_ERROR_OPERATION)[keyof typeof PERPS_ERROR_OPERATION];\n\nexport const PERPS_ERROR_ACTION = {\n ConnectionConnection: 'connection_connection',\n FinancialDeposit: 'financial_deposit',\n FinancialWithdrawal: 'financial_withdrawal',\n PlaceOrder: 'place_order',\n EditOrder: 'edit_order',\n CancelOrder: 'cancel_order',\n ClosePosition: 'close_position',\n PositionTpslUpdate: 'position_tpsl_update',\n UpdateMargin: 'update_margin',\n FlipPosition: 'flip_position',\n} as const;\n\nexport type PerpsErrorAction =\n (typeof PERPS_ERROR_ACTION)[keyof typeof PERPS_ERROR_ACTION];\n\nexport const PERPS_ERROR_COMPONENT = {\n ConnectionManager: 'PerpsConnectionManager',\n} as const;\n\nexport type PerpsErrorComponent =\n (typeof PERPS_ERROR_COMPONENT)[keyof typeof PERPS_ERROR_COMPONENT];\n\nexport type PerpsErrorTags = {\n operation: PerpsErrorOperation;\n action?: PerpsErrorAction;\n component?: PerpsErrorComponent;\n};\n\nexport type PerpsLoggerOptions = NonNullable<\n Parameters<PerpsLogger['error']>[1]\n>;\n\nconst PROVIDER_ERROR_REPORTED = Symbol('providerErrorReported');\n\ntype ProviderErrorReported = {\n [PROVIDER_ERROR_REPORTED]?: true;\n};\n\n/**\n * Mark a failed provider result whose underlying error was already reported.\n *\n * The symbol remains internal to Core and survives object spreads used by the\n * aggregated provider without adding a consumer-facing result field.\n *\n * @param result - Provider result to mark.\n * @returns The same result with internal reporting metadata.\n */\nexport function markProviderErrorReported<Result extends object>(\n result: Result,\n): Result & ProviderErrorReported {\n return Object.assign(result, { [PROVIDER_ERROR_REPORTED]: true as const });\n}\n\n/**\n * Check whether a provider already reported the error represented by a result.\n *\n * @param result - Provider result returned to a service.\n * @returns Whether the provider owns reporting for the failure.\n */\nexport function wasProviderErrorReported(result: object): boolean {\n return (result as ProviderErrorReported)[PROVIDER_ERROR_REPORTED] === true;\n}\n\n/**\n * Build a Perps logger payload with bounded, searchable Sentry tags.\n *\n * Diagnostic data is deliberately kept separate from tags so identifiers,\n * symbols, messages, and method names cannot accidentally create unbounded\n * tag cardinality.\n *\n * @param options - Error context options.\n * @param options.contextName - Stable name for the diagnostic context.\n * @param options.method - Method or boundary where the error occurred.\n * @param options.provider - Optional provider identifier.\n * @param options.network - Optional network environment.\n * @param options.errorTags - Optional bounded dashboard tags.\n * @param options.data - Optional diagnostic data retained in context.\n * @returns Logger options accepted by the platform-provided Perps logger.\n */\nexport function createPerpsErrorContext(options: {\n contextName: string;\n method: string;\n provider?: string;\n network?: 'mainnet' | 'testnet';\n errorTags?: PerpsErrorTags;\n data?: Record<string, unknown>;\n}): PerpsLoggerOptions {\n const { contextName, method, provider, network, errorTags, data } = options;\n const tags: Record<string, string | number> = {\n feature: PERPS_CONSTANTS.FeatureName,\n };\n\n if (provider !== undefined) {\n tags.provider = provider;\n }\n if (network !== undefined) {\n tags.network = network;\n }\n if (errorTags !== undefined) {\n tags.operation = errorTags.operation;\n if (errorTags.action !== undefined) {\n tags.action = errorTags.action;\n }\n if (errorTags.component !== undefined) {\n tags.component = errorTags.component;\n }\n }\n\n return {\n tags,\n context: {\n name: contextName,\n data: {\n method,\n ...data,\n },\n },\n };\n}\n"]}
|
|
1
|
+
{"version":3,"file":"errorContext.js","sourceRoot":"","sources":["../../src/utils/errorContext.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,6BAA6B,CAAC;AAG9D,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACnC,oBAAoB,EAAE,uBAAuB;IAC7C,eAAe,EAAE,kBAAkB;IACnC,kBAAkB,EAAE,qBAAqB;IACzC,mBAAmB,EAAE,sBAAsB;CACnC,CAAC;AAKX,MAAM,CAAC,MAAM,kBAAkB,GAAG;IAChC,oBAAoB,EAAE,uBAAuB;IAC7C,gBAAgB,EAAE,mBAAmB;IACrC,mBAAmB,EAAE,sBAAsB;IAC3C,UAAU,EAAE,aAAa;IACzB,SAAS,EAAE,YAAY;IACvB,WAAW,EAAE,cAAc;IAC3B,aAAa,EAAE,gBAAgB;IAC/B,kBAAkB,EAAE,sBAAsB;IAC1C,YAAY,EAAE,eAAe;IAC7B,YAAY,EAAE,eAAe;CACrB,CAAC;AAKX,MAAM,CAAC,MAAM,qBAAqB,GAAG;IACnC,iBAAiB,EAAE,wBAAwB;CACnC,CAAC;AAeX,MAAM,uBAAuB,GAAG,MAAM,CAAC,uBAAuB,CAAC,CAAC;AAMhE;;;;;;;;GAQG;AACH,MAAM,UAAU,yBAAyB,CACvC,MAAc;IAEd,OAAO,MAAM,CAAC,MAAM,CAAC,MAAM,EAAE,EAAE,CAAC,uBAAuB,CAAC,EAAE,IAAa,EAAE,CAAC,CAAC;AAC7E,CAAC;AAED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AAS,KAAK,SAAS,EAAE,CAAC;QAC5B,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC,SAAS,CAAC;QACrC,IAAI,SAAS,CAAC,MAAM,KAAK,SAAS,EAAE,CAAC;YACnC,IAAI,CAAC,MAAM,GAAG,SAAS,CAAC,MAAM,CAAC;QACjC,CAAC;QACD,IAAI,SAAS,CAAC,SAAS,KAAK,SAAS,EAAE,CAAC;YACtC,IAAI,CAAC,SAAS,GAAG,SAAS,CAAC,SAAS,CAAC;QACvC,CAAC;IACH,CAAC;IAED,OAAO;QACL,IAAI;QACJ,OAAO,EAAE;YACP,IAAI,EAAE,WAAW;YACjB,IAAI,EAAE;gBACJ,MAAM;gBACN,GAAG,IAAI;aACR;SACF;KACF,CAAC;AACJ,CAAC","sourcesContent":["import { PERPS_CONSTANTS } from '../constants/perpsConfig.js';\nimport type { PerpsLogger } from '../types/index.js';\n\nexport const PERPS_ERROR_OPERATION = {\n ConnectionManagement: 'connection_management',\n OrderManagement: 'order_management',\n PositionManagement: 'position_management',\n FinancialOperations: 'financial_operations',\n} as const;\n\nexport type PerpsErrorOperation =\n (typeof PERPS_ERROR_OPERATION)[keyof typeof PERPS_ERROR_OPERATION];\n\nexport const PERPS_ERROR_ACTION = {\n ConnectionConnection: 'connection_connection',\n FinancialDeposit: 'financial_deposit',\n FinancialWithdrawal: 'financial_withdrawal',\n PlaceOrder: 'place_order',\n EditOrder: 'edit_order',\n CancelOrder: 'cancel_order',\n ClosePosition: 'close_position',\n PositionTpslUpdate: 'position_tpsl_update',\n UpdateMargin: 'update_margin',\n FlipPosition: 'flip_position',\n} as const;\n\nexport type PerpsErrorAction =\n (typeof PERPS_ERROR_ACTION)[keyof typeof PERPS_ERROR_ACTION];\n\nexport const PERPS_ERROR_COMPONENT = {\n ConnectionManager: 'PerpsConnectionManager',\n} as const;\n\nexport type PerpsErrorComponent =\n (typeof PERPS_ERROR_COMPONENT)[keyof typeof PERPS_ERROR_COMPONENT];\n\nexport type PerpsErrorTags = {\n operation: PerpsErrorOperation;\n action?: PerpsErrorAction;\n component?: PerpsErrorComponent;\n};\n\nexport type PerpsLoggerOptions = NonNullable<\n Parameters<PerpsLogger['error']>[1]\n>;\n\nconst PROVIDER_ERROR_REPORTED = Symbol('providerErrorReported');\n\ntype ProviderErrorReported = {\n [PROVIDER_ERROR_REPORTED]?: true;\n};\n\n/**\n * Mark a failed provider result whose underlying error was already reported.\n *\n * The symbol remains internal to Core and survives object spreads used by the\n * aggregated provider without adding a consumer-facing result field.\n *\n * @param result - Provider result to mark.\n * @returns The same result with internal reporting metadata.\n */\nexport function markProviderErrorReported<Result extends object>(\n result: Result,\n): Result & ProviderErrorReported {\n return Object.assign(result, { [PROVIDER_ERROR_REPORTED]: true as const });\n}\n\n/**\n * Check whether a provider already reported the error represented by a result.\n *\n * @param result - Provider result returned to a service.\n * @returns Whether the provider owns reporting for the failure.\n */\nexport function wasProviderErrorReported(result: object): boolean {\n return (result as ProviderErrorReported)[PROVIDER_ERROR_REPORTED] === true;\n}\n\nconst PROVIDER_ERROR_CAUSE = Symbol('providerErrorCause');\n\ntype ProviderErrorCause = {\n [PROVIDER_ERROR_CAUSE]?: Error;\n};\n\n/**\n * Attach the normalized error behind a failed provider result.\n *\n * Results carry only stable codes, so a service that owns reporting would\n * otherwise rebuild an error from `result.error` and lose the original\n * failure. The property is non-enumerable so the result's observable shape is\n * unchanged; a wrapper that spreads a result must carry it over with\n * `carryProviderErrorCause`.\n *\n * @param result - Provider result to annotate.\n * @param error - Normalized error whose `cause` holds the original failure.\n * @returns The same result with internal reporting metadata.\n */\nexport function attachProviderErrorCause<Result extends object>(\n result: Result,\n error: Error,\n): Result & ProviderErrorCause {\n return Object.defineProperty(result, PROVIDER_ERROR_CAUSE, {\n value: error,\n enumerable: false,\n configurable: true,\n });\n}\n\n/**\n * Copy an attached provider error from one result onto a derived result.\n *\n * @param source - Result that may carry an attached error.\n * @param target - Result derived from `source`, typically by spreading it.\n * @returns `target`, annotated with the source's error when it had one.\n */\nexport function carryProviderErrorCause<Result extends object>(\n source: object,\n target: Result,\n): Result {\n const error = getProviderErrorCause(source);\n return error ? attachProviderErrorCause(target, error) : target;\n}\n\n/**\n * Read the normalized error a provider attached to a failed result.\n *\n * @param result - Provider result returned to a service.\n * @returns The attached error, or undefined when the provider attached none.\n */\nexport function getProviderErrorCause(result: object): Error | undefined {\n return (result as ProviderErrorCause)[PROVIDER_ERROR_CAUSE];\n}\n\n/**\n * Build a Perps logger payload with bounded, searchable Sentry tags.\n *\n * Diagnostic data is deliberately kept separate from tags so identifiers,\n * symbols, messages, and method names cannot accidentally create unbounded\n * tag cardinality.\n *\n * @param options - Error context options.\n * @param options.contextName - Stable name for the diagnostic context.\n * @param options.method - Method or boundary where the error occurred.\n * @param options.provider - Optional provider identifier.\n * @param options.network - Optional network environment.\n * @param options.errorTags - Optional bounded dashboard tags.\n * @param options.data - Optional diagnostic data retained in context.\n * @returns Logger options accepted by the platform-provided Perps logger.\n */\nexport function createPerpsErrorContext(options: {\n contextName: string;\n method: string;\n provider?: string;\n network?: 'mainnet' | 'testnet';\n errorTags?: PerpsErrorTags;\n data?: Record<string, unknown>;\n}): PerpsLoggerOptions {\n const { contextName, method, provider, network, errorTags, data } = options;\n const tags: Record<string, string | number> = {\n feature: PERPS_CONSTANTS.FeatureName,\n };\n\n if (provider !== undefined) {\n tags.provider = provider;\n }\n if (network !== undefined) {\n tags.network = network;\n }\n if (errorTags !== undefined) {\n tags.operation = errorTags.operation;\n if (errorTags.action !== undefined) {\n tags.action = errorTags.action;\n }\n if (errorTags.component !== undefined) {\n tags.component = errorTags.component;\n }\n }\n\n return {\n tags,\n context: {\n name: contextName,\n data: {\n method,\n ...data,\n },\n },\n };\n}\n"]}
|
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { PerpsControllerError } from '../errors.js';
|
|
1
2
|
/**
|
|
2
3
|
* Detects expected cancellation/abort errors that should not be reported to Sentry.
|
|
3
4
|
* These occur during normal navigation or view teardown when in-flight fetch requests
|
|
@@ -47,4 +48,21 @@ export declare function isHyperLiquidUserNotFoundError(error: unknown): boolean;
|
|
|
47
48
|
* @returns True if the error indicates multi-sig signing is required.
|
|
48
49
|
*/
|
|
49
50
|
export declare function isHyperLiquidMultiSigRequiredError(error: unknown): boolean;
|
|
51
|
+
/**
|
|
52
|
+
* Normalize a Hyperliquid failure into the stable error contract exposed to
|
|
53
|
+
* clients.
|
|
54
|
+
*
|
|
55
|
+
* Known venue messages receive actionable codes. Unknown messages are replaced
|
|
56
|
+
* by `UNKNOWN_ERROR`, while every newly normalized error keeps the original
|
|
57
|
+
* failure in `cause` for telemetry and debugging.
|
|
58
|
+
*
|
|
59
|
+
* A `PerpsControllerError` that already carries details is returned as is, as
|
|
60
|
+
* is `PRICE_MOVED`, whose numeric payload is computed locally. Any other
|
|
61
|
+
* structured error without details is tagged with the provider so every
|
|
62
|
+
* Hyperliquid result exposes the same details contract.
|
|
63
|
+
*
|
|
64
|
+
* @param error - Failure thrown by Hyperliquid or its SDK.
|
|
65
|
+
* @returns A structured error safe to expose through provider results.
|
|
66
|
+
*/
|
|
67
|
+
export declare function normalizeHyperLiquidError(error: unknown): PerpsControllerError;
|
|
50
68
|
//# sourceMappingURL=errorUtils.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errorUtils.d.ts","sourceRoot":"","sources":["../../src/utils/errorUtils.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"errorUtils.d.ts","sourceRoot":"","sources":["../../src/utils/errorUtils.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAqGpD;;;;;;;GAOG;AACH,wBAAgB,YAAY,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CASpD;AAED;;;;;;GAMG;AACH,wBAAgB,oBAAoB,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAK5D;AAED;;;;;;;;GAQG;AACH,wBAAgB,WAAW,CAAC,KAAK,EAAE,OAAO,EAAE,OAAO,CAAC,EAAE,MAAM,GAAG,KAAK,CAkBnE;AAED;;;;;;;;GAQG;AACH,wBAAgB,8BAA8B,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAKtE;AAED;;;;;;;;;;GAUG;AACH,wBAAgB,kCAAkC,CAAC,KAAK,EAAE,OAAO,GAAG,OAAO,CAK1E;AAED;;;;;;;;;;;;;;;GAeG;AACH,wBAAgB,yBAAyB,CACvC,KAAK,EAAE,OAAO,GACb,oBAAoB,CA2CtB"}
|
package/dist/utils/errorUtils.js
CHANGED
|
@@ -3,8 +3,97 @@
|
|
|
3
3
|
* Includes generic error helpers and Perps error classification helpers.
|
|
4
4
|
*/
|
|
5
5
|
import { hasProperty } from '@metamask/utils';
|
|
6
|
-
import {
|
|
6
|
+
import { PerpsControllerError } from '../errors.js';
|
|
7
|
+
import { isPerpsErrorCode, PERPS_ERROR_CODES } from '../perpsErrorCodes.js';
|
|
7
8
|
import { hasErrorInCauseChain } from '../services/causeChain.js';
|
|
9
|
+
const HYPERLIQUID_ERROR_MAPPINGS = [
|
|
10
|
+
// Keep specific post-only rejections ahead of generic failure patterns.
|
|
11
|
+
{
|
|
12
|
+
pattern: /post only order would have immediately matched/iu,
|
|
13
|
+
code: PERPS_ERROR_CODES.ORDER_POST_ONLY_WOULD_CROSS,
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
pattern: /only post-only orders allowed immediately after network upgrade/iu,
|
|
17
|
+
code: PERPS_ERROR_CODES.ORDER_POST_ONLY_WINDOW,
|
|
18
|
+
},
|
|
19
|
+
{
|
|
20
|
+
// Hyperliquid cannot distinguish never placed, already cancelled, and filled
|
|
21
|
+
// here. Share this classification with strategy cancels, and do not match
|
|
22
|
+
// "filled" alone: a partial fill can still leave an order resting.
|
|
23
|
+
pattern: /never placed|already cancel(?:ed|led)|(?:order|twap) not found/iu,
|
|
24
|
+
code: PERPS_ERROR_CODES.ORDER_NO_LONGER_OPEN,
|
|
25
|
+
},
|
|
26
|
+
{
|
|
27
|
+
pattern: /isolated position does not have sufficient margin available to decrease leverage|cannot reduce.*leverage|leverage reduction/iu,
|
|
28
|
+
code: PERPS_ERROR_CODES.ORDER_LEVERAGE_REDUCTION_FAILED,
|
|
29
|
+
},
|
|
30
|
+
{
|
|
31
|
+
pattern: /multi-sig required|multisig required/iu,
|
|
32
|
+
code: PERPS_ERROR_CODES.EXCHANGE_MULTI_SIG_REQUIRED,
|
|
33
|
+
},
|
|
34
|
+
{
|
|
35
|
+
pattern: /must deposit before performing actions/iu,
|
|
36
|
+
code: PERPS_ERROR_CODES.EXCHANGE_ACCOUNT_NOT_FOUND,
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
pattern: /invalid nonce/iu,
|
|
40
|
+
code: PERPS_ERROR_CODES.EXCHANGE_INVALID_NONCE,
|
|
41
|
+
},
|
|
42
|
+
{
|
|
43
|
+
pattern: /order must have minimum value/iu,
|
|
44
|
+
code: PERPS_ERROR_CODES.ORDER_SIZE_MIN,
|
|
45
|
+
},
|
|
46
|
+
{
|
|
47
|
+
pattern: /timeout|timed out/iu,
|
|
48
|
+
code: PERPS_ERROR_CODES.CONNECTION_TIMEOUT,
|
|
49
|
+
},
|
|
50
|
+
{
|
|
51
|
+
pattern: /service unavailable|temporarily unavailable|\bstatus(?: code)?\s*[:=]?\s*503\b|\bhttp(?:\/\d(?:\.\d)?)?\s+503\b/iu,
|
|
52
|
+
code: PERPS_ERROR_CODES.SERVICE_UNAVAILABLE,
|
|
53
|
+
},
|
|
54
|
+
{
|
|
55
|
+
pattern: /network error|fetch failed|connection failed/iu,
|
|
56
|
+
code: PERPS_ERROR_CODES.NETWORK_ERROR,
|
|
57
|
+
},
|
|
58
|
+
{
|
|
59
|
+
pattern: /rate limit|too many .*requests|throttl/iu,
|
|
60
|
+
code: PERPS_ERROR_CODES.RATE_LIMIT_EXCEEDED,
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
pattern: /margin available|not enough margin|insufficient margin/iu,
|
|
64
|
+
code: PERPS_ERROR_CODES.INSUFFICIENT_MARGIN,
|
|
65
|
+
},
|
|
66
|
+
{
|
|
67
|
+
// "builder has insufficient balance" is the venue's builder-fee refusal,
|
|
68
|
+
// not the user's balance. It stays UNKNOWN_ERROR.
|
|
69
|
+
pattern: /(?<!builder has )insufficient balance|not enough balance/iu,
|
|
70
|
+
code: PERPS_ERROR_CODES.INSUFFICIENT_BALANCE,
|
|
71
|
+
},
|
|
72
|
+
{
|
|
73
|
+
pattern: /reduce only|reduce-only|reduceOnly/iu,
|
|
74
|
+
code: PERPS_ERROR_CODES.REDUCE_ONLY_VIOLATION,
|
|
75
|
+
},
|
|
76
|
+
{
|
|
77
|
+
pattern: /position would flip|would flip position/iu,
|
|
78
|
+
code: PERPS_ERROR_CODES.POSITION_WOULD_FLIP,
|
|
79
|
+
},
|
|
80
|
+
{
|
|
81
|
+
pattern: /no position found/iu,
|
|
82
|
+
code: PERPS_ERROR_CODES.POSITION_NOT_FOUND,
|
|
83
|
+
},
|
|
84
|
+
{
|
|
85
|
+
pattern: /slippage|price moved too much/iu,
|
|
86
|
+
code: PERPS_ERROR_CODES.SLIPPAGE_EXCEEDED,
|
|
87
|
+
},
|
|
88
|
+
{
|
|
89
|
+
pattern: /could not immediately match|insufficient liquidity|no liquidity|IOC.*cancel/iu,
|
|
90
|
+
code: PERPS_ERROR_CODES.IOC_CANCEL,
|
|
91
|
+
},
|
|
92
|
+
{
|
|
93
|
+
pattern: /order rejected|rejected order/iu,
|
|
94
|
+
code: PERPS_ERROR_CODES.ORDER_REJECTED,
|
|
95
|
+
},
|
|
96
|
+
];
|
|
8
97
|
/**
|
|
9
98
|
* Detects expected cancellation/abort errors that should not be reported to Sentry.
|
|
10
99
|
* These occur during normal navigation or view teardown when in-flight fetch requests
|
|
@@ -85,4 +174,47 @@ export function isHyperLiquidMultiSigRequiredError(error) {
|
|
|
85
174
|
const lower = ensureError(error).message.toLowerCase();
|
|
86
175
|
return (lower.includes('multi-sig required') || lower.includes('multisig required'));
|
|
87
176
|
}
|
|
177
|
+
/**
|
|
178
|
+
* Normalize a Hyperliquid failure into the stable error contract exposed to
|
|
179
|
+
* clients.
|
|
180
|
+
*
|
|
181
|
+
* Known venue messages receive actionable codes. Unknown messages are replaced
|
|
182
|
+
* by `UNKNOWN_ERROR`, while every newly normalized error keeps the original
|
|
183
|
+
* failure in `cause` for telemetry and debugging.
|
|
184
|
+
*
|
|
185
|
+
* A `PerpsControllerError` that already carries details is returned as is, as
|
|
186
|
+
* is `PRICE_MOVED`, whose numeric payload is computed locally. Any other
|
|
187
|
+
* structured error without details is tagged with the provider so every
|
|
188
|
+
* Hyperliquid result exposes the same details contract.
|
|
189
|
+
*
|
|
190
|
+
* @param error - Failure thrown by Hyperliquid or its SDK.
|
|
191
|
+
* @returns A structured error safe to expose through provider results.
|
|
192
|
+
*/
|
|
193
|
+
export function normalizeHyperLiquidError(error) {
|
|
194
|
+
if (error instanceof PerpsControllerError) {
|
|
195
|
+
const { errorCode: structuredCode } = error;
|
|
196
|
+
if (error.errorDetails !== undefined ||
|
|
197
|
+
structuredCode === PERPS_ERROR_CODES.PRICE_MOVED) {
|
|
198
|
+
return error;
|
|
199
|
+
}
|
|
200
|
+
return new PerpsControllerError(structuredCode, structuredCode, { code: structuredCode, provider: 'hyperliquid' }, { cause: error.cause ?? error });
|
|
201
|
+
}
|
|
202
|
+
const cause = ensureError(error, 'normalizeHyperLiquidError');
|
|
203
|
+
let errorCode;
|
|
204
|
+
if (isHyperLiquidUserNotFoundError(cause)) {
|
|
205
|
+
errorCode = PERPS_ERROR_CODES.EXCHANGE_ACCOUNT_NOT_FOUND;
|
|
206
|
+
}
|
|
207
|
+
else if (isPerpsErrorCode(cause.message) &&
|
|
208
|
+
cause.message !== PERPS_ERROR_CODES.PRICE_MOVED) {
|
|
209
|
+
errorCode = cause.message;
|
|
210
|
+
}
|
|
211
|
+
else {
|
|
212
|
+
errorCode = HYPERLIQUID_ERROR_MAPPINGS.find(({ pattern }) => pattern.test(cause.message))?.code;
|
|
213
|
+
}
|
|
214
|
+
const normalizedCode = errorCode ?? PERPS_ERROR_CODES.UNKNOWN_ERROR;
|
|
215
|
+
return new PerpsControllerError(normalizedCode, normalizedCode, {
|
|
216
|
+
code: normalizedCode,
|
|
217
|
+
provider: 'hyperliquid',
|
|
218
|
+
}, { cause });
|
|
219
|
+
}
|
|
88
220
|
//# sourceMappingURL=errorUtils.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"errorUtils.js","sourceRoot":"","sources":["../../src/utils/errorUtils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAE9C,OAAO,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC1D,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEjE;;;;;;;GAOG;AACH,MAAM,UAAU,YAAY,CAAC,KAAc;IACzC,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;QAC3B,OAAO,CACL,KAAK,CAAC,IAAI,KAAK,YAAY;YAC3B,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,mBAAmB,CAAC;YAC3C,KAAK,CAAC,OAAO,CAAC,QAAQ,CAAC,2BAA2B,CAAC,CACpD,CAAC;IACJ,CAAC;IACD,OAAO,KAAK,CAAC;AACf,CAAC;AAED;;;;;;GAMG;AACH,MAAM,UAAU,oBAAoB,CAAC,KAAc;IACjD,OAAO,oBAAoB,CACzB,KAAK,EACL,CAAC,OAAO,EAAE,EAAE,CAAC,OAAO,CAAC,OAAO,KAAK,iBAAiB,CAAC,cAAc,CAClE,CAAC;AACJ,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,WAAW,CAAC,KAAc,EAAE,OAAgB;IAC1D,IAAI,KAAK,YAAY,KAAK,EAAE,CAAC;QAC3B,OAAO,KAAK,CAAC;IACf,CAAC;IACD,8DAA8D;IAC9D,oFAAoF;IACpF,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;QAC1C,MAAM,WAAW,GAAG,qCAAqC,CAAC;QAC1D,OAAO,IAAI,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,GAAG,WAAW,KAAK,OAAO,GAAG,CAAC,CAAC,CAAC,WAAW,CAAC,CAAC;IAC1E,CAAC;IACD,IAAI,OAAO,KAAK,KAAK,QAAQ,EAAE,CAAC;QAC9B,OAAO,IAAI,KAAK,CAAC,KAAK,CAAC,CAAC;IAC1B,CAAC;IACD,OAAO,IAAI,KAAK,CACd,OAAO,KAAK,KAAK,QAAQ,IAAI,KAAK,KAAK,IAAI,IAAI,WAAW,CAAC,KAAK,EAAE,SAAS,CAAC;QAC1E,CAAC,CAAC,MAAM,CAAC,KAAK,CAAC,OAAO,CAAC;QACvB,CAAC,CAAC,eAAe,CACpB,CAAC;AACJ,CAAC;AAED;;;;;;;;GAQG;AACH,MAAM,UAAU,8BAA8B,CAAC,KAAc;IAC3D,MAAM,KAAK,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC;IACvD,OAAO,CACL,KAAK,CAAC,QAAQ,CAAC,oBAAoB,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,gBAAgB,CAAC,CACzE,CAAC;AACJ,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,UAAU,kCAAkC,CAAC,KAAc;IAC/D,MAAM,KAAK,GAAG,WAAW,CAAC,KAAK,CAAC,CAAC,OAAO,CAAC,WAAW,EAAE,CAAC;IACvD,OAAO,CACL,KAAK,CAAC,QAAQ,CAAC,oBAAoB,CAAC,IAAI,KAAK,CAAC,QAAQ,CAAC,mBAAmB,CAAC,CAC5E,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Utility functions for error handling across Perps controller code.\n * Includes generic error helpers and Perps error classification helpers.\n */\nimport { hasProperty } from '@metamask/utils';\n\nimport { PERPS_ERROR_CODES } from '../perpsErrorCodes.js';\nimport { hasErrorInCauseChain } from '../services/causeChain.js';\n\n/**\n * Detects expected cancellation/abort errors that should not be reported to Sentry.\n * These occur during normal navigation or view teardown when in-flight fetch requests\n * are cancelled via AbortController.\n *\n * @param error - The error to check.\n * @returns True if the error is an expected abort/cancellation.\n */\nexport function isAbortError(error: unknown): boolean {\n if (error instanceof Error) {\n return (\n error.name === 'AbortError' ||\n error.message.includes('signal is aborted') ||\n error.message.includes('The operation was aborted')\n );\n }\n return false;\n}\n\n/**\n * Detects keyring-locked errors, including SDK-wrapped errors that preserve the\n * original error in `cause`.\n *\n * @param error - The error to check.\n * @returns True if any error in the cause chain is KEYRING_LOCKED.\n */\nexport function isKeyringLockedError(error: unknown): boolean {\n return hasErrorInCauseChain(\n error,\n (current) => current.message === PERPS_ERROR_CODES.KEYRING_LOCKED,\n );\n}\n\n/**\n * Ensures we have a proper Error object for logging.\n * Converts unknown/string errors to proper Error instances.\n * Handles undefined/null specially for better Sentry context.\n *\n * @param error - The caught error (could be Error, string, or unknown)\n * @param context - Optional context string to help identify the source of the error\n * @returns A proper Error instance\n */\nexport function ensureError(error: unknown, context?: string): Error {\n if (error instanceof Error) {\n return error;\n }\n // Handle undefined/null specifically for better error context\n // e.g. Hyperliquid SDK may reject with undefined when AbortSignal.reason is not set\n if (error === undefined || error === null) {\n const baseMessage = 'Unknown error (no details provided)';\n return new Error(context ? `${baseMessage} [${context}]` : baseMessage);\n }\n if (typeof error === 'string') {\n return new Error(error);\n }\n return new Error(\n typeof error === 'object' && error !== null && hasProperty(error, 'message')\n ? String(error.message)\n : 'Unknown error',\n );\n}\n\n/**\n * Hyperliquid rejects user-scoped exchange writes (`agentSetAbstraction`,\n * `setReferrer`, ...) with this exact message when the\n * wallet has never funded a Hyperliquid account. It is a benign pre-account\n * state, not an error we should forward to Sentry.\n *\n * @param error - The caught error.\n * @returns True if the error matches the Hyperliquid \"user not on chain yet\" rejection.\n */\nexport function isHyperLiquidUserNotFoundError(error: unknown): boolean {\n const lower = ensureError(error).message.toLowerCase();\n return (\n lower.includes('user or api wallet') && lower.includes('does not exist')\n );\n}\n\n/**\n * Hyperliquid rejects every single-signer exchange write for an account that\n * has been converted to multi-sig (`ApiRequestError: Multi-sig required`).\n * MetaMask signs Perps actions with a single agent/user wallet, so this is a\n * permanent account-shape condition rather than a failure we should retry or\n * forward to Sentry. Only the hyphenated spelling has been observed from the\n * venue; the unhyphenated variant is matched defensively.\n *\n * @param error - The caught error.\n * @returns True if the error indicates multi-sig signing is required.\n */\nexport function isHyperLiquidMultiSigRequiredError(error: unknown): boolean {\n const lower = ensureError(error).message.toLowerCase();\n return (\n lower.includes('multi-sig required') || lower.includes('multisig required')\n );\n}\n"]}
|
|
1
|
+
{"version":3,"file":"errorUtils.js","sourceRoot":"","sources":["../../src/utils/errorUtils.ts"],"names":[],"mappings":"AAAA;;;GAGG;AACH,OAAO,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAE9C,OAAO,EAAE,oBAAoB,EAAE,MAAM,cAAc,CAAC;AAEpD,OAAO,EAAE,gBAAgB,EAAE,iBAAiB,EAAE,MAAM,uBAAuB,CAAC;AAC5E,OAAO,EAAE,oBAAoB,EAAE,MAAM,2BAA2B,CAAC;AAEjE,MAAM,0BAA0B,GAG1B;IACJ,wEAAwE;IACxE;QACE,OAAO,EAAE,kDAAkD;QAC3D,IAAI,EAAE,iBAAiB,CAAC,2BAA2B;KACpD;IACD;QACE,OAAO,EACL,mEAAmE;QACrE,IAAI,EAAE,iBAAiB,CAAC,sBAAsB;KAC/C;IACD;QACE,6EAA6E;QAC7E,0EAA0E;QAC1E,mEAAmE;QACnE,OAAO,EAAE,kEAAkE;QAC3E,IAAI,EAAE,iBAAiB,CAAC,oBAAoB;KAC7C;IACD;QACE,OAAO,EACL,+HAA+H;QACjI,IAAI,EAAE,iBAAiB,CAAC,+BAA+B;KACxD;IACD;QACE,OAAO,EAAE,wCAAwC;QACjD,IAAI,EA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iB,CAAC,0BAA0B,CAAC;IAC3D,CAAC;SAAM,IACL,gBAAgB,CAAC,KAAK,CAAC,OAAO,CAAC;QAC/B,KAAK,CAAC,OAAO,KAAK,iBAAiB,CAAC,WAAW,EAC/C,CAAC;QACD,SAAS,GAAG,KAAK,CAAC,OAAO,CAAC;IAC5B,CAAC;SAAM,CAAC;QACN,SAAS,GAAG,0BAA0B,CAAC,IAAI,CAAC,CAAC,EAAE,OAAO,EAAE,EAAE,EAAE,CAC1D,OAAO,CAAC,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,CAC5B,EAAE,IAAI,CAAC;IACV,CAAC;IAED,MAAM,cAAc,GAAG,SAAS,IAAI,iBAAiB,CAAC,aAAa,CAAC;IACpE,OAAO,IAAI,oBAAoB,CAC7B,cAAc,EACd,cAAc,EACd;QACE,IAAI,EAAE,cAAc;QACpB,QAAQ,EAAE,aAAa;KACxB,EACD,EAAE,KAAK,EAAE,CACV,CAAC;AACJ,CAAC","sourcesContent":["/**\n * Utility functions for error handling across Perps controller code.\n * Includes generic error helpers and Perps error classification helpers.\n */\nimport { hasProperty } from '@metamask/utils';\n\nimport { PerpsControllerError } from '../errors.js';\nimport type { HyperLiquidErrorDetails } from '../errors.js';\nimport { isPerpsErrorCode, PERPS_ERROR_CODES } from '../perpsErrorCodes.js';\nimport { hasErrorInCauseChain } from '../services/causeChain.js';\n\nconst HYPERLIQUID_ERROR_MAPPINGS: readonly {\n pattern: RegExp;\n code: HyperLiquidErrorDetails['code'];\n}[] = [\n // Keep specific post-only rejections ahead of generic failure patterns.\n {\n pattern: /post only order would have immediately matched/iu,\n code: PERPS_ERROR_CODES.ORDER_POST_ONLY_WOULD_CROSS,\n },\n {\n pattern:\n /only post-only orders allowed immediately after network upgrade/iu,\n code: PERPS_ERROR_CODES.ORDER_POST_ONLY_WINDOW,\n },\n {\n // Hyperliquid cannot distinguish never placed, already cancelled, and filled\n // here. Share this classification with strategy cancels, and do not match\n // \"filled\" alone: a partial fill can still leave an order resting.\n pattern: /never placed|already cancel(?:ed|led)|(?:order|twap) not found/iu,\n code: PERPS_ERROR_CODES.ORDER_NO_LONGER_OPEN,\n },\n {\n pattern:\n /isolated position does not have sufficient margin available to decrease leverage|cannot reduce.*leverage|leverage reduction/iu,\n code: PERPS_ERROR_CODES.ORDER_LEVERAGE_REDUCTION_FAILED,\n },\n {\n pattern: /multi-sig required|multisig required/iu,\n code: PERPS_ERROR_CODES.EXCHANGE_MULTI_SIG_REQUIRED,\n },\n {\n pattern: /must deposit before performing actions/iu,\n code: PERPS_ERROR_CODES.EXCHANGE_ACCOUNT_NOT_FOUND,\n },\n {\n pattern: /invalid nonce/iu,\n code: PERPS_ERROR_CODES.EXCHANGE_INVALID_NONCE,\n },\n {\n pattern: /order must have minimum value/iu,\n code: PERPS_ERROR_CODES.ORDER_SIZE_MIN,\n },\n {\n pattern: /timeout|timed out/iu,\n code: PERPS_ERROR_CODES.CONNECTION_TIMEOUT,\n },\n {\n pattern:\n /service unavailable|temporarily unavailable|\\bstatus(?: code)?\\s*[:=]?\\s*503\\b|\\bhttp(?:\\/\\d(?:\\.\\d)?)?\\s+503\\b/iu,\n code: PERPS_ERROR_CODES.SERVICE_UNAVAILABLE,\n },\n {\n pattern: /network error|fetch failed|connection failed/iu,\n code: PERPS_ERROR_CODES.NETWORK_ERROR,\n },\n {\n pattern: /rate limit|too many .*requests|throttl/iu,\n code: PERPS_ERROR_CODES.RATE_LIMIT_EXCEEDED,\n },\n {\n pattern: /margin available|not enough margin|insufficient margin/iu,\n code: PERPS_ERROR_CODES.INSUFFICIENT_MARGIN,\n },\n {\n // \"builder has insufficient balance\" is the venue's builder-fee refusal,\n // not the user's balance. It stays UNKNOWN_ERROR.\n pattern: /(?<!builder has )insufficient balance|not enough balance/iu,\n code: PERPS_ERROR_CODES.INSUFFICIENT_BALANCE,\n },\n {\n pattern: /reduce only|reduce-only|reduceOnly/iu,\n code: PERPS_ERROR_CODES.REDUCE_ONLY_VIOLATION,\n },\n {\n pattern: /position would flip|would flip position/iu,\n code: PERPS_ERROR_CODES.POSITION_WOULD_FLIP,\n },\n {\n pattern: /no position found/iu,\n code: PERPS_ERROR_CODES.POSITION_NOT_FOUND,\n },\n {\n pattern: /slippage|price moved too much/iu,\n code: PERPS_ERROR_CODES.SLIPPAGE_EXCEEDED,\n },\n {\n pattern:\n /could not immediately match|insufficient liquidity|no liquidity|IOC.*cancel/iu,\n code: PERPS_ERROR_CODES.IOC_CANCEL,\n },\n {\n pattern: /order rejected|rejected order/iu,\n code: PERPS_ERROR_CODES.ORDER_REJECTED,\n },\n];\n\n/**\n * Detects expected cancellation/abort errors that should not be reported to Sentry.\n * These occur during normal navigation or view teardown when in-flight fetch requests\n * are cancelled via AbortController.\n *\n * @param error - The error to check.\n * @returns True if the error is an expected abort/cancellation.\n */\nexport function isAbortError(error: unknown): boolean {\n if (error instanceof Error) {\n return (\n error.name === 'AbortError' ||\n error.message.includes('signal is aborted') ||\n error.message.includes('The operation was aborted')\n );\n }\n return false;\n}\n\n/**\n * Detects keyring-locked errors, including SDK-wrapped errors that preserve the\n * original error in `cause`.\n *\n * @param error - The error to check.\n * @returns True if any error in the cause chain is KEYRING_LOCKED.\n */\nexport function isKeyringLockedError(error: unknown): boolean {\n return hasErrorInCauseChain(\n error,\n (current) => current.message === PERPS_ERROR_CODES.KEYRING_LOCKED,\n );\n}\n\n/**\n * Ensures we have a proper Error object for logging.\n * Converts unknown/string errors to proper Error instances.\n * Handles undefined/null specially for better Sentry context.\n *\n * @param error - The caught error (could be Error, string, or unknown)\n * @param context - Optional context string to help identify the source of the error\n * @returns A proper Error instance\n */\nexport function ensureError(error: unknown, context?: string): Error {\n if (error instanceof Error) {\n return error;\n }\n // Handle undefined/null specifically for better error context\n // e.g. Hyperliquid SDK may reject with undefined when AbortSignal.reason is not set\n if (error === undefined || error === null) {\n const baseMessage = 'Unknown error (no details provided)';\n return new Error(context ? `${baseMessage} [${context}]` : baseMessage);\n }\n if (typeof error === 'string') {\n return new Error(error);\n }\n return new Error(\n typeof error === 'object' && error !== null && hasProperty(error, 'message')\n ? String(error.message)\n : 'Unknown error',\n );\n}\n\n/**\n * Hyperliquid rejects user-scoped exchange writes (`agentSetAbstraction`,\n * `setReferrer`, ...) with this exact message when the\n * wallet has never funded a Hyperliquid account. It is a benign pre-account\n * state, not an error we should forward to Sentry.\n *\n * @param error - The caught error.\n * @returns True if the error matches the Hyperliquid \"user not on chain yet\" rejection.\n */\nexport function isHyperLiquidUserNotFoundError(error: unknown): boolean {\n const lower = ensureError(error).message.toLowerCase();\n return (\n lower.includes('user or api wallet') && lower.includes('does not exist')\n );\n}\n\n/**\n * Hyperliquid rejects every single-signer exchange write for an account that\n * has been converted to multi-sig (`ApiRequestError: Multi-sig required`).\n * MetaMask signs Perps actions with a single agent/user wallet, so this is a\n * permanent account-shape condition rather than a failure we should retry or\n * forward to Sentry. Only the hyphenated spelling has been observed from the\n * venue; the unhyphenated variant is matched defensively.\n *\n * @param error - The caught error.\n * @returns True if the error indicates multi-sig signing is required.\n */\nexport function isHyperLiquidMultiSigRequiredError(error: unknown): boolean {\n const lower = ensureError(error).message.toLowerCase();\n return (\n lower.includes('multi-sig required') || lower.includes('multisig required')\n );\n}\n\n/**\n * Normalize a Hyperliquid failure into the stable error contract exposed to\n * clients.\n *\n * Known venue messages receive actionable codes. Unknown messages are replaced\n * by `UNKNOWN_ERROR`, while every newly normalized error keeps the original\n * failure in `cause` for telemetry and debugging.\n *\n * A `PerpsControllerError` that already carries details is returned as is, as\n * is `PRICE_MOVED`, whose numeric payload is computed locally. Any other\n * structured error without details is tagged with the provider so every\n * Hyperliquid result exposes the same details contract.\n *\n * @param error - Failure thrown by Hyperliquid or its SDK.\n * @returns A structured error safe to expose through provider results.\n */\nexport function normalizeHyperLiquidError(\n error: unknown,\n): PerpsControllerError {\n if (error instanceof PerpsControllerError) {\n const { errorCode: structuredCode } = error;\n if (\n error.errorDetails !== undefined ||\n structuredCode === PERPS_ERROR_CODES.PRICE_MOVED\n ) {\n return error;\n }\n return new PerpsControllerError(\n structuredCode,\n structuredCode,\n { code: structuredCode, provider: 'hyperliquid' },\n { cause: error.cause ?? error },\n );\n }\n\n const cause = ensureError(error, 'normalizeHyperLiquidError');\n let errorCode: HyperLiquidErrorDetails['code'] | undefined;\n\n if (isHyperLiquidUserNotFoundError(cause)) {\n errorCode = PERPS_ERROR_CODES.EXCHANGE_ACCOUNT_NOT_FOUND;\n } else if (\n isPerpsErrorCode(cause.message) &&\n cause.message !== PERPS_ERROR_CODES.PRICE_MOVED\n ) {\n errorCode = cause.message;\n } else {\n errorCode = HYPERLIQUID_ERROR_MAPPINGS.find(({ pattern }) =>\n pattern.test(cause.message),\n )?.code;\n }\n\n const normalizedCode = errorCode ?? PERPS_ERROR_CODES.UNKNOWN_ERROR;\n return new PerpsControllerError(\n normalizedCode,\n normalizedCode,\n {\n code: normalizedCode,\n provider: 'hyperliquid',\n },\n { cause },\n );\n}\n"]}
|
|
@@ -12,11 +12,18 @@ export type ValidationDebugLogger = PerpsDebugLogger | undefined;
|
|
|
12
12
|
* Validation utilities for HyperLiquid operations
|
|
13
13
|
*/
|
|
14
14
|
/**
|
|
15
|
-
* Create
|
|
15
|
+
* Create a client-facing error response.
|
|
16
|
+
*
|
|
17
|
+
* Every Hyperliquid result that reports failure goes through
|
|
18
|
+
* `normalizeHyperLiquidError`, so the returned message is a stable
|
|
19
|
+
* `PerpsErrorCode`. The original failure stays on the normalized error's
|
|
20
|
+
* `cause` for telemetry and is not copied into the result's public fields; the
|
|
21
|
+
* normalized error rides along as internal metadata so a service that owns
|
|
22
|
+
* reporting can log it (see `getProviderErrorCause`).
|
|
16
23
|
*
|
|
17
24
|
* @param error - The error that occurred
|
|
18
25
|
* @param defaultResponse - The default response object to use as template
|
|
19
|
-
* @returns The error response with success=false and error
|
|
26
|
+
* @returns The error response with success=false and a normalized error
|
|
20
27
|
*/
|
|
21
28
|
export declare function createErrorResult<TValue extends {
|
|
22
29
|
success: boolean;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"hyperLiquidValidation.d.ts","sourceRoot":"","sources":["../../src/utils/hyperLiquidValidation.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;
|
|
1
|
+
{"version":3,"file":"hyperLiquidValidation.d.ts","sourceRoot":"","sources":["../../src/utils/hyperLiquidValidation.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,WAAW,EAAE,GAAG,EAAE,MAAM,iBAAiB,CAAC;AAaxD,OAAO,KAAK,EAAE,sBAAsB,EAAE,MAAM,cAAc,CAAC;AAE3D,OAAO,KAAK,EACV,uBAAuB,EACvB,gBAAgB,EACjB,MAAM,mBAAmB,CAAC;AAC3B,OAAO,KAAK,EAAE,SAAS,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAWtE;;;;GAIG;AACH,MAAM,MAAM,qBAAqB,GAAG,gBAAgB,GAAG,SAAS,CAAC;AAEjE;;GAEG;AAEH;;;;;;;;;;;;;GAaG;AACH,wBAAgB,iBAAiB,CAC/B,MAAM,SAAS;IAAE,OAAO,EAAE,OAAO,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,EACnD,KAAK,EAAE,OAAO,EAAE,eAAe,EAAE,MAAM,GAAG,MAAM,GAAG,sBAAsB,CAe1E;AAED;;;;;;;;;GASG;AACH,wBAAgB,wBAAwB,CACtC,MAAM,EAAE;IACN,OAAO,CAAC,EAAE,WAAW,CAAC;IACtB,MAAM,CAAC,EAAE,MAAM,CAAC;IAChB,WAAW,CAAC,EAAE,GAAG,CAAC;CACnB,EACD,WAAW,CAAC,EAAE,qBAAqB,GAClC;IAAE,OAAO,EAAE,OAAO,CAAC;IAAC,KAAK,CAAC,EAAE,MAAM,CAAA;CAAE,CAkEtC;AAED;;;;;;;;;GASG;AACH,wBAAgB,qBAAqB,CACnC,MAAM,EAAE;IACN,OAAO,CAAC,EAAE,WAA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amask/utils';
|
|
2
2
|
import { HYPERLIQUID_ASSET_CONFIGS, HYPERLIQUID_ORDER_CAPABILITIES, BASIS_POINTS_DIVISOR, getSupportedAssets, TRADING_DEFAULTS, } from '../constants/hyperLiquidConfig.js';
|
|
3
3
|
import { CHASE_ORDER_CONFIG, HYPERLIQUID_ORDER_LIMITS, } from '../constants/perpsConfig.js';
|
|
4
|
-
import { PerpsControllerError } from '../errors.js';
|
|
5
4
|
import { PERPS_ERROR_CODES } from '../perpsErrorCodes.js';
|
|
6
5
|
import { validateScaleOrder, validateTwapOrder } from './advancedOrders.js';
|
|
7
|
-
import {
|
|
6
|
+
import { attachProviderErrorCause } from './errorContext.js';
|
|
7
|
+
import { normalizeHyperLiquidError } from './errorUtils.js';
|
|
8
8
|
import { getTriggerExecution, isLimitExecutionOrderType, isStrategyOrderType, isTriggerOrderType, } from './orderTypes.js';
|
|
9
9
|
/**
|
|
10
10
|
* Validation utilities for HyperLiquid operations
|
|
11
11
|
*/
|
|
12
12
|
/**
|
|
13
|
-
* Create
|
|
13
|
+
* Create a client-facing error response.
|
|
14
|
+
*
|
|
15
|
+
* Every Hyperliquid result that reports failure goes through
|
|
16
|
+
* `normalizeHyperLiquidError`, so the returned message is a stable
|
|
17
|
+
* `PerpsErrorCode`. The original failure stays on the normalized error's
|
|
18
|
+
* `cause` for telemetry and is not copied into the result's public fields; the
|
|
19
|
+
* normalized error rides along as internal metadata so a service that owns
|
|
20
|
+
* reporting can log it (see `getProviderErrorCause`).
|
|
14
21
|
*
|
|
15
22
|
* @param error - The error that occurred
|
|
16
23
|
* @param defaultResponse - The default response object to use as template
|
|
17
|
-
* @returns The error response with success=false and error
|
|
24
|
+
* @returns The error response with success=false and a normalized error
|
|
18
25
|
*/
|
|
19
26
|
export function createErrorResult(error, defaultResponse) {
|
|
20
|
-
const normalizedError =
|
|
21
|
-
|
|
22
|
-
if (error instanceof PerpsControllerError) {
|
|
23
|
-
errorFields = {
|
|
24
|
-
errorCode: error.errorCode,
|
|
25
|
-
...(error.errorDetails === undefined
|
|
26
|
-
? {}
|
|
27
|
-
: { errorDetails: error.errorDetails }),
|
|
28
|
-
};
|
|
29
|
-
}
|
|
30
|
-
return {
|
|
27
|
+
const normalizedError = normalizeHyperLiquidError(error);
|
|
28
|
+
return attachProviderErrorCause({
|
|
31
29
|
...defaultResponse,
|
|
32
30
|
success: false,
|
|
33
31
|
error: normalizedError.message,
|
|
34
|
-
|
|
35
|
-
|
|
32
|
+
errorCode: normalizedError.errorCode,
|
|
33
|
+
...(normalizedError.errorDetails === undefined
|
|
34
|
+
? {}
|
|
35
|
+
: { errorDetails: normalizedError.errorDetails }),
|
|
36
|
+
}, normalizedError);
|
|
36
37
|
}
|
|
37
38
|
/**
|
|
38
39
|
* Validate withdrawal parameters.
|