@agether/sdk 1.6.1 → 1.6.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.
package/dist/index.mjs CHANGED
@@ -135,9 +135,9 @@ var CONTRACT_ADDRESSES = {
135
135
  morphoBlue: "0xBBBBBbbBBb9cC5e90e3b3Af64bdAF62C37EEFFCb"
136
136
  },
137
137
  [8453 /* Base */]: {
138
- accountFactory: "0x7D5D56416bAEA06a9DCBe3092eF335724C6320a0",
139
- validationRegistry: "0xd196C32D2149270F56E209ba7aEE67CE9ceD2001",
140
- agentReputation: "0x65c9cA1211809D3CF3A2707558198eb2b2bE623c",
138
+ accountFactory: "0x871eb6b07964bc308bf68b18ca5824AFE5Cb0c8b",
139
+ validationRegistry: "0x5a2FF014C68a2498554B5786ED92483E6d56D06f",
140
+ agentReputation: "0x47adEA82a8975a60D81483CD39C377F905988DF1",
141
141
  usdc: "0x833589fCD6eDb6E08f4c7C32D4f71b54bdA02913",
142
142
  identityRegistry: "0x8004A169FB4a3325136EB29fA0ceB6D2e539a432",
143
143
  morphoBlue: "0xBBBBBbbBBb9cC5e90e3b3Af64bdAF62C37EEFFCb"
@@ -696,6 +696,179 @@ var MorphoClient = class {
696
696
  };
697
697
  }
698
698
  // ════════════════════════════════════════════════════════
