@hodlmarkets/sdk 0.1.3 → 0.1.4
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/dist/errors.cjs +32 -3
- package/dist/errors.cjs.map +1 -1
- package/dist/errors.d.cts +9 -0
- package/dist/errors.d.ts +9 -0
- package/dist/errors.js +32 -3
- package/dist/errors.js.map +1 -1
- package/dist/index.cjs +32 -3
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +32 -3
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/errors.cjs
CHANGED
|
@@ -28,10 +28,39 @@ var HODL_ERRORS = {
|
|
|
28
28
|
8018: "vAMM is not active",
|
|
29
29
|
8020: "Invalid amount"
|
|
30
30
|
};
|
|
31
|
+
function fromCode(code) {
|
|
32
|
+
if (code in HODL_ERRORS) return new HodlError(code, HODL_ERRORS[code]);
|
|
33
|
+
return null;
|
|
34
|
+
}
|
|
31
35
|
function parseHodlError(err) {
|
|
32
|
-
if (err
|
|
33
|
-
|
|
34
|
-
|
|
36
|
+
if (!err || typeof err !== "object") return null;
|
|
37
|
+
const e = err;
|
|
38
|
+
const ixErr = e.InstructionError ?? e.error?.InstructionError ?? e.transactionError?.InstructionError;
|
|
39
|
+
if (Array.isArray(ixErr) && ixErr.length === 2) {
|
|
40
|
+
const custom = ixErr[1]?.Custom;
|
|
41
|
+
if (typeof custom === "number") {
|
|
42
|
+
const result = fromCode(custom);
|
|
43
|
+
if (result) return result;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
const anchorCode = e.error?.errorCode?.number ?? e.errorCode?.number;
|
|
47
|
+
if (typeof anchorCode === "number") {
|
|
48
|
+
const result = fromCode(anchorCode);
|
|
49
|
+
if (result) return result;
|
|
50
|
+
}
|
|
51
|
+
const logs = e.logs ?? e.transactionLogs ?? [];
|
|
52
|
+
if (Array.isArray(logs)) {
|
|
53
|
+
for (const log of logs) {
|
|
54
|
+
if (typeof log !== "string") continue;
|
|
55
|
+
const match = log.match(/Error Number: (\d+)/);
|
|
56
|
+
if (match) {
|
|
57
|
+
const result = fromCode(Number(match[1]));
|
|
58
|
+
if (result) return result;
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
if ("code" in e && typeof e.code === "number") {
|
|
63
|
+
return fromCode(e.code);
|
|
35
64
|
}
|
|
36
65
|
return null;
|
|
37
66
|
}
|
package/dist/errors.cjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/errors.ts"],"names":[],"mappings":";;;AAAO,IAAM,SAAA,GAAN,cAAwB,KAAA,CAAM;AAAA,EACnC,WAAA,CAA4B,MAAc,OAAA,EAAiB;AACzD,IAAA,KAAA,CAAM,OAAO,CAAA;AADa,IAAA,IAAA,CAAA,IAAA,GAAA,IAAA;AAE1B,IAAA,IAAA,CAAK,IAAA,GAAO,WAAA;AAAA,EACd;AACF;AAEO,IAAM,WAAA,GAAsC;AAAA,EACjD,IAAA,EAAM,sCAAA;AAAA,EACN,IAAA,EAAM,qBAAA;AAAA,EACN,IAAA,EAAM,sCAAA;AAAA,EACN,IAAA,EAAM,4BAAA;AAAA,EACN,IAAA,EAAM,6BAAA;AAAA,EACN,IAAA,EAAM,mDAAA;AAAA,EACN,IAAA,EAAM,8BAAA;AAAA,EACN,IAAA,EAAM,mDAAA;AAAA,EACN,IAAA,EAAM,4BAAA;AAAA,EACN,IAAA,EAAM,qDAAA;AAAA,EACN,IAAA,EAAM,8BAAA;AAAA,EACN,IAAA,EAAM,mCAAA;AAAA,EACN,IAAA,EAAM,sBAAA;AAAA,EACN,IAAA,EAAM,0CAAA;AAAA,EACN,IAAA,EAAM,gBAAA;AAAA,EACN,IAAA,EAAM,wBAAA;AAAA,EACN,IAAA,EAAM,oBAAA;AAAA,EACN,IAAA,EAAM;AACR;
|
|
1
|
+
{"version":3,"sources":["../src/errors.ts"],"names":[],"mappings":";;;AAAO,IAAM,SAAA,GAAN,cAAwB,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AAM,WAAW,CAAA,EAAG;AAC9C,IAAA,MAAM,MAAA,GAAS,KAAA,CAAM,CAAC,CAAA,EAAG,MAAA;AACzB,IAAA,IAAI,OAAO,WAAW,QAAA,EAAU;AAC9B,MAAA,MAAM,MAAA,GAAS,SAAS,MAAM,CAAA;AAC9B,MAAA,IAAI,QAAQ,OAAO,MAAA;AAAA,IACrB;AAAA,EACF;AAGA,EAAA,MAAM,aAAa,CAAA,CAAE,KAAA,EAAO,SAAA,EAAW,MAAA,IAAU,EAAE,SAAA,EAAW,MAAA;AAC9D,EAAA,IAAI,OAAO,eAAe,QAAA,EAAU;AAClC,IAAA,MAAM,MAAA,GAAS,SAAS,UAAU,CAAA;AAClC,IAAA,IAAI,QAAQ,OAAO,MAAA;AAAA,EACrB;AAGA,EAAA,MAAM,IAAA,GAAkB,CAAA,CAAE,IAAA,IAAQ,CAAA,CAAE,mBAAmB,EAAC;AACxD,EAAA,IAAI,KAAA,CAAM,OAAA,CAAQ,IAAI,CAAA,EAAG;AACvB,IAAA,KAAA,MAAW,OAAO,IAAA,EAAM;AACtB,MAAA,IAAI,OAAO,QAAQ,QAAA,EAAU;AAC7B,MAAA,MAAM,KAAA,GAAQ,GAAA,CAAI,KAAA,CAAM,qBAAqB,CAAA;AAC7C,MAAA,IAAI,KAAA,EAAO;AACT,QAAA,MAAM,SAAS,QAAA,CAAS,MAAA,CAAO,KAAA,CAAM,CAAC,CAAC,CAAC,CAAA;AACxC,QAAA,IAAI,QAAQ,OAAO,MAAA;AAAA,MACrB;AAAA,IACF;AAAA,EACF;AAGA,EAAA,IAAI,MAAA,IAAU,CAAA,IAAK,OAAO,CAAA,CAAE,SAAS,QAAA,EAAU;AAC7C,IAAA,OAAO,QAAA,CAAS,EAAE,IAAI,CAAA;AAAA,EACxB;AAEA,EAAA,OAAO,IAAA;AACT","file":"errors.cjs","sourcesContent":["export class HodlError extends Error {\n constructor(public readonly code: number, message: string) {\n super(message)\n this.name = 'HodlError'\n }\n}\n\nexport const HODL_ERRORS: Record<number, string> = {\n 7005: 'Insufficient token reserves in vault',\n 7100: 'Arithmetic overflow',\n 7102: 'Division by zero or invalid division',\n 7104: 'Token calculation overflow',\n 7105: 'Token calculation underflow',\n 7200: 'Slippage exceeded: output below minimum threshold',\n 7312: 'Accrual calculation overflow',\n 7315: 'Position is not empty, must sell all tokens first',\n 7319: 'Invalid protocol authority',\n 7321: 'Position must have tokens to perform this operation',\n 7500: 'Position not owned by signer',\n 7505: 'SOL vault PDA derivation mismatch',\n 7601: 'Invalid vesting type',\n 7604: 'Invalid percentage value (must be 0-100)',\n 7901: 'Not authorized',\n 8014: 'Insufficient liquidity',\n 8018: 'vAMM is not active',\n 8020: 'Invalid amount',\n}\n\nfunction fromCode(code: number): HodlError | null {\n if (code in HODL_ERRORS) return new HodlError(code, HODL_ERRORS[code])\n return null\n}\n\n/**\n * Parse a HODL program error from any Solana error shape.\n *\n * Handles:\n * - SendTransactionError with InstructionError: [index, { Custom: code }]\n * - Anchor-style errors with error.errorCode.number\n * - Transaction logs containing \"Error Number: <code>\"\n * - Plain { code } objects\n */\nexport function parseHodlError(err: unknown): HodlError | null {\n if (!err || typeof err !== 'object') return null\n\n const e = err as Record<string, any>\n\n // 1. InstructionError: [index, { Custom: code }]\n // From SendTransactionError.transactionError or err.error\n const ixErr = e.InstructionError ?? e.error?.InstructionError\n ?? e.transactionError?.InstructionError\n if (Array.isArray(ixErr) && ixErr.length === 2) {\n const custom = ixErr[1]?.Custom\n if (typeof custom === 'number') {\n const result = fromCode(custom)\n if (result) return result\n }\n }\n\n // 2. Anchor errorCode.number (from AnchorError)\n const anchorCode = e.error?.errorCode?.number ?? e.errorCode?.number\n if (typeof anchorCode === 'number') {\n const result = fromCode(anchorCode)\n if (result) return result\n }\n\n // 3. Transaction logs: \"Error Number: 7601\"\n const logs: unknown[] = e.logs ?? e.transactionLogs ?? []\n if (Array.isArray(logs)) {\n for (const log of logs) {\n if (typeof log !== 'string') continue\n const match = log.match(/Error Number: (\\d+)/)\n if (match) {\n const result = fromCode(Number(match[1]))\n if (result) return result\n }\n }\n }\n\n // 4. Direct { code } property\n if ('code' in e && typeof e.code === 'number') {\n return fromCode(e.code)\n }\n\n return null\n}\n"]}
|
package/dist/errors.d.cts
CHANGED
|
@@ -3,6 +3,15 @@ declare class HodlError extends Error {
|
|
|
3
3
|
constructor(code: number, message: string);
|
|
4
4
|
}
|
|
5
5
|
declare const HODL_ERRORS: Record<number, string>;
|
|
6
|
+
/**
|
|
7
|
+
* Parse a HODL program error from any Solana error shape.
|
|
8
|
+
*
|
|
9
|
+
* Handles:
|
|
10
|
+
* - SendTransactionError with InstructionError: [index, { Custom: code }]
|
|
11
|
+
* - Anchor-style errors with error.errorCode.number
|
|
12
|
+
* - Transaction logs containing "Error Number: <code>"
|
|
13
|
+
* - Plain { code } objects
|
|
14
|
+
*/
|
|
6
15
|
declare function parseHodlError(err: unknown): HodlError | null;
|
|
7
16
|
|
|
8
17
|
export { HODL_ERRORS, HodlError, parseHodlError };
|
package/dist/errors.d.ts
CHANGED
|
@@ -3,6 +3,15 @@ declare class HodlError extends Error {
|
|
|
3
3
|
constructor(code: number, message: string);
|
|
4
4
|
}
|
|
5
5
|
declare const HODL_ERRORS: Record<number, string>;
|
|
6
|
+
/**
|
|
7
|
+
* Parse a HODL program error from any Solana error shape.
|
|
8
|
+
*
|
|
9
|
+
* Handles:
|
|
10
|
+
* - SendTransactionError with InstructionError: [index, { Custom: code }]
|
|
11
|
+
* - Anchor-style errors with error.errorCode.number
|
|
12
|
+
* - Transaction logs containing "Error Number: <code>"
|
|
13
|
+
* - Plain { code } objects
|
|
14
|
+
*/
|
|
6
15
|
declare function parseHodlError(err: unknown): HodlError | null;
|
|
7
16
|
|
|
8
17
|
export { HODL_ERRORS, HodlError, parseHodlError };
|
package/dist/errors.js
CHANGED
|
@@ -26,10 +26,39 @@ var HODL_ERRORS = {
|
|
|
26
26
|
8018: "vAMM is not active",
|
|
27
27
|
8020: "Invalid amount"
|
|
28
28
|
};
|
|
29
|
+
function fromCode(code) {
|
|
30
|
+
if (code in HODL_ERRORS) return new HodlError(code, HODL_ERRORS[code]);
|
|
31
|
+
return null;
|
|
32
|
+
}
|
|
29
33
|
function parseHodlError(err) {
|
|
30
|
-
if (err
|
|
31
|
-
|
|
32
|
-
|
|
34
|
+
if (!err || typeof err !== "object") return null;
|
|
35
|
+
const e = err;
|
|
36
|
+
const ixErr = e.InstructionError ?? e.error?.InstructionError ?? e.transactionError?.InstructionError;
|
|
37
|
+
if (Array.isArray(ixErr) && ixErr.length === 2) {
|
|
38
|
+
const custom = ixErr[1]?.Custom;
|
|
39
|
+
if (typeof custom === "number") {
|
|
40
|
+
const result = fromCode(custom);
|
|
41
|
+
if (result) return result;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
const anchorCode = e.error?.errorCode?.number ?? e.errorCode?.number;
|
|
45
|
+
if (typeof anchorCode === "number") {
|
|
46
|
+
const result = fromCode(anchorCode);
|
|
47
|
+
if (result) return result;
|
|
48
|
+
}
|
|
49
|
+
const logs = e.logs ?? e.transactionLogs ?? [];
|
|
50
|
+
if (Array.isArray(logs)) {
|
|
51
|
+
for (const log of logs) {
|
|
52
|
+
if (typeof log !== "string") continue;
|
|
53
|
+
const match = log.match(/Error Number: (\d+)/);
|
|
54
|
+
if (match) {
|
|
55
|
+
const result = fromCode(Number(match[1]));
|
|
56
|
+
if (result) return result;
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if ("code" in e && typeof e.code === "number") {
|
|
61
|
+
return fromCode(e.code);
|
|
33
62
|
}
|
|
34
63
|
return null;
|
|
35
64
|
}
|
package/dist/errors.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/errors.ts"],"names":[],"mappings":";AAAO,IAAM,SAAA,GAAN,cAAwB,KAAA,CAAM;AAAA,EACnC,WAAA,CAA4B,MAAc,OAAA,EAAiB;AACzD,IAAA,KAAA,CAAM,OAA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ACrB;AAGA,EAAA,MAAM,IAAA,GAAkB,CAAA,CAAE,IAAA,IAAQ,CAAA,CAAE,mBAAmB,EAAC;AACxD,EAAA,IAAI,KAAA,CAAM,OAAA,CAAQ,IAAI,CAAA,EAAG;AACvB,IAAA,KAAA,MAAW,OAAO,IAAA,EAAM;AACtB,MAAA,IAAI,OAAO,QAAQ,QAAA,EAAU;AAC7B,MAAA,MAAM,KAAA,GAAQ,GAAA,CAAI,KAAA,CAAM,qBAAqB,CAAA;AAC7C,MAAA,IAAI,KAAA,EAAO;AACT,QAAA,MAAM,SAAS,QAAA,CAAS,MAAA,CAAO,KAAA,CAAM,CAAC,CAAC,CAAC,CAAA;AACxC,QAAA,IAAI,QAAQ,OAAO,MAAA;AAAA,MACrB;AAAA,IACF;AAAA,EACF;AAGA,EAAA,IAAI,MAAA,IAAU,CAAA,IAAK,OAAO,CAAA,CAAE,SAAS,QAAA,EAAU;AAC7C,IAAA,OAAO,QAAA,CAAS,EAAE,IAAI,CAAA;AAAA,EACxB;AAEA,EAAA,OAAO,IAAA;AACT","file":"errors.js","sourcesContent":["export class HodlError extends Error {\n constructor(public readonly code: number, message: string) {\n super(message)\n this.name = 'HodlError'\n }\n}\n\nexport const HODL_ERRORS: Record<number, string> = {\n 7005: 'Insufficient token reserves in vault',\n 7100: 'Arithmetic overflow',\n 7102: 'Division by zero or invalid division',\n 7104: 'Token calculation overflow',\n 7105: 'Token calculation underflow',\n 7200: 'Slippage exceeded: output below minimum threshold',\n 7312: 'Accrual calculation overflow',\n 7315: 'Position is not empty, must sell all tokens first',\n 7319: 'Invalid protocol authority',\n 7321: 'Position must have tokens to perform this operation',\n 7500: 'Position not owned by signer',\n 7505: 'SOL vault PDA derivation mismatch',\n 7601: 'Invalid vesting type',\n 7604: 'Invalid percentage value (must be 0-100)',\n 7901: 'Not authorized',\n 8014: 'Insufficient liquidity',\n 8018: 'vAMM is not active',\n 8020: 'Invalid amount',\n}\n\nfunction fromCode(code: number): HodlError | null {\n if (code in HODL_ERRORS) return new HodlError(code, HODL_ERRORS[code])\n return null\n}\n\n/**\n * Parse a HODL program error from any Solana error shape.\n *\n * Handles:\n * - SendTransactionError with InstructionError: [index, { Custom: code }]\n * - Anchor-style errors with error.errorCode.number\n * - Transaction logs containing \"Error Number: <code>\"\n * - Plain { code } objects\n */\nexport function parseHodlError(err: unknown): HodlError | null {\n if (!err || typeof err !== 'object') return null\n\n const e = err as Record<string, any>\n\n // 1. InstructionError: [index, { Custom: code }]\n // From SendTransactionError.transactionError or err.error\n const ixErr = e.InstructionError ?? e.error?.InstructionError\n ?? e.transactionError?.InstructionError\n if (Array.isArray(ixErr) && ixErr.length === 2) {\n const custom = ixErr[1]?.Custom\n if (typeof custom === 'number') {\n const result = fromCode(custom)\n if (result) return result\n }\n }\n\n // 2. Anchor errorCode.number (from AnchorError)\n const anchorCode = e.error?.errorCode?.number ?? e.errorCode?.number\n if (typeof anchorCode === 'number') {\n const result = fromCode(anchorCode)\n if (result) return result\n }\n\n // 3. Transaction logs: \"Error Number: 7601\"\n const logs: unknown[] = e.logs ?? e.transactionLogs ?? []\n if (Array.isArray(logs)) {\n for (const log of logs) {\n if (typeof log !== 'string') continue\n const match = log.match(/Error Number: (\\d+)/)\n if (match) {\n const result = fromCode(Number(match[1]))\n if (result) return result\n }\n }\n }\n\n // 4. Direct { code } property\n if ('code' in e && typeof e.code === 'number') {\n return fromCode(e.code)\n }\n\n return null\n}\n"]}
|
package/dist/index.cjs
CHANGED
|
@@ -313,10 +313,39 @@ var HODL_ERRORS = {
|
|
|
313
313
|
8018: "vAMM is not active",
|
|
314
314
|
8020: "Invalid amount"
|
|
315
315
|
};
|
|
316
|
+
function fromCode(code) {
|
|
317
|
+
if (code in HODL_ERRORS) return new HodlError(code, HODL_ERRORS[code]);
|
|
318
|
+
return null;
|
|
319
|
+
}
|
|
316
320
|
function parseHodlError(err) {
|
|
317
|
-
if (err
|
|
318
|
-
|
|
319
|
-
|
|
321
|
+
if (!err || typeof err !== "object") return null;
|
|
322
|
+
const e = err;
|
|
323
|
+
const ixErr = e.InstructionError ?? e.error?.InstructionError ?? e.transactionError?.InstructionError;
|
|
324
|
+
if (Array.isArray(ixErr) && ixErr.length === 2) {
|
|
325
|
+
const custom = ixErr[1]?.Custom;
|
|
326
|
+
if (typeof custom === "number") {
|
|
327
|
+
const result = fromCode(custom);
|
|
328
|
+
if (result) return result;
|
|
329
|
+
}
|
|
330
|
+
}
|
|
331
|
+
const anchorCode = e.error?.errorCode?.number ?? e.errorCode?.number;
|
|
332
|
+
if (typeof anchorCode === "number") {
|
|
333
|
+
const result = fromCode(anchorCode);
|
|
334
|
+
if (result) return result;
|
|
335
|
+
}
|
|
336
|
+
const logs = e.logs ?? e.transactionLogs ?? [];
|
|
337
|
+
if (Array.isArray(logs)) {
|
|
338
|
+
for (const log of logs) {
|
|
339
|
+
if (typeof log !== "string") continue;
|
|
340
|
+
const match = log.match(/Error Number: (\d+)/);
|
|
341
|
+
if (match) {
|
|
342
|
+
const result = fromCode(Number(match[1]));
|
|
343
|
+
if (result) return result;
|
|
344
|
+
}
|
|
345
|
+
}
|
|
346
|
+
}
|
|
347
|
+
if ("code" in e && typeof e.code === "number") {
|
|
348
|
+
return fromCode(e.code);
|
|
320
349
|
}
|
|
321
350
|
return null;
|
|
322
351
|
}
|