@elizaos/plugin-wallet 2.0.0-beta.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.
Files changed (200) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +64 -0
  3. package/auto-enable.ts +76 -0
  4. package/dist/LpManagementService-BWrQ5-cO.mjs +353 -0
  5. package/dist/MockLpService-D_Apn4Fd.mjs +99 -0
  6. package/dist/aerodrome-CfnESC32.mjs +890 -0
  7. package/dist/chunk-hT5z_Zn9.mjs +35 -0
  8. package/dist/index.d.mts +34727 -0
  9. package/dist/index.mjs +21590 -0
  10. package/dist/lib/server-wallet-trade.d.mts +34 -0
  11. package/dist/lib/server-wallet-trade.mjs +306 -0
  12. package/dist/meteora-BPX39hZo.mjs +22640 -0
  13. package/dist/orca-Bybp1HXO.mjs +249 -0
  14. package/dist/pancakeswp-CkEXlXti.mjs +604 -0
  15. package/dist/plugin-ZO_MTyd0.mjs +529 -0
  16. package/dist/raydium-rfaM9yEf.mjs +539 -0
  17. package/dist/sdk/index.d.mts +32492 -0
  18. package/dist/sdk/index.mjs +6415 -0
  19. package/dist/types-D5252NZk.mjs +487 -0
  20. package/dist/uniswap-CReXgXVN.mjs +573 -0
  21. package/dist/wallet-action.d.mts +6 -0
  22. package/dist/wallet-action.mjs +820 -0
  23. package/package.json +152 -0
  24. package/src/actions/failure-codes.ts +79 -0
  25. package/src/actions/index.ts +1 -0
  26. package/src/analytics/birdeye/actions/wallet-search-address.ts +9 -0
  27. package/src/analytics/birdeye/birdeye-task.ts +175 -0
  28. package/src/analytics/birdeye/birdeye.ts +813 -0
  29. package/src/analytics/birdeye/constants.ts +74 -0
  30. package/src/analytics/birdeye/providers/agent-portfolio-provider.ts +18 -0
  31. package/src/analytics/birdeye/providers/market.ts +227 -0
  32. package/src/analytics/birdeye/providers/portfolio-factory.test.ts +138 -0
  33. package/src/analytics/birdeye/providers/portfolio-factory.ts +252 -0
  34. package/src/analytics/birdeye/providers/trending.ts +365 -0
  35. package/src/analytics/birdeye/providers/wallet.ts +14 -0
  36. package/src/analytics/birdeye/search-category.test.ts +207 -0
  37. package/src/analytics/birdeye/search-category.ts +506 -0
  38. package/src/analytics/birdeye/service.ts +992 -0
  39. package/src/analytics/birdeye/tasks/birdeye.ts +232 -0
  40. package/src/analytics/birdeye/types/api/common.ts +305 -0
  41. package/src/analytics/birdeye/types/api/defi.ts +220 -0
  42. package/src/analytics/birdeye/types/api/pair.ts +200 -0
  43. package/src/analytics/birdeye/types/api/search.ts +86 -0
  44. package/src/analytics/birdeye/types/api/token.ts +635 -0
  45. package/src/analytics/birdeye/types/api/trader.ts +76 -0
  46. package/src/analytics/birdeye/types/api/wallet.ts +181 -0
  47. package/src/analytics/birdeye/types/shared.ts +106 -0
  48. package/src/analytics/birdeye/utils.ts +700 -0
  49. package/src/analytics/dexscreener/errors.ts +28 -0
  50. package/src/analytics/dexscreener/index.ts +3 -0
  51. package/src/analytics/dexscreener/search-category.test.ts +49 -0
  52. package/src/analytics/dexscreener/search-category.ts +42 -0
  53. package/src/analytics/dexscreener/service.ts +595 -0
  54. package/src/analytics/dexscreener/types.ts +128 -0
  55. package/src/analytics/lpinfo/index.d.ts +7 -0
  56. package/src/analytics/lpinfo/index.ts +52 -0
  57. package/src/analytics/lpinfo/kamino/README.md +102 -0
  58. package/src/analytics/lpinfo/kamino/index.ts +24 -0
  59. package/src/analytics/lpinfo/kamino/providers/kaminoLiquidityProvider.ts +422 -0
  60. package/src/analytics/lpinfo/kamino/providers/kaminoPoolProvider.ts +365 -0
  61. package/src/analytics/lpinfo/kamino/providers/kaminoProvider.ts +496 -0
  62. package/src/analytics/lpinfo/kamino/services/kaminoLiquidityService.ts +1123 -0
  63. package/src/analytics/lpinfo/kamino/services/kaminoService.ts +758 -0
  64. package/src/analytics/lpinfo/steer/README.md +169 -0
  65. package/src/analytics/lpinfo/steer/index.ts +23 -0
  66. package/src/analytics/lpinfo/steer/providers/steerLiquidityProvider.ts +544 -0
  67. package/src/analytics/lpinfo/steer/services/steerLiquidityService.ts +1690 -0
  68. package/src/analytics/lpinfo/steer/steer-display-types.ts +99 -0
  69. package/src/analytics/news/index.ts +52 -0
  70. package/src/analytics/news/interfaces/types.ts +222 -0
  71. package/src/analytics/news/providers/defiNewsProvider.ts +734 -0
  72. package/src/analytics/news/services/newsDataService.ts +332 -0
  73. package/src/analytics/news/utils/formatters.ts +151 -0
  74. package/src/analytics/token-info/action.ts +240 -0
  75. package/src/analytics/token-info/index.ts +3 -0
  76. package/src/analytics/token-info/params.ts +215 -0
  77. package/src/analytics/token-info/providers.ts +681 -0
  78. package/src/analytics/token-info/service.ts +168 -0
  79. package/src/analytics/token-info/types.ts +74 -0
  80. package/src/audit/audit-log.ts +45 -0
  81. package/src/browser-shim/build-shim.ts +123 -0
  82. package/src/browser-shim/index.ts +5 -0
  83. package/src/browser-shim/shim.template.js +563 -0
  84. package/src/chains/evm/.github/workflows/npm-deploy.yml +112 -0
  85. package/src/chains/evm/LICENSE +21 -0
  86. package/src/chains/evm/README.md +106 -0
  87. package/src/chains/evm/actions/helpers.ts +147 -0
  88. package/src/chains/evm/actions/swap.ts +839 -0
  89. package/src/chains/evm/actions/transfer.ts +254 -0
  90. package/src/chains/evm/biome.json +61 -0
  91. package/src/chains/evm/bridge-router.ts +660 -0
  92. package/src/chains/evm/build.ts +89 -0
  93. package/src/chains/evm/chain-handler.ts +416 -0
  94. package/src/chains/evm/constants.ts +23 -0
  95. package/src/chains/evm/contracts/artifacts/OZGovernor.json +1707 -0
  96. package/src/chains/evm/contracts/artifacts/TimelockController.json +1007 -0
  97. package/src/chains/evm/contracts/artifacts/VoteToken.json +895 -0
  98. package/src/chains/evm/dex/aerodrome/index.ts +34 -0
  99. package/src/chains/evm/dex/aerodrome/services/AerodromeLpService.ts +558 -0
  100. package/src/chains/evm/dex/aerodrome/types.ts +318 -0
  101. package/src/chains/evm/dex/pancakeswp/index.ts +35 -0
  102. package/src/chains/evm/dex/pancakeswp/services/PancakeSwapV3LpService.ts +743 -0
  103. package/src/chains/evm/dex/pancakeswp/types.ts +65 -0
  104. package/src/chains/evm/dex/uniswap/index.ts +35 -0
  105. package/src/chains/evm/dex/uniswap/services/UniswapV3LpService.ts +759 -0
  106. package/src/chains/evm/dex/uniswap/types.ts +390 -0
  107. package/src/chains/evm/generated/specs/spec-helpers.ts +73 -0
  108. package/src/chains/evm/generated/specs/specs.ts +151 -0
  109. package/src/chains/evm/gov-router.ts +250 -0
  110. package/src/chains/evm/index.browser.ts +16 -0
  111. package/src/chains/evm/index.ts +31 -0
  112. package/src/chains/evm/prompts.ts +193 -0
  113. package/src/chains/evm/providers/get-balance.ts +123 -0
  114. package/src/chains/evm/providers/wallet.ts +715 -0
  115. package/src/chains/evm/routes/sign.ts +333 -0
  116. package/src/chains/evm/rpc-providers.ts +410 -0
  117. package/src/chains/evm/service.ts +140 -0
  118. package/src/chains/evm/templates/index.ts +10 -0
  119. package/src/chains/evm/types/index.ts +432 -0
  120. package/src/chains/evm/vitest.config.ts +18 -0
  121. package/src/chains/registry.ts +668 -0
  122. package/src/chains/solana/README.md +367 -0
  123. package/src/chains/wallet-action.ts +533 -0
  124. package/src/chains/wallet-router.test.ts +296 -0
  125. package/src/contracts.ts +65 -0
  126. package/src/core-augmentation.ts +10 -0
  127. package/src/index.ts +71 -0
  128. package/src/lib/server-wallet-trade.ts +192 -0
  129. package/src/lib/wallet-export-guard.ts +330 -0
  130. package/src/lp/actions/liquidity.ts +827 -0
  131. package/src/lp/e2e/real-token-tests.ts +428 -0
  132. package/src/lp/e2e/scenarios.ts +470 -0
  133. package/src/lp/e2e/test-utils.ts +145 -0
  134. package/src/lp/lp-manager-entry.ts +303 -0
  135. package/src/lp/services/ConcentratedLiquidityService.ts +120 -0
  136. package/src/lp/services/DexInteractionService.ts +226 -0
  137. package/src/lp/services/LpManagementService.test.ts +148 -0
  138. package/src/lp/services/LpManagementService.ts +632 -0
  139. package/src/lp/services/UserLpProfileService.ts +163 -0
  140. package/src/lp/services/VaultService.ts +153 -0
  141. package/src/lp/services/YieldOptimizationService.ts +344 -0
  142. package/src/lp/services/__tests__/MockLpService.ts +146 -0
  143. package/src/lp/tasks/LpAutoRebalanceTask.ts +117 -0
  144. package/src/lp/tasks/__tests__/LpAutoRebalanceTask.test.ts +370 -0
  145. package/src/lp/types.ts +582 -0
  146. package/src/lp/utils/solanaClient.ts +143 -0
  147. package/src/plugin.ts +125 -0
  148. package/src/policy/policy.ts +19 -0
  149. package/src/providers/canonical-provider.ts +27 -0
  150. package/src/providers/unified-wallet-provider.ts +79 -0
  151. package/src/register-routes.ts +11 -0
  152. package/src/routes/plugin.ts +47 -0
  153. package/src/routes/wallet-market-overview-route.ts +869 -0
  154. package/src/sdk/abi.ts +258 -0
  155. package/src/sdk/bridge/abis.ts +126 -0
  156. package/src/sdk/bridge/client.ts +518 -0
  157. package/src/sdk/bridge/index.ts +56 -0
  158. package/src/sdk/bridge/solana.ts +604 -0
  159. package/src/sdk/bridge/types.ts +202 -0
  160. package/src/sdk/convenience.ts +347 -0
  161. package/src/sdk/escrow/MutualStakeEscrow.ts +480 -0
  162. package/src/sdk/escrow/types.ts +64 -0
  163. package/src/sdk/escrow/verifiers.ts +73 -0
  164. package/src/sdk/identity/erc8004.ts +692 -0
  165. package/src/sdk/identity/reputation.ts +449 -0
  166. package/src/sdk/identity/uaid.ts +497 -0
  167. package/src/sdk/identity/validation.ts +372 -0
  168. package/src/sdk/index.ts +763 -0
  169. package/src/sdk/policy/SpendingPolicy.ts +260 -0
  170. package/src/sdk/policy/UptoBillingPolicy.ts +320 -0
  171. package/src/sdk/router/PaymentRouter.ts +215 -0
  172. package/src/sdk/router/index.ts +8 -0
  173. package/src/sdk/swap/SwapModule.ts +310 -0
  174. package/src/sdk/swap/abi.ts +117 -0
  175. package/src/sdk/swap/index.ts +34 -0
  176. package/src/sdk/swap/types.ts +135 -0
  177. package/src/sdk/tokens/decimals.ts +140 -0
  178. package/src/sdk/tokens/registry.ts +911 -0
  179. package/src/sdk/tokens/solana.ts +419 -0
  180. package/src/sdk/tokens/transfers.ts +327 -0
  181. package/src/sdk/types.ts +158 -0
  182. package/src/sdk/wallet-core.ts +115 -0
  183. package/src/sdk/x402/budget.ts +168 -0
  184. package/src/sdk/x402/chains/abstract/index.ts +280 -0
  185. package/src/sdk/x402/client.ts +320 -0
  186. package/src/sdk/x402/index.ts +46 -0
  187. package/src/sdk/x402/middleware.ts +92 -0
  188. package/src/sdk/x402/multi-asset.ts +144 -0
  189. package/src/sdk/x402/types.ts +156 -0
  190. package/src/services/wallet-backend-service.ts +328 -0
  191. package/src/types/wallet-router.ts +227 -0
  192. package/src/utils/intent-trajectory.ts +106 -0
  193. package/src/wallet/backend.ts +62 -0
  194. package/src/wallet/errors.ts +49 -0
  195. package/src/wallet/index.ts +27 -0
  196. package/src/wallet/local-eoa-backend.ts +201 -0
  197. package/src/wallet/pending.ts +60 -0
  198. package/src/wallet/select-backend.ts +47 -0
  199. package/src/wallet/steward-backend.ts +161 -0
  200. package/src/wallet-action.ts +1 -0