699
+ // Balance & Borrowing Capacity
700
+ // ════════════════════════════════════════════════════════
701
+ /**
702
+ * Get the USDC balance of the AgentAccount.
703
+ * @returns USDC balance in raw units (6 decimals)
704
+ */
705
+ async getUsdcBalance() {
706
+ const acctAddr = await this.getAccountAddress();
707
+ const usdc = new Contract2(this.config.contracts.usdc, ERC20_ABI, this.provider);
708
+ return usdc.balanceOf(acctAddr);
709
+ }
710
+ /**
711
+ * Calculate the maximum additional USDC that can be borrowed
712
+ * given the agent's current collateral and debt across all markets.
713
+ *
714
+ * For each market with collateral deposited:
715
+ * maxBorrow = (collateralValue * LLTV) - currentDebt
716
+ *
717
+ * Uses the Morpho oracle to price collateral → loan token.
718
+ *
719
+ * @returns Maximum additional USDC borrowable (6 decimals)
720
+ */
721
+ async getMaxBorrowable() {
722
+ const acctAddr = await this.getAccountAddress();
723
+ const markets = await this.getMarkets();
724
+ let totalAdditional = 0n;
725
+ const byMarket = [];
726
+ for (const m of markets) {
727
+ if (!m.collateralAsset || m.collateralAsset.address === ethers2.ZeroAddress) continue;
728
+ try {
729
+ const pos = await this.morphoBlue.position(m.uniqueKey, acctAddr);
730
+ if (pos.collateral === 0n) continue;
731
+ const mktState = await this.morphoBlue.market(m.uniqueKey);
732
+ const totalBorrowShares = BigInt(mktState.totalBorrowShares);
733
+ const totalBorrowAssets = BigInt(mktState.totalBorrowAssets);
734
+ const currentDebt = totalBorrowShares > 0n ? BigInt(pos.borrowShares) * totalBorrowAssets / totalBorrowShares : 0n;
735
+ let collateralValueInLoan;
736
+ try {
737
+ const oracleContract = new Contract2(m.oracle, [
738
+ "function price() view returns (uint256)"
739
+ ], this.provider);
740
+ const oraclePrice = await oracleContract.price();
741
+ const ORACLE_PRICE_SCALE = 10n ** 36n;
742
+ collateralValueInLoan = BigInt(pos.collateral) * oraclePrice / ORACLE_PRICE_SCALE;
743
+ } catch {
744
+ continue;
745
+ }
746
+ const maxBorrowTotal = collateralValueInLoan * m.lltv / 10n ** 18n;
747
+ const maxAdditional = maxBorrowTotal > currentDebt ? maxBorrowTotal - currentDebt : 0n;
748
+ totalAdditional += maxAdditional;
749
+ byMarket.push({
750
+ collateralToken: m.collateralAsset.symbol,
751
+ maxAdditional,
752
+ currentDebt,
753
+ collateralValue: collateralValueInLoan
754
+ });
755
+ } catch {
756
+ continue;
757
+ }
758
+ }
759
+ return { total: totalAdditional, byMarket };
760
+ }
761
+ // ════════════════════════════════════════════════════════
762
+ // Market Rates & Yield Estimation
763
+ // ════════════════════════════════════════════════════════
764
+ /**
765
+ * Fetch current supply/borrow APY for a collateral market from Morpho GraphQL API.
766
+ *
767
+ * Note: On Morpho Blue, collateral does NOT earn yield directly. Supply APY
768
+ * is what lenders earn; borrow APY is what borrowers pay.
769
+ */
770
+ async getMarketRates(collateralSymbolOrAddress) {
771
+ const chainId = this.config.chainId;
772
+ const usdcAddr = this.config.contracts.usdc.toLowerCase();
773
+ let collateralFilter = "";
774
+ if (collateralSymbolOrAddress) {
775
+ const colInfo = BASE_COLLATERALS[collateralSymbolOrAddress];
776
+ const colAddr = (colInfo?.address ?? collateralSymbolOrAddress).toLowerCase();
777
+ collateralFilter = `, collateralAssetAddress_in: ["${colAddr}"]`;
778
+ }
779
+ const query = `{
780
+ markets(
781
+ first: 50
782
+ orderBy: SupplyAssetsUsd
783
+ orderDirection: Desc
784
+ where: { chainId_in: [${chainId}], loanAssetAddress_in: ["${usdcAddr}"]${collateralFilter} }
785
+ ) {
786
+ items {
787
+ uniqueKey
788
+ lltv
789
+ loanAsset { address symbol decimals }
790
+ collateralAsset { address symbol decimals }
791
+ state {
792
+ borrowAssets
793
+ supplyAssets
794
+ utilization
795
+ supplyApy
796
+ borrowApy
797
+ }
798
+ }
799
+ }
800
+ }`;
801
+ try {
802
+ const resp = await axios.post(MORPHO_API_URL, { query }, { timeout: 1e4 });
803
+ const items = resp.data?.data?.markets?.items ?? [];
804
+ return items.filter((m) => m.collateralAsset?.address && m.collateralAsset.address !== ethers2.ZeroAddress).map((m) => ({
805
+ collateralToken: m.collateralAsset.symbol,
806
+ loanToken: m.loanAsset.symbol,
807
+ supplyApy: m.state?.supplyApy ? Number(m.state.supplyApy) : 0,
808
+ borrowApy: m.state?.borrowApy ? Number(m.state.borrowApy) : 0,
809
+ utilization: m.state?.utilization ? Number(m.state.utilization) : 0,
810
+ totalSupplyUsd: m.state?.supplyAssets ? Number(m.state.supplyAssets) / 1e6 : 0,
811
+ totalBorrowUsd: m.state?.borrowAssets ? Number(m.state.borrowAssets) / 1e6 : 0,
812
+ lltv: `${(Number(m.lltv) / 1e16).toFixed(0)}%`,
813
+ marketId: m.uniqueKey
814
+ }));
815
+ } catch {
816
+ return [];
817
+ }
818
+ }
819
+ /**
820
+ * Estimate theoretical yield for a given collateral amount over a period.
821
+ *
822
+ * ⚠️ IMPORTANT: On Morpho Blue, collateral does NOT earn yield directly.
823
+ * This estimates what the collateral WOULD earn if it were instead supplied
824
+ * as a lender (not used as collateral). This is a theoretical upper bound
825
+ * useful for setting spending caps.
826
+ *
827
+ * @param collateralSymbol - e.g. 'WETH'
828
+ * @param amount - collateral amount in human-readable (e.g. '1.5')
829
+ * @param periodDays - estimation period in days (default: 1)
830
+ * @param ethPriceUsd - ETH price in USD for value conversion (if not provided, uses oracle)
831
+ * @returns Estimated yield in USD for the period
832
+ */
833
+ async getYieldEstimate(collateralSymbol, amount, periodDays = 1, ethPriceUsd) {
834
+ const colInfo = BASE_COLLATERALS[collateralSymbol];
835
+ if (!colInfo) throw new AgetherError(`Unknown collateral: ${collateralSymbol}`, "UNKNOWN_COLLATERAL");
836
+ const rates = await this.getMarketRates(collateralSymbol);
837
+ if (rates.length === 0) {
838
+ throw new AgetherError(`No market found for ${collateralSymbol}`, "MARKET_NOT_FOUND");
839
+ }
840
+ const market = rates[0];
841
+ const supplyApy = market.supplyApy;
842
+ let collateralValueUsd;
843
+ if (ethPriceUsd) {
844
+ collateralValueUsd = parseFloat(amount) * ethPriceUsd;
845
+ } else {
846
+ try {
847
+ const params = await this.findMarketForCollateral(collateralSymbol);
848
+ const oracleContract = new Contract2(params.oracle, [
849
+ "function price() view returns (uint256)"
850
+ ], this.provider);
851
+ const oraclePrice = await oracleContract.price();
852
+ const ORACLE_PRICE_SCALE = 10n ** 36n;
853
+ const amountWei = ethers2.parseUnits(amount, colInfo.decimals);
854
+ const valueInUsdc = amountWei * oraclePrice / ORACLE_PRICE_SCALE;
855
+ collateralValueUsd = Number(valueInUsdc) / 1e6;
856
+ } catch {
857
+ throw new AgetherError("Cannot determine collateral value. Provide ethPriceUsd.", "PRICE_UNAVAILABLE");
858
+ }
859
+ }
860
+ const estimatedYieldUsd = collateralValueUsd * supplyApy * (periodDays / 365);
861
+ return {
862
+ collateralToken: collateralSymbol,
863
+ amount,
864
+ periodDays,
865
+ theoreticalSupplyApy: supplyApy,
866
+ estimatedYieldUsd,
867
+ collateralValueUsd,
868
+ disclaimer: "Collateral on Morpho Blue does NOT earn yield directly. This estimates what it WOULD earn if supplied as a lender instead. Use as a theoretical spending cap."
869
+ };
870
+ }
871
+ // ════════════════════════════════════════════════════════
699
872
  // Lending Operations (all via AgentAccount.executeBatch)
