@agether/sdk 1.2.1 → 1.3.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
@@ -881,8 +881,19 @@ declare class MorphoCreditClient {
881
881
  */
882
882
  getAccountForAgent(agentId: string | bigint): Promise<string>;
883
883
  private ensureCreditProvider;
884
+ /**
885
+ * Approve ERC-20 token from EOA → MorphoCredit, then deposit for account.
886
+ * This is 2 EOA txs (approve + depositCollateralFor) — cannot be batched
887
+ * because both are called from EOA, not from AgentAccount.
888
+ */
884
889
  private approveAndDeposit;
885
- private executeBorrow;
890
+ private morphoIface;
891
+ private erc20Iface;
892
+ /**
893
+ * Execute multiple calls via AgentAccount.executeBatch() in a single tx.
894
+ * Each call is { target, value, data }.
895
+ */
896
+ private batch;
886
897
  /**
887
898
  * Deposit collateral from EOA into Morpho for own AgentAccount.
888
899
  * Does NOT borrow — use `borrow()` or `depositAndBorrow()` for that.
package/dist/index.d.ts CHANGED
@@ -881,8 +881,19 @@ declare class MorphoCreditClient {
881
881
  */
882
882
  getAccountForAgent(agentId: string | bigint): Promise<string>;
883
883
  private ensureCreditProvider;
884
+ /**
885
+ * Approve ERC-20 token from EOA → MorphoCredit, then deposit for account.
886
+ * This is 2 EOA txs (approve + depositCollateralFor) — cannot be batched
887
+ * because both are called from EOA, not from AgentAccount.
888
+ */
884
889
  private approveAndDeposit;
885
- private executeBorrow;
890
+ private morphoIface;
891
+ private erc20Iface;
892
+ /**
893
+ * Execute multiple calls via AgentAccount.executeBatch() in a single tx.
894
+ * Each call is { target, value, data }.
895
+ */
896
+ private batch;
886
897
  /**
887
898
  * Deposit collateral from EOA into Morpho for own AgentAccount.
888
899
  * Does NOT borrow — use `borrow()` or `depositAndBorrow()` for that.
package/dist/index.js CHANGED
@@ -150,6 +150,7 @@ var AGENT_ACCOUNT_ABI = [
150
150
  "function balanceOf(address token) view returns (uint256)",
151
151
  "function ethBalance() view returns (uint256)",
152
152
  "function execute(address target, uint256 value, bytes data) payable returns (bytes)",
153
+ "function executeBatch(address[] targets, uint256[] values, bytes[] datas) payable returns (bytes[])",
153
154
  "function drawCredit(address creditProvider, uint256 amount)",
154
155
  "function repayCredit(address creditProvider, uint256 amount)",
155
156
  "function fund(address token, uint256 amount)",
@@ -1511,6 +1512,9 @@ var BASE_COLLATERALS = {
1511
1512
  };
1512
1513
  var MorphoCreditClient = class {
1513
1514
  constructor(config, collaterals) {
1515
+ // ── Batch helper ──
1516
+ this.morphoIface = new import_ethers5.ethers.Interface(MORPHO_CREDIT_ABI);
1517
+ this.erc20Iface = new import_ethers5.ethers.Interface(ERC20_ABI);
1514
1518
  this.config = config;
1515
1519
  const provider = new import_ethers5.ethers.JsonRpcProvider(config.rpcUrl);
1516
1520
  this.signer = new import_ethers5.ethers.Wallet(config.privateKey, provider);
@@ -1556,6 +1560,11 @@ var MorphoCreditClient = class {
1556
1560
  } catch {
1557
1561
  }
1558
1562
  }
1563
+ /**
1564
+ * Approve ERC-20 token from EOA → MorphoCredit, then deposit for account.
1565
+ * This is 2 EOA txs (approve + depositCollateralFor) — cannot be batched
1566
+ * because both are called from EOA, not from AgentAccount.
1567
+ */
1559
1568
  async approveAndDeposit(accountAddr, tokenInfo, amount) {
1560
1569
  const token = new import_ethers5.Contract(tokenInfo.address, ERC20_ABI, this.signer);
1561
1570
  const balance = await token.balanceOf(this.signer.address);
@@ -1570,11 +1579,17 @@ var MorphoCreditClient = class {
1570
1579
  await depositTx.wait();
1571
1580
  return depositTx.hash;
1572
1581
  }
1573
- async executeBorrow(accountAddr, collateralAddr, borrowAmount) {
1582
+ /**
1583
+ * Execute multiple calls via AgentAccount.executeBatch() in a single tx.
1584
+ * Each call is { target, value, data }.
1585
+ */
1586
+ async batch(accountAddr, calls) {
1574
1587
  const account = new import_ethers5.Contract(accountAddr, AGENT_ACCOUNT_ABI, this.signer);
1575
- const iface = new import_ethers5.ethers.Interface(MORPHO_CREDIT_ABI);
1576
- const calldata = iface.encodeFunctionData("drawWithCollateral", [collateralAddr, borrowAmount]);
1577
- const tx = await account.execute(this.config.contracts.morphoCredit, 0, calldata);
1588
+ const tx = await account.executeBatch(
1589
+ calls.map((c) => c.target),
1590
+ calls.map((c) => c.value),
1591
+ calls.map((c) => c.data)
1592
+ );
1578
1593
  await tx.wait();
1579
1594
  return tx.hash;
1580
1595
  }
@@ -1621,11 +1636,14 @@ var MorphoCreditClient = class {
1621
1636
  const accountAddr = await this.getAccountAddress();
1622
1637
  const depositTxHash = await this.approveAndDeposit(accountAddr, tokenInfo, collateralWei);
1623
1638
  await this.ensureCreditProvider(accountAddr);
1624
- const borrowTxHash = await this.executeBorrow(accountAddr, tokenInfo.address, borrowWei);
1639
+ const account = new import_ethers5.Contract(accountAddr, AGENT_ACCOUNT_ABI, this.signer);
1640
+ const calldata = this.morphoIface.encodeFunctionData("drawWithCollateral", [tokenInfo.address, borrowWei]);
1641
+ const borrowTx = await account.execute(this.config.contracts.morphoCredit, 0, calldata);
1642
+ await borrowTx.wait();
1625
1643
  const totalDebt = await this.morpho.getTotalDebt(accountAddr);
1626
1644
  return {
1627
1645
  depositTx: depositTxHash,
1628
- borrowTx: borrowTxHash,
1646
+ borrowTx: borrowTx.hash,
1629
1647
  collateral: { amount: collateralWei, token: tokenSymbol },
1630
1648
  borrowed: borrowWei,
1631
1649
  agentAccount: accountAddr,
@@ -1656,10 +1674,14 @@ var MorphoCreditClient = class {
1656
1674
  if (!activeToken) {
1657
1675
  throw new Error("No collateral deposited. Use deposit() or depositAndBorrow() first.");
1658
1676
  }
1659
- const txHash = await this.executeBorrow(accountAddr, activeToken.address, borrowWei);
1677
+ await this.ensureCreditProvider(accountAddr);
1678
+ const account = new import_ethers5.Contract(accountAddr, AGENT_ACCOUNT_ABI, this.signer);
1679
+ const calldata = this.morphoIface.encodeFunctionData("drawWithCollateral", [activeToken.address, borrowWei]);
1680
+ const tx = await account.execute(this.config.contracts.morphoCredit, 0, calldata);
1681
+ await tx.wait();
1660
1682
  const totalDebt = await this.morpho.getTotalDebt(accountAddr);
1661
1683
  return {
1662
- tx: txHash,
1684
+ tx: tx.hash,
1663
1685
  amount: borrowWei,
1664
1686
  agentAccount: accountAddr,
1665
1687
  totalDebt,
@@ -1704,8 +1726,11 @@ var MorphoCreditClient = class {
1704
1726
  const borrowWei = import_ethers5.ethers.parseUnits(borrowAmount, 6);
1705
1727
  try {
1706
1728
  await this.ensureCreditProvider(accountAddr);
1707
- const borrowTxHash = await this.executeBorrow(accountAddr, tokenInfo.address, borrowWei);
1708
- result.borrowTx = borrowTxHash;
1729
+ const account = new import_ethers5.Contract(accountAddr, AGENT_ACCOUNT_ABI, this.signer);
1730
+ const calldata = this.morphoIface.encodeFunctionData("drawWithCollateral", [tokenInfo.address, borrowWei]);
1731
+ const tx = await account.execute(this.config.contracts.morphoCredit, 0, calldata);
1732
+ await tx.wait();
1733
+ result.borrowTx = tx.hash;
1709
1734
  result.borrowed = borrowWei;
1710
1735
  } catch (e) {
1711
1736
  throw new Error(`Borrow failed (caller may not own this AgentAccount): ${e.message}`);
@@ -1741,17 +1766,20 @@ var MorphoCreditClient = class {
1741
1766
  if (balance < amountWei) {
1742
1767
  throw new Error(`Insufficient USDC in AgentAccount: $${import_ethers5.ethers.formatUnits(balance, 6)}`);
1743
1768
  }
1744
- const account = new import_ethers5.Contract(accountAddr, AGENT_ACCOUNT_ABI, this.signer);
1745
- const erc20Iface = new import_ethers5.ethers.Interface(ERC20_ABI);
1746
- const morphoIface = new import_ethers5.ethers.Interface(MORPHO_CREDIT_ABI);
1747
- const approveData = erc20Iface.encodeFunctionData("approve", [this.config.contracts.morphoCredit, amountWei]);
1748
- const approveTx = await account.execute(this.config.contracts.usdc, 0, approveData);
1749
- await approveTx.wait();
1750
- const repayData = morphoIface.encodeFunctionData("repayWithCollateral", [collateralAddr, amountWei]);
1751
- const tx = await account.execute(this.config.contracts.morphoCredit, 0, repayData);
1752
- await tx.wait();
1769
+ const txHash = await this.batch(accountAddr, [
1770
+ {
1771
+ target: this.config.contracts.usdc,
1772
+ value: 0n,
1773
+ data: this.erc20Iface.encodeFunctionData("approve", [this.config.contracts.morphoCredit, amountWei])
1774
+ },
1775
+ {
1776
+ target: this.config.contracts.morphoCredit,
1777
+ value: 0n,
1778
+ data: this.morphoIface.encodeFunctionData("repayWithCollateral", [collateralAddr, amountWei])
1779
+ }
1780
+ ]);
1753
1781
  const totalDebt = await this.morpho.getTotalDebt(accountAddr);
1754
- return { tx: tx.hash, amount: amountWei, remainingDebt: totalDebt };
1782
+ return { tx: txHash, amount: amountWei, remainingDebt: totalDebt };
1755
1783
  }
1756
1784
  /**
1757
1785
  * Withdraw collateral from Morpho back to EOA.
@@ -1772,18 +1800,21 @@ var MorphoCreditClient = class {
1772
1800
  `Cannot withdraw more than deposited: max ${import_ethers5.ethers.formatUnits(pos.collateralAmount, tokenInfo.decimals)} ${tokenSymbol}`
1773
1801
  );
1774
1802
  }
1775
- const account = new import_ethers5.Contract(accountAddr, AGENT_ACCOUNT_ABI, this.signer);
1776
- const morphoIface = new import_ethers5.ethers.Interface(MORPHO_CREDIT_ABI);
1777
- const withdrawData = morphoIface.encodeFunctionData("withdrawCollateral", [tokenInfo.address, withdrawAmount]);
1778
- const tx1 = await account.execute(this.config.contracts.morphoCredit, 0, withdrawData);
1779
- await tx1.wait();
1780
- const erc20Iface = new import_ethers5.ethers.Interface(ERC20_ABI);
1781
- const transferData = erc20Iface.encodeFunctionData("transfer", [this.signer.address, withdrawAmount]);
1782
- const tx2 = await account.execute(tokenInfo.address, 0, transferData);
1783
- await tx2.wait();
1803
+ const txHash = await this.batch(accountAddr, [
1804
+ {
1805
+ target: this.config.contracts.morphoCredit,
1806
+ value: 0n,
1807
+ data: this.morphoIface.encodeFunctionData("withdrawCollateral", [tokenInfo.address, withdrawAmount])
1808
+ },
1809
+ {
1810
+ target: tokenInfo.address,
1811
+ value: 0n,
1812
+ data: this.erc20Iface.encodeFunctionData("transfer", [this.signer.address, withdrawAmount])
1813
+ }
1814
+ ]);
1784
1815
  const newPos = await this.morpho.getPosition(accountAddr, tokenInfo.address);
1785
1816
  return {
1786
- tx: tx1.hash,
1817
+ tx: txHash,
1787
1818
  amount: withdrawAmount,
1788
1819
  token: tokenSymbol,
1789
1820
  destination: this.signer.address,
package/dist/index.mjs CHANGED
@@ -79,6 +79,7 @@ var AGENT_ACCOUNT_ABI = [
79
79
  "function balanceOf(address token) view returns (uint256)",
80
80
  "function ethBalance() view returns (uint256)",
81
81
  "function execute(address target, uint256 value, bytes data) payable returns (bytes)",
82
+ "function executeBatch(address[] targets, uint256[] values, bytes[] datas) payable returns (bytes[])",
82
83
  "function drawCredit(address creditProvider, uint256 amount)",
83
84
  "function repayCredit(address creditProvider, uint256 amount)",
84
85
  "function fund(address token, uint256 amount)",
@@ -1440,6 +1441,9 @@ var BASE_COLLATERALS = {
1440
1441
  };
1441
1442
  var MorphoCreditClient = class {
1442
1443
  constructor(config, collaterals) {
1444
+ // ── Batch helper ──
1445
+ this.morphoIface = new ethers4.Interface(MORPHO_CREDIT_ABI);
1446
+ this.erc20Iface = new ethers4.Interface(ERC20_ABI);
1443
1447
  this.config = config;
1444
1448
  const provider = new ethers4.JsonRpcProvider(config.rpcUrl);
1445
1449
  this.signer = new ethers4.Wallet(config.privateKey, provider);
@@ -1485,6 +1489,11 @@ var MorphoCreditClient = class {
1485
1489
  } catch {
1486
1490
  }
1487
1491
  }
1492
+ /**
1493
+ * Approve ERC-20 token from EOA → MorphoCredit, then deposit for account.
1494
+ * This is 2 EOA txs (approve + depositCollateralFor) — cannot be batched
1495
+ * because both are called from EOA, not from AgentAccount.
1496
+ */
1488
1497
  async approveAndDeposit(accountAddr, tokenInfo, amount) {
1489
1498
  const token = new Contract3(tokenInfo.address, ERC20_ABI, this.signer);
1490
1499
  const balance = await token.balanceOf(this.signer.address);
@@ -1499,11 +1508,17 @@ var MorphoCreditClient = class {
1499
1508
  await depositTx.wait();
1500
1509
  return depositTx.hash;
1501
1510
  }
1502
- async executeBorrow(accountAddr, collateralAddr, borrowAmount) {
1511
+ /**
1512
+ * Execute multiple calls via AgentAccount.executeBatch() in a single tx.
1513
+ * Each call is { target, value, data }.
1514
+ */
1515
+ async batch(accountAddr, calls) {
1503
1516
  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);
1517
+ const tx = await account.executeBatch(
1518
+ calls.map((c) => c.target),
1519
+ calls.map((c) => c.value),
1520
+ calls.map((c) => c.data)
1521
+ );
1507
1522
  await tx.wait();
1508
1523
  return tx.hash;
1509
1524
  }
@@ -1550,11 +1565,14 @@ var MorphoCreditClient = class {
1550
1565
  const accountAddr = await this.getAccountAddress();
1551
1566
  const depositTxHash = await this.approveAndDeposit(accountAddr, tokenInfo, collateralWei);
1552
1567
  await this.ensureCreditProvider(accountAddr);
1553
- const borrowTxHash = await this.executeBorrow(accountAddr, tokenInfo.address, borrowWei);
1568
+ const account = new Contract3(accountAddr, AGENT_ACCOUNT_ABI, this.signer);
1569
+ const calldata = this.morphoIface.encodeFunctionData("drawWithCollateral", [tokenInfo.address, borrowWei]);
1570
+ const borrowTx = await account.execute(this.config.contracts.morphoCredit, 0, calldata);
1571
+ await borrowTx.wait();
1554
1572
  const totalDebt = await this.morpho.getTotalDebt(accountAddr);
1555
1573
  return {
1556
1574
  depositTx: depositTxHash,
1557
- borrowTx: borrowTxHash,
1575
+ borrowTx: borrowTx.hash,
1558
1576
  collateral: { amount: collateralWei, token: tokenSymbol },
1559
1577
  borrowed: borrowWei,
1560
1578
  agentAccount: accountAddr,
@@ -1585,10 +1603,14 @@ var MorphoCreditClient = class {
1585
1603
  if (!activeToken) {
1586
1604
  throw new Error("No collateral deposited. Use deposit() or depositAndBorrow() first.");
1587
1605
  }
1588
- const txHash = await this.executeBorrow(accountAddr, activeToken.address, borrowWei);
1606
+ await this.ensureCreditProvider(accountAddr);
1607
+ const account = new Contract3(accountAddr, AGENT_ACCOUNT_ABI, this.signer);
1608
+ const calldata = this.morphoIface.encodeFunctionData("drawWithCollateral", [activeToken.address, borrowWei]);
1609
+ const tx = await account.execute(this.config.contracts.morphoCredit, 0, calldata);
1610
+ await tx.wait();
1589
1611
  const totalDebt = await this.morpho.getTotalDebt(accountAddr);
1590
1612
  return {
1591
- tx: txHash,
1613
+ tx: tx.hash,
1592
1614
  amount: borrowWei,
1593
1615
  agentAccount: accountAddr,
1594
1616
  totalDebt,
@@ -1633,8 +1655,11 @@ var MorphoCreditClient = class {
1633
1655
  const borrowWei = ethers4.parseUnits(borrowAmount, 6);
1634
1656
  try {
1635
1657
  await this.ensureCreditProvider(accountAddr);
1636
- const borrowTxHash = await this.executeBorrow(accountAddr, tokenInfo.address, borrowWei);
1637
- result.borrowTx = borrowTxHash;
1658
+ const account = new Contract3(accountAddr, AGENT_ACCOUNT_ABI, this.signer);
1659
+ const calldata = this.morphoIface.encodeFunctionData("drawWithCollateral", [tokenInfo.address, borrowWei]);
1660
+ const tx = await account.execute(this.config.contracts.morphoCredit, 0, calldata);
1661
+ await tx.wait();
1662
+ result.borrowTx = tx.hash;
1638
1663
  result.borrowed = borrowWei;
1639
1664
  } catch (e) {
1640
1665
  throw new Error(`Borrow failed (caller may not own this AgentAccount): ${e.message}`);
@@ -1670,17 +1695,20 @@ var MorphoCreditClient = class {
1670
1695
  if (balance < amountWei) {
1671
1696
  throw new Error(`Insufficient USDC in AgentAccount: $${ethers4.formatUnits(balance, 6)}`);
1672
1697
  }
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();
1698
+ const txHash = await this.batch(accountAddr, [
1699
+ {
1700
+ target: this.config.contracts.usdc,
1701
+ value: 0n,
1702
+ data: this.erc20Iface.encodeFunctionData("approve", [this.config.contracts.morphoCredit, amountWei])
1703
+ },
1704
+ {
1705
+ target: this.config.contracts.morphoCredit,
1706
+ value: 0n,
1707
+ data: this.morphoIface.encodeFunctionData("repayWithCollateral", [collateralAddr, amountWei])
1708
+ }
1709
+ ]);
1682
1710
  const totalDebt = await this.morpho.getTotalDebt(accountAddr);
1683
- return { tx: tx.hash, amount: amountWei, remainingDebt: totalDebt };
1711
+ return { tx: txHash, amount: amountWei, remainingDebt: totalDebt };
1684
1712
  }
1685
1713
  /**
1686
1714
  * Withdraw collateral from Morpho back to EOA.
@@ -1701,18 +1729,21 @@ var MorphoCreditClient = class {
1701
1729
  `Cannot withdraw more than deposited: max ${ethers4.formatUnits(pos.collateralAmount, tokenInfo.decimals)} ${tokenSymbol}`
1702
1730
  );
1703
1731
  }
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();
1732
+ const txHash = await this.batch(accountAddr, [
1733
+ {
1734
+ target: this.config.contracts.morphoCredit,
1735
+ value: 0n,
1736
+ data: this.morphoIface.encodeFunctionData("withdrawCollateral", [tokenInfo.address, withdrawAmount])
1737
+ },
1738
+ {
1739
+ target: tokenInfo.address,
1740
+ value: 0n,
1741
+ data: this.erc20Iface.encodeFunctionData("transfer", [this.signer.address, withdrawAmount])
1742
+ }
1743
+ ]);
1713
1744
  const newPos = await this.morpho.getPosition(accountAddr, tokenInfo.address);
1714
1745
  return {
1715
- tx: tx1.hash,
1746
+ tx: txHash,
1716
1747
  amount: withdrawAmount,
1717
1748
  token: tokenSymbol,
1718
1749
  destination: this.signer.address,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agether/sdk",
3
- "version": "1.2.1",
3
+ "version": "1.3.0",
4
4
  "description": "TypeScript SDK for Agether - autonomous credit for AI agents on Base",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist/index.mjs",