@symbiome-forge/cow-sdk-wasm 0.1.0-alpha.1
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/LICENSE +674 -0
- package/README.md +278 -0
- package/dist/cloudflare/callbacks.cjs +2 -0
- package/dist/cloudflare/callbacks.d.ts +55 -0
- package/dist/cloudflare/callbacks.js +1 -0
- package/dist/cloudflare/envelope.cjs +2 -0
- package/dist/cloudflare/envelope.d.ts +5 -0
- package/dist/cloudflare/envelope.js +1 -0
- package/dist/cloudflare/errors.cjs +150 -0
- package/dist/cloudflare/errors.d.ts +82 -0
- package/dist/cloudflare/errors.js +145 -0
- package/dist/cloudflare/index.cjs +286 -0
- package/dist/cloudflare/index.d.ts +78 -0
- package/dist/cloudflare/index.js +1 -0
- package/dist/cloudflare/index.mjs +223 -0
- package/dist/cloudflare/internal.cjs +127 -0
- package/dist/cloudflare/internal.d.ts +40 -0
- package/dist/cloudflare/internal.js +115 -0
- package/dist/cloudflare/options.cjs +2 -0
- package/dist/cloudflare/options.d.ts +70 -0
- package/dist/cloudflare/options.js +1 -0
- package/dist/cloudflare/raw/cloudflare.cjs +65 -0
- package/dist/cloudflare/raw/cloudflare.d.ts +30 -0
- package/dist/cloudflare/raw/cloudflare.js +29 -0
- package/dist/default/callbacks.cjs +2 -0
- package/dist/default/callbacks.d.ts +55 -0
- package/dist/default/callbacks.js +1 -0
- package/dist/default/envelope.cjs +2 -0
- package/dist/default/envelope.d.ts +5 -0
- package/dist/default/envelope.js +1 -0
- package/dist/default/errors.cjs +150 -0
- package/dist/default/errors.d.ts +82 -0
- package/dist/default/errors.js +145 -0
- package/dist/default/index.cjs +359 -0
- package/dist/default/index.d.ts +94 -0
- package/dist/default/index.js +1 -0
- package/dist/default/index.mjs +295 -0
- package/dist/default/internal.cjs +127 -0
- package/dist/default/internal.d.ts +40 -0
- package/dist/default/internal.js +115 -0
- package/dist/default/options.cjs +2 -0
- package/dist/default/options.d.ts +70 -0
- package/dist/default/options.js +1 -0
- package/dist/default/raw/default.cjs +66 -0
- package/dist/default/raw/default.d.ts +31 -0
- package/dist/default/raw/default.js +30 -0
- package/dist/orderbook/callbacks.cjs +2 -0
- package/dist/orderbook/callbacks.d.ts +55 -0
- package/dist/orderbook/callbacks.js +1 -0
- package/dist/orderbook/envelope.cjs +2 -0
- package/dist/orderbook/envelope.d.ts +5 -0
- package/dist/orderbook/envelope.js +1 -0
- package/dist/orderbook/errors.cjs +150 -0
- package/dist/orderbook/errors.d.ts +82 -0
- package/dist/orderbook/errors.js +145 -0
- package/dist/orderbook/index.cjs +195 -0
- package/dist/orderbook/index.d.ts +57 -0
- package/dist/orderbook/index.js +1 -0
- package/dist/orderbook/index.mjs +139 -0
- package/dist/orderbook/internal.cjs +127 -0
- package/dist/orderbook/internal.d.ts +40 -0
- package/dist/orderbook/internal.js +115 -0
- package/dist/orderbook/options.cjs +2 -0
- package/dist/orderbook/options.d.ts +70 -0
- package/dist/orderbook/options.js +1 -0
- package/dist/orderbook/raw/orderbook.cjs +58 -0
- package/dist/orderbook/raw/orderbook.d.ts +23 -0
- package/dist/orderbook/raw/orderbook.js +22 -0
- package/dist/raw/cloudflare-web/cow_sdk_wasm.d.ts +2870 -0
- package/dist/raw/cloudflare-web/cow_sdk_wasm.js +2183 -0
- package/dist/raw/cloudflare-web/cow_sdk_wasm_bg.wasm +0 -0
- package/dist/raw/cloudflare-web/cow_sdk_wasm_bg.wasm.d.ts +65 -0
- package/dist/raw/default-bundler/cow_sdk_wasm.d.ts +2916 -0
- package/dist/raw/default-bundler/cow_sdk_wasm.js +9 -0
- package/dist/raw/default-bundler/cow_sdk_wasm_bg.js +2297 -0
- package/dist/raw/default-bundler/cow_sdk_wasm_bg.wasm +0 -0
- package/dist/raw/default-bundler/cow_sdk_wasm_bg.wasm.d.ts +75 -0
- package/dist/raw/default-nodejs/cow_sdk_wasm.cjs +2330 -0
- package/dist/raw/default-nodejs/cow_sdk_wasm.d.ts +2916 -0
- package/dist/raw/default-nodejs/cow_sdk_wasm_bg.wasm.d.ts +75 -0
- package/dist/raw/orderbook-bundler/cow_sdk_wasm.d.ts +1876 -0
- package/dist/raw/orderbook-bundler/cow_sdk_wasm.js +9 -0
- package/dist/raw/orderbook-bundler/cow_sdk_wasm_bg.js +1636 -0
- package/dist/raw/orderbook-bundler/cow_sdk_wasm_bg.wasm +0 -0
- package/dist/raw/orderbook-bundler/cow_sdk_wasm_bg.wasm.d.ts +50 -0
- package/dist/raw/orderbook-nodejs/cow_sdk_wasm.cjs +1661 -0
- package/dist/raw/orderbook-nodejs/cow_sdk_wasm.d.ts +1876 -0
- package/dist/raw/orderbook-nodejs/cow_sdk_wasm_bg.wasm.d.ts +50 -0
- package/dist/raw/signing-bundler/cow_sdk_wasm.d.ts +774 -0
- package/dist/raw/signing-bundler/cow_sdk_wasm.js +9 -0
- package/dist/raw/signing-bundler/cow_sdk_wasm_bg.js +1059 -0
- package/dist/raw/signing-bundler/cow_sdk_wasm_bg.wasm +0 -0
- package/dist/raw/signing-bundler/cow_sdk_wasm_bg.wasm.d.ts +27 -0
- package/dist/raw/signing-nodejs/cow_sdk_wasm.cjs +1078 -0
- package/dist/raw/signing-nodejs/cow_sdk_wasm.d.ts +774 -0
- package/dist/raw/signing-nodejs/cow_sdk_wasm_bg.wasm.d.ts +27 -0
- package/dist/signing/callbacks.cjs +2 -0
- package/dist/signing/callbacks.d.ts +55 -0
- package/dist/signing/callbacks.js +1 -0
- package/dist/signing/envelope.cjs +2 -0
- package/dist/signing/envelope.d.ts +5 -0
- package/dist/signing/envelope.js +1 -0
- package/dist/signing/errors.cjs +150 -0
- package/dist/signing/errors.d.ts +82 -0
- package/dist/signing/errors.js +145 -0
- package/dist/signing/index.cjs +93 -0
- package/dist/signing/index.d.ts +23 -0
- package/dist/signing/index.js +1 -0
- package/dist/signing/index.mjs +44 -0
- package/dist/signing/internal.cjs +127 -0
- package/dist/signing/internal.d.ts +40 -0
- package/dist/signing/internal.js +115 -0
- package/dist/signing/options.cjs +2 -0
- package/dist/signing/options.d.ts +70 -0
- package/dist/signing/options.js +1 -0
- package/dist/signing/raw/signing.cjs +52 -0
- package/dist/signing/raw/signing.d.ts +17 -0
- package/dist/signing/raw/signing.js +16 -0
- package/package.json +112 -0
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import init, * as wasm from "../../raw/cloudflare-web/cow_sdk_wasm.js";
|
|
2
|
+
export const initializeRaw = init;
|
|
3
|
+
export const RawOrderBookClient = wasm.OrderBookClient;
|
|
4
|
+
export const RawTradingClient = wasm.TradingClient;
|
|
5
|
+
export const __cow_sdk_wasm_init = wasm.__cow_sdk_wasm_init;
|
|
6
|
+
export const appDataDoc = wasm.appDataDoc;
|
|
7
|
+
export const appDataHexToCid = wasm.appDataHexToCid;
|
|
8
|
+
export const appDataInfo = wasm.appDataInfo;
|
|
9
|
+
export const buildCancelOrderTx = wasm.buildCancelOrderTx;
|
|
10
|
+
export const buildPresignTx = wasm.buildPresignTx;
|
|
11
|
+
export const cidToAppDataHex = wasm.cidToAppDataHex;
|
|
12
|
+
export const computeOrderUid = wasm.computeOrderUid;
|
|
13
|
+
export const decodeEthFlowLog = wasm.decodeEthFlowLog;
|
|
14
|
+
export const decodeSettlementLog = wasm.decodeSettlementLog;
|
|
15
|
+
export const deploymentAddresses = wasm.deploymentAddresses;
|
|
16
|
+
export const domainSeparator = wasm.domainSeparator;
|
|
17
|
+
export const eip1271SignaturePayload = wasm.eip1271SignaturePayload;
|
|
18
|
+
export const orderTypedData = wasm.orderTypedData;
|
|
19
|
+
export const signCancellationEthSignDigest = wasm.signCancellationEthSignDigest;
|
|
20
|
+
export const signCancellationWithEip1193 = wasm.signCancellationWithEip1193;
|
|
21
|
+
export const signCancellationWithTypedDataSigner = wasm.signCancellationWithTypedDataSigner;
|
|
22
|
+
export const signOrderEthSignDigest = wasm.signOrderEthSignDigest;
|
|
23
|
+
export const signOrderWithCustomEip1271 = wasm.signOrderWithCustomEip1271;
|
|
24
|
+
export const signOrderWithEip1193 = wasm.signOrderWithEip1193;
|
|
25
|
+
export const signOrderWithEip1271 = wasm.signOrderWithEip1271;
|
|
26
|
+
export const signOrderWithTypedDataSigner = wasm.signOrderWithTypedDataSigner;
|
|
27
|
+
export const supportedChainIds = wasm.supportedChainIds;
|
|
28
|
+
export const validateAppDataDoc = wasm.validateAppDataDoc;
|
|
29
|
+
export const wasmVersion = wasm.wasmVersion;
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
export type SupportedChainId = 1 | 100 | 137 | 8453 | 42161 | 43114 | 56 | 11155111 | 9745 | 57073 | 59144;
|
|
2
|
+
export type CowEnv = "prod" | "staging";
|
|
3
|
+
export type CowFetchMethod = "GET" | "POST" | "PUT" | "DELETE" | "PATCH";
|
|
4
|
+
export interface CowFetchRequest {
|
|
5
|
+
method: CowFetchMethod;
|
|
6
|
+
url: string;
|
|
7
|
+
headers: Record<string, string>;
|
|
8
|
+
body?: string;
|
|
9
|
+
timeoutMs?: number;
|
|
10
|
+
signal?: AbortSignal;
|
|
11
|
+
}
|
|
12
|
+
export interface CowFetchResponse {
|
|
13
|
+
status: number;
|
|
14
|
+
statusText?: string;
|
|
15
|
+
headers?: Record<string, string>;
|
|
16
|
+
body?: string;
|
|
17
|
+
}
|
|
18
|
+
export type CowFetchCallback = (request: CowFetchRequest) => Promise<CowFetchResponse> | CowFetchResponse;
|
|
19
|
+
export interface TypedDataDomainDto {
|
|
20
|
+
name: string;
|
|
21
|
+
version: string;
|
|
22
|
+
chainId: number;
|
|
23
|
+
verifyingContract: string;
|
|
24
|
+
}
|
|
25
|
+
export interface TypedDataFieldDto {
|
|
26
|
+
name: string;
|
|
27
|
+
type: string;
|
|
28
|
+
}
|
|
29
|
+
export interface TypedDataEnvelopeDto {
|
|
30
|
+
domain: TypedDataDomainDto;
|
|
31
|
+
types: Record<string, TypedDataFieldDto[]>;
|
|
32
|
+
primaryType: string;
|
|
33
|
+
message: unknown;
|
|
34
|
+
}
|
|
35
|
+
export interface CowEip1271SignRequest {
|
|
36
|
+
order: unknown;
|
|
37
|
+
typedData: TypedDataEnvelopeDto;
|
|
38
|
+
owner: string;
|
|
39
|
+
chainId: number;
|
|
40
|
+
}
|
|
41
|
+
export interface ContractCallDto {
|
|
42
|
+
address: string;
|
|
43
|
+
method: string;
|
|
44
|
+
abiJson: string;
|
|
45
|
+
argsJson: string;
|
|
46
|
+
}
|
|
47
|
+
export type TypedDataSignerCallback = (envelope: TypedDataEnvelopeDto) => Promise<string> | string;
|
|
48
|
+
export type Eip1193RequestCallback = (request: {
|
|
49
|
+
method: string;
|
|
50
|
+
params?: unknown[];
|
|
51
|
+
}) => Promise<unknown> | unknown;
|
|
52
|
+
export type DigestSignerCallback = (digest: string) => Promise<string> | string;
|
|
53
|
+
export type CowEip1271SignCallback = (request: CowEip1271SignRequest) => Promise<string> | string;
|
|
54
|
+
export type CustomEip1271Callback = CowEip1271SignCallback;
|
|
55
|
+
export type ContractReadCallback = (request: ContractCallDto) => Promise<string> | string;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,150 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.normalizeError = normalizeError;
|
|
4
|
+
exports.cancelledError = cancelledError;
|
|
5
|
+
exports.invalidInput = invalidInput;
|
|
6
|
+
const knownKinds = new Set([
|
|
7
|
+
"invalidInput",
|
|
8
|
+
"unknownEnumValue",
|
|
9
|
+
"unsupportedChain",
|
|
10
|
+
"walletRequest",
|
|
11
|
+
"walletTimeout",
|
|
12
|
+
"transport",
|
|
13
|
+
"orderbook",
|
|
14
|
+
"subgraph",
|
|
15
|
+
"signing",
|
|
16
|
+
"appData",
|
|
17
|
+
"cancelled",
|
|
18
|
+
"internal",
|
|
19
|
+
"__unknown"
|
|
20
|
+
]);
|
|
21
|
+
function normalizeError(raw) {
|
|
22
|
+
if (isRecord(raw)) {
|
|
23
|
+
// The Rust `WasmError` serializes with serde `rename_all(_fields) = "camelCase"`
|
|
24
|
+
// through a json-compatible serializer, so it already crosses the boundary as a
|
|
25
|
+
// camelCase plain object — no field renaming is needed here.
|
|
26
|
+
const normalized = raw;
|
|
27
|
+
const kind = typeof normalized.kind === "string" ? normalized.kind : undefined;
|
|
28
|
+
if (kind && knownKinds.has(kind)) {
|
|
29
|
+
const schemaVersion = normalized.schemaVersion === "__unknown" ? "__unknown" : "v1";
|
|
30
|
+
if (kind === "__unknown") {
|
|
31
|
+
return {
|
|
32
|
+
schemaVersion,
|
|
33
|
+
kind: "__unknown",
|
|
34
|
+
message: unknownMessage(),
|
|
35
|
+
raw: normalized.raw ?? raw
|
|
36
|
+
};
|
|
37
|
+
}
|
|
38
|
+
return withActionableMessage({
|
|
39
|
+
...normalized,
|
|
40
|
+
schemaVersion,
|
|
41
|
+
kind
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
if (kind) {
|
|
45
|
+
return {
|
|
46
|
+
schemaVersion: normalized.schemaVersion === "__unknown" ? "__unknown" : "v1",
|
|
47
|
+
kind: "__unknown",
|
|
48
|
+
message: unknownMessage(),
|
|
49
|
+
raw
|
|
50
|
+
};
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
if (raw instanceof Error) {
|
|
54
|
+
return (classifyDeserializationFailure(raw.message) ?? {
|
|
55
|
+
schemaVersion: "v1",
|
|
56
|
+
kind: "internal",
|
|
57
|
+
message: internalMessage(raw.message)
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
return (classifyDeserializationFailure(String(raw)) ?? {
|
|
61
|
+
schemaVersion: "v1",
|
|
62
|
+
kind: "internal",
|
|
63
|
+
message: internalMessage(String(raw))
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
// Input-DTO deserialization failures cross the wasm boundary as a plain
|
|
67
|
+
// `Error`: the generated wasm-bindgen glue throws the serde message, so it
|
|
68
|
+
// never carries a structured `kind`. These are CALLER input errors — a value
|
|
69
|
+
// that does not match the documented input type (unknown enum variant,
|
|
70
|
+
// missing/unknown field, wrong type) — not SDK-internal faults, so they must
|
|
71
|
+
// normalize to `invalidInput` rather than `internal` (whose contract implies
|
|
72
|
+
// an SDK bug). The verbatim detail is preserved because it already names the
|
|
73
|
+
// offending field/variant and the expected set, e.g.
|
|
74
|
+
// "unknown variant `teleport`, expected `sell` or `buy`".
|
|
75
|
+
const DESERIALIZATION_FAILURE_PATTERNS = [
|
|
76
|
+
/unknown variant `/,
|
|
77
|
+
/missing field `/,
|
|
78
|
+
/unknown field `/,
|
|
79
|
+
/duplicate field `/,
|
|
80
|
+
/invalid type:/,
|
|
81
|
+
/invalid length\b/,
|
|
82
|
+
/invalid value:/,
|
|
83
|
+
/data did not match any variant/
|
|
84
|
+
];
|
|
85
|
+
function classifyDeserializationFailure(message) {
|
|
86
|
+
if (!DESERIALIZATION_FAILURE_PATTERNS.some((pattern) => pattern.test(message))) {
|
|
87
|
+
return undefined;
|
|
88
|
+
}
|
|
89
|
+
const detail = message.replace(/^Error:\s*/, "");
|
|
90
|
+
const field = detail.match(/(?:missing|unknown|duplicate) field `([^`]+)`/)?.[1];
|
|
91
|
+
const reason = `Invalid SDK input: ${detail}. Check the value against the documented input type and retry.`;
|
|
92
|
+
return field !== undefined
|
|
93
|
+
? { schemaVersion: "v1", kind: "invalidInput", field, message: reason }
|
|
94
|
+
: { schemaVersion: "v1", kind: "invalidInput", message: reason };
|
|
95
|
+
}
|
|
96
|
+
function cancelledError() {
|
|
97
|
+
return {
|
|
98
|
+
schemaVersion: "v1",
|
|
99
|
+
kind: "cancelled",
|
|
100
|
+
message: "Operation was cancelled. Create a fresh AbortController or retry without an already-aborted signal."
|
|
101
|
+
};
|
|
102
|
+
}
|
|
103
|
+
function invalidInput(field, reason) {
|
|
104
|
+
return {
|
|
105
|
+
schemaVersion: "v1",
|
|
106
|
+
kind: "invalidInput",
|
|
107
|
+
field,
|
|
108
|
+
message: `Invalid \`${field}\`: ${reason}. Check the value supplied for \`${field}\` and retry with a valid SDK input.`
|
|
109
|
+
};
|
|
110
|
+
}
|
|
111
|
+
function isRecord(value) {
|
|
112
|
+
return typeof value === "object" && value !== null;
|
|
113
|
+
}
|
|
114
|
+
function withActionableMessage(error) {
|
|
115
|
+
if ("message" in error && typeof error.message === "string" && error.message.length > 0) {
|
|
116
|
+
return error;
|
|
117
|
+
}
|
|
118
|
+
switch (error.kind) {
|
|
119
|
+
case "unknownEnumValue":
|
|
120
|
+
return {
|
|
121
|
+
...error,
|
|
122
|
+
message: `Unsupported value \`${error.value}\` for \`${error.field}\`. Use one of the documented values for this field.`
|
|
123
|
+
};
|
|
124
|
+
case "unsupportedChain":
|
|
125
|
+
return {
|
|
126
|
+
...error,
|
|
127
|
+
message: `Unsupported chain ID ${error.chainId}. Call supportedChainIds() before constructing requests and route unsupported networks to another integration.`
|
|
128
|
+
};
|
|
129
|
+
case "walletTimeout":
|
|
130
|
+
return {
|
|
131
|
+
...error,
|
|
132
|
+
message: `Wallet request timed out after ${error.timeoutMs} ms. Increase walletConfig.timeoutMs or ask the user to approve the wallet request before the timeout.`
|
|
133
|
+
};
|
|
134
|
+
case "cancelled":
|
|
135
|
+
return cancelledError();
|
|
136
|
+
case "__unknown":
|
|
137
|
+
return {
|
|
138
|
+
...error,
|
|
139
|
+
message: unknownMessage()
|
|
140
|
+
};
|
|
141
|
+
default:
|
|
142
|
+
return error;
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
function internalMessage(detail) {
|
|
146
|
+
return `SDK internal error: ${detail}. This indicates serialization or invariant failure; retry with the same inputs only after checking the reported input shape.`;
|
|
147
|
+
}
|
|
148
|
+
function unknownMessage() {
|
|
149
|
+
return "SDK received an unrecognized error variant. Inspect raw, preserve it in logs without credentials, and update the SDK if the variant is now documented.";
|
|
150
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
import type { SchemaVersion } from "./envelope.js";
|
|
2
|
+
/**
|
|
3
|
+
* Coarse, switchable classification of an orderbook rejection. Lets a consumer
|
|
4
|
+
* branch on the action a rejection calls for — fix the request, fund the
|
|
5
|
+
* wallet, re-quote, wait, or escalate — without matching every wire tag. The
|
|
6
|
+
* `__unknown` member is the forward-compatible sentinel for a category a newer
|
|
7
|
+
* SDK may introduce.
|
|
8
|
+
*/
|
|
9
|
+
export type OrderBookRejectionCategory = "authorization" | "insufficientFunds" | "invalidOrder" | "notFound" | "conflict" | "unfulfillable" | "server" | "__unknown";
|
|
10
|
+
export type CowError = {
|
|
11
|
+
schemaVersion: "v1";
|
|
12
|
+
kind: "invalidInput";
|
|
13
|
+
message: string;
|
|
14
|
+
field?: string;
|
|
15
|
+
} | {
|
|
16
|
+
schemaVersion: "v1";
|
|
17
|
+
kind: "unknownEnumValue";
|
|
18
|
+
message: string;
|
|
19
|
+
field: string;
|
|
20
|
+
value: string;
|
|
21
|
+
} | {
|
|
22
|
+
schemaVersion: "v1";
|
|
23
|
+
kind: "unsupportedChain";
|
|
24
|
+
message: string;
|
|
25
|
+
chainId: number;
|
|
26
|
+
} | {
|
|
27
|
+
schemaVersion: "v1";
|
|
28
|
+
kind: "walletRequest";
|
|
29
|
+
method: string;
|
|
30
|
+
code?: number;
|
|
31
|
+
message: string;
|
|
32
|
+
} | {
|
|
33
|
+
schemaVersion: "v1";
|
|
34
|
+
kind: "walletTimeout";
|
|
35
|
+
message: string;
|
|
36
|
+
timeoutMs: number;
|
|
37
|
+
} | {
|
|
38
|
+
schemaVersion: "v1";
|
|
39
|
+
kind: "transport";
|
|
40
|
+
class: string;
|
|
41
|
+
message: string;
|
|
42
|
+
status?: number;
|
|
43
|
+
headers?: [string, string][];
|
|
44
|
+
body?: string;
|
|
45
|
+
} | {
|
|
46
|
+
schemaVersion: "v1";
|
|
47
|
+
kind: "orderbook";
|
|
48
|
+
code?: string;
|
|
49
|
+
category?: OrderBookRejectionCategory;
|
|
50
|
+
message: string;
|
|
51
|
+
retryable: boolean;
|
|
52
|
+
retryAfterMs?: number;
|
|
53
|
+
} | {
|
|
54
|
+
schemaVersion: "v1";
|
|
55
|
+
kind: "subgraph";
|
|
56
|
+
message: string;
|
|
57
|
+
} | {
|
|
58
|
+
schemaVersion: "v1";
|
|
59
|
+
kind: "signing";
|
|
60
|
+
message: string;
|
|
61
|
+
} | {
|
|
62
|
+
schemaVersion: "v1";
|
|
63
|
+
kind: "appData";
|
|
64
|
+
class?: string;
|
|
65
|
+
message: string;
|
|
66
|
+
} | {
|
|
67
|
+
schemaVersion: "v1";
|
|
68
|
+
kind: "cancelled";
|
|
69
|
+
message: string;
|
|
70
|
+
} | {
|
|
71
|
+
schemaVersion: "v1";
|
|
72
|
+
kind: "internal";
|
|
73
|
+
message: string;
|
|
74
|
+
} | {
|
|
75
|
+
schemaVersion: SchemaVersion;
|
|
76
|
+
kind: "__unknown";
|
|
77
|
+
message: string;
|
|
78
|
+
raw: unknown;
|
|
79
|
+
};
|
|
80
|
+
export declare function normalizeError(raw: unknown): CowError;
|
|
81
|
+
export declare function cancelledError(): CowError;
|
|
82
|
+
export declare function invalidInput(field: string, reason: string): CowError;
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
const knownKinds = new Set([
|
|
2
|
+
"invalidInput",
|
|
3
|
+
"unknownEnumValue",
|
|
4
|
+
"unsupportedChain",
|
|
5
|
+
"walletRequest",
|
|
6
|
+
"walletTimeout",
|
|
7
|
+
"transport",
|
|
8
|
+
"orderbook",
|
|
9
|
+
"subgraph",
|
|
10
|
+
"signing",
|
|
11
|
+
"appData",
|
|
12
|
+
"cancelled",
|
|
13
|
+
"internal",
|
|
14
|
+
"__unknown"
|
|
15
|
+
]);
|
|
16
|
+
export function normalizeError(raw) {
|
|
17
|
+
if (isRecord(raw)) {
|
|
18
|
+
// The Rust `WasmError` serializes with serde `rename_all(_fields) = "camelCase"`
|
|
19
|
+
// through a json-compatible serializer, so it already crosses the boundary as a
|
|
20
|
+
// camelCase plain object — no field renaming is needed here.
|
|
21
|
+
const normalized = raw;
|
|
22
|
+
const kind = typeof normalized.kind === "string" ? normalized.kind : undefined;
|
|
23
|
+
if (kind && knownKinds.has(kind)) {
|
|
24
|
+
const schemaVersion = normalized.schemaVersion === "__unknown" ? "__unknown" : "v1";
|
|
25
|
+
if (kind === "__unknown") {
|
|
26
|
+
return {
|
|
27
|
+
schemaVersion,
|
|
28
|
+
kind: "__unknown",
|
|
29
|
+
message: unknownMessage(),
|
|
30
|
+
raw: normalized.raw ?? raw
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
return withActionableMessage({
|
|
34
|
+
...normalized,
|
|
35
|
+
schemaVersion,
|
|
36
|
+
kind
|
|
37
|
+
});
|
|
38
|
+
}
|
|
39
|
+
if (kind) {
|
|
40
|
+
return {
|
|
41
|
+
schemaVersion: normalized.schemaVersion === "__unknown" ? "__unknown" : "v1",
|
|
42
|
+
kind: "__unknown",
|
|
43
|
+
message: unknownMessage(),
|
|
44
|
+
raw
|
|
45
|
+
};
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
if (raw instanceof Error) {
|
|
49
|
+
return (classifyDeserializationFailure(raw.message) ?? {
|
|
50
|
+
schemaVersion: "v1",
|
|
51
|
+
kind: "internal",
|
|
52
|
+
message: internalMessage(raw.message)
|
|
53
|
+
});
|
|
54
|
+
}
|
|
55
|
+
return (classifyDeserializationFailure(String(raw)) ?? {
|
|
56
|
+
schemaVersion: "v1",
|
|
57
|
+
kind: "internal",
|
|
58
|
+
message: internalMessage(String(raw))
|
|
59
|
+
});
|
|
60
|
+
}
|
|
61
|
+
// Input-DTO deserialization failures cross the wasm boundary as a plain
|
|
62
|
+
// `Error`: the generated wasm-bindgen glue throws the serde message, so it
|
|
63
|
+
// never carries a structured `kind`. These are CALLER input errors — a value
|
|
64
|
+
// that does not match the documented input type (unknown enum variant,
|
|
65
|
+
// missing/unknown field, wrong type) — not SDK-internal faults, so they must
|
|
66
|
+
// normalize to `invalidInput` rather than `internal` (whose contract implies
|
|
67
|
+
// an SDK bug). The verbatim detail is preserved because it already names the
|
|
68
|
+
// offending field/variant and the expected set, e.g.
|
|
69
|
+
// "unknown variant `teleport`, expected `sell` or `buy`".
|
|
70
|
+
const DESERIALIZATION_FAILURE_PATTERNS = [
|
|
71
|
+
/unknown variant `/,
|
|
72
|
+
/missing field `/,
|
|
73
|
+
/unknown field `/,
|
|
74
|
+
/duplicate field `/,
|
|
75
|
+
/invalid type:/,
|
|
76
|
+
/invalid length\b/,
|
|
77
|
+
/invalid value:/,
|
|
78
|
+
/data did not match any variant/
|
|
79
|
+
];
|
|
80
|
+
function classifyDeserializationFailure(message) {
|
|
81
|
+
if (!DESERIALIZATION_FAILURE_PATTERNS.some((pattern) => pattern.test(message))) {
|
|
82
|
+
return undefined;
|
|
83
|
+
}
|
|
84
|
+
const detail = message.replace(/^Error:\s*/, "");
|
|
85
|
+
const field = detail.match(/(?:missing|unknown|duplicate) field `([^`]+)`/)?.[1];
|
|
86
|
+
const reason = `Invalid SDK input: ${detail}. Check the value against the documented input type and retry.`;
|
|
87
|
+
return field !== undefined
|
|
88
|
+
? { schemaVersion: "v1", kind: "invalidInput", field, message: reason }
|
|
89
|
+
: { schemaVersion: "v1", kind: "invalidInput", message: reason };
|
|
90
|
+
}
|
|
91
|
+
export function cancelledError() {
|
|
92
|
+
return {
|
|
93
|
+
schemaVersion: "v1",
|
|
94
|
+
kind: "cancelled",
|
|
95
|
+
message: "Operation was cancelled. Create a fresh AbortController or retry without an already-aborted signal."
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
export function invalidInput(field, reason) {
|
|
99
|
+
return {
|
|
100
|
+
schemaVersion: "v1",
|
|
101
|
+
kind: "invalidInput",
|
|
102
|
+
field,
|
|
103
|
+
message: `Invalid \`${field}\`: ${reason}. Check the value supplied for \`${field}\` and retry with a valid SDK input.`
|
|
104
|
+
};
|
|
105
|
+
}
|
|
106
|
+
function isRecord(value) {
|
|
107
|
+
return typeof value === "object" && value !== null;
|
|
108
|
+
}
|
|
109
|
+
function withActionableMessage(error) {
|
|
110
|
+
if ("message" in error && typeof error.message === "string" && error.message.length > 0) {
|
|
111
|
+
return error;
|
|
112
|
+
}
|
|
113
|
+
switch (error.kind) {
|
|
114
|
+
case "unknownEnumValue":
|
|
115
|
+
return {
|
|
116
|
+
...error,
|
|
117
|
+
message: `Unsupported value \`${error.value}\` for \`${error.field}\`. Use one of the documented values for this field.`
|
|
118
|
+
};
|
|
119
|
+
case "unsupportedChain":
|
|
120
|
+
return {
|
|
121
|
+
...error,
|
|
122
|
+
message: `Unsupported chain ID ${error.chainId}. Call supportedChainIds() before constructing requests and route unsupported networks to another integration.`
|
|
123
|
+
};
|
|
124
|
+
case "walletTimeout":
|
|
125
|
+
return {
|
|
126
|
+
...error,
|
|
127
|
+
message: `Wallet request timed out after ${error.timeoutMs} ms. Increase walletConfig.timeoutMs or ask the user to approve the wallet request before the timeout.`
|
|
128
|
+
};
|
|
129
|
+
case "cancelled":
|
|
130
|
+
return cancelledError();
|
|
131
|
+
case "__unknown":
|
|
132
|
+
return {
|
|
133
|
+
...error,
|
|
134
|
+
message: unknownMessage()
|
|
135
|
+
};
|
|
136
|
+
default:
|
|
137
|
+
return error;
|
|
138
|
+
}
|
|
139
|
+
}
|
|
140
|
+
function internalMessage(detail) {
|
|
141
|
+
return `SDK internal error: ${detail}. This indicates serialization or invariant failure; retry with the same inputs only after checking the reported input shape.`;
|
|
142
|
+
}
|
|
143
|
+
function unknownMessage() {
|
|
144
|
+
return "SDK received an unrecognized error variant. Inspect raw, preserve it in logs without credentials, and update the SDK if the variant is now documented.";
|
|
145
|
+
}
|