700
873
  // ════════════════════════════════════════════════════════
701
874
  /**
@@ -1088,6 +1261,9 @@ var X402Client = class {
1088
1261
  const client = new x402Client();
1089
1262
  registerExactEvmScheme(client, { signer });
1090
1263
  this.paidFetch = wrapFetchWithPayment(fetch, client);
1264
+ const today = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
1265
+ const dailyLimit = config.dailySpendLimitUsdc ? BigInt(Math.round(parseFloat(config.dailySpendLimitUsdc) * 1e6)) : 0n;
1266
+ this._spendingTracker = { date: today, totalBorrowed: 0n, dailyLimit };
1091
1267
  }
1092
1268
  async get(url, opts) {
1093
1269
  return this.request(url, { ...opts, method: "GET" });
@@ -1103,6 +1279,82 @@ var X402Client = class {
1103
1279
  getAddress() {
1104
1280
  return this.address;
1105
1281
  }
1282
+ /** Get the current spending tracker state */
1283
+ getSpendingTracker() {
1284
+ this._resetTrackerIfNewDay();
1285
+ return { ...this._spendingTracker };
1286
+ }
1287
+ /** Get remaining daily spending allowance in USDC (human-readable) */
1288
+ getRemainingDailyAllowance() {
1289
+ this._resetTrackerIfNewDay();
1290
+ if (this._spendingTracker.dailyLimit === 0n) return "unlimited";
1291
+ const remaining = this._spendingTracker.dailyLimit - this._spendingTracker.totalBorrowed;
1292
+ return (Number(remaining > 0n ? remaining : 0n) / 1e6).toFixed(2);
1293
+ }
1294
+ /**
1295
+ * Pay with auto-draw: Make an x402 request with automatic Morpho borrowing.
1296
+ *
1297
+ * Flow:
1298
+ * 1. Check USDC balance on AgentAccount
1299
+ * 2. Probe the URL to discover payment amount (if 402)
1300
+ * 3. If insufficient USDC, calculate deficit
1301
+ * 4. Check spending limit
1302
+ * 5. Borrow from Morpho via MorphoClient
1303
+ * 6. Proceed with x402 payment
1304
+ */
1305
+ async payWithAutoDraw(url, opts) {
1306
+ const { morphoClient, ...fetchOpts } = opts || {};
1307
+ if (!this.config.autoDraw || !morphoClient) {
1308
+ return this.request(url, fetchOpts);
1309
+ }
1310
+ try {
1311
+ const usdcBalance = await morphoClient.getUsdcBalance();
1312
+ console.log(` [auto-draw] AgentAccount USDC balance: ${(Number(usdcBalance) / 1e6).toFixed(2)}`);
1313
+ const paymentAmount = await this._probePaymentAmount(url, fetchOpts);
1314
+ if (paymentAmount !== null) {
1315
+ console.log(` [auto-draw] Payment required: ${(Number(paymentAmount) / 1e6).toFixed(6)} USDC`);
1316
+ const bufferStr = this.config.autoDrawBuffer || "0.5";
1317
+ const buffer = BigInt(Math.round(parseFloat(bufferStr) * 1e6));
1318
+ const needed = paymentAmount + buffer;
1319
+ if (usdcBalance < needed) {
1320
+ const deficit = needed - usdcBalance;
1321
+ console.log(` [auto-draw] Insufficient balance. Need to borrow ${(Number(deficit) / 1e6).toFixed(2)} USDC`);
1322
+ const limitCheck = await this._checkSpendingLimit(deficit, morphoClient);
1323
+ if (!limitCheck.allowed) {
1324
+ return {
1325
+ success: false,
1326
+ error: `Auto-draw blocked: ${limitCheck.reason}`
1327
+ };
1328
+ }
1329
+ const maxBorrowable = await morphoClient.getMaxBorrowable();
1330
+ if (maxBorrowable.total < deficit) {
1331
+ return {
1332
+ success: false,
1333
+ error: `Auto-draw failed: insufficient collateral. Need ${(Number(deficit) / 1e6).toFixed(2)} USDC but can only borrow ${(Number(maxBorrowable.total) / 1e6).toFixed(2)} USDC more.`
1334
+ };
1335
+ }
1336
+ const borrowAmount = (Number(deficit) / 1e6).toFixed(6);
1337
+ console.log(` [auto-draw] Borrowing ${borrowAmount} USDC from Morpho...`);
1338
+ const borrowResult = await morphoClient.borrow(borrowAmount);
1339
+ console.log(` [auto-draw] Borrow tx: ${borrowResult.tx}`);
1340
+ this._trackSpending(deficit);
1341
+ const result = await this.request(url, fetchOpts);
1342
+ return {
1343
+ ...result,
1344
+ autoDrawInfo: {
1345
+ borrowed: borrowAmount,
1346
+ borrowTx: borrowResult.tx,
1347
+ reason: `USDC balance insufficient (had ${(Number(usdcBalance) / 1e6).toFixed(2)}, needed ${(Number(needed) / 1e6).toFixed(2)})`
1348
+ }
1349
+ };
1350
+ }
1351
+ }
1352
+ return this.request(url, fetchOpts);
1353
+ } catch (error) {
1354
+ console.log(` [auto-draw] Auto-draw check failed: ${error instanceof Error ? error.message : String(error)}. Proceeding with normal request.`);
1355
+ return this.request(url, fetchOpts);
1356
+ }
1357
+ }
1106
1358
  // ──────────── Core request — @x402/fetch handles 402 automatically ────────────