@@ -0,0 +1,135 @@
1
+ /**
2
+ * @module swap/types
3
+ * Type definitions and constants for the multi-chain Uniswap V3 SwapModule.
4
+ *
5
+ * Supported swap chains: Base, Arbitrum, Optimism, Polygon.
6
+ * All router/quoter addresses verified against official Uniswap V3 deployment docs:
7
+ * - Base: https://docs.uniswap.org/contracts/v3/reference/deployments/base-deployments
8
+ * - Arbitrum: https://docs.uniswap.org/contracts/v3/reference/deployments/arbitrum-deployments
9
+ * - Optimism: https://docs.uniswap.org/contracts/v3/reference/deployments/optimism-deployments
10
+ * - Polygon: https://docs.uniswap.org/contracts/v3/reference/deployments/polygon-deployments
11
+ *
12
+ * Note: Base uses a unique SwapRouter02 address; Arbitrum, Optimism, and Polygon
13
+ * share the same universal SwapRouter02 deployment address.
14
+ */
15
+ import type { Address, Hash } from "viem";
16
+
17
+ /** Uniswap V3 fee tier options */
18
+ export type UniswapFeeTier = 100 | 500 | 3000 | 10000;
19
+
20
+ /** Chains supported for Uniswap V3 swap */
21
+ export type SwapChain = "base" | "arbitrum" | "optimism" | "polygon";
22
+
23
+ export interface SwapQuote {
24
+ tokenIn: Address;
25
+ tokenOut: Address;
26
+ amountInRaw: bigint;
27
+ amountInNet: bigint;
28
+ feeAmount: bigint;
29
+ amountOut: bigint;
30
+ amountOutMinimum: bigint;
31
+ poolFeeTier: UniswapFeeTier;
32
+ effectiveRate: number;
33
+ gasEstimate: bigint;
34
+ }
35
+
36
+ export interface SwapOptions {
37
+ slippageBps?: number;
38
+ feeTiers?: UniswapFeeTier[];
39
+ deadlineSecs?: number;
40
+ feeWallet?: Address;
41
+ }
42
+
43
+ export interface SwapResult {
44
+ txHash: Hash;
45
+ feeTxHash?: Hash;
46
+ quote: SwapQuote;
47
+ approvalRequired: boolean;
48
+ approvalTxHash?: Hash;
49
+ }
50
+
51
+ export interface SwapModuleConfig {
52
+ routerAddress: Address;
53
+ quoterAddress: Address;
54
+ feeBps: number;
55
+ feeWallet: Address;
56
+ /** Chain this SwapModule is configured for */
57
+ chain: SwapChain;
58
+ }
59
+
60
+ // ─── Token Addresses ───
61
+
62
+ /** Well-known Base Mainnet token addresses */
63
+ export const BASE_TOKENS = {
64
+ USDC: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913" as const,
65
+ WETH: "0x4200000000000000000000000000000000000006" as const,
66
+ cbETH: "0x2Ae3F1Ec7F1F5012CFEab0185bfc7aa3cf0DEc22" as const,
67
+ WNATIVE: "0x4200000000000000000000000000000000000006" as const,
68
+ };
69
+
70
+ /** Well-known Arbitrum token addresses */
71
+ export const ARBITRUM_TOKENS = {
72
+ USDC: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831" as const,
73
+ WETH: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1" as const,
74
+ WNATIVE: "0x82aF49447D8a07e3bd95BD0d56f35241523fBab1" as const,
75
+ };
76
+
77
+ /** Well-known Optimism token addresses */
78
+ export const OPTIMISM_TOKENS = {
79
+ USDC: "0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85" as const,
80
+ WETH: "0x4200000000000000000000000000000000000006" as const,
81
+ OP: "0x4200000000000000000000000000000000000042" as const,
82
+ WNATIVE: "0x4200000000000000000000000000000000000006" as const,
83
+ };
84
+
85
+ /** Well-known Polygon token addresses */
86
+ export const POLYGON_TOKENS = {
87
+ USDC: "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359" as const,
88
+ WETH: "0x7ceB23fD6bC0adD59E62ac25578270cFf1b9f619" as const,
89
+ WMATIC: "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270" as const,
90
+ WNATIVE: "0x0d500B1d8E8eF31E21C99d1Db9A6444d3ADf1270" as const,
91
+ };
92
+
93
+ // ─── Uniswap V3 Contract Addresses ───
94
+
95
+ /**
96
+ * Uniswap V3 router and quoter addresses.
97
+ *
98
+ * Base has a different router (SwapRouter02 deployed at a unique address due to BASE chain launch).
99
+ * Arbitrum, Optimism, and Polygon use the same universal SwapRouter02 address.
100
+ *
101
+ * Sources:
102
+ * - Base: https://docs.uniswap.org/contracts/v3/reference/deployments/base-deployments
103
+ * - Arbitrum: https://docs.uniswap.org/contracts/v3/reference/deployments/arbitrum-deployments
104
+ * - Optimism: https://docs.uniswap.org/contracts/v3/reference/deployments/optimism-deployments
105
+ * - Polygon: https://docs.uniswap.org/contracts/v3/reference/deployments/polygon-deployments
106
+ */
107
+ export const UNISWAP_V3_ADDRESSES: Record<
108
+ SwapChain,
109
+ { ROUTER: Address; QUOTER_V2: Address }
110
+ > = {
111
+ base: {
112
+ ROUTER: "0x2626664c2603336E57B271c5C0b26F421741e481",
113
+ QUOTER_V2: "0x3d4e44Eb1374240CE5F1B871ab261CD16335B76a",
114
+ },
115
+ arbitrum: {
116
+ ROUTER: "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45",
117
+ QUOTER_V2: "0x61fFE014bA17989E743c5F6cB21bF9697530B21e",
118
+ },
119
+ optimism: {
120
+ ROUTER: "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45",
121
+ QUOTER_V2: "0x61fFE014bA17989E743c5F6cB21bF9697530B21e",
122
+ },
123
+ polygon: {
124
+ ROUTER: "0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45",
125
+ QUOTER_V2: "0x61fFE014bA17989E743c5F6cB21bF9697530B21e",
126
+ },
127
+ };
128
+
129
+ /** Protocol fee in bps — 0.875% */
130
+ export const PROTOCOL_FEE_BPS = 875;
131
+ /** Protocol fee collector address (all EVM chains) */
132
+ export const PROTOCOL_FEE_COLLECTOR =
133
+ "0xff86829393C6C26A4EC122bE0Cc3E466Ef876AdD" as const;
134
+ /** Default slippage in bps — 0.5% */
135
+ export const DEFAULT_SLIPPAGE_BPS = 50;
@@ -0,0 +1,140 @@
1
+ /**
2
+ * @module tokens/decimals
3
+ * Token decimal normalization utilities for multi-token agent wallets.
4
+ *
5
+ * Handles safe conversion between human-readable amounts ("1.5") and raw
6
+ * on-chain bigint amounts (1500000n for USDC with 6 decimals). All arithmetic
7
+ * is done in integer domain to avoid floating point precision errors.
8
+ */
9
+
10
+ import type { Address } from "viem";
11
+
12
+ /** Minimal token info required for formatting */
13
+ export interface TokenInfo {
14
+ symbol: string;
15
+ decimals: number;
16
+ address: Address;
17
+ name?: string;
18
+ chainId?: number;
19
+ }
20
+
21
+ /**
22
+ * Convert a human-readable amount string to raw bigint (on-chain units).
23
+ *
24
+ * @param amount - Human-readable amount, e.g. "1.5" or "1000"
25
+ * @param decimals - Token decimal places (e.g. 6 for USDC, 18 for WETH)
26
+ * @returns Raw on-chain amount as bigint
27
+ *
28
+ * @example
29
+ * toRaw("1.5", 6) // → 1500000n (1.5 USDC)
30
+ * toRaw("1.0", 18) // → 1000000000000000000n (1 WETH)
31
+ * toRaw("0.001", 8) // → 100000n (0.001 WBTC)
32
+ */
33
+ export function toRaw(amount: string | bigint, decimals: number): bigint {
34
+ if (typeof amount === "bigint") return amount;
35
+
36
+ const str = amount.trim();
37
+ if (!str || str === "0") return 0n;
38
+
39
+ // Validate: only digits and one optional decimal point, optional leading minus
40
+ if (!/^-?\d+(\.\d+)?$/.test(str)) {
41
+ throw new Error(`toRaw: invalid amount string "${amount}"`);
42
+ }
43
+
44
+ const negative = str.startsWith("-");
45
+ const abs = negative ? str.slice(1) : str;
46
+
47
+ const dotIndex = abs.indexOf(".");
48
+ if (dotIndex === -1) {
49
+ // Integer amount
50
+ const result = BigInt(abs) * 10n ** BigInt(decimals);
51
+ return negative ? -result : result;
52
+ }
53
+
54
+ const intPart = abs.slice(0, dotIndex);
55
+ let fracPart = abs.slice(dotIndex + 1);
56
+
57
+ // Truncate or pad fractional part to `decimals` digits
58
+ if (fracPart.length > decimals) {
59
+ fracPart = fracPart.slice(0, decimals); // truncate (floor for positive amounts)
60
+ } else {
61
+ fracPart = fracPart.padEnd(decimals, "0");
62
+ }
63
+
64
+ const raw =
65
+ BigInt(intPart || "0") * 10n ** BigInt(decimals) + BigInt(fracPart);
66
+
67
+ return negative ? -raw : raw;
68
+ }
69
+
70
+ /**
71
+ * Convert a raw bigint on-chain amount to a human-readable string.
72
+ *
73
+ * @param amount - Raw on-chain amount
74
+ * @param decimals - Token decimal places
75
+ * @returns Human-readable string, trimmed trailing zeros
76
+ *
77
+ * @example
78
+ * toHuman(1500000n, 6) // → "1.5"
79
+ * toHuman(1000000000000000000n, 18) // → "1.0"
80
+ * toHuman(0n, 6) // → "0.0"
81
+ */
82
+ export function toHuman(amount: bigint, decimals: number): string {
83
+ if (decimals === 0) return amount.toString();
84
+
85
+ const negative = amount < 0n;
86
+ const abs = negative ? -amount : amount;
87
+
88
+ const divisor = 10n ** BigInt(decimals);
89
+ const intPart = abs / divisor;
90
+ const fracRaw = abs % divisor;
91
+
92
+ // Pad fractional part to `decimals` digits
93
+ const fracStr = fracRaw.toString().padStart(decimals, "0");
94
+
95
+ // Trim trailing zeros but keep at least one decimal digit
96
+ const trimmed = fracStr.replace(/0+$/, "") || "0";
97
+
98
+ const result = `${intPart}.${trimmed}`;
99
+ return negative ? `-${result}` : result;
100
+ }
101
+
102
+ /**
103
+ * Format a raw bigint amount with token symbol for display.
104
+ *
105
+ * @param amount - Raw on-chain amount
106
+ * @param token - Token info (symbol + decimals required)
107
+ * @param displayDecimals - Max decimal places to show (default: 4 for high-precision, 2 for stablecoins)
108
+ * @returns Formatted string e.g. "1.50 USDC" or "0.0023 WETH"
109
+ *
110
+ * @example
111
+ * formatBalance(1500000n, { symbol: 'USDC', decimals: 6, address: '0x...' })
112
+ * // → "1.50 USDC"
113
+ */
114
+ export function formatBalance(
115
+ amount: bigint,
116
+ token: Pick<TokenInfo, "symbol" | "decimals">,
117
+ displayDecimals?: number,
118
+ ): string {
119
+ const human = toHuman(amount, token.decimals);
120
+ const maxDisplay = displayDecimals ?? (token.decimals <= 6 ? 2 : 4);
121
+
122
+ // Parse the human string and reformat to maxDisplay decimal places
123
+ const [intPart, fracPart = ""] = human.split(".");
124
+ const padded = fracPart.padEnd(maxDisplay, "0").slice(0, maxDisplay);
125
+
126
+ // If all fraction digits are zero, still show them for consistent formatting
127
+ return `${intPart}.${padded} ${token.symbol}`;
128
+ }
129
+
130
+ /**
131
+ * Parse amount that may be either a string (human-readable) or bigint (raw).
132
+ * Returns raw bigint. Used in transfer functions to accept flexible input.
133
+ *
134
+ * @param amount - Either a human-readable string "1.5" or a raw bigint
135
+ * @param decimals - Required only when amount is a string
136
+ */
137
+ export function parseAmount(amount: string | bigint, decimals: number): bigint {
138
+ if (typeof amount === "bigint") return amount;
139
+ return toRaw(amount, decimals);
140
+ }