@agether/sdk 1.2.0 → 1.2.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/dist/cli.js +295 -5
- package/dist/index.d.mts +194 -1
- package/dist/index.d.ts +194 -1
- package/dist/index.js +429 -17
- package/dist/index.mjs +427 -16
- package/package.json +1 -1
- package/dist/cli.d.mts +0 -2
- package/dist/cli.d.ts +0 -19
- package/dist/cli.d.ts.map +0 -1
- package/dist/cli.mjs +0 -0
- package/dist/clients/AgentIdentityClient.d.ts +0 -163
- package/dist/clients/AgentIdentityClient.d.ts.map +0 -1
- package/dist/clients/AgentIdentityClient.js +0 -293
- package/dist/clients/AgetherClient.d.ts +0 -101
- package/dist/clients/AgetherClient.d.ts.map +0 -1
- package/dist/clients/AgetherClient.js +0 -272
- package/dist/clients/ScoringClient.d.ts +0 -138
- package/dist/clients/ScoringClient.d.ts.map +0 -1
- package/dist/clients/ScoringClient.js +0 -135
- package/dist/clients/VaultClient.d.ts +0 -62
- package/dist/clients/VaultClient.d.ts.map +0 -1
- package/dist/clients/VaultClient.js +0 -157
- package/dist/clients/WalletClient.d.ts +0 -73
- package/dist/clients/WalletClient.d.ts.map +0 -1
- package/dist/clients/WalletClient.js +0 -174
- package/dist/clients/X402Client.d.ts +0 -61
- package/dist/clients/X402Client.d.ts.map +0 -1
- package/dist/clients/X402Client.js +0 -303
- package/dist/index.d.ts.map +0 -1
- package/dist/types/index.d.ts +0 -220
- package/dist/types/index.d.ts.map +0 -1
- package/dist/types/index.js +0 -52
- package/dist/utils/abis.d.ts +0 -21
- package/dist/utils/abis.d.ts.map +0 -1
- package/dist/utils/abis.js +0 -134
- package/dist/utils/config.d.ts +0 -31
- package/dist/utils/config.d.ts.map +0 -1
- package/dist/utils/config.js +0 -117
- package/dist/utils/format.d.ts +0 -44
- package/dist/utils/format.d.ts.map +0 -1
- package/dist/utils/format.js +0 -75
package/dist/index.mjs
CHANGED
|
@@ -1127,7 +1127,7 @@ function chainIdFromNetwork(network) {
|
|
|
1127
1127
|
const m = network.match(/^eip155:(\d+)$/);
|
|
1128
1128
|
return m ? Number(m[1]) : 1;
|
|
1129
1129
|
}
|
|
1130
|
-
var
|
|
1130
|
+
var _X402Client = class _X402Client {
|
|
1131
1131
|
constructor(config) {
|
|
1132
1132
|
this.config = config;
|
|
1133
1133
|
const provider = new ethers3.JsonRpcProvider(config.rpcUrl);
|
|
@@ -1176,6 +1176,9 @@ var X402Client = class {
|
|
|
1176
1176
|
console.log(` amount : ${requirements.amount} (atomic)`);
|
|
1177
1177
|
console.log(` asset : ${requirements.asset}`);
|
|
1178
1178
|
console.log(` payTo : ${requirements.payTo}`);
|
|
1179
|
+
if (this.config.autoDraw && this.config.accountAddress && this.config.morphoCreditAddress) {
|
|
1180
|
+
await this.ensureBalance(BigInt(requirements.amount));
|
|
1181
|
+
}
|
|
1179
1182
|
console.log(" [3/4] Signing EIP-3009 transferWithAuthorization\u2026");
|
|
1180
1183
|
const paymentPayload = await this.buildPaymentPayload(requirements, resource, url);
|
|
1181
1184
|
const paymentB64 = Buffer.from(JSON.stringify(paymentPayload)).toString("base64");
|
|
@@ -1336,6 +1339,43 @@ var X402Client = class {
|
|
|
1336
1339
|
}
|
|
1337
1340
|
};
|
|
1338
1341
|
}
|
|
1342
|
+
async ensureBalance(requiredAmount) {
|
|
1343
|
+
const accountAddr = this.config.accountAddress;
|
|
1344
|
+
const morphoAddr = this.config.morphoCreditAddress;
|
|
1345
|
+
const provider = this.wallet.provider;
|
|
1346
|
+
const usdcAddr = USDC_DOMAINS["eip155:8453"]?.address || USDC_DOMAINS["eip155:1"].address;
|
|
1347
|
+
const usdc = new ethers3.Contract(usdcAddr, _X402Client.ERC20_BALANCE_ABI, provider);
|
|
1348
|
+
const balance = await usdc.balanceOf(accountAddr);
|
|
1349
|
+
const needed = requiredAmount + requiredAmount / 10n;
|
|
1350
|
+
if (balance >= needed) return;
|
|
1351
|
+
const deficit = needed - balance;
|
|
1352
|
+
const minDraw = ethers3.parseUnits("10", 6);
|
|
1353
|
+
const drawAmount = deficit > minDraw ? deficit : minDraw;
|
|
1354
|
+
console.log(` [auto-draw] USDC balance $${ethers3.formatUnits(balance, 6)} < needed $${ethers3.formatUnits(needed, 6)}`);
|
|
1355
|
+
const morpho = new ethers3.Contract(morphoAddr, _X402Client.MORPHO_DRAW_ABI, provider);
|
|
1356
|
+
let collateralAddr = null;
|
|
1357
|
+
for (const addr of _X402Client.AUTO_DRAW_COLLATERALS) {
|
|
1358
|
+
try {
|
|
1359
|
+
const pos = await morpho.getPosition(accountAddr, addr);
|
|
1360
|
+
if (pos.collateralAmount > 0n) {
|
|
1361
|
+
collateralAddr = addr;
|
|
1362
|
+
break;
|
|
1363
|
+
}
|
|
1364
|
+
} catch {
|
|
1365
|
+
continue;
|
|
1366
|
+
}
|
|
1367
|
+
}
|
|
1368
|
+
if (!collateralAddr) {
|
|
1369
|
+
throw new Error("autoDraw failed: no collateral deposited in Morpho. Deposit collateral first.");
|
|
1370
|
+
}
|
|
1371
|
+
const account = new ethers3.Contract(accountAddr, _X402Client.AGENT_ACCOUNT_EXEC_ABI, this.wallet);
|
|
1372
|
+
const morphoIface = new ethers3.Interface(_X402Client.MORPHO_DRAW_ABI);
|
|
1373
|
+
const calldata = morphoIface.encodeFunctionData("drawWithCollateral", [collateralAddr, drawAmount]);
|
|
1374
|
+
console.log(` [auto-draw] Borrowing $${ethers3.formatUnits(drawAmount, 6)} from Morpho...`);
|
|
1375
|
+
const tx = await account.execute(morphoAddr, 0, calldata);
|
|
1376
|
+
await tx.wait();
|
|
1377
|
+
console.log(` [auto-draw] \u2713 Borrowed (tx: ${tx.hash.slice(0, 14)}\u2026)`);
|
|
1378
|
+
}
|
|
1339
1379
|
// ──────────── Risk check via our backend ────────────
|
|
1340
1380
|
async riskCheck(paymentPayload, reqs) {
|
|
1341
1381
|
try {
|
|
@@ -1367,17 +1407,387 @@ var X402Client = class {
|
|
|
1367
1407
|
}
|
|
1368
1408
|
}
|
|
1369
1409
|
};
|
|
1410
|
+
// ──────────── Auto-draw (Flow 9) ────────────
|
|
1411
|
+
//
|
|
1412
|
+
// When autoDraw is enabled and the AgentAccount has insufficient USDC,
|
|
1413
|
+
// automatically borrow from Morpho credit line to cover the payment.
|
|
1414
|
+
_X402Client.MORPHO_DRAW_ABI = [
|
|
1415
|
+
"function drawWithCollateral(address collateralToken, uint256 amount)",
|
|
1416
|
+
"function getPosition(address account, address collateralToken) view returns (tuple(uint256 collateralAmount, uint256 borrowedAmount, uint256 borrowShares, bool isActive))"
|
|
1417
|
+
];
|
|
1418
|
+
_X402Client.AGENT_ACCOUNT_EXEC_ABI = [
|
|
1419
|
+
"function execute(address target, uint256 value, bytes data) payable returns (bytes)"
|
|
1420
|
+
];
|
|
1421
|
+
_X402Client.ERC20_BALANCE_ABI = [
|
|
1422
|
+
"function balanceOf(address) view returns (uint256)"
|
|
1423
|
+
];
|
|
1424
|
+
_X402Client.AUTO_DRAW_COLLATERALS = [
|
|
1425
|
+
"0x4200000000000000000000000000000000000006",
|
|
1426
|
+
// WETH
|
|
1427
|
+
"0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452",
|
|
1428
|
+
// wstETH
|
|
1429
|
+
"0x2Ae3F1Ec7F1F5012CFEab0185bfc7aa3cf0DEc22"
|
|
1430
|
+
// cbETH
|
|
1431
|
+
];
|
|
1432
|
+
var X402Client = _X402Client;
|
|
1433
|
+
|
|
1434
|
+
// src/clients/MorphoCreditClient.ts
|
|
1435
|
+
import { ethers as ethers4, Contract as Contract3 } from "ethers";
|
|
1436
|
+
var BASE_COLLATERALS = {
|
|
1437
|
+
WETH: { symbol: "WETH", address: "0x4200000000000000000000000000000000000006", decimals: 18 },
|
|
1438
|
+
wstETH: { symbol: "wstETH", address: "0xc1CBa3fCea344f92D9239c08C0568f6F2F0ee452", decimals: 18 },
|
|
1439
|
+
cbETH: { symbol: "cbETH", address: "0x2Ae3F1Ec7F1F5012CFEab0185bfc7aa3cf0DEc22", decimals: 18 }
|
|
1440
|
+
};
|
|
1441
|
+
var MorphoCreditClient = class {
|
|
1442
|
+
constructor(config, collaterals) {
|
|
1443
|
+
this.config = config;
|
|
1444
|
+
const provider = new ethers4.JsonRpcProvider(config.rpcUrl);
|
|
1445
|
+
this.signer = new ethers4.Wallet(config.privateKey, provider);
|
|
1446
|
+
this.morpho = new Contract3(config.contracts.morphoCredit, MORPHO_CREDIT_ABI, this.signer);
|
|
1447
|
+
this.factory = new Contract3(config.contracts.accountFactory, ACCOUNT_FACTORY_ABI, this.signer);
|
|
1448
|
+
this.collaterals = collaterals || BASE_COLLATERALS;
|
|
1449
|
+
}
|
|
1450
|
+
// ── Helpers ──
|
|
1451
|
+
resolveToken(tokenSymbol) {
|
|
1452
|
+
const token = this.collaterals[tokenSymbol];
|
|
1453
|
+
if (!token) {
|
|
1454
|
+
throw new Error(`Unsupported collateral token: ${tokenSymbol}. Supported: ${Object.keys(this.collaterals).join(", ")}`);
|
|
1455
|
+
}
|
|
1456
|
+
return token;
|
|
1457
|
+
}
|
|
1458
|
+
/**
|
|
1459
|
+
* Get the AgentAccount address for the configured agentId.
|
|
1460
|
+
*/
|
|
1461
|
+
async getAccountAddress() {
|
|
1462
|
+
if (this.accountAddress) return this.accountAddress;
|
|
1463
|
+
const addr = await this.factory.getAccount(this.config.agentId.toString());
|
|
1464
|
+
if (addr === ethers4.ZeroAddress) {
|
|
1465
|
+
throw new Error("No AgentAccount found. Register first.");
|
|
1466
|
+
}
|
|
1467
|
+
this.accountAddress = addr;
|
|
1468
|
+
return addr;
|
|
1469
|
+
}
|
|
1470
|
+
/**
|
|
1471
|
+
* Get the AgentAccount address for any agentId.
|
|
1472
|
+
*/
|
|
1473
|
+
async getAccountForAgent(agentId) {
|
|
1474
|
+
const addr = await this.factory.getAccount(agentId.toString());
|
|
1475
|
+
if (addr === ethers4.ZeroAddress) {
|
|
1476
|
+
throw new Error(`No AgentAccount for agent #${agentId}`);
|
|
1477
|
+
}
|
|
1478
|
+
return addr;
|
|
1479
|
+
}
|
|
1480
|
+
async ensureCreditProvider(accountAddr) {
|
|
1481
|
+
const account = new Contract3(accountAddr, AGENT_ACCOUNT_ABI, this.signer);
|
|
1482
|
+
try {
|
|
1483
|
+
const tx = await account.approveCreditProvider(this.config.contracts.morphoCredit);
|
|
1484
|
+
await tx.wait();
|
|
1485
|
+
} catch {
|
|
1486
|
+
}
|
|
1487
|
+
}
|
|
1488
|
+
async approveAndDeposit(accountAddr, tokenInfo, amount) {
|
|
1489
|
+
const token = new Contract3(tokenInfo.address, ERC20_ABI, this.signer);
|
|
1490
|
+
const balance = await token.balanceOf(this.signer.address);
|
|
1491
|
+
if (balance < amount) {
|
|
1492
|
+
throw new Error(
|
|
1493
|
+
`Insufficient ${tokenInfo.symbol}: have ${ethers4.formatUnits(balance, tokenInfo.decimals)}, need ${ethers4.formatUnits(amount, tokenInfo.decimals)}`
|
|
1494
|
+
);
|
|
1495
|
+
}
|
|
1496
|
+
const approveTx = await token.approve(this.config.contracts.morphoCredit, amount);
|
|
1497
|
+
await approveTx.wait();
|
|
1498
|
+
const depositTx = await this.morpho.depositCollateralFor(accountAddr, tokenInfo.address, amount);
|
|
1499
|
+
await depositTx.wait();
|
|
1500
|
+
return depositTx.hash;
|
|
1501
|
+
}
|
|
1502
|
+
async executeBorrow(accountAddr, collateralAddr, borrowAmount) {
|
|
1503
|
+
const account = new Contract3(accountAddr, AGENT_ACCOUNT_ABI, this.signer);
|
|
1504
|
+
const iface = new ethers4.Interface(MORPHO_CREDIT_ABI);
|
|
1505
|
+
const calldata = iface.encodeFunctionData("drawWithCollateral", [collateralAddr, borrowAmount]);
|
|
1506
|
+
const tx = await account.execute(this.config.contracts.morphoCredit, 0, calldata);
|
|
1507
|
+
await tx.wait();
|
|
1508
|
+
return tx.hash;
|
|
1509
|
+
}
|
|
1510
|
+
// ══════════════════════════════════════════
|
|
1511
|
+
// Flow 3: Deposit collateral only
|
|
1512
|
+
// ══════════════════════════════════════════
|
|
1513
|
+
/**
|
|
1514
|
+
* Deposit collateral from EOA into Morpho for own AgentAccount.
|
|
1515
|
+
* Does NOT borrow — use `borrow()` or `depositAndBorrow()` for that.
|
|
1516
|
+
*
|
|
1517
|
+
* @param tokenSymbol - Collateral token (WETH, wstETH, cbETH)
|
|
1518
|
+
* @param amount - Human-readable amount (e.g. "0.05")
|
|
1519
|
+
*/
|
|
1520
|
+
async deposit(tokenSymbol, amount) {
|
|
1521
|
+
const tokenInfo = this.resolveToken(tokenSymbol);
|
|
1522
|
+
const amountWei = ethers4.parseUnits(amount, tokenInfo.decimals);
|
|
1523
|
+
const accountAddr = await this.getAccountAddress();
|
|
1524
|
+
const txHash = await this.approveAndDeposit(accountAddr, tokenInfo, amountWei);
|
|
1525
|
+
await this.ensureCreditProvider(accountAddr);
|
|
1526
|
+
const pos = await this.morpho.getPosition(accountAddr, tokenInfo.address);
|
|
1527
|
+
return {
|
|
1528
|
+
tx: txHash,
|
|
1529
|
+
amount: amountWei,
|
|
1530
|
+
token: tokenSymbol,
|
|
1531
|
+
agentAccount: accountAddr,
|
|
1532
|
+
totalCollateral: pos.collateralAmount
|
|
1533
|
+
};
|
|
1534
|
+
}
|
|
1535
|
+
// ══════════════════════════════════════════
|
|
1536
|
+
// Flow 2: Deposit + borrow in one call
|
|
1537
|
+
// ══════════════════════════════════════════
|
|
1538
|
+
/**
|
|
1539
|
+
* Deposit collateral AND borrow USDC in a single SDK call.
|
|
1540
|
+
* USDC lands in the AgentAccount.
|
|
1541
|
+
*
|
|
1542
|
+
* @param tokenSymbol - Collateral token (WETH, wstETH, cbETH)
|
|
1543
|
+
* @param collateralAmount - Human-readable collateral amount (e.g. "0.05")
|
|
1544
|
+
* @param borrowAmount - Human-readable USDC amount to borrow (e.g. "50")
|
|
1545
|
+
*/
|
|
1546
|
+
async depositAndBorrow(tokenSymbol, collateralAmount, borrowAmount) {
|
|
1547
|
+
const tokenInfo = this.resolveToken(tokenSymbol);
|
|
1548
|
+
const collateralWei = ethers4.parseUnits(collateralAmount, tokenInfo.decimals);
|
|
1549
|
+
const borrowWei = ethers4.parseUnits(borrowAmount, 6);
|
|
1550
|
+
const accountAddr = await this.getAccountAddress();
|
|
1551
|
+
const depositTxHash = await this.approveAndDeposit(accountAddr, tokenInfo, collateralWei);
|
|
1552
|
+
await this.ensureCreditProvider(accountAddr);
|
|
1553
|
+
const borrowTxHash = await this.executeBorrow(accountAddr, tokenInfo.address, borrowWei);
|
|
1554
|
+
const totalDebt = await this.morpho.getTotalDebt(accountAddr);
|
|
1555
|
+
return {
|
|
1556
|
+
depositTx: depositTxHash,
|
|
1557
|
+
borrowTx: borrowTxHash,
|
|
1558
|
+
collateral: { amount: collateralWei, token: tokenSymbol },
|
|
1559
|
+
borrowed: borrowWei,
|
|
1560
|
+
agentAccount: accountAddr,
|
|
1561
|
+
totalDebt
|
|
1562
|
+
};
|
|
1563
|
+
}
|
|
1564
|
+
// ══════════════════════════════════════════
|
|
1565
|
+
// Flow 8: Borrow against existing collateral
|
|
1566
|
+
// ══════════════════════════════════════════
|
|
1567
|
+
/**
|
|
1568
|
+
* Borrow USDC against already-deposited collateral.
|
|
1569
|
+
* Auto-detects which collateral token has a position.
|
|
1570
|
+
* USDC lands in AgentAccount — ready for x402 payments.
|
|
1571
|
+
*
|
|
1572
|
+
* @param amount - Human-readable USDC amount (e.g. "100")
|
|
1573
|
+
*/
|
|
1574
|
+
async borrow(amount) {
|
|
1575
|
+
const borrowWei = ethers4.parseUnits(amount, 6);
|
|
1576
|
+
const accountAddr = await this.getAccountAddress();
|
|
1577
|
+
let activeToken = null;
|
|
1578
|
+
for (const info of Object.values(this.collaterals)) {
|
|
1579
|
+
const pos = await this.morpho.getPosition(accountAddr, info.address);
|
|
1580
|
+
if (pos.collateralAmount > 0n) {
|
|
1581
|
+
activeToken = info;
|
|
1582
|
+
break;
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
if (!activeToken) {
|
|
1586
|
+
throw new Error("No collateral deposited. Use deposit() or depositAndBorrow() first.");
|
|
1587
|
+
}
|
|
1588
|
+
const txHash = await this.executeBorrow(accountAddr, activeToken.address, borrowWei);
|
|
1589
|
+
const totalDebt = await this.morpho.getTotalDebt(accountAddr);
|
|
1590
|
+
return {
|
|
1591
|
+
tx: txHash,
|
|
1592
|
+
amount: borrowWei,
|
|
1593
|
+
agentAccount: accountAddr,
|
|
1594
|
+
totalDebt,
|
|
1595
|
+
collateralToken: activeToken.symbol
|
|
1596
|
+
};
|
|
1597
|
+
}
|
|
1598
|
+
// ══════════════════════════════════════════
|
|
1599
|
+
// Flows 4-7: Sponsor (deposit for another agent)
|
|
1600
|
+
// ══════════════════════════════════════════
|
|
1601
|
+
/**
|
|
1602
|
+
* Deposit collateral for another agent. Caller pays from their wallet.
|
|
1603
|
+
* Optionally borrow USDC for the agent (only works if caller owns the AgentAccount).
|
|
1604
|
+
*
|
|
1605
|
+
* Supports both agentId lookup and direct address.
|
|
1606
|
+
*
|
|
1607
|
+
* @param target - `{ agentId: "17676" }` or `{ address: "0x..." }`
|
|
1608
|
+
* @param tokenSymbol - Collateral token
|
|
1609
|
+
* @param amount - Human-readable collateral amount
|
|
1610
|
+
* @param borrowAmount - Optional: USDC to borrow (only if caller is owner)
|
|
1611
|
+
*/
|
|
1612
|
+
async sponsor(target, tokenSymbol, amount, borrowAmount) {
|
|
1613
|
+
const tokenInfo = this.resolveToken(tokenSymbol);
|
|
1614
|
+
const collateralWei = ethers4.parseUnits(amount, tokenInfo.decimals);
|
|
1615
|
+
let accountAddr;
|
|
1616
|
+
let targetAgentId;
|
|
1617
|
+
if ("agentId" in target) {
|
|
1618
|
+
targetAgentId = target.agentId.toString();
|
|
1619
|
+
accountAddr = await this.getAccountForAgent(target.agentId);
|
|
1620
|
+
} else {
|
|
1621
|
+
accountAddr = target.address;
|
|
1622
|
+
}
|
|
1623
|
+
const depositTxHash = await this.approveAndDeposit(accountAddr, tokenInfo, collateralWei);
|
|
1624
|
+
const result = {
|
|
1625
|
+
depositTx: depositTxHash,
|
|
1626
|
+
targetAccount: accountAddr,
|
|
1627
|
+
targetAgentId,
|
|
1628
|
+
collateral: { amount: collateralWei, token: tokenSymbol },
|
|
1629
|
+
totalCollateral: 0n,
|
|
1630
|
+
totalDebt: 0n
|
|
1631
|
+
};
|
|
1632
|
+
if (borrowAmount) {
|
|
1633
|
+
const borrowWei = ethers4.parseUnits(borrowAmount, 6);
|
|
1634
|
+
try {
|
|
1635
|
+
await this.ensureCreditProvider(accountAddr);
|
|
1636
|
+
const borrowTxHash = await this.executeBorrow(accountAddr, tokenInfo.address, borrowWei);
|
|
1637
|
+
result.borrowTx = borrowTxHash;
|
|
1638
|
+
result.borrowed = borrowWei;
|
|
1639
|
+
} catch (e) {
|
|
1640
|
+
throw new Error(`Borrow failed (caller may not own this AgentAccount): ${e.message}`);
|
|
1641
|
+
}
|
|
1642
|
+
}
|
|
1643
|
+
const pos = await this.morpho.getPosition(accountAddr, tokenInfo.address);
|
|
1644
|
+
result.totalCollateral = pos.collateralAmount;
|
|
1645
|
+
result.totalDebt = pos.borrowedAmount;
|
|
1646
|
+
return result;
|
|
1647
|
+
}
|
|
1648
|
+
// ══════════════════════════════════════════
|
|
1649
|
+
// Repay & Withdraw
|
|
1650
|
+
// ══════════════════════════════════════════
|
|
1651
|
+
/**
|
|
1652
|
+
* Repay borrowed USDC from AgentAccount back to Morpho.
|
|
1653
|
+
*
|
|
1654
|
+
* @param amount - Human-readable USDC amount (e.g. "50")
|
|
1655
|
+
*/
|
|
1656
|
+
async repay(amount) {
|
|
1657
|
+
const amountWei = ethers4.parseUnits(amount, 6);
|
|
1658
|
+
const accountAddr = await this.getAccountAddress();
|
|
1659
|
+
let collateralAddr = null;
|
|
1660
|
+
for (const info of Object.values(this.collaterals)) {
|
|
1661
|
+
const pos = await this.morpho.getPosition(accountAddr, info.address);
|
|
1662
|
+
if (pos.borrowedAmount > 0n) {
|
|
1663
|
+
collateralAddr = info.address;
|
|
1664
|
+
break;
|
|
1665
|
+
}
|
|
1666
|
+
}
|
|
1667
|
+
if (!collateralAddr) throw new Error("No Morpho debt to repay.");
|
|
1668
|
+
const usdc = new Contract3(this.config.contracts.usdc, ERC20_ABI, this.signer.provider);
|
|
1669
|
+
const balance = await usdc.balanceOf(accountAddr);
|
|
1670
|
+
if (balance < amountWei) {
|
|
1671
|
+
throw new Error(`Insufficient USDC in AgentAccount: $${ethers4.formatUnits(balance, 6)}`);
|
|
1672
|
+
}
|
|
1673
|
+
const account = new Contract3(accountAddr, AGENT_ACCOUNT_ABI, this.signer);
|
|
1674
|
+
const erc20Iface = new ethers4.Interface(ERC20_ABI);
|
|
1675
|
+
const morphoIface = new ethers4.Interface(MORPHO_CREDIT_ABI);
|
|
1676
|
+
const approveData = erc20Iface.encodeFunctionData("approve", [this.config.contracts.morphoCredit, amountWei]);
|
|
1677
|
+
const approveTx = await account.execute(this.config.contracts.usdc, 0, approveData);
|
|
1678
|
+
await approveTx.wait();
|
|
1679
|
+
const repayData = morphoIface.encodeFunctionData("repayWithCollateral", [collateralAddr, amountWei]);
|
|
1680
|
+
const tx = await account.execute(this.config.contracts.morphoCredit, 0, repayData);
|
|
1681
|
+
await tx.wait();
|
|
1682
|
+
const totalDebt = await this.morpho.getTotalDebt(accountAddr);
|
|
1683
|
+
return { tx: tx.hash, amount: amountWei, remainingDebt: totalDebt };
|
|
1684
|
+
}
|
|
1685
|
+
/**
|
|
1686
|
+
* Withdraw collateral from Morpho back to EOA.
|
|
1687
|
+
*
|
|
1688
|
+
* @param tokenSymbol - Collateral token
|
|
1689
|
+
* @param amount - Human-readable amount or "all"
|
|
1690
|
+
*/
|
|
1691
|
+
async withdraw(tokenSymbol, amount) {
|
|
1692
|
+
const tokenInfo = this.resolveToken(tokenSymbol);
|
|
1693
|
+
const accountAddr = await this.getAccountAddress();
|
|
1694
|
+
const pos = await this.morpho.getPosition(accountAddr, tokenInfo.address);
|
|
1695
|
+
if (pos.collateralAmount === 0n) {
|
|
1696
|
+
throw new Error(`No ${tokenSymbol} collateral deposited.`);
|
|
1697
|
+
}
|
|
1698
|
+
const withdrawAmount = amount.toLowerCase() === "all" ? pos.collateralAmount : ethers4.parseUnits(amount, tokenInfo.decimals);
|
|
1699
|
+
if (withdrawAmount > pos.collateralAmount) {
|
|
1700
|
+
throw new Error(
|
|
1701
|
+
`Cannot withdraw more than deposited: max ${ethers4.formatUnits(pos.collateralAmount, tokenInfo.decimals)} ${tokenSymbol}`
|
|
1702
|
+
);
|
|
1703
|
+
}
|
|
1704
|
+
const account = new Contract3(accountAddr, AGENT_ACCOUNT_ABI, this.signer);
|
|
1705
|
+
const morphoIface = new ethers4.Interface(MORPHO_CREDIT_ABI);
|
|
1706
|
+
const withdrawData = morphoIface.encodeFunctionData("withdrawCollateral", [tokenInfo.address, withdrawAmount]);
|
|
1707
|
+
const tx1 = await account.execute(this.config.contracts.morphoCredit, 0, withdrawData);
|
|
1708
|
+
await tx1.wait();
|
|
1709
|
+
const erc20Iface = new ethers4.Interface(ERC20_ABI);
|
|
1710
|
+
const transferData = erc20Iface.encodeFunctionData("transfer", [this.signer.address, withdrawAmount]);
|
|
1711
|
+
const tx2 = await account.execute(tokenInfo.address, 0, transferData);
|
|
1712
|
+
await tx2.wait();
|
|
1713
|
+
const newPos = await this.morpho.getPosition(accountAddr, tokenInfo.address);
|
|
1714
|
+
return {
|
|
1715
|
+
tx: tx1.hash,
|
|
1716
|
+
amount: withdrawAmount,
|
|
1717
|
+
token: tokenSymbol,
|
|
1718
|
+
destination: this.signer.address,
|
|
1719
|
+
remainingCollateral: newPos.collateralAmount
|
|
1720
|
+
};
|
|
1721
|
+
}
|
|
1722
|
+
// ══════════════════════════════════════════
|
|
1723
|
+
// View methods
|
|
1724
|
+
// ══════════════════════════════════════════
|
|
1725
|
+
/**
|
|
1726
|
+
* Get position for a specific collateral token.
|
|
1727
|
+
*/
|
|
1728
|
+
async getPosition(tokenSymbol) {
|
|
1729
|
+
const tokenInfo = this.resolveToken(tokenSymbol);
|
|
1730
|
+
const accountAddr = await this.getAccountAddress();
|
|
1731
|
+
const pos = await this.morpho.getPosition(accountAddr, tokenInfo.address);
|
|
1732
|
+
return {
|
|
1733
|
+
token: tokenSymbol,
|
|
1734
|
+
collateralAmount: pos.collateralAmount,
|
|
1735
|
+
borrowedAmount: pos.borrowedAmount,
|
|
1736
|
+
borrowShares: pos.borrowShares,
|
|
1737
|
+
isActive: pos.isActive
|
|
1738
|
+
};
|
|
1739
|
+
}
|
|
1740
|
+
/**
|
|
1741
|
+
* Get all active positions across all collateral tokens.
|
|
1742
|
+
*/
|
|
1743
|
+
async getAllPositions() {
|
|
1744
|
+
const accountAddr = await this.getAccountAddress();
|
|
1745
|
+
const positions = [];
|
|
1746
|
+
for (const [symbol, info] of Object.entries(this.collaterals)) {
|
|
1747
|
+
const pos = await this.morpho.getPosition(accountAddr, info.address);
|
|
1748
|
+
if (pos.isActive || pos.collateralAmount > 0n || pos.borrowedAmount > 0n) {
|
|
1749
|
+
positions.push({
|
|
1750
|
+
token: symbol,
|
|
1751
|
+
collateralAmount: pos.collateralAmount,
|
|
1752
|
+
borrowedAmount: pos.borrowedAmount,
|
|
1753
|
+
borrowShares: pos.borrowShares,
|
|
1754
|
+
isActive: pos.isActive
|
|
1755
|
+
});
|
|
1756
|
+
}
|
|
1757
|
+
}
|
|
1758
|
+
return positions;
|
|
1759
|
+
}
|
|
1760
|
+
/**
|
|
1761
|
+
* Get total debt across all positions.
|
|
1762
|
+
*/
|
|
1763
|
+
async getTotalDebt() {
|
|
1764
|
+
const accountAddr = await this.getAccountAddress();
|
|
1765
|
+
return this.morpho.getTotalDebt(accountAddr);
|
|
1766
|
+
}
|
|
1767
|
+
/**
|
|
1768
|
+
* Get USDC balance in the AgentAccount.
|
|
1769
|
+
*/
|
|
1770
|
+
async getAccountUSDC() {
|
|
1771
|
+
const accountAddr = await this.getAccountAddress();
|
|
1772
|
+
const usdc = new Contract3(this.config.contracts.usdc, ERC20_ABI, this.signer.provider);
|
|
1773
|
+
return usdc.balanceOf(accountAddr);
|
|
1774
|
+
}
|
|
1775
|
+
/** Get the wallet (signer) address */
|
|
1776
|
+
getAddress() {
|
|
1777
|
+
return this.signer.address;
|
|
1778
|
+
}
|
|
1779
|
+
};
|
|
1370
1780
|
|
|
1371
1781
|
// src/clients/WalletClient.ts
|
|
1372
|
-
import { ethers as
|
|
1782
|
+
import { ethers as ethers5, Contract as Contract4, Wallet } from "ethers";
|
|
1373
1783
|
var WalletClient = class {
|
|
1374
1784
|
constructor(config) {
|
|
1375
1785
|
this.privateKey = null;
|
|
1376
1786
|
this.factoryAddress = config.factoryAddress;
|
|
1377
|
-
this.usdcAddress = config.usdcAddress ||
|
|
1787
|
+
this.usdcAddress = config.usdcAddress || ethers5.ZeroAddress;
|
|
1378
1788
|
this.chainId = config.chainId;
|
|
1379
1789
|
this.rpcUrl = config.rpcUrl;
|
|
1380
|
-
this.provider = new
|
|
1790
|
+
this.provider = new ethers5.JsonRpcProvider(config.rpcUrl);
|
|
1381
1791
|
this.privateKey = config.privateKey || null;
|
|
1382
1792
|
}
|
|
1383
1793
|
/**
|
|
@@ -1387,20 +1797,20 @@ var WalletClient = class {
|
|
|
1387
1797
|
if (!this.privateKey) {
|
|
1388
1798
|
throw new Error("Signer not configured - provide privateKey");
|
|
1389
1799
|
}
|
|
1390
|
-
const freshProvider = new
|
|
1800
|
+
const freshProvider = new ethers5.JsonRpcProvider(this.rpcUrl);
|
|
1391
1801
|
return new Wallet(this.privateKey, freshProvider);
|
|
1392
1802
|
}
|
|
1393
1803
|
getFactoryContract() {
|
|
1394
1804
|
if (this.privateKey) {
|
|
1395
|
-
return new
|
|
1805
|
+
return new Contract4(this.factoryAddress, ACCOUNT_FACTORY_ABI, this.getFreshSigner());
|
|
1396
1806
|
}
|
|
1397
|
-
return new
|
|
1807
|
+
return new Contract4(this.factoryAddress, ACCOUNT_FACTORY_ABI, this.provider);
|
|
1398
1808
|
}
|
|
1399
1809
|
getAccountContract(accountAddress) {
|
|
1400
1810
|
if (this.privateKey) {
|
|
1401
|
-
return new
|
|
1811
|
+
return new Contract4(accountAddress, AGENT_ACCOUNT_ABI, this.getFreshSigner());
|
|
1402
1812
|
}
|
|
1403
|
-
return new
|
|
1813
|
+
return new Contract4(accountAddress, AGENT_ACCOUNT_ABI, this.provider);
|
|
1404
1814
|
}
|
|
1405
1815
|
getChainId() {
|
|
1406
1816
|
return this.chainId;
|
|
@@ -1413,7 +1823,7 @@ var WalletClient = class {
|
|
|
1413
1823
|
async getAccount(agentId) {
|
|
1414
1824
|
const factory = this.getFactoryContract();
|
|
1415
1825
|
const address = await factory.getAccount(agentId);
|
|
1416
|
-
if (address ===
|
|
1826
|
+
if (address === ethers5.ZeroAddress) {
|
|
1417
1827
|
return null;
|
|
1418
1828
|
}
|
|
1419
1829
|
return address;
|
|
@@ -1448,7 +1858,7 @@ var WalletClient = class {
|
|
|
1448
1858
|
account.ethBalance()
|
|
1449
1859
|
]);
|
|
1450
1860
|
let usdcBalance = 0n;
|
|
1451
|
-
if (this.usdcAddress !==
|
|
1861
|
+
if (this.usdcAddress !== ethers5.ZeroAddress) {
|
|
1452
1862
|
usdcBalance = await account.balanceOf(this.usdcAddress);
|
|
1453
1863
|
}
|
|
1454
1864
|
return {
|
|
@@ -1460,7 +1870,7 @@ var WalletClient = class {
|
|
|
1460
1870
|
};
|
|
1461
1871
|
}
|
|
1462
1872
|
async getProviderStatus(accountAddress, creditProvider) {
|
|
1463
|
-
const provider = new
|
|
1873
|
+
const provider = new Contract4(
|
|
1464
1874
|
creditProvider,
|
|
1465
1875
|
CREDIT_PROVIDER_ABI,
|
|
1466
1876
|
this.provider
|
|
@@ -1483,11 +1893,11 @@ var WalletClient = class {
|
|
|
1483
1893
|
throw new Error("Signer not configured");
|
|
1484
1894
|
}
|
|
1485
1895
|
const signer = this.getFreshSigner();
|
|
1486
|
-
const token = new
|
|
1896
|
+
const token = new Contract4(tokenAddress, ERC20_ABI, signer);
|
|
1487
1897
|
const approveTx = await token.approve(accountAddress, amount);
|
|
1488
1898
|
await approveTx.wait();
|
|
1489
1899
|
const signer2 = this.getFreshSigner();
|
|
1490
|
-
const account = new
|
|
1900
|
+
const account = new Contract4(accountAddress, AGENT_ACCOUNT_ABI, signer2);
|
|
1491
1901
|
const tx = await account.fund(tokenAddress, amount);
|
|
1492
1902
|
const receipt = await tx.wait();
|
|
1493
1903
|
return receipt.hash;
|
|
@@ -1533,8 +1943,8 @@ var WalletClient = class {
|
|
|
1533
1943
|
}
|
|
1534
1944
|
// ============ Utility ============
|
|
1535
1945
|
generatePaymentMessageHash(accountAddress, recipient, amount, nonce, deadline, chainId) {
|
|
1536
|
-
return
|
|
1537
|
-
|
|
1946
|
+
return ethers5.keccak256(
|
|
1947
|
+
ethers5.AbiCoder.defaultAbiCoder().encode(
|
|
1538
1948
|
["address", "uint256", "uint256", "uint256", "uint256", "address"],
|
|
1539
1949
|
[recipient, amount, nonce, deadline, chainId, accountAddress]
|
|
1540
1950
|
)
|
|
@@ -1600,6 +2010,7 @@ export {
|
|
|
1600
2010
|
InsufficientCreditError,
|
|
1601
2011
|
LP_VAULT_ABI,
|
|
1602
2012
|
MORPHO_CREDIT_ABI,
|
|
2013
|
+
MorphoCreditClient,
|
|
1603
2014
|
REPUTATION_CREDIT_ABI,
|
|
1604
2015
|
ScoringClient,
|
|
1605
2016
|
ScoringRejectedError,
|
package/package.json
CHANGED
package/dist/cli.d.mts
DELETED
package/dist/cli.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
#!/usr/bin/env node
|
|
2
|
-
/**
|
|
3
|
-
* Agether CLI — Direct On-Chain Credit for AI Agents
|
|
4
|
-
*
|
|
5
|
-
* All commands sign transactions directly with the agent's private key.
|
|
6
|
-
* No simulation routes. No mocks. Production-grade.
|
|
7
|
-
*
|
|
8
|
-
* Usage:
|
|
9
|
-
* agether init <private-key> # Initialize with private key
|
|
10
|
-
* agether register [--name] [--code-url] # Register on real ERC-8004 + KYA
|
|
11
|
-
* agether apply --limit 10000 # Apply for $10,000 credit (on-chain)
|
|
12
|
-
* agether draw --amount 1000 # Draw $1,000 (on-chain)
|
|
13
|
-
* agether repay --amount 500 # Repay $500 (on-chain)
|
|
14
|
-
* agether status # Check credit line status (on-chain)
|
|
15
|
-
* agether score # Get credit score (backend)
|
|
16
|
-
* agether balance # Check ETH + USDC balances (on-chain)
|
|
17
|
-
*/
|
|
18
|
-
export {};
|
|
19
|
-
//# sourceMappingURL=cli.d.ts.map
|
package/dist/cli.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"cli.d.ts","sourceRoot":"","sources":["../src/cli.ts"],"names":[],"mappings":";AACA;;;;;;;;;;;;;;;GAeG"}
|
package/dist/cli.mjs
DELETED
|
File without changes
|