1107
1359
  async request(url, options) {
1108
1360
  try {
@@ -1152,6 +1404,102 @@ var X402Client = class {
1152
1404
  };
1153
1405
  }
1154
1406
  }
1407
+ // ──────────── Auto-Draw Helpers ────────────
1408
+ /**
1409
+ * Probe a URL to discover payment requirements without paying.
1410
+ * Makes a request and parses the 402 PAYMENT-REQUIRED header.
1411
+ * @returns Payment amount in raw USDC units (6 decimals), or null if not a 402.
1412
+ */
1413
+ async _probePaymentAmount(url, options) {
1414
+ try {
1415
+ const response = await fetch(url, {
1416
+ ...options,
1417
+ headers: {
1418
+ ...options?.headers,
1419
+ "X-Agent-Id": this.config.agentId || ""
1420
+ }
1421
+ });
1422
+ if (response.status !== 402) return null;
1423
+ const paymentHeader = response.headers.get("X-PAYMENT") || response.headers.get("PAYMENT-REQUIRED");
1424
+ if (!paymentHeader) return null;
1425
+ try {
1426
+ const decoded = JSON.parse(Buffer.from(paymentHeader, "base64").toString("utf-8"));
1427
+ const requirements = Array.isArray(decoded) ? decoded : decoded.accepts || [decoded];
1428
+ if (requirements.length > 0) {
1429
+ const amount = requirements[0].maxAmountRequired || requirements[0].amount;
1430
+ if (amount) {
1431
+ return BigInt(amount);
1432
+ }
1433
+ }
1434
+ } catch {
1435
+ }
1436
+ return null;
1437
+ } catch {
1438
+ return null;
1439
+ }
1440
+ }
1441
+ /** Reset spending tracker if it's a new day */
1442
+ _resetTrackerIfNewDay() {
1443
+ const today = (/* @__PURE__ */ new Date()).toISOString().split("T")[0];
1444
+ if (this._spendingTracker.date !== today) {
1445
+ this._spendingTracker = {
1446
+ date: today,
1447
+ totalBorrowed: 0n,
1448
+ dailyLimit: this._spendingTracker.dailyLimit
1449
+ };
1450
+ }
1451
+ }
1452
+ /** Track a new spending amount */
1453
+ _trackSpending(amount) {
1454
+ this._resetTrackerIfNewDay();
1455
+ this._spendingTracker.totalBorrowed += amount;
1456
+ }
1457
+ /**
1458
+ * Check if a borrow amount is within spending limits.
1459
+ * Considers both fixed daily limits and yield-limited spending.
1460
+ */
1461
+ async _checkSpendingLimit(amount, morphoClient) {
1462
+ this._resetTrackerIfNewDay();
1463
+ if (this.config.yieldLimitedSpending) {
1464
+ try {
1465
+ const status = await morphoClient.getStatus();
1466
+ let totalDailyYieldUsdc = 0;
1467
+ for (const pos of status.positions) {
1468
+ if (parseFloat(pos.collateral) > 0) {
1469
+ try {
1470
+ const estimate = await morphoClient.getYieldEstimate(
1471
+ pos.collateralToken,
1472
+ pos.collateral,
1473
+ 1
1474
+ // 1 day
1475
+ );
1476
+ totalDailyYieldUsdc += estimate.estimatedYieldUsd;
1477
+ } catch {
1478
+ }
1479
+ }
1480
+ }
1481
+ const yieldLimit = BigInt(Math.round(totalDailyYieldUsdc * 1e6));
1482
+ const newTotal = this._spendingTracker.totalBorrowed + amount;
1483
+ if (yieldLimit > 0n && newTotal > yieldLimit) {
1484
+ return {
1485
+ allowed: false,
1486
+ reason: `Yield-limited spending exceeded. Daily yield cap: $${(Number(yieldLimit) / 1e6).toFixed(2)}, already spent: $${(Number(this._spendingTracker.totalBorrowed) / 1e6).toFixed(2)}, requested: $${(Number(amount) / 1e6).toFixed(2)}`
1487
+ };
1488
+ }
1489
+ } catch {
1490
+ }
1491
+ }
1492
+ if (this._spendingTracker.dailyLimit > 0n) {
1493
+ const newTotal = this._spendingTracker.totalBorrowed + amount;
1494
+ if (newTotal > this._spendingTracker.dailyLimit) {
1495
+ return {
1496
+ allowed: false,
1497
+ reason: `Daily spending limit exceeded. Limit: $${(Number(this._spendingTracker.dailyLimit) / 1e6).toFixed(2)}, already spent: $${(Number(this._spendingTracker.totalBorrowed) / 1e6).toFixed(2)}, requested: $${(Number(amount) / 1e6).toFixed(2)}`
1498
+ };
1499
+ }
1500
+ }
1501
+ return { allowed: true };
1502
+ }
1155
1503
  };
