@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.
- package/LICENSE +21 -0
- package/README.md +64 -0
- package/auto-enable.ts +76 -0
- package/dist/LpManagementService-BWrQ5-cO.mjs +353 -0
- package/dist/MockLpService-D_Apn4Fd.mjs +99 -0
- package/dist/aerodrome-CfnESC32.mjs +890 -0
- package/dist/chunk-hT5z_Zn9.mjs +35 -0
- package/dist/index.d.mts +34727 -0
- package/dist/index.mjs +21590 -0
- package/dist/lib/server-wallet-trade.d.mts +34 -0
- package/dist/lib/server-wallet-trade.mjs +306 -0
- package/dist/meteora-BPX39hZo.mjs +22640 -0
- package/dist/orca-Bybp1HXO.mjs +249 -0
- package/dist/pancakeswp-CkEXlXti.mjs +604 -0
- package/dist/plugin-ZO_MTyd0.mjs +529 -0
- package/dist/raydium-rfaM9yEf.mjs +539 -0
- package/dist/sdk/index.d.mts +32492 -0
- package/dist/sdk/index.mjs +6415 -0
- package/dist/types-D5252NZk.mjs +487 -0
- package/dist/uniswap-CReXgXVN.mjs +573 -0
- package/dist/wallet-action.d.mts +6 -0
- package/dist/wallet-action.mjs +820 -0
- package/package.json +152 -0
- package/src/actions/failure-codes.ts +79 -0
- package/src/actions/index.ts +1 -0
- package/src/analytics/birdeye/actions/wallet-search-address.ts +9 -0
- package/src/analytics/birdeye/birdeye-task.ts +175 -0
- package/src/analytics/birdeye/birdeye.ts +813 -0
- package/src/analytics/birdeye/constants.ts +74 -0
- package/src/analytics/birdeye/providers/agent-portfolio-provider.ts +18 -0
- package/src/analytics/birdeye/providers/market.ts +227 -0
- package/src/analytics/birdeye/providers/portfolio-factory.test.ts +138 -0
- package/src/analytics/birdeye/providers/portfolio-factory.ts +252 -0
- package/src/analytics/birdeye/providers/trending.ts +365 -0
- package/src/analytics/birdeye/providers/wallet.ts +14 -0
- package/src/analytics/birdeye/search-category.test.ts +207 -0
- package/src/analytics/birdeye/search-category.ts +506 -0
- package/src/analytics/birdeye/service.ts +992 -0
- package/src/analytics/birdeye/tasks/birdeye.ts +232 -0
- package/src/analytics/birdeye/types/api/common.ts +305 -0
- package/src/analytics/birdeye/types/api/defi.ts +220 -0
- package/src/analytics/birdeye/types/api/pair.ts +200 -0
- package/src/analytics/birdeye/types/api/search.ts +86 -0
- package/src/analytics/birdeye/types/api/token.ts +635 -0
- package/src/analytics/birdeye/types/api/trader.ts +76 -0
- package/src/analytics/birdeye/types/api/wallet.ts +181 -0
- package/src/analytics/birdeye/types/shared.ts +106 -0
- package/src/analytics/birdeye/utils.ts +700 -0
- package/src/analytics/dexscreener/errors.ts +28 -0
- package/src/analytics/dexscreener/index.ts +3 -0
- package/src/analytics/dexscreener/search-category.test.ts +49 -0
- package/src/analytics/dexscreener/search-category.ts +42 -0
- package/src/analytics/dexscreener/service.ts +595 -0
- package/src/analytics/dexscreener/types.ts +128 -0
- package/src/analytics/lpinfo/index.d.ts +7 -0
- package/src/analytics/lpinfo/index.ts +52 -0
- package/src/analytics/lpinfo/kamino/README.md +102 -0
- package/src/analytics/lpinfo/kamino/index.ts +24 -0
- package/src/analytics/lpinfo/kamino/providers/kaminoLiquidityProvider.ts +422 -0
- package/src/analytics/lpinfo/kamino/providers/kaminoPoolProvider.ts +365 -0
- package/src/analytics/lpinfo/kamino/providers/kaminoProvider.ts +496 -0
- package/src/analytics/lpinfo/kamino/services/kaminoLiquidityService.ts +1123 -0
- package/src/analytics/lpinfo/kamino/services/kaminoService.ts +758 -0
- package/src/analytics/lpinfo/steer/README.md +169 -0
- package/src/analytics/lpinfo/steer/index.ts +23 -0
- package/src/analytics/lpinfo/steer/providers/steerLiquidityProvider.ts +544 -0
- package/src/analytics/lpinfo/steer/services/steerLiquidityService.ts +1690 -0
- package/src/analytics/lpinfo/steer/steer-display-types.ts +99 -0
- package/src/analytics/news/index.ts +52 -0
- package/src/analytics/news/interfaces/types.ts +222 -0
- package/src/analytics/news/providers/defiNewsProvider.ts +734 -0
- package/src/analytics/news/services/newsDataService.ts +332 -0
- package/src/analytics/news/utils/formatters.ts +151 -0
- package/src/analytics/token-info/action.ts +240 -0
- package/src/analytics/token-info/index.ts +3 -0
- package/src/analytics/token-info/params.ts +215 -0
- package/src/analytics/token-info/providers.ts +681 -0
- package/src/analytics/token-info/service.ts +168 -0
- package/src/analytics/token-info/types.ts +74 -0
- package/src/audit/audit-log.ts +45 -0
- package/src/browser-shim/build-shim.ts +123 -0
- package/src/browser-shim/index.ts +5 -0
- package/src/browser-shim/shim.template.js +563 -0
- package/src/chains/evm/.github/workflows/npm-deploy.yml +112 -0
- package/src/chains/evm/LICENSE +21 -0
- package/src/chains/evm/README.md +106 -0
- package/src/chains/evm/actions/helpers.ts +147 -0
- package/src/chains/evm/actions/swap.ts +839 -0
- package/src/chains/evm/actions/transfer.ts +254 -0
- package/src/chains/evm/biome.json +61 -0
- package/src/chains/evm/bridge-router.ts +660 -0
- package/src/chains/evm/build.ts +89 -0
- package/src/chains/evm/chain-handler.ts +416 -0
- package/src/chains/evm/constants.ts +23 -0
- package/src/chains/evm/contracts/artifacts/OZGovernor.json +1707 -0
- package/src/chains/evm/contracts/artifacts/TimelockController.json +1007 -0
- package/src/chains/evm/contracts/artifacts/VoteToken.json +895 -0
- package/src/chains/evm/dex/aerodrome/index.ts +34 -0
- package/src/chains/evm/dex/aerodrome/services/AerodromeLpService.ts +558 -0
- package/src/chains/evm/dex/aerodrome/types.ts +318 -0
- package/src/chains/evm/dex/pancakeswp/index.ts +35 -0
- package/src/chains/evm/dex/pancakeswp/services/PancakeSwapV3LpService.ts +743 -0
- package/src/chains/evm/dex/pancakeswp/types.ts +65 -0
- package/src/chains/evm/dex/uniswap/index.ts +35 -0
- package/src/chains/evm/dex/uniswap/services/UniswapV3LpService.ts +759 -0
- package/src/chains/evm/dex/uniswap/types.ts +390 -0
- package/src/chains/evm/generated/specs/spec-helpers.ts +73 -0
- package/src/chains/evm/generated/specs/specs.ts +151 -0
- package/src/chains/evm/gov-router.ts +250 -0
- package/src/chains/evm/index.browser.ts +16 -0
- package/src/chains/evm/index.ts +31 -0
- package/src/chains/evm/prompts.ts +193 -0
- package/src/chains/evm/providers/get-balance.ts +123 -0
- package/src/chains/evm/providers/wallet.ts +715 -0
- package/src/chains/evm/routes/sign.ts +333 -0
- package/src/chains/evm/rpc-providers.ts +410 -0
- package/src/chains/evm/service.ts +140 -0
- package/src/chains/evm/templates/index.ts +10 -0
- package/src/chains/evm/types/index.ts +432 -0
- package/src/chains/evm/vitest.config.ts +18 -0
- package/src/chains/registry.ts +668 -0
- package/src/chains/solana/README.md +367 -0
- package/src/chains/wallet-action.ts +533 -0
- package/src/chains/wallet-router.test.ts +296 -0
- package/src/contracts.ts +65 -0
- package/src/core-augmentation.ts +10 -0
- package/src/index.ts +71 -0
- package/src/lib/server-wallet-trade.ts +192 -0
- package/src/lib/wallet-export-guard.ts +330 -0
- package/src/lp/actions/liquidity.ts +827 -0
- package/src/lp/e2e/real-token-tests.ts +428 -0
- package/src/lp/e2e/scenarios.ts +470 -0
- package/src/lp/e2e/test-utils.ts +145 -0
- package/src/lp/lp-manager-entry.ts +303 -0
- package/src/lp/services/ConcentratedLiquidityService.ts +120 -0
- package/src/lp/services/DexInteractionService.ts +226 -0
- package/src/lp/services/LpManagementService.test.ts +148 -0
- package/src/lp/services/LpManagementService.ts +632 -0
- package/src/lp/services/UserLpProfileService.ts +163 -0
- package/src/lp/services/VaultService.ts +153 -0
- package/src/lp/services/YieldOptimizationService.ts +344 -0
- package/src/lp/services/__tests__/MockLpService.ts +146 -0
- package/src/lp/tasks/LpAutoRebalanceTask.ts +117 -0
- package/src/lp/tasks/__tests__/LpAutoRebalanceTask.test.ts +370 -0
- package/src/lp/types.ts +582 -0
- package/src/lp/utils/solanaClient.ts +143 -0
- package/src/plugin.ts +125 -0
- package/src/policy/policy.ts +19 -0
- package/src/providers/canonical-provider.ts +27 -0
- package/src/providers/unified-wallet-provider.ts +79 -0
- package/src/register-routes.ts +11 -0
- package/src/routes/plugin.ts +47 -0
- package/src/routes/wallet-market-overview-route.ts +869 -0
- package/src/sdk/abi.ts +258 -0
- package/src/sdk/bridge/abis.ts +126 -0
- package/src/sdk/bridge/client.ts +518 -0
- package/src/sdk/bridge/index.ts +56 -0
- package/src/sdk/bridge/solana.ts +604 -0
- package/src/sdk/bridge/types.ts +202 -0
- package/src/sdk/convenience.ts +347 -0
- package/src/sdk/escrow/MutualStakeEscrow.ts +480 -0
- package/src/sdk/escrow/types.ts +64 -0
- package/src/sdk/escrow/verifiers.ts +73 -0
- package/src/sdk/identity/erc8004.ts +692 -0
- package/src/sdk/identity/reputation.ts +449 -0
- package/src/sdk/identity/uaid.ts +497 -0
- package/src/sdk/identity/validation.ts +372 -0
- package/src/sdk/index.ts +763 -0
- package/src/sdk/policy/SpendingPolicy.ts +260 -0
- package/src/sdk/policy/UptoBillingPolicy.ts +320 -0
- package/src/sdk/router/PaymentRouter.ts +215 -0
- package/src/sdk/router/index.ts +8 -0
- package/src/sdk/swap/SwapModule.ts +310 -0
- package/src/sdk/swap/abi.ts +117 -0
- package/src/sdk/swap/index.ts +34 -0
- package/src/sdk/swap/types.ts +135 -0
- package/src/sdk/tokens/decimals.ts +140 -0
- package/src/sdk/tokens/registry.ts +911 -0
- package/src/sdk/tokens/solana.ts +419 -0
- package/src/sdk/tokens/transfers.ts +327 -0
- package/src/sdk/types.ts +158 -0
- package/src/sdk/wallet-core.ts +115 -0
- package/src/sdk/x402/budget.ts +168 -0
- package/src/sdk/x402/chains/abstract/index.ts +280 -0
- package/src/sdk/x402/client.ts +320 -0
- package/src/sdk/x402/index.ts +46 -0
- package/src/sdk/x402/middleware.ts +92 -0
- package/src/sdk/x402/multi-asset.ts +144 -0
- package/src/sdk/x402/types.ts +156 -0
- package/src/services/wallet-backend-service.ts +328 -0
- package/src/types/wallet-router.ts +227 -0
- package/src/utils/intent-trajectory.ts +106 -0
- package/src/wallet/backend.ts +62 -0
- package/src/wallet/errors.ts +49 -0
- package/src/wallet/index.ts +27 -0
- package/src/wallet/local-eoa-backend.ts +201 -0
- package/src/wallet/pending.ts +60 -0
- package/src/wallet/select-backend.ts +47 -0
- package/src/wallet/steward-backend.ts +161 -0
- package/src/wallet-action.ts +1 -0
|
@@ -0,0 +1,202 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module bridge/types
|
|
3
|
+
* Type definitions and constants for the CCTP V2 cross-chain USDC bridge.
|
|
4
|
+
*
|
|
5
|
+
* Supports 10 EVM chains + Solana via Circle's Cross-Chain Transfer Protocol V2.
|
|
6
|
+
* EVM chains: Ethereum, Avalanche, Optimism, Arbitrum, Base, Polygon,
|
|
7
|
+
* Unichain, Linea, Sonic, Worldchain.
|
|
8
|
+
* Non-EVM: Solana (via bridge/solana.ts — uses separate CCTP V2 programs).
|
|
9
|
+
*
|
|
10
|
+
* Contract addresses verified against:
|
|
11
|
+
* - CCTP V2 EVM: https://developers.circle.com/cctp/references/contract-addresses
|
|
12
|
+
* - Solana V2 programs: https://developers.circle.com/cctp/references/solana-programs
|
|
13
|
+
* - USDC addresses: https://developers.circle.com/stablecoins/usdc-contract-addresses
|
|
14
|
+
*/
|
|
15
|
+
import type { Address, Hash, Hex } from "viem";
|
|
16
|
+
|
|
17
|
+
/** Supported bridge destination chains */
|
|
18
|
+
export type BridgeChain =
|
|
19
|
+
| "ethereum"
|
|
20
|
+
| "avalanche"
|
|
21
|
+
| "optimism"
|
|
22
|
+
| "arbitrum"
|
|
23
|
+
| "base"
|
|
24
|
+
| "polygon"
|
|
25
|
+
| "unichain"
|
|
26
|
+
| "linea"
|
|
27
|
+
| "sonic"
|
|
28
|
+
| "worldchain"
|
|
29
|
+
| "solana";
|
|
30
|
+
|
|
31
|
+
/** EVM-only bridge chains (excludes non-EVM like Solana) */
|
|
32
|
+
export type EVMBridgeChain = Exclude<BridgeChain, "solana">;
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* CCTP V2 domain IDs — official Circle domain identifiers.
|
|
36
|
+
* Source: https://developers.circle.com/cctp/references/contract-addresses
|
|
37
|
+
* Solana domain: 5 — https://developers.circle.com/cctp/docs/solana
|
|
38
|
+
*/
|
|
39
|
+
export const CCTP_DOMAIN_IDS: Record<BridgeChain, number> = {
|
|
40
|
+
ethereum: 0,
|
|
41
|
+
avalanche: 1,
|
|
42
|
+
optimism: 2,
|
|
43
|
+
arbitrum: 3,
|
|
44
|
+
base: 6,
|
|
45
|
+
polygon: 7,
|
|
46
|
+
unichain: 10,
|
|
47
|
+
linea: 11,
|
|
48
|
+
sonic: 13,
|
|
49
|
+
worldchain: 14,
|
|
50
|
+
solana: 5,
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* EVM chain IDs for EVM-compatible bridge chains.
|
|
55
|
+
* Solana is excluded — use solana.ts for Solana-specific operations.
|
|
56
|
+
*/
|
|
57
|
+
export const BRIDGE_CHAIN_IDS: Record<EVMBridgeChain, number> = {
|
|
58
|
+
ethereum: 1,
|
|
59
|
+
avalanche: 43114,
|
|
60
|
+
optimism: 10,
|
|
61
|
+
arbitrum: 42161,
|
|
62
|
+
base: 8453,
|
|
63
|
+
polygon: 137,
|
|
64
|
+
unichain: 130,
|
|
65
|
+
linea: 59144,
|
|
66
|
+
sonic: 146,
|
|
67
|
+
worldchain: 480,
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
* USDC contract addresses per chain (native Circle USDC, not bridged variants).
|
|
72
|
+
* Solana USDC: EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v (stored as string, not Address/Hex)
|
|
73
|
+
*/
|
|
74
|
+
export const USDC_CONTRACT: Record<EVMBridgeChain, Address> = {
|
|
75
|
+
ethereum: "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48",
|
|
76
|
+
avalanche: "0xB97EF9Ef8734C71904D8002F8b6Bc66Dd9c48a6E",
|
|
77
|
+
optimism: "0x0b2C639c533813f4Aa9D7837CAf62653d097Ff85",
|
|
78
|
+
arbitrum: "0xaf88d065e77c8cC2239327C5EDb3A432268e5831",
|
|
79
|
+
base: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
|
|
80
|
+
polygon: "0x3c499c542cEF5E3811e1192ce70d8cC03d5c3359",
|
|
81
|
+
unichain: "0x078D782b760474a361dDA0AF3839290b0EF57AD6",
|
|
82
|
+
linea: "0x176211869cA2b568f2A7D4EE941E073a821EE1ff",
|
|
83
|
+
sonic: "0x29219dd400f2Bf60E5a23d13Be72B486D4038894",
|
|
84
|
+
worldchain: "0x79A02482A880bCE3B13e09Da970dC34db4CD24d1",
|
|
85
|
+
};
|
|
86
|
+
|
|
87
|
+
/** Solana USDC mint address */
|
|
88
|
+
export const SOLANA_USDC_MINT =
|
|
89
|
+
"EPjFWdd5AufqSSqeM2qN1xzybapC8G4wEGGkZwyTDt1v" as const;
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* CCTP V2 TokenMessengerV2 — deterministic CREATE2 deployment, same address on all EVM chains.
|
|
93
|
+
* Source: https://developers.circle.com/cctp/references/contract-addresses
|
|
94
|
+
* Verified on-chain: BaseScan, Etherscan, Arbiscan (active transactions as of 2026-03-10)
|
|
95
|
+
* Solana uses a different program — see bridge/solana.ts
|
|
96
|
+
*/
|
|
97
|
+
const CCTP_V2_TOKEN_MESSENGER: Address =
|
|
98
|
+
"0x28b5a0e9C621a5BadaA536219b3a228C8168cf5d";
|
|
99
|
+
|
|
100
|
+
export const TOKEN_MESSENGER_V2: Record<EVMBridgeChain, Address> = {
|
|
101
|
+
ethereum: CCTP_V2_TOKEN_MESSENGER,
|
|
102
|
+
avalanche: CCTP_V2_TOKEN_MESSENGER,
|
|
103
|
+
optimism: CCTP_V2_TOKEN_MESSENGER,
|
|
104
|
+
arbitrum: CCTP_V2_TOKEN_MESSENGER,
|
|
105
|
+
base: CCTP_V2_TOKEN_MESSENGER,
|
|
106
|
+
polygon: CCTP_V2_TOKEN_MESSENGER,
|
|
107
|
+
unichain: CCTP_V2_TOKEN_MESSENGER,
|
|
108
|
+
linea: CCTP_V2_TOKEN_MESSENGER,
|
|
109
|
+
sonic: CCTP_V2_TOKEN_MESSENGER,
|
|
110
|
+
worldchain: CCTP_V2_TOKEN_MESSENGER,
|
|
111
|
+
};
|
|
112
|
+
|
|
113
|
+
/**
|
|
114
|
+
* CCTP V2 MessageTransmitterV2 — deterministic CREATE2 deployment, same address on all EVM chains.
|
|
115
|
+
* Source: https://developers.circle.com/cctp/references/contract-addresses
|
|
116
|
+
* Solana uses a different program — see bridge/solana.ts
|
|
117
|
+
*/
|
|
118
|
+
const CCTP_V2_MESSAGE_TRANSMITTER: Address =
|
|
119
|
+
"0x81D40F21F12A8F0E3252Bccb954D722d4c464B64";
|
|
120
|
+
|
|
121
|
+
export const MESSAGE_TRANSMITTER_V2: Record<EVMBridgeChain, Address> = {
|
|
122
|
+
ethereum: CCTP_V2_MESSAGE_TRANSMITTER,
|
|
123
|
+
avalanche: CCTP_V2_MESSAGE_TRANSMITTER,
|
|
124
|
+
optimism: CCTP_V2_MESSAGE_TRANSMITTER,
|
|
125
|
+
arbitrum: CCTP_V2_MESSAGE_TRANSMITTER,
|
|
126
|
+
base: CCTP_V2_MESSAGE_TRANSMITTER,
|
|
127
|
+
polygon: CCTP_V2_MESSAGE_TRANSMITTER,
|
|
128
|
+
unichain: CCTP_V2_MESSAGE_TRANSMITTER,
|
|
129
|
+
linea: CCTP_V2_MESSAGE_TRANSMITTER,
|
|
130
|
+
sonic: CCTP_V2_MESSAGE_TRANSMITTER,
|
|
131
|
+
worldchain: CCTP_V2_MESSAGE_TRANSMITTER,
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
/**
|
|
135
|
+
* CCTP V2 minFinalityThreshold values.
|
|
136
|
+
* - FAST (0): ~12 seconds. Circle fast attestation.
|
|
137
|
+
* - FINALIZED (1000): Full on-chain finality.
|
|
138
|
+
*/
|
|
139
|
+
export const FINALITY_THRESHOLD = {
|
|
140
|
+
FAST: 0,
|
|
141
|
+
FINALIZED: 1000,
|
|
142
|
+
} as const;
|
|
143
|
+
|
|
144
|
+
/** Circle IRIS attestation API base URL */
|
|
145
|
+
export const CIRCLE_ATTESTATION_API = "https://iris-api.circle.com";
|
|
146
|
+
/** Max attestation polling attempts before timeout */
|
|
147
|
+
export const MAX_ATTESTATION_POLLS = 60;
|
|
148
|
+
/** Polling interval for attestation (milliseconds) */
|
|
149
|
+
export const ATTESTATION_POLL_INTERVAL_MS = 5000;
|
|
150
|
+
|
|
151
|
+
/** Options for a bridge operation */
|
|
152
|
+
export interface BridgeOptions {
|
|
153
|
+
minFinalityThreshold?: number;
|
|
154
|
+
maxFee?: bigint;
|
|
155
|
+
destinationAddress?: Address;
|
|
156
|
+
destinationRpcUrl?: string;
|
|
157
|
+
attestationApiUrl?: string;
|
|
158
|
+
}
|
|
159
|
+
|
|
160
|
+
/** Result of a bridge.burn() operation */
|
|
161
|
+
export interface BurnResult {
|
|
162
|
+
burnTxHash: Hash;
|
|
163
|
+
nonce: bigint;
|
|
164
|
+
messageHash: Hex;
|
|
165
|
+
messageBytes: Hex;
|
|
166
|
+
sourceDomain: number;
|
|
167
|
+
destinationDomain: number;
|
|
168
|
+
}
|
|
169
|
+
|
|
170
|
+
/** Circle attestation status */
|
|
171
|
+
export type AttestationStatus = "pending_confirmations" | "complete" | "error";
|
|
172
|
+
|
|
173
|
+
/** Circle IRIS API response for an attestation */
|
|
174
|
+
export interface AttestationResponse {
|
|
175
|
+
status: AttestationStatus;
|
|
176
|
+
attestation: Hex | null;
|
|
177
|
+
error?: string;
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
/** Result of the full EVM↔EVM bridge() operation */
|
|
181
|
+
export interface BridgeResult {
|
|
182
|
+
burnTxHash: Hash;
|
|
183
|
+
mintTxHash: Hash;
|
|
184
|
+
amount: bigint;
|
|
185
|
+
fromChain: EVMBridgeChain;
|
|
186
|
+
toChain: EVMBridgeChain;
|
|
187
|
+
recipient: Address;
|
|
188
|
+
nonce: bigint;
|
|
189
|
+
elapsedMs: number;
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** Error codes for actionable bridge error messages */
|
|
193
|
+
export type BridgeErrorCode =
|
|
194
|
+
| "UNSUPPORTED_CHAIN"
|
|
195
|
+
| "INSUFFICIENT_ALLOWANCE"
|
|
196
|
+
| "INSUFFICIENT_BALANCE"
|
|
197
|
+
| "BURN_FAILED"
|
|
198
|
+
| "ATTESTATION_TIMEOUT"
|
|
199
|
+
| "ATTESTATION_ERROR"
|
|
200
|
+
| "MINT_FAILED"
|
|
201
|
+
| "INVALID_AMOUNT"
|
|
202
|
+
| "NO_WALLET_CLIENT";
|
|
@@ -0,0 +1,347 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @module convenience
|
|
3
|
+
* Environment-variable-driven wallet bootstrap helpers.
|
|
4
|
+
*
|
|
5
|
+
* Provides three dead-simple factory functions that read all configuration
|
|
6
|
+
* from process environment variables so that any agent can set up a fully
|
|
7
|
+
* functional wallet in 3 lines without touching viem directly:
|
|
8
|
+
*
|
|
9
|
+
* ```typescript
|
|
10
|
+
* import { walletFromEnv, setPolicyFromEnv, x402FromEnv } from 'agentwallet-sdk';
|
|
11
|
+
*
|
|
12
|
+
* const wallet = walletFromEnv();
|
|
13
|
+
* await setPolicyFromEnv(wallet);
|
|
14
|
+
* const client = x402FromEnv(wallet);
|
|
15
|
+
* ```
|
|
16
|
+
*
|
|
17
|
+
* ### Environment variables
|
|
18
|
+
*
|
|
19
|
+
* | Variable | Required | Default | Description |
|
|
20
|
+
* |-----------------------|----------|----------------|--------------------------------------------------|
|
|
21
|
+
* | AGENT_PRIVATE_KEY | ✅ yes | — | EOA private key (0x-prefixed hex, 32 bytes) |
|
|
22
|
+
* | AGENT_WALLET_ADDRESS | ✅ yes | — | AgentAccountV2 smart-wallet address |
|
|
23
|
+
* | CHAIN_NAME | no | base | Chain name (base, mainnet, arbitrum, …) |
|
|
24
|
+
* | CHAIN_ID | no | — | Alternative to CHAIN_NAME — numeric chain id |
|
|
25
|
+
* | RPC_URL | no | chain default | Custom JSON-RPC endpoint |
|
|
26
|
+
* | SPEND_LIMIT_PER_TX | no | — | Per-tx USDC limit (decimal, e.g. "1.00") |
|
|
27
|
+
* | SPEND_LIMIT_DAILY | no | — | Daily USDC limit (decimal, e.g. "10.00") |
|
|
28
|
+
* | SPEND_ALLOWLIST | no | — | Comma-separated recipient addresses |
|
|
29
|
+
* | X402_SUPPORTED_NETWORKS | no | (all mainnet) | Comma-separated "chain:chainId" network strings |
|
|
30
|
+
* | X402_GLOBAL_DAILY_LIMIT | no | — | Global daily x402 USDC limit (decimal) |
|
|
31
|
+
* | X402_PER_REQUEST_MAX | no | — | Per-request max USDC spend (decimal) |
|
|
32
|
+
*/
|
|
33
|
+
|
|
34
|
+
import type { Address, Chain, Hex } from "viem";
|
|
35
|
+
import { createWalletClient, http, parseUnits } from "viem";
|
|
36
|
+
import { privateKeyToAccount } from "viem/accounts";
|
|
37
|
+
import {
|
|
38
|
+
arbitrum,
|
|
39
|
+
base,
|
|
40
|
+
baseSepolia,
|
|
41
|
+
mainnet,
|
|
42
|
+
optimism,
|
|
43
|
+
polygon,
|
|
44
|
+
} from "viem/chains";
|
|
45
|
+
import type { AgentWalletConfig } from "./types.js";
|
|
46
|
+
import { createWallet, NATIVE_TOKEN, setSpendPolicy } from "./wallet-core.js";
|
|
47
|
+
import { createX402Client } from "./x402/middleware.js";
|
|
48
|
+
import { DEFAULT_SUPPORTED_NETWORKS, USDC_ADDRESSES } from "./x402/types.js";
|
|
49
|
+
|
|
50
|
+
// ─── Chain registry ────────────────────────────────────────────────────────
|
|
51
|
+
|
|
52
|
+
/**
|
|
53
|
+
* Well-known chains, keyed by name (lower-case) or numeric id.
|
|
54
|
+
* Extend this map if you need additional L2s.
|
|
55
|
+
*/
|
|
56
|
+
const CHAIN_MAP: Record<string, Chain> = {
|
|
57
|
+
base,
|
|
58
|
+
"base-sepolia": baseSepolia,
|
|
59
|
+
basesepolia: baseSepolia,
|
|
60
|
+
mainnet,
|
|
61
|
+
ethereum: mainnet,
|
|
62
|
+
eth: mainnet,
|
|
63
|
+
arbitrum,
|
|
64
|
+
arb: arbitrum,
|
|
65
|
+
optimism,
|
|
66
|
+
op: optimism,
|
|
67
|
+
polygon,
|
|
68
|
+
matic: polygon,
|
|
69
|
+
};
|
|
70
|
+
|
|
71
|
+
/** Fallback RPC endpoints for chains that have no public default in viem. */
|
|
72
|
+
const DEFAULT_RPC: Record<string, string> = {
|
|
73
|
+
base: "https://mainnet.base.org",
|
|
74
|
+
"base-sepolia": "https://sepolia.base.org",
|
|
75
|
+
mainnet: "https://cloudflare-eth.com",
|
|
76
|
+
ethereum: "https://cloudflare-eth.com",
|
|
77
|
+
arbitrum: "https://arb1.arbitrum.io/rpc",
|
|
78
|
+
optimism: "https://mainnet.optimism.io",
|
|
79
|
+
polygon: "https://polygon-rpc.com",
|
|
80
|
+
};
|
|
81
|
+
|
|
82
|
+
/**
|
|
83
|
+
* Resolve a chain object from either a name string or a numeric chain id.
|
|
84
|
+
* Tries CHAIN_MAP by name, then falls back to scanning by chainId.
|
|
85
|
+
*
|
|
86
|
+
* @param nameOrId - Chain name ("base", "arbitrum", …) or numeric id (8453, …).
|
|
87
|
+
* @returns The viem Chain object, or `base` as the default.
|
|
88
|
+
*/
|
|
89
|
+
function resolveChain(nameOrId: string | number): Chain {
|
|
90
|
+
if (typeof nameOrId === "number") {
|
|
91
|
+
const found = Object.values(CHAIN_MAP).find((c) => c.id === nameOrId);
|
|
92
|
+
return found ?? base;
|
|
93
|
+
}
|
|
94
|
+
const key = nameOrId.toLowerCase().trim();
|
|
95
|
+
return CHAIN_MAP[key] ?? base;
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/** Maps a viem chain to the string key expected by `createWallet` (subset of networks). */
|
|
99
|
+
function chainToCreateWalletKey(chain: Chain): AgentWalletConfig["chain"] {
|
|
100
|
+
switch (chain.id) {
|
|
101
|
+
case 8453:
|
|
102
|
+
return "base";
|
|
103
|
+
case 84532:
|
|
104
|
+
return "base-sepolia";
|
|
105
|
+
case 1:
|
|
106
|
+
return "ethereum";
|
|
107
|
+
case 42161:
|
|
108
|
+
return "arbitrum";
|
|
109
|
+
case 137:
|
|
110
|
+
return "polygon";
|
|
111
|
+
default:
|
|
112
|
+
throw new Error(
|
|
113
|
+
`[walletFromEnv] Unsupported chain for AgentWallet (${chain.name}, id ${chain.id}). ` +
|
|
114
|
+
`Use base, base-sepolia, ethereum, arbitrum, or polygon.`,
|
|
115
|
+
);
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
// ─── USDC scaling ──────────────────────────────────────────────────────────
|
|
120
|
+
|
|
121
|
+
const USDC_DECIMALS = 6;
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Parse a human-readable USDC amount (e.g. "1.50") into base units (bigint).
|
|
125
|
+
* Returns `undefined` if the string is falsy or not a valid number.
|
|
126
|
+
*
|
|
127
|
+
* @param value - Decimal string such as "0.50" or "100".
|
|
128
|
+
*/
|
|
129
|
+
function parseUsdc(value: string | undefined): bigint | undefined {
|
|
130
|
+
if (!value || value.trim() === "") return undefined;
|
|
131
|
+
try {
|
|
132
|
+
return parseUnits(value.trim(), USDC_DECIMALS);
|
|
133
|
+
} catch {
|
|
134
|
+
console.warn(`[walletFromEnv] Could not parse USDC amount: "${value}"`);
|
|
135
|
+
return undefined;
|
|
136
|
+
}
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
// ─── Public API ────────────────────────────────────────────────────────────
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Create a fully configured AgentWallet from environment variables.
|
|
143
|
+
*
|
|
144
|
+
* Reads:
|
|
145
|
+
* - **AGENT_PRIVATE_KEY** — EOA signing key (required; 0x-prefixed 32-byte hex)
|
|
146
|
+
* - **AGENT_WALLET_ADDRESS** — AgentAccountV2 contract address (required)
|
|
147
|
+
* - **CHAIN_NAME** or **CHAIN_ID** — target chain (optional; defaults to Base mainnet)
|
|
148
|
+
* - **RPC_URL** — custom RPC endpoint (optional; uses chain default if omitted)
|
|
149
|
+
*
|
|
150
|
+
* @param options - Optional overrides for chain and rpcUrl.
|
|
151
|
+
* @param options.chain - Chain name that takes precedence over the env var.
|
|
152
|
+
* @param options.rpcUrl - RPC endpoint that takes precedence over the env var.
|
|
153
|
+
* @returns A fully configured wallet object (same shape as `createWallet()`).
|
|
154
|
+
*
|
|
155
|
+
* @throws {Error} If AGENT_PRIVATE_KEY is missing or not a valid hex key.
|
|
156
|
+
* @throws {Error} If AGENT_WALLET_ADDRESS is missing.
|
|
157
|
+
*
|
|
158
|
+
* @example
|
|
159
|
+
* ```typescript
|
|
160
|
+
* const wallet = walletFromEnv({ chain: 'base-sepolia' });
|
|
161
|
+
* ```
|
|
162
|
+
*/
|
|
163
|
+
export function walletFromEnv(options?: {
|
|
164
|
+
chain?: string;
|
|
165
|
+
rpcUrl?: string;
|
|
166
|
+
}): ReturnType<typeof createWallet> {
|
|
167
|
+
// ── Private key ────────────────────────────────────────────────────────
|
|
168
|
+
const rawKey = process.env.AGENT_PRIVATE_KEY;
|
|
169
|
+
if (!rawKey || rawKey.trim() === "") {
|
|
170
|
+
throw new Error(
|
|
171
|
+
"[walletFromEnv] AGENT_PRIVATE_KEY environment variable is required. " +
|
|
172
|
+
"Set it to the 0x-prefixed 32-byte hex private key of your agent EOA.",
|
|
173
|
+
);
|
|
174
|
+
}
|
|
175
|
+
const privateKey = (rawKey.startsWith("0x") ? rawKey : `0x${rawKey}`) as Hex;
|
|
176
|
+
|
|
177
|
+
// ── Wallet (smart-contract) address ────────────────────────────────────
|
|
178
|
+
const walletAddress = process.env.AGENT_WALLET_ADDRESS;
|
|
179
|
+
if (!walletAddress || walletAddress.trim() === "") {
|
|
180
|
+
throw new Error(
|
|
181
|
+
"[walletFromEnv] AGENT_WALLET_ADDRESS environment variable is required. " +
|
|
182
|
+
"Set it to the address of your deployed AgentAccountV2 smart wallet.",
|
|
183
|
+
);
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
// ── Chain resolution ────────────────────────────────────────────────────
|
|
187
|
+
const chainSource =
|
|
188
|
+
options?.chain ??
|
|
189
|
+
process.env.CHAIN_NAME ??
|
|
190
|
+
(process.env.CHAIN_ID ? process.env.CHAIN_ID : undefined);
|
|
191
|
+
|
|
192
|
+
const chain = chainSource
|
|
193
|
+
? resolveChain(
|
|
194
|
+
/^\d+$/.test(String(chainSource))
|
|
195
|
+
? parseInt(chainSource, 10)
|
|
196
|
+
: String(chainSource),
|
|
197
|
+
)
|
|
198
|
+
: base;
|
|
199
|
+
|
|
200
|
+
// ── RPC URL ─────────────────────────────────────────────────────────────
|
|
201
|
+
const rpcUrl =
|
|
202
|
+
options?.rpcUrl ??
|
|
203
|
+
process.env.RPC_URL ??
|
|
204
|
+
DEFAULT_RPC[chain.name.toLowerCase()] ??
|
|
205
|
+
DEFAULT_RPC.base;
|
|
206
|
+
|
|
207
|
+
// ── Build viem walletClient ──────────────────────────────────────────────
|
|
208
|
+
const account = privateKeyToAccount(privateKey);
|
|
209
|
+
const walletClient = createWalletClient({
|
|
210
|
+
account,
|
|
211
|
+
chain,
|
|
212
|
+
transport: http(rpcUrl),
|
|
213
|
+
});
|
|
214
|
+
|
|
215
|
+
// ── Delegate to core createWallet ────────────────────────────────────────
|
|
216
|
+
return createWallet({
|
|
217
|
+
accountAddress: walletAddress as Address,
|
|
218
|
+
chain: chainToCreateWalletKey(chain),
|
|
219
|
+
rpcUrl,
|
|
220
|
+
walletClient,
|
|
221
|
+
});
|
|
222
|
+
}
|
|
223
|
+
|
|
224
|
+
/**
|
|
225
|
+
* Apply a spending policy to a wallet from environment variables.
|
|
226
|
+
*
|
|
227
|
+
* Reads:
|
|
228
|
+
* - **SPEND_LIMIT_PER_TX** — maximum USDC per single transaction (e.g. "1.00")
|
|
229
|
+
* - **SPEND_LIMIT_DAILY** — maximum USDC per 24-hour period (e.g. "10.00")
|
|
230
|
+
* - **SPEND_ALLOWLIST** — comma-separated ERC-20 token addresses to restrict
|
|
231
|
+
*
|
|
232
|
+
* If neither limit is set, logs a warning and falls back to queue-for-approval
|
|
233
|
+
* mode (perTxLimit = 0, periodLimit = 0) so all agent transactions must be
|
|
234
|
+
* manually approved. This is the safest default.
|
|
235
|
+
*
|
|
236
|
+
* @param wallet - Wallet returned by `walletFromEnv()` (or `createWallet()`).
|
|
237
|
+
* @returns Promise that resolves to the on-chain tx hash of the policy update.
|
|
238
|
+
*
|
|
239
|
+
* @example
|
|
240
|
+
* ```typescript
|
|
241
|
+
* await setPolicyFromEnv(wallet);
|
|
242
|
+
* ```
|
|
243
|
+
*/
|
|
244
|
+
export async function setPolicyFromEnv(
|
|
245
|
+
wallet: ReturnType<typeof createWallet>,
|
|
246
|
+
): Promise<string> {
|
|
247
|
+
const perTxLimit = parseUsdc(process.env.SPEND_LIMIT_PER_TX);
|
|
248
|
+
const periodLimit = parseUsdc(process.env.SPEND_LIMIT_DAILY);
|
|
249
|
+
|
|
250
|
+
// Determine which USDC token address to use for this chain
|
|
251
|
+
const chainId = wallet.chain.id;
|
|
252
|
+
const networkKey = Object.keys(USDC_ADDRESSES).find((k) => {
|
|
253
|
+
const parts = k.split(":");
|
|
254
|
+
return parts.length === 2 && parseInt(parts[1], 10) === chainId;
|
|
255
|
+
});
|
|
256
|
+
// Use USDC if found for this chain, otherwise fall back to native ETH (zero address)
|
|
257
|
+
const token: Address = networkKey
|
|
258
|
+
? (USDC_ADDRESSES[networkKey] as Address)
|
|
259
|
+
: NATIVE_TOKEN;
|
|
260
|
+
|
|
261
|
+
// Warn and use safe defaults when no limits are configured
|
|
262
|
+
if (perTxLimit === undefined && periodLimit === undefined) {
|
|
263
|
+
console.warn(
|
|
264
|
+
"[setPolicyFromEnv] Neither SPEND_LIMIT_PER_TX nor SPEND_LIMIT_DAILY is set. " +
|
|
265
|
+
"Defaulting to queue-for-approval mode (all agent transactions require owner sign-off). " +
|
|
266
|
+
"Set these env vars to enable autonomous spending.",
|
|
267
|
+
);
|
|
268
|
+
const hash = await setSpendPolicy(wallet, {
|
|
269
|
+
token,
|
|
270
|
+
perTxLimit: 0n,
|
|
271
|
+
periodLimit: 0n,
|
|
272
|
+
periodLength: 86400, // 24 h
|
|
273
|
+
});
|
|
274
|
+
console.info(
|
|
275
|
+
`[setPolicyFromEnv] Queue-for-approval policy set. tx: ${hash}`,
|
|
276
|
+
);
|
|
277
|
+
return hash;
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
const resolvedPerTxLimit = perTxLimit ?? 0n;
|
|
281
|
+
const resolvedPeriodLimit = periodLimit ?? 0n;
|
|
282
|
+
|
|
283
|
+
const hash = await setSpendPolicy(wallet, {
|
|
284
|
+
token,
|
|
285
|
+
perTxLimit: resolvedPerTxLimit,
|
|
286
|
+
periodLimit: resolvedPeriodLimit,
|
|
287
|
+
periodLength: 86400, // 24 h
|
|
288
|
+
});
|
|
289
|
+
|
|
290
|
+
console.info(
|
|
291
|
+
`[setPolicyFromEnv] Policy set — perTx: ${resolvedPerTxLimit} base units, ` +
|
|
292
|
+
`daily: ${resolvedPeriodLimit} base units on ${wallet.chain.name}. tx: ${hash}`,
|
|
293
|
+
);
|
|
294
|
+
|
|
295
|
+
return hash;
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
/**
|
|
299
|
+
* Create an x402 payment client from environment variables.
|
|
300
|
+
*
|
|
301
|
+
* Reads:
|
|
302
|
+
* - **X402_SUPPORTED_NETWORKS** — comma-separated "chain:chainId" strings
|
|
303
|
+
* (e.g. "base:8453,arbitrum:42161"); defaults to all supported mainnets
|
|
304
|
+
* - **X402_GLOBAL_DAILY_LIMIT** — total daily USDC budget across all x402
|
|
305
|
+
* services (decimal, e.g. "5.00")
|
|
306
|
+
* - **X402_PER_REQUEST_MAX** — per-request USDC ceiling (decimal)
|
|
307
|
+
*
|
|
308
|
+
* Sensible defaults: supports all 10 mainnet x402 chains, unlimited daily spend
|
|
309
|
+
* (rely on the agent's on-chain spend policy as the hard limit), 1 auto-retry.
|
|
310
|
+
*
|
|
311
|
+
* @param wallet - Wallet returned by `walletFromEnv()` (or `createWallet()`).
|
|
312
|
+
* @returns A configured X402Client instance.
|
|
313
|
+
*
|
|
314
|
+
* @example
|
|
315
|
+
* ```typescript
|
|
316
|
+
* const client = x402FromEnv(wallet);
|
|
317
|
+
* const data = await client.fetch('https://api.example.com/premium-endpoint');
|
|
318
|
+
* ```
|
|
319
|
+
*/
|
|
320
|
+
export function x402FromEnv(
|
|
321
|
+
wallet: ReturnType<typeof createWallet>,
|
|
322
|
+
): ReturnType<typeof createX402Client> {
|
|
323
|
+
// Supported networks — override with comma-separated list or use all defaults
|
|
324
|
+
const networksEnv = process.env.X402_SUPPORTED_NETWORKS;
|
|
325
|
+
const supportedNetworks: string[] = networksEnv
|
|
326
|
+
? networksEnv
|
|
327
|
+
.split(",")
|
|
328
|
+
.map((s: string) => s.trim())
|
|
329
|
+
.filter(Boolean)
|
|
330
|
+
: [...DEFAULT_SUPPORTED_NETWORKS];
|
|
331
|
+
|
|
332
|
+
// Global daily limit
|
|
333
|
+
const globalDailyLimitUsdc = parseUsdc(process.env.X402_GLOBAL_DAILY_LIMIT);
|
|
334
|
+
const globalPerRequestMaxUsdc = parseUsdc(process.env.X402_PER_REQUEST_MAX);
|
|
335
|
+
|
|
336
|
+
return createX402Client(wallet, {
|
|
337
|
+
supportedNetworks,
|
|
338
|
+
...(globalDailyLimitUsdc !== undefined && {
|
|
339
|
+
globalDailyLimit: globalDailyLimitUsdc,
|
|
340
|
+
}),
|
|
341
|
+
...(globalPerRequestMaxUsdc !== undefined && {
|
|
342
|
+
globalPerRequestMax: globalPerRequestMaxUsdc,
|
|
343
|
+
}),
|
|
344
|
+
autoPay: true,
|
|
345
|
+
maxRetries: 1,
|
|
346
|
+
});
|
|
347
|
+
}
|