@agether/sdk 2.12.2 → 2.14.0

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.
@@ -0,0 +1,242 @@
1
+ /**
2
+ * AgetherClient — Primary protocol client for AI agent operations
3
+ *
4
+ * Handles:
5
+ * - Registration (mint ERC-8004 NFT + deploy Safe account)
6
+ * - Account management (create, query, fund)
7
+ * - Balance queries (EOA + Safe, including collateral tokens)
8
+ * - Withdrawals from Safe to EOA (ERC-20 and ETH, via UserOps)
9
+ * - Agent-to-agent sponsorship
10
+ * - Identity checks (KYA, existence, ownership)
11
+ * - Credit scoring queries
12
+ *
13
+ * For Morpho Blue lending operations, use MorphoClient.
14
+ *
15
+ * Architecture (v2 — Safe + Safe7579):
16
+ * 1. Agent registers via ERC-8004 → gets agentId
17
+ * 2. Agether4337Factory.createAccount(agentId) → Safe proxy with Safe7579 adapter
18
+ * 3. Withdrawals from Safe execute via ERC-4337 UserOps
19
+ * 4. Use MorphoClient for lending (supply, borrow, repay, withdraw collateral)
20
+ */
21
+ import { ethers, Signer } from 'ethers';
22
+ import { AgetherConfig, TransactionResult, ChainId } from '../types';
23
+ export interface AgetherClientOptions {
24
+ config: AgetherConfig;
25
+ signer: Signer;
26
+ agentId?: bigint;
27
+ /** @internal Private key for signer refresh (set by fromPrivateKey) */
28
+ _privateKey?: string;
29
+ }
30
+ export interface RegisterResult {
31
+ agentId: string;
32
+ address: string;
33
+ agentAccount: string;
34
+ alreadyRegistered: boolean;
35
+ kyaRequired: boolean;
36
+ tx?: string;
37
+ }
38
+ export interface WithdrawFromAccountResult {
39
+ tx: string;
40
+ token: string;
41
+ amount: string;
42
+ destination: string;
43
+ }
44
+ export interface BalancesResult {
45
+ agentId: string;
46
+ address: string;
47
+ eth: string;
48
+ usdc: string;
49
+ collateral: Record<string, string>;
50
+ agentAccount?: {
51
+ address: string;
52
+ eth: string;
53
+ usdc: string;
54
+ collateral: Record<string, string>;
55
+ };
56
+ }
57
+ export declare class AgetherClient {
58
+ private config;
59
+ private signer;
60
+ private agentId;
61
+ private agether4337Factory;
62
+ private identityRegistry;
63
+ private agether8004Scorer;
64
+ private validationModule;
65
+ private entryPoint;
66
+ private accountAddress?;
67
+ private _eoaAddress?;
68
+ private _privateKey?;
69
+ private _rpcUrl;
70
+ private _useExternalSigner;
71
+ constructor(options: AgetherClientOptions);
72
+ /**
73
+ * Create an AgetherClient for a **new** agent (no agentId yet).
74
+ * Only `register()` and `getBalances()` are available until registration completes.
75
+ */
76
+ static fromPrivateKey(privateKey: string, chainIdOrConfig: ChainId | AgetherConfig): AgetherClient;
77
+ /**
78
+ * Create an AgetherClient for an **existing** agent with a known agentId.
79
+ */
80
+ static fromPrivateKey(privateKey: string, agentId: bigint, chainIdOrConfig: ChainId | AgetherConfig): AgetherClient;
81
+ /**
82
+ * Create an AgetherClient from an **external signer** for a **new** agent (no agentId yet).
83
+ *
84
+ * Accepts any `ethers.AbstractSigner` — Privy, Bankr, Turnkey, MetaMask, etc.
85
+ * The signer **must** already be connected to a provider on the correct chain.
86
+ *
87
+ * Validates that the signer's provider chain matches the requested chain.
88
+ *
89
+ * Only `register()` and `getBalances()` are available until registration completes.
90
+ *
91
+ * @example
92
+ * ```ts
93
+ * const client = await AgetherClient.fromSigner(privySigner, ChainId.Base);
94
+ * const result = await client.register();
95
+ * ```
96
+ */
97
+ static fromSigner(signer: ethers.AbstractSigner, chainIdOrConfig: ChainId | AgetherConfig): Promise<AgetherClient>;
98
+ /**
99
+ * Create an AgetherClient from an **external signer** for an **existing** agent.
100
+ *
101
+ * @example
102
+ * ```ts
103
+ * const client = await AgetherClient.fromSigner(privySigner, 42n, ChainId.Base);
104
+ * ```
105
+ */
106
+ static fromSigner(signer: ethers.AbstractSigner, agentId: bigint, chainIdOrConfig: ChainId | AgetherConfig): Promise<AgetherClient>;
107
+ /**
108
+ * Register: create ERC-8004 identity + Safe account in one flow.
109
+ * If already registered, returns existing state.
110
+ *
111
+ * Sets `this.agentId` on success so subsequent operations work immediately.
112
+ *
113
+ * @param options.name Agent display name (stored in on-chain metadata URI)
114
+ * @param options.description Agent description (defaults to 'AI agent registered via @agether/sdk')
115
+ */
116
+ register(options?: {
117
+ name?: string;
118
+ description?: string;
119
+ }): Promise<RegisterResult>;
120
+ /** Mint a new ERC-8004 identity and return the agentId.
121
+ * Builds a JSON metadata document per ERC-8004 spec and encodes it as a data: URI.
122
+ */
123
+ private _mintNewIdentity;
124
+ /**
125
+ * Deploy a Safe account smart wallet for the agent.
126
+ * The caller must own the ERC-8004 NFT.
127
+ */
128
+ createAccount(): Promise<string>;
129
+ /** Get the Safe account address for the current agent. Cached after first call. */
130
+ getAccountAddress(): Promise<string>;
131
+ /** Check whether the Safe account has been deployed. */
132
+ accountExists(): Promise<boolean>;
133
+ /**
134
+ * Get ETH, USDC, and collateral token balances for EOA and Safe account.
135
+ *
136
+ * Collateral tokens are resolved from a built-in registry of well-known
137
+ * tokens per chain (WETH, wstETH, cbETH).
138
+ */
139
+ getBalances(): Promise<BalancesResult>;
140
+ /**
141
+ * Fund the Safe account with USDC from EOA.
142
+ * This is a simple ERC-20 transfer (does NOT require a UserOp).
143
+ */
144
+ fundAccount(usdcAmount: string): Promise<TransactionResult>;
145
+ /**
146
+ * Withdraw an ERC-20 token from Safe account to EOA.
147
+ * Executes a transfer via Safe UserOp.
148
+ *
149
+ * @param tokenSymbol - Token to withdraw (e.g. 'USDC', 'WETH', 'wstETH') or 0x address
150
+ * @param amount - Amount to withdraw (human-readable, e.g. '100', or 'all')
151
+ */
152
+ withdrawToken(tokenSymbol: string, amount: string): Promise<WithdrawFromAccountResult>;
153
+ /**
154
+ * Withdraw ETH from Safe account to EOA.
155
+ * Executes a native ETH transfer via Safe UserOp.
156
+ *
157
+ * @param amount - ETH amount (e.g. '0.01' or 'all')
158
+ */
159
+ withdrawEth(amount: string): Promise<WithdrawFromAccountResult>;
160
+ /**
161
+ * Send tokens to another agent's Safe account (or any address).
162
+ * Transfers from EOA (does NOT require a UserOp).
163
+ *
164
+ * @param target - `{ agentId: '42' }` or `{ address: '0x...' }`
165
+ * @param tokenSymbol - Token to send (e.g. 'WETH', 'USDC') or 0x address
166
+ * @param amount - Amount to send (human-readable)
167
+ */
168
+ sponsor(target: {
169
+ agentId?: string;
170
+ address?: string;
171
+ }, tokenSymbol: string, amount: string): Promise<{
172
+ tx: string;
173
+ targetAccount: string;
174
+ targetAgentId?: string;
175
+ }>;
176
+ /**
177
+ * Check if the KYA gate is active on the validation module.
178
+ * If validationRegistry is not set, all txs pass (KYA disabled).
179
+ */
180
+ isKyaRequired(): Promise<boolean>;
181
+ /**
182
+ * Check if this agent's code is KYA-approved.
183
+ * Uses the ERC8004ValidationModule.isKYAApproved(account) view.
184
+ */
185
+ isKyaApproved(): Promise<boolean>;
186
+ /** Check whether the agent's ERC-8004 NFT exists. */
187
+ identityExists(): Promise<boolean>;
188
+ /** Get the owner address of the ERC-8004 NFT. */
189
+ getIdentityOwner(): Promise<string>;
190
+ /** Read the agent's current credit score from the Agether8004Scorer contract. */
191
+ getCreditScore(): Promise<bigint>;
192
+ /** Check if the score is fresh (within MAX_ORACLE_AGE). */
193
+ isScoreFresh(): Promise<{
194
+ fresh: boolean;
195
+ age: bigint;
196
+ }>;
197
+ /** Check if the agent meets a minimum score threshold. */
198
+ isEligible(minScore?: bigint): Promise<{
199
+ eligible: boolean;
200
+ currentScore: bigint;
201
+ }>;
202
+ get chainId(): ChainId;
203
+ get contracts(): import("..").ContractAddresses;
204
+ get currentAccountAddress(): string | undefined;
205
+ getSigner(): Signer;
206
+ getAgentId(): bigint;
207
+ /** Require agentId to be set, throw a helpful error otherwise. */
208
+ private _requireAgentId;
209
+ /** Resolve EOA signer address (async, cached). */
210
+ private _getSignerAddress;
211
+ /**
212
+ * Resolve a token symbol or address to { address, symbol, decimals }.
213
+ *
214
+ * Supports:
215
+ * - `'USDC'` → from chain config
216
+ * - Well-known symbols (`'WETH'`, `'wstETH'`, `'cbETH'`) → built-in per-chain registry
217
+ * - `'0x...'` address → reads decimals and symbol onchain
218
+ */
219
+ private _resolveToken;
220
+ /**
221
+ * Refresh signer and rebind contracts for fresh nonce.
222
+ *
223
+ * For the privateKey path: recreates provider + wallet.
224
+ * For external signers: just rebinds contract instances.
225
+ */
226
+ private _refreshSigner;
227
+ /**
228
+ * Pack two uint128 values into a single bytes32:
229
+ * bytes32 = (hi << 128) | lo
230
+ */
231
+ private _packUint128;
232
+ /**
233
+ * Build, sign and submit a PackedUserOperation through EntryPoint.handleOps.
234
+ */
235
+ private _submitUserOp;
236
+ /**
237
+ * Execute a single call via Safe7579 account (ERC-7579 single mode)
238
+ * through an ERC-4337 UserOperation.
239
+ */
240
+ private _exec;
241
+ }
242
+ //# sourceMappingURL=AgetherClient.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"AgetherClient.d.ts","sourceRoot":"","sources":["../../src/clients/AgetherClient.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;;;;;;GAmBG;AAEH,OAAO,EAAE,MAAM,EAAE,MAAM,EAAY,MAAM,QAAQ,CAAC;AAClD,OAAO,EACL,aAAa,EACb,iBAAiB,EAEjB,OAAO,EACR,MAAM,UAAU,CAAC;AAqClB,MAAM,WAAW,oBAAoB;IACnC,MAAM,EAAE,aAAa,CAAC;IACtB,MAAM,EAAE,MAAM,CAAC;IACf,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,uEAAuE;IACvE,WAAW,CAAC,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,cAAc;IAC7B,OAAO,EAAE,MAAM,CAAC;IAChB,OAAO,EAAE,MAAM,CAAC;IAChB,YAAY,EAAE,MAAM,CAAC;IACrB,iBAAiB,EAAE,OAAO,CAAC;IAC3B,WAAW,EAAE,OAAO,CAAC;IACrB,EAAE,CAAC,EAAE,MAAM,CAAC;CACb;AAED,MAAM,WAAW,yBAAyB;IACxC,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IAC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