1156
1504
 
1157
1505
  // src/clients/ScoringClient.ts
@@ -0,0 +1,121 @@
1
+ /**
2
+ * Agether SDK Types
3
+ *
4
+ * Architecture:
5
+ * - Agent registers via ERC-8004 → gets agentId
6
+ * - AccountFactory creates AgentAccount (KYA-gated smart wallet) per agent
7
+ * - AgentReputation: oracle-based credit scoring
8
+ * - Morpho Blue: direct overcollateralized lending (agents interact directly)
9
+ * - x402: HTTP payment protocol for scoring API
10
+ */
11
+ export declare enum ChainId {
12
+ Ethereum = 1,
13
+ Base = 8453,
14
+ BaseSepolia = 84532,
15
+ Sepolia = 11155111,
16
+ Hardhat = 31337
17
+ }
18
+ /** Morpho Blue MarketParams struct */
19
+ export interface MorphoMarketParams {
20
+ loanToken: string;
21
+ collateralToken: string;
22
+ oracle: string;
23
+ irm: string;
24
+ lltv: bigint;
25
+ }
26
+ /** Morpho Blue on-chain position for an account */
27
+ export interface MorphoPosition {
28
+ supplyShares: bigint;
29
+ borrowShares: bigint;
30
+ collateral: bigint;
31
+ }
32
+ /** Morpho market info (from GraphQL API or on-chain) */
33
+ export interface MorphoMarketInfo {
34
+ uniqueKey: string;
35
+ loanAsset: {
36
+ address: string;
37
+ symbol: string;
38
+ decimals: number;
39
+ };
40
+ collateralAsset: {
41
+ address: string;
42
+ symbol: string;
43
+ decimals: number;
44
+ };
45
+ oracle: string;
46
+ irm: string;
47
+ lltv: bigint;
48
+ totalSupplyAssets: bigint;
49
+ totalBorrowAssets: bigint;
50
+ utilization: number;
51
+ }
52
+ /** On-chain score attestation from AgentReputation contract */
53
+ export interface ScoreAttestation {
54
+ score: bigint;
55
+ timestamp: bigint;
56
+ signer: string;
57
+ }
58
+ /** Score result from backend scoring API */
59
+ export interface ScoreResult {
60
+ agentId: string;
61
+ score: number;
62
+ timestamp: number;
63
+ breakdown: {
64
+ kyaBonus: number;
65
+ accountBonus: number;
66
+ balanceBonus: number;
67
+ historyBonus: number;
68
+ baseScore: number;
69
+ };
70
+ txHash?: string;
71
+ }
72
+ export interface TransactionResult {
73
+ txHash: string;
74
+ blockNumber: number;
75
+ status: 'success' | 'failed';
76
+ gasUsed: bigint;
77
+ }
78
+ export interface X402PaymentRequest {
79
+ service: string;
80
+ amount: bigint;
81
+ asset: string;
82
+ chain: ChainId;
83
+ recipient: string;
84
+ }
85
+ export interface X402PaymentResult {
86
+ paymentId: string;
87
+ txHash: string;
88
+ amount: bigint;
89
+ chain: ChainId;
90
+ status: 'pending' | 'confirmed' | 'failed';
91
+ }
92
+ export interface AgetherConfig {
93
+ chainId: ChainId;
94
+ rpcUrl: string;
95
+ contracts: ContractAddresses;
96
+ scoringEndpoint?: string;
97
+ kyaEndpoint?: string;
98
+ }
99
+ export interface ContractAddresses {
100
+ accountFactory: string;
101
+ validationRegistry: string;
102
+ agentReputation: string;
103
+ usdc: string;
104
+ identityRegistry: string;
105
+ morphoBlue: string;
106
+ }
107
+ export declare class AgetherError extends Error {
108
+ code: string;
109
+ details?: Record<string, unknown> | undefined;
110
+ constructor(message: string, code: string, details?: Record<string, unknown> | undefined);
111
+ }
112
+ export declare class InsufficientBalanceError extends AgetherError {
113
+ constructor(available: bigint, required: bigint);
114
+ }
115
+ export declare class ScoringRejectedError extends AgetherError {
116
+ constructor(riskScore: number, reason?: string);
117
+ }
118
+ export declare class AgentNotApprovedError extends AgetherError {
119
+ constructor(agentId: bigint);
120
+ }
121
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @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AAU,EAAE,MAAM,CAAC;CACpB;AAID,qBAAa,YAAa,SAAQ,KAAK;IAG5B,IAAI,EAAE,MAAM;IACZ,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;gBAFxC,OAAO,EAAE,MAAM,EACR,IAAI,EAAE,MAAM,EACZ,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,YAAA;CAK3C;AAED,qBAAa,wBAAyB,SAAQ,YAAY;gBAC5C,SAAS,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM;CAOhD;AAED,qBAAa,oBAAqB,SAAQ,YAAY;gBACxC,SAAS,EAAE,MAAM,EAAE,MAAM,CAAC,EAAE,MAAM;CAO/C;AAED,qBAAa,qBAAsB,SAAQ,YAAY;gBACzC,OAAO,EAAE,MAAM;CAO5B"}
@@ -0,0 +1,43 @@
1
+ /**
2
+ * Agether SDK Types
3
+ *
4
+ * Architecture:
5
+ * - Agent registers via ERC-8004 → gets agentId
6
+ * - AccountFactory creates AgentAccount (KYA-gated smart wallet) per agent
7
+ * - AgentReputation: oracle-based credit scoring
8
+ * - Morpho Blue: direct overcollateralized lending (agents interact directly)
9
+ * - x402: HTTP payment protocol for scoring API
10
+ */
11
+ // ============ Enums ============
12
+ export var ChainId;
13
+ (function (ChainId) {
14
+ ChainId[ChainId["Ethereum"] = 1] = "Ethereum";
15
+ ChainId[ChainId["Base"] = 8453] = "Base";
16
+ ChainId[ChainId["BaseSepolia"] = 84532] = "BaseSepolia";
17
+ ChainId[ChainId["Sepolia"] = 11155111] = "Sepolia";
18
+ ChainId[ChainId["Hardhat"] = 31337] = "Hardhat";
19
+ })(ChainId || (ChainId = {}));
20
+ // ============ Error Types ============
21
+ export class AgetherError extends Error {
22
+ constructor(message, code, details) {
23
+ super(message);
24
+ this.code = code;
25
+ this.details = details;
26
+ this.name = 'AgetherError';
27
+ }
28
+ }
29
+ export class InsufficientBalanceError extends AgetherError {
30
+ constructor(available, required) {
31
+ super(`Insufficient balance: available ${available}, required ${required}`, 'INSUFFICIENT_BALANCE', { available: available.toString(), required: required.toString() });
32
+ }
33
+ }
34
+ export class ScoringRejectedError extends AgetherError {
35
+ constructor(riskScore, reason) {
36
+ super(`Scoring rejected: risk score ${riskScore}${reason ? `, ${reason}` : ''}`, 'SCORING_REJECTED', { riskScore, reason });
37
+ }
38
+ }
39
+ export class AgentNotApprovedError extends AgetherError {
40
+ constructor(agentId) {
41
+ super(`Agent ${agentId} is not KYA-approved. Submit code for validation first.`, 'AGENT_NOT_APPROVED', { agentId: agentId.toString() });
42
+ }
43
+ }
@@ -0,0 +1,18 @@
1
+ /**
2
+ * Contract ABIs (minimal for SDK)
3
+ *
4
+ * Architecture:
5
+ * - AccountFactory + AgentAccount (KYA-gated smart wallets)
6
+ * - AgentReputation (oracle-based credit scores)
7
+ * - ValidationRegistry (KYA code validation)
8
+ * - ERC-8004 IdentityRegistry
9
+ * - Morpho Blue (direct overcollateralized lending)
10
+ */
11
+ export declare const IDENTITY_REGISTRY_ABI: string[];
12
+ export declare const ACCOUNT_FACTORY_ABI: string[];
13
+ export declare const AGENT_ACCOUNT_ABI: string[];
14
+ export declare const AGENT_REPUTATION_ABI: string[];
15
+ export declare const VALIDATION_REGISTRY_ABI: string[];
16
+ export declare const MORPHO_BLUE_ABI: string[];
17
+ export declare const ERC20_ABI: string[];
18
+ //# sourceMappingURL=abis.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"abis.d.ts","sourceRoot":"","sources":["../../src/utils/abis.ts"],"names":[],"mappings":"AAAA;;;;;;;;;GASG;AAIH,eAAO,MAAM,qBAAqB,UAOjC,CAAC;AAIF,eAAO,MAAM,mBAAmB,UAQ/B,CAAC;AAIF,eAAO,MAAM,iBAAiB,UAc7B,CAAC;AAIF,eAAO,MAAM,oBAAoB,UAShC,CAAC;AAIF,eAAO,MAAM,uBAAuB,UAKnC,CAAC;AAIF,eAAO,MAAM,eAAe,UAiB3B,CAAC;AAIF,eAAO,MAAM,SAAS,UASrB,CAAC"}
@@ -0,0 +1,93 @@
1
+ /**
2
+ * Contract ABIs (minimal for SDK)
3
+ *
4
+ * Architecture:
5
+ * - AccountFactory + AgentAccount (KYA-gated smart wallets)
6
+ * - AgentReputation (oracle-based credit scores)
7
+ * - ValidationRegistry (KYA code validation)
8
+ * - ERC-8004 IdentityRegistry
9
+ * - Morpho Blue (direct overcollateralized lending)
10
+ */
11
+ // ── ERC-8004 Identity Registry ──
12
+ export const IDENTITY_REGISTRY_ABI = [
13
+ 'function ownerOf(uint256 agentId) view returns (address)',
14
+ 'function balanceOf(address owner) view returns (uint256)',
15
+ 'function totalSupply() view returns (uint256)',
16
+ 'function exists(uint256 agentId) view returns (bool)',
17
+ 'function register() returns (uint256 agentId)',
18
+ 'event Transfer(address indexed from, address indexed to, uint256 indexed tokenId)',
19
+ ];
20
+ // ── Account Factory ──
21
+ export const ACCOUNT_FACTORY_ABI = [
22
+ 'function getAccount(uint256 agentId) view returns (address)',
23
+ 'function accountExists(uint256 agentId) view returns (bool)',
24
+ 'function predictAddress(uint256 agentId) view returns (address)',
25
+ 'function totalAccounts() view returns (uint256)',
26
+ 'function getAgentId(address account) view returns (uint256)',
27
+ 'function createAccount(uint256 agentId) returns (address account)',
28
+ 'event AccountCreated(uint256 indexed agentId, address indexed account, address indexed owner)',
29
+ ];
30
+ // ── Agent Account (KYA-gated smart wallet) ──
31
+ export const AGENT_ACCOUNT_ABI = [
32
+ 'function agentId() view returns (uint256)',
33
+ 'function owner() view returns (address)',
34
+ 'function factory() view returns (address)',
35
+ 'function validationRegistry() view returns (address)',
36
+ 'function identityRegistry() view returns (address)',
37
+ 'function balanceOf(address token) view returns (uint256)',
38
+ 'function ethBalance() view returns (uint256)',
39
+ 'function execute(address target, uint256 value, bytes data) payable returns (bytes)',
40
+ 'function executeBatch(address[] targets, uint256[] values, bytes[] datas) payable returns (bytes[])',
41
+ 'function fund(address token, uint256 amount)',
42
+ 'function withdraw(address token, uint256 amount, address to)',
43
+ 'function withdrawETH(uint256 amount, address to)',
44
+ 'function isValidSignature(bytes32 hash, bytes signature) view returns (bytes4)',
45
+ ];
46
+ // ── Agent Reputation (oracle-based scoring) ──
47
+ export const AGENT_REPUTATION_ABI = [
48
+ 'function getCreditScore(uint256 agentId) view returns (uint256)',
49
+ 'function getAttestation(uint256 agentId) view returns (tuple(uint256 score, uint256 timestamp, address signer))',
50
+ 'function isScoreFresh(uint256 agentId) view returns (bool fresh, uint256 age)',
51
+ 'function isEligible(uint256 agentId, uint256 minScore) view returns (bool eligible, uint256 currentScore)',
52
+ 'function oracleSigner() view returns (address)',
53
+ 'function submitScore(uint256 agentId, uint256 score_, uint256 timestamp_, bytes signature)',
54
+ 'function setOracleSigner(address signer_)',
55
+ 'event ScoreUpdated(uint256 indexed agentId, uint256 score, uint256 timestamp, address signer)',
56
+ ];
57
+ // ── Validation Registry (KYA) ──
58
+ export const VALIDATION_REGISTRY_ABI = [
59
+ 'function isAgentCodeApproved(uint256 agentId) view returns (bool)',
60
+ 'function isAgentCodeApprovedForTag(uint256 agentId, string tag) view returns (bool)',
61
+ 'function getAgentValidations(uint256 agentId) view returns (bytes32[])',
62
+ 'function getValidation(bytes32 requestHash) view returns (tuple(address validatorAddress, uint256 agentId, string requestURI, uint8 response, string responseURI, bytes32 responseHash, string tag, uint256 requestedAt, uint256 respondedAt, bool hasResponse))',
63
+ ];
64
+ // ── Morpho Blue (direct lending protocol) ──
65
+ export const MORPHO_BLUE_ABI = [
66
+ // Supply & Withdraw (lending side)
67
+ 'function supply(tuple(address loanToken, address collateralToken, address oracle, address irm, uint256 lltv) marketParams, uint256 assets, uint256 shares, address onBehalf, bytes data) returns (uint256 assetsSupplied, uint256 sharesSupplied)',
68
+ 'function withdraw(tuple(address loanToken, address collateralToken, address oracle, address irm, uint256 lltv) marketParams, uint256 assets, uint256 shares, address onBehalf, address receiver) returns (uint256 assetsWithdrawn, uint256 sharesWithdrawn)',
69
+ // Collateral
70
+ 'function supplyCollateral(tuple(address loanToken, address collateralToken, address oracle, address irm, uint256 lltv) marketParams, uint256 assets, address onBehalf, bytes data)',
71
+ 'function withdrawCollateral(tuple(address loanToken, address collateralToken, address oracle, address irm, uint256 lltv) marketParams, uint256 assets, address onBehalf, address receiver)',
72
+ // Borrow & Repay
73
+ 'function borrow(tuple(address loanToken, address collateralToken, address oracle, address irm, uint256 lltv) marketParams, uint256 assets, uint256 shares, address onBehalf, address receiver) returns (uint256 assetsBorrowed, uint256 sharesBorrowed)',
74
+ 'function repay(tuple(address loanToken, address collateralToken, address oracle, address irm, uint256 lltv) marketParams, uint256 assets, uint256 shares, address onBehalf, bytes data) returns (uint256 assetsRepaid, uint256 sharesRepaid)',
75
+ // Authorization
76
+ 'function setAuthorization(address authorized, bool newIsAuthorized)',
77
+ 'function isAuthorized(address authorizer, address authorized) view returns (bool)',
78
+ // Views
79
+ 'function position(bytes32 id, address user) view returns (uint256 supplyShares, uint128 borrowShares, uint128 collateral)',
80
+ 'function market(bytes32 id) view returns (uint128 totalSupplyAssets, uint128 totalSupplyShares, uint128 totalBorrowAssets, uint128 totalBorrowShares, uint128 lastUpdate, uint128 fee)',
81
+ 'function idToMarketParams(bytes32 id) view returns (tuple(address loanToken, address collateralToken, address oracle, address irm, uint256 lltv))',
82
+ ];
83
+ // ── ERC-20 ──
84
+ export const ERC20_ABI = [
85
+ 'function balanceOf(address account) view returns (uint256)',
86
+ 'function allowance(address owner, address spender) view returns (uint256)',
87
+ 'function approve(address spender, uint256 amount) returns (bool)',
88
+ 'function transfer(address to, uint256 amount) returns (bool)',
89
+ 'function transferFrom(address from, address to, uint256 amount) returns (bool)',
90
+ 'function decimals() view returns (uint8)',
91
+ 'function symbol() view returns (string)',
92
+ 'function name() view returns (string)',
93
+ ];