@agether/sdk 2.8.1 → 2.11.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.
package/dist/index.d.mts CHANGED
@@ -136,7 +136,36 @@ declare class AgentNotApprovedError extends AgetherError {
136
136
  interface AgetherClientOptions {
137
137
  config: AgetherConfig;
138
138
  signer: Signer;
139
- agentId: bigint;
139
+ agentId?: bigint;
140
+ /** @internal Private key for signer refresh (set by fromPrivateKey) */
141
+ _privateKey?: string;
142
+ }
143
+ interface RegisterResult {
144
+ agentId: string;
145
+ address: string;
146
+ agentAccount: string;
147
+ alreadyRegistered: boolean;
148
+ kyaRequired: boolean;
149
+ tx?: string;
150
+ }
151
+ interface WithdrawFromAccountResult {
152
+ tx: string;
153
+ token: string;
154
+ amount: string;
155
+ destination: string;
156
+ }
157
+ interface BalancesResult {
158
+ agentId: string;
159
+ address: string;
160
+ eth: string;
161
+ usdc: string;
162
+ collateral: Record<string, string>;
163
+ agentAccount?: {
164
+ address: string;
165
+ eth: string;
166
+ usdc: string;
167
+ collateral: Record<string, string>;
168
+ };
140
169
  }
141
170
  declare class AgetherClient {
142
171
  private config;
@@ -146,28 +175,107 @@ declare class AgetherClient {
146
175
  private identityRegistry;
147
176
  private agether8004Scorer;
148
177
  private validationModule;
178
+ private entryPoint;
149
179
  private accountAddress?;
180
+ private _eoaAddress?;
181
+ private _privateKey?;
182
+ private _rpcUrl;
183
+ private _useExternalSigner;
150
184
  constructor(options: AgetherClientOptions);
185
+ /**
186
+ * Create an AgetherClient for a **new** agent (no agentId yet).
187
+ * Only `register()` and `getBalances()` are available until registration completes.
188
+ */
189
+ static fromPrivateKey(privateKey: string, chainIdOrConfig: ChainId | AgetherConfig): AgetherClient;
190
+ /**
191
+ * Create an AgetherClient for an **existing** agent with a known agentId.
192
+ */
151
193
  static fromPrivateKey(privateKey: string, agentId: bigint, chainIdOrConfig: ChainId | AgetherConfig): AgetherClient;
194
+ /**
195
+ * Create an AgetherClient from an **external signer** for a **new** agent (no agentId yet).
196
+ *
197
+ * Accepts any `ethers.AbstractSigner` — Privy, Bankr, Turnkey, MetaMask, etc.
198
+ * The signer **must** already be connected to a provider.
199
+ *
200
+ * Only `register()` and `getBalances()` are available until registration completes.
201
+ *
202
+ * @example
203
+ * ```ts
204
+ * const client = AgetherClient.fromSigner(privySigner, ChainId.Base);
205
+ * const result = await client.register();
206
+ * ```
207
+ */
208
+ static fromSigner(signer: ethers.AbstractSigner, chainIdOrConfig: ChainId | AgetherConfig): AgetherClient;
209
+ /**
210
+ * Create an AgetherClient from an **external signer** for an **existing** agent.
211
+ *
212
+ * @example
213
+ * ```ts
214
+ * const client = AgetherClient.fromSigner(privySigner, 42n, ChainId.Base);
215
+ * ```
216
+ */
217
+ static fromSigner(signer: ethers.AbstractSigner, agentId: bigint, chainIdOrConfig: ChainId | AgetherConfig): AgetherClient;
218
+ /**
219
+ * Register: create ERC-8004 identity + Safe account in one flow.
220
+ * If already registered, returns existing state.
221
+ *
222
+ * Sets `this.agentId` on success so subsequent operations work immediately.
223
+ */
224
+ register(): Promise<RegisterResult>;
225
+ /** Mint a new ERC-8004 identity and return the agentId. */
226
+ private _mintNewIdentity;
227
+ /**
228
+ * Deploy a Safe account smart wallet for the agent.
229
+ * The caller must own the ERC-8004 NFT.
230
+ */
152
231
  createAccount(): Promise<string>;
232
+ /** Get the Safe account address for the current agent. Cached after first call. */
153
233
  getAccountAddress(): Promise<string>;
234
+ /** Check whether the Safe account has been deployed. */
154
235
  accountExists(): Promise<boolean>;
155
- getBalances(): Promise<{
156
- eoa: {
157
- eth: string;
158
- usdc: string;
159
- };
160
- account?: {
161
- address: string;
162
- eth: string;
163
- usdc: string;
164
- };
165
- }>;
236
+ /**
237
+ * Get ETH, USDC, and collateral token balances for EOA and Safe account.
238
+ *
239
+ * Collateral tokens are resolved from a built-in registry of well-known
240
+ * tokens per chain (WETH, wstETH, cbETH).
241
+ */
242
+ getBalances(): Promise<BalancesResult>;
166
243
  /**
167
244
  * Fund the Safe account with USDC from EOA.
168
245
  * This is a simple ERC-20 transfer (does NOT require a UserOp).
169
246
  */
170
247
  fundAccount(usdcAmount: string): Promise<TransactionResult>;
248
+ /**
249
+ * Withdraw an ERC-20 token from Safe account to EOA.
250
+ * Executes a transfer via Safe UserOp.
251
+ *
252
+ * @param tokenSymbol - Token to withdraw (e.g. 'USDC', 'WETH', 'wstETH') or 0x address
253
+ * @param amount - Amount to withdraw (human-readable, e.g. '100', or 'all')
254
+ */
255
+ withdrawToken(tokenSymbol: string, amount: string): Promise<WithdrawFromAccountResult>;
256
+ /**
257
+ * Withdraw ETH from Safe account to EOA.
258
+ * Executes a native ETH transfer via Safe UserOp.
259
+ *
260
+ * @param amount - ETH amount (e.g. '0.01' or 'all')
261
+ */
262
+ withdrawEth(amount: string): Promise<WithdrawFromAccountResult>;
263
+ /**
264
+ * Send tokens to another agent's Safe account (or any address).
265
+ * Transfers from EOA (does NOT require a UserOp).
266
+ *
267
+ * @param target - `{ agentId: '42' }` or `{ address: '0x...' }`
268
+ * @param tokenSymbol - Token to send (e.g. 'WETH', 'USDC') or 0x address
269
+ * @param amount - Amount to send (human-readable)
270
+ */
271
+ sponsor(target: {
272
+ agentId?: string;
273
+ address?: string;
274
+ }, tokenSymbol: string, amount: string): Promise<{
275
+ tx: string;
276
+ targetAccount: string;
277
+ targetAgentId?: string;
278
+ }>;
171
279
  /**
172
280
  * Check if the KYA gate is active on the validation module.
173
281
  * If validationRegistry is not set, all txs pass (KYA disabled).
@@ -178,13 +286,18 @@ declare class AgetherClient {
178
286
  * Uses the ERC8004ValidationModule.isKYAApproved(account) view.
179
287
  */
180
288
  isKyaApproved(): Promise<boolean>;
289
+ /** Check whether the agent's ERC-8004 NFT exists. */
181
290
  identityExists(): Promise<boolean>;
291
+ /** Get the owner address of the ERC-8004 NFT. */
182
292
  getIdentityOwner(): Promise<string>;
293
+ /** Read the agent's current credit score from the Agether8004Scorer contract. */
183
294
  getCreditScore(): Promise<bigint>;
295
+ /** Check if the score is fresh (within MAX_ORACLE_AGE). */
184
296
  isScoreFresh(): Promise<{
185
297
  fresh: boolean;
186
298
  age: bigint;
187
299
  }>;
300
+ /** Check if the agent meets a minimum score threshold. */
188
301
  isEligible(minScore?: bigint): Promise<{
189
302
  eligible: boolean;
190
303
  currentScore: bigint;
@@ -194,10 +307,46 @@ declare class AgetherClient {
194
307
  get currentAccountAddress(): string | undefined;
195
308
  getSigner(): Signer;
196
309
  getAgentId(): bigint;
310
+ /** Require agentId to be set, throw a helpful error otherwise. */
311
+ private _requireAgentId;
312
+ /** Resolve EOA signer address (async, cached). */
313
+ private _getSignerAddress;
314
+ /**
315
+ * Resolve a token symbol or address to { address, symbol, decimals }.
316
+ *
317
+ * Supports:
318
+ * - `'USDC'` → from chain config
319
+ * - Well-known symbols (`'WETH'`, `'wstETH'`, `'cbETH'`) → built-in per-chain registry
320
+ * - `'0x...'` address → reads decimals and symbol onchain
321
+ */
322
+ private _resolveToken;
323
+ /**
324
+ * Refresh signer and rebind contracts for fresh nonce.
325
+ *
326
+ * For the privateKey path: recreates provider + wallet.
327
+ * For external signers: just rebinds contract instances.
328
+ */
329
+ private _refreshSigner;
330
+ /**
331
+ * Pack two uint128 values into a single bytes32:
332
+ * bytes32 = (hi << 128) | lo
333
+ */
334
+ private _packUint128;
335
+ /**
336
+ * Build, sign and submit a PackedUserOperation through EntryPoint.handleOps.
337
+ */
338
+ private _submitUserOp;
339
+ /**
340
+ * Execute a single call via Safe7579 account (ERC-7579 single mode)
341
+ * through an ERC-4337 UserOperation.
342
+ */
343
+ private _exec;
197
344
  }
198
345
 
199
346
  /**
200
- * MorphoClient — Direct Morpho Blue lending via Safe account (ERC-4337 UserOps)
347
+ * MorphoClient — Morpho Blue lending only (supply, borrow, repay, withdraw collateral)
348
+ *
349
+ * For registration, identity, balances, withdrawals, and sponsorship, use AgetherClient.
201
350
  *
202
351
  * Architecture (v2 — Safe + Safe7579):
203
352
  * EOA signs UserOp → EntryPoint.handleOps() → Safe → Safe7579 → execute → Morpho Blue
@@ -225,15 +374,13 @@ type AgetherSigner = ethers.AbstractSigner;
225
374
  /** Base configuration fields shared by both signing modes. */
226
375
  interface MorphoClientBaseConfig {
227
376
  rpcUrl: string;
228
- agentId?: string;
377
+ /** ERC-8004 agent ID (required). Use AgetherClient.register() first to get one. */
378
+ agentId: string;
229
379
  chainId?: ChainId;
230
380
  contracts?: Partial<{
231
381
  agether4337Factory: string;
232
382
  morphoBlue: string;
233
383
  usdc: string;
234
- agether8004Scorer: string;
235
- identityRegistry: string;
236
- erc8004ValidationModule: string;
237
384
  entryPoint: string;
238
385
  }>;
239
386
  }
@@ -275,28 +422,6 @@ type MorphoClientConfig = MorphoClientBaseConfig & ({
275
422
  signer: ethers.AbstractSigner;
276
423
  privateKey?: never;
277
424
  });
278
- interface BalancesResult {
279
- agentId: string;
280
- address: string;
281
- eth: string;
282
- usdc: string;
283
- collateral: Record<string, string>;
284
- agentAccount?: {
285
- address: string;
286
- eth: string;
287
- usdc: string;
288
- collateral: Record<string, string>;
289
- };
290
- }
291
- interface RegisterResult {
292
- agentId: string;
293
- address: string;
294
- agentAccount: string;
295
- alreadyRegistered: boolean;
296
- /** Whether the KYA (code verification) gate is active on this factory */
297
- kyaRequired: boolean;
298
- tx?: string;
299
- }
300
425
  interface PositionResult {
301
426
  marketId: string;
302
427
  collateralToken: string;
@@ -342,17 +467,6 @@ interface WithdrawResult {
342
467
  remainingCollateral: string;
343
468
  destination: string;
344
469
  }
345
- interface FundResult {
346
- tx: string;
347
- amount: string;
348
- agentAccount: string;
349
- }
350
- interface WithdrawFromAccountResult {
351
- tx: string;
352
- token: string;
353
- amount: string;
354
- destination: string;
355
- }
356
470
  interface SupplyAssetResult {
357
471
  tx: string;
358
472
  amount: string;
@@ -394,10 +508,7 @@ declare class MorphoClient {
394
508
  private _eoaAddress?;
395
509
  private agether4337Factory;
396
510
  private morphoBlue;
397
- private agether8004Scorer;
398
- private identityRegistry;
399
511
  private entryPoint;
400
- private validationModule;
401
512
  private _accountAddress?;
402
513
  private _marketCache;
403
514
  /** Dynamic token registry: symbol (uppercase) → { address, symbol, decimals } */
@@ -405,12 +516,6 @@ declare class MorphoClient {
405
516
  private _discoveredMarkets?;
406
517
  private _discoveredAt;
407
518
  constructor(config: MorphoClientConfig);
408
- /**
409
- * Check whether the KYA (Know Your Agent) code verification gate is active.
410
- * Reads the ERC8004ValidationModule's validationRegistry — when set to
411
- * a non-zero address, the module enforces KYA code approval.
412
- */
413
- isKyaRequired(): Promise<boolean>;
414
519
  /** Resolve the AgentAccount address (cached, with retry for flaky RPCs). */
415
520
  getAccountAddress(): Promise<string>;
416
521
  getAgentId(): string;
@@ -427,32 +532,6 @@ declare class MorphoClient {
427
532
  * Result is cached after the first call.
428
533
  */
429
534
  getSignerAddress(): Promise<string>;
430
- /** Mint a new ERC-8004 identity and return the agentId. */
431
- private _mintNewIdentity;
432
- /**
433
- * Register: create ERC-8004 identity + AgentAccount in one flow.
434
- * If already registered, returns existing state.
435
- */
436
- register(_name?: string): Promise<RegisterResult>;
437
- /** Get ETH / USDC / collateral balances for EOA and AgentAccount. */
438
- getBalances(): Promise<BalancesResult>;
439
- /** Transfer USDC from EOA to AgentAccount. */
440
- fundAccount(usdcAmount: string): Promise<FundResult>;
441
- /**
442
- * Withdraw (transfer) a token from AgentAccount to EOA.
443
- * Executes an ERC-20 transfer via Safe UserOp.
444
- *
445
- * @param tokenSymbol - Token to withdraw (e.g. 'USDC', 'WETH', 'wstETH')
446
- * @param amount - Amount to withdraw (human-readable, e.g. '100' for 100 USDC, or 'all')
447
- */
448
- withdrawToken(tokenSymbol: string, amount: string): Promise<WithdrawFromAccountResult>;
449
- /**
450
- * Withdraw ETH from AgentAccount to EOA.
451
- * Executes a native ETH transfer via Safe UserOp.
452
- *
453
- * @param amount - ETH amount (e.g. '0.01' or 'all')
454
- */
455
- withdrawEth(amount: string): Promise<WithdrawFromAccountResult>;
456
535
  /**
457
536
  * Fetch USDC borrow markets on Base from Morpho API.
458
537
  * Caches results for 5 minutes.
@@ -620,28 +699,6 @@ declare class MorphoClient {
620
699
  * @param receiver - defaults to EOA wallet
621
700
  */
622
701
  withdrawCollateral(tokenSymbol: string, amount: string, marketParams?: MorphoMarketParams, receiver?: string): Promise<WithdrawResult>;
623
- /**
624
- * Sponsor: transfer collateral to another agent's AgentAccount.
625
- * (The agent must then supplyCollateral themselves via their own account.)
626
- */
627
- sponsor(target: {
628
- agentId?: string;
629
- address?: string;
630
- }, tokenSymbol: string, amount: string): Promise<{
631
- tx: string;
632
- targetAccount: string;
633
- targetAgentId?: string;
634
- }>;
635
- getCreditScore(): Promise<bigint>;
636
- getAttestation(): Promise<ScoreAttestation>;
637
- isEligible(minScore?: bigint): Promise<{
638
- eligible: boolean;
639
- currentScore: bigint;
640
- }>;
641
- isScoreFresh(): Promise<{
642
- fresh: boolean;
643
- age: bigint;
644
- }>;
645
702
  /**
646
703
  * Refresh the signer and re-bind contract instances.
647
704
  *
@@ -694,11 +751,6 @@ declare class MorphoClient {
694
751
  * @param symbolOrAddress - e.g. 'WETH', 'wstETH', or '0x4200...'
695
752
  */
696
753
  private _resolveToken;
697
- /**
698
- * Get all discovered collateral tokens (for balance iteration, etc.).
699
- * Returns unique tokens from the Morpho API market discovery.
700
- */
701
- private _getDiscoveredTokens;
702
754
  /**
703
755
  * Compute net deposited amounts per market using Morpho GraphQL API.
704
756
  *
@@ -1308,4 +1360,4 @@ declare function getMorphoBlueAddress(chainId: ChainId): string;
1308
1360
  */
1309
1361
  declare function getContractAddresses(chainId: ChainId): ContractAddresses;
1310
1362
 
1311
- export { ACCOUNT_FACTORY_ABI, AGENT_REPUTATION_ABI, AGETHER_4337_FACTORY_ABI, AGETHER_8004_SCORER_ABI, AGETHER_8004_VALIDATION_MODULE_ABI, AGETHER_HOOK_MULTIPLEXER_ABI, AgentIdentityClient, type AgentIdentityClientOptions, AgentNotApprovedError, AgetherClient, type AgetherClientOptions, type AgetherConfig, AgetherError, type AgetherSigner, type AgetherViemWallet, type BalancesResult, type BorrowResult, ChainId, type ContractAddresses, type DepositAndBorrowResult, type DepositResult, ENTRYPOINT_V07_ABI, ERC20_ABI, ERC8004_VALIDATION_MODULE_ABI, type FundResult, HOOK_MULTIPLEXER_ABI, IDENTITY_REGISTRY_ABI, InsufficientBalanceError, MORPHO_BLUE_ABI, MorphoClient, type MorphoClientConfig, type MorphoMarketInfo, type MorphoMarketParams, type MorphoPosition, type PayFromYieldResult, type PaymentRequirements, type PositionResult, type RegisterResult, type RepayResult, SAFE7579_ACCOUNT_ABI, SAFE_AGENT_FACTORY_ABI, type ScoreAttestation, type ScoreResult, ScoringClient, type ScoringClientConfig, ScoringRejectedError, type SpendingTracker, type StatusResult, type SupplyAssetResult, type SupplyPositionResult, type TransactionResult, VALIDATION_REGISTRY_ABI, type WithdrawFromAccountResult, type WithdrawResult, type WithdrawSupplyResult, X402Client, type X402Config, type X402PaymentRequest, type X402PaymentResult, type X402Response, bpsToRate, createConfig, formatAPR, formatAddress, formatHealthFactor, formatPercent, formatTimestamp, formatUSD, formatUnits, getContractAddresses, getDefaultConfig, getMorphoBlueAddress, getUSDCAddress, parseUnits, rateToBps };
1363
+ export { ACCOUNT_FACTORY_ABI, AGENT_REPUTATION_ABI, AGETHER_4337_FACTORY_ABI, AGETHER_8004_SCORER_ABI, AGETHER_8004_VALIDATION_MODULE_ABI, AGETHER_HOOK_MULTIPLEXER_ABI, AgentIdentityClient, type AgentIdentityClientOptions, AgentNotApprovedError, AgetherClient, type AgetherClientOptions, type AgetherConfig, AgetherError, type AgetherSigner, type AgetherViemWallet, type BalancesResult, type BorrowResult, ChainId, type ContractAddresses, type DepositAndBorrowResult, type DepositResult, ENTRYPOINT_V07_ABI, ERC20_ABI, ERC8004_VALIDATION_MODULE_ABI, HOOK_MULTIPLEXER_ABI, IDENTITY_REGISTRY_ABI, InsufficientBalanceError, MORPHO_BLUE_ABI, MorphoClient, type MorphoClientConfig, type MorphoMarketInfo, type MorphoMarketParams, type MorphoPosition, type PayFromYieldResult, type PaymentRequirements, type PositionResult, type RegisterResult, type RepayResult, SAFE7579_ACCOUNT_ABI, SAFE_AGENT_FACTORY_ABI, type ScoreAttestation, type ScoreResult, ScoringClient, type ScoringClientConfig, ScoringRejectedError, type SpendingTracker, type StatusResult, type SupplyAssetResult, type SupplyPositionResult, type TransactionResult, VALIDATION_REGISTRY_ABI, type WithdrawFromAccountResult, type WithdrawResult, type WithdrawSupplyResult, X402Client, type X402Config, type X402PaymentRequest, type X402PaymentResult, type X402Response, bpsToRate, createConfig, formatAPR, formatAddress, formatHealthFactor, formatPercent, formatTimestamp, formatUSD, formatUnits, getContractAddresses, getDefaultConfig, getMorphoBlueAddress, getUSDCAddress, parseUnits, rateToBps };