@cubee_ee/sdk 0.2.1 → 0.2.3

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.
@@ -1,23 +1,61 @@
1
+ import cubicPoolIdl from "../idl/cubic_pool.json";
1
2
  import { SdkError, SdkErrorCode } from "../types/result";
2
3
 
3
- const CONTRACT_ERROR_MAP: Record<number, { code: SdkErrorCode; message: string }> = {
4
- // cubic-pool error codes (see programs/cubic-pool/src/errors.rs)
5
- 6000: { code: "invalid_input", message: "Invalid token count" },
6
- 6001: { code: "invalid_input", message: "Invalid token index" },
7
- 6002: { code: "invalid_input", message: "Invalid weights — must sum to 10000" },
8
- 6003: { code: "invalid_input", message: "Invalid virtual balances" },
9
- 6004: { code: "insufficient_funds", message: "Insufficient pool liquidity" },
10
- 6005: { code: "slippage_exceeded", message: "Slippage tolerance exceeded" },
11
- 6006: { code: "invalid_input", message: "Invalid token amounts" },
12
- 6007: { code: "slippage_exceeded", message: "BPT received is below the minimum requested" },
13
- 6009: { code: "invalid_input", message: "Swap fee rate exceeds the allowed maximum" },
14
- 6011: { code: "math_overflow", message: "Math overflow" },
15
- 6012: { code: "math_overflow", message: "Math underflow" },
16
- 6022: { code: "pool_disabled", message: "Pool is disabled" },
17
- 6023: { code: "swaps_disabled", message: "Swaps are disabled on this pool" },
18
- 6025: { code: "invalid_input", message: "Zero amount" },
4
+ const ANCHOR_NAME_TO_SDK: Record<string, SdkErrorCode> = {
5
+ InvalidTokenCount: "invalid_input",
6
+ InvalidTokenIndex: "invalid_input",
7
+ InvalidWeights: "invalid_input",
8
+ InvalidVirtualBalances: "invalid_input",
9
+ InsufficientLiquidity: "insufficient_funds",
10
+ SlippageExceeded: "slippage_exceeded",
11
+ InvalidAmounts: "invalid_input",
12
+ InsufficientBptOut: "slippage_exceeded",
13
+ InsufficientTokensOut: "slippage_exceeded",
14
+ FeeRateMaxExceeded: "invalid_input",
15
+ ProtocolFeeRateMaxExceeded: "invalid_input",
16
+ MathOverflow: "math_overflow",
17
+ MathUnderflow: "math_overflow",
18
+ DivisionByZero: "math_overflow",
19
+ TokenMintMismatch: "invalid_input",
20
+ InvalidTokenDecimals: "invalid_input",
21
+ AmountOutExceedsBalance: "invalid_input",
22
+ InvalidBptAmount: "invalid_input",
23
+ Unauthorized: "invalid_input",
24
+ InvalidMint: "invalid_input",
25
+ PoolDisabled: "pool_disabled",
26
+ SwapsDisabled: "swaps_disabled",
27
+ PoolMustBeDisabled: "invalid_input",
28
+ ZeroAmount: "invalid_input",
29
+ ZeroFeeAmount: "invalid_input",
30
+ BannedExtension: "invalid_input",
31
+ TokenProgramMismatch: "invalid_input",
32
+ UserTokenAccountOwnerMismatch: "invalid_input",
33
+ InitialLiquidityTooSmall: "invalid_input",
34
+ InvalidTokenProgram: "invalid_input",
35
+ InvalidVault: "invalid_input",
36
+ InvalidSourceOwner: "invalid_input",
37
+ WouldBreakRentExempt: "invalid_input",
38
+ PoolAdminDisabled: "invalid_input",
39
+ ProtocolAdminUnset: "invalid_input",
40
+ NoPendingAdmin: "invalid_input",
41
+ NotPendingAdmin: "invalid_input",
42
+ InvalidPendingAdmin: "invalid_input",
43
+ ProtocolAdminNotTreasury: "invalid_input",
19
44
  };
20
45
 
46
+ const CONTRACT_ERROR_MAP: Record<number, { code: SdkErrorCode; message: string }> =
47
+ Object.fromEntries(
48
+ (cubicPoolIdl.errors as Array<{ code: number; name: string; msg: string }>).map(
49
+ (anchorError) => [
50
+ anchorError.code,
51
+ {
52
+ code: ANCHOR_NAME_TO_SDK[anchorError.name] ?? "invalid_input",
53
+ message: anchorError.msg,
54
+ },
55
+ ]
56
+ )
57
+ );
58
+
21
59
  /**
22
60
  * Convert a raw error (possibly from Anchor, web3.js, or fetch) into a
23
61
  * cleanly-typed SdkError. Used by safeCall wrappers so callers always
@@ -39,7 +77,7 @@ export function toSdkError(cause: unknown): SdkError {
39
77
  return { code: "rpc_timeout", humanMessage: "RPC timed out", cause };
40
78
  }
41
79
  if (/429|rate limit/i.test(msg)) {
42
- return { code: "rpc_rate_limited", humanMessage: "RPC rate-limited slow down", cause };
80
+ return { code: "rpc_rate_limited", humanMessage: "RPC rate-limited - slow down", cause };
43
81
  }
44
82
  if (/fetch failed|ECONNREFUSED|ENOTFOUND|Proxy error|-32056|-32052|403/i.test(msg)) {
45
83
  return { code: "rpc_unavailable", humanMessage: "RPC endpoint unreachable", cause };
@@ -76,3 +114,8 @@ function extractErrorCode(msg: string): number | null {
76
114
  if (dec) return parseInt(dec[1], 10);
77
115
  return null;
78
116
  }
117
+
118
+ /** @internal Exported for tests */
119
+ export function contractErrorMapForTests(): typeof CONTRACT_ERROR_MAP {
120
+ return CONTRACT_ERROR_MAP;
121
+ }