@agether/sdk 2.2.0 → 2.3.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 CHANGED
@@ -887,9 +887,20 @@ var init_MorphoClient = __esm({
887
887
  const weiAmount = import_ethers.ethers.parseUnits(amount, colInfo.decimals);
888
888
  const morphoAddr = this.config.contracts.morphoBlue;
889
889
  const colToken = new import_ethers.Contract(colInfo.address, ERC20_ABI, this._signer);
890
- const transferTx = await colToken.transfer(acctAddr, weiAmount);
891
- await transferTx.wait();
892
- this._refreshSigner();
890
+ const acctBalance = await colToken.balanceOf(acctAddr);
891
+ if (acctBalance < weiAmount) {
892
+ const shortfall = weiAmount - acctBalance;
893
+ const eoaBalance = await colToken.balanceOf(await this.getSignerAddress());
894
+ if (eoaBalance < shortfall) {
895
+ throw new AgetherError(
896
+ `Insufficient ${tokenSymbol}. Need ${amount}, AgentAccount has ${import_ethers.ethers.formatUnits(acctBalance, colInfo.decimals)}, EOA has ${import_ethers.ethers.formatUnits(eoaBalance, colInfo.decimals)}.`,
897
+ "INSUFFICIENT_BALANCE"
898
+ );
899
+ }
900
+ const transferTx = await colToken.transfer(acctAddr, shortfall);
901
+ await transferTx.wait();
902
+ this._refreshSigner();
903
+ }
893
904
  const targets = [colInfo.address, morphoAddr];
894
905
  const values = [0n, 0n];
895
906
  const datas = [
@@ -964,9 +975,20 @@ var init_MorphoClient = __esm({
964
975
  const borrowWei = import_ethers.ethers.parseUnits(borrowUsdcAmount, 6);
965
976
  const morphoAddr = this.config.contracts.morphoBlue;
966
977
  const colToken = new import_ethers.Contract(colInfo.address, ERC20_ABI, this._signer);
967
- const transferTx = await colToken.transfer(acctAddr, colWei);
968
- await transferTx.wait();
969
- this._refreshSigner();
978
+ const acctBalance = await colToken.balanceOf(acctAddr);
979
+ if (acctBalance < colWei) {
980
+ const shortfall = colWei - acctBalance;
981
+ const eoaBalance = await colToken.balanceOf(await this.getSignerAddress());
982
+ if (eoaBalance < shortfall) {
983
+ throw new AgetherError(
984
+ `Insufficient ${tokenSymbol}. Need ${collateralAmount}, AgentAccount has ${import_ethers.ethers.formatUnits(acctBalance, colInfo.decimals)}, EOA has ${import_ethers.ethers.formatUnits(eoaBalance, colInfo.decimals)}.`,
985
+ "INSUFFICIENT_BALANCE"
986
+ );
987
+ }
988
+ const transferTx = await colToken.transfer(acctAddr, shortfall);
989
+ await transferTx.wait();
990
+ this._refreshSigner();
991
+ }
970
992
  const targets = [colInfo.address, morphoAddr, morphoAddr];
971
993
  const values = [0n, 0n, 0n];
972
994
  const datas = [
@@ -1280,7 +1302,9 @@ var init_MorphoClient = __esm({
1280
1302
  */
1281
1303
  async _submitUserOp(callData) {
1282
1304
  const sender = await this.getAccountAddress();
1283
- const nonce = await this.entryPoint.getNonce(sender, 0);
1305
+ const validatorAddr = this.config.contracts.erc8004ValidationModule;
1306
+ const nonceKey = BigInt(validatorAddr) << 32n;
1307
+ const nonce = await this.entryPoint.getNonce(sender, nonceKey);
1284
1308
  const feeData = await this.provider.getFeeData();
1285
1309
  const maxFeePerGas = feeData.maxFeePerGas ?? import_ethers.ethers.parseUnits("0.5", "gwei");
1286
1310
  const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas ?? import_ethers.ethers.parseUnits("0.1", "gwei");
@@ -1289,6 +1313,18 @@ var init_MorphoClient = __esm({
1289
1313
  const preVerificationGas = 100000n;
1290
1314
  const accountGasLimits = this._packUint128(verificationGasLimit, callGasLimit);
1291
1315
  const gasFees = this._packUint128(maxPriorityFeePerGas, maxFeePerGas);
1316
+ const requiredPrefund = (verificationGasLimit + callGasLimit + preVerificationGas) * maxFeePerGas;
1317
+ const accountBalance = await this.provider.getBalance(sender);
1318
+ if (accountBalance < requiredPrefund) {
1319
+ const topUp = requiredPrefund - accountBalance;
1320
+ const topUpWithBuffer = topUp * 120n / 100n;
1321
+ const fundTx = await this._signer.sendTransaction({
1322
+ to: sender,
1323
+ value: topUpWithBuffer
1324
+ });
1325
+ await fundTx.wait();
1326
+ this._refreshSigner();
1327
+ }
1292
1328
  const userOp = {
1293
1329
  sender,
1294
1330
  nonce,
package/dist/index.js CHANGED
@@ -1136,9 +1136,20 @@ var MorphoClient = class {
1136
1136
  const weiAmount = import_ethers2.ethers.parseUnits(amount, colInfo.decimals);
1137
1137
  const morphoAddr = this.config.contracts.morphoBlue;
1138
1138
  const colToken = new import_ethers2.Contract(colInfo.address, ERC20_ABI, this._signer);
1139
- const transferTx = await colToken.transfer(acctAddr, weiAmount);
1140
- await transferTx.wait();
1141
- this._refreshSigner();
1139
+ const acctBalance = await colToken.balanceOf(acctAddr);
1140
+ if (acctBalance < weiAmount) {
1141
+ const shortfall = weiAmount - acctBalance;
1142
+ const eoaBalance = await colToken.balanceOf(await this.getSignerAddress());
1143
+ if (eoaBalance < shortfall) {
1144
+ throw new AgetherError(
1145
+ `Insufficient ${tokenSymbol}. Need ${amount}, AgentAccount has ${import_ethers2.ethers.formatUnits(acctBalance, colInfo.decimals)}, EOA has ${import_ethers2.ethers.formatUnits(eoaBalance, colInfo.decimals)}.`,
1146
+ "INSUFFICIENT_BALANCE"
1147
+ );
1148
+ }
1149
+ const transferTx = await colToken.transfer(acctAddr, shortfall);
1150
+ await transferTx.wait();
1151
+ this._refreshSigner();
1152
+ }
1142
1153
  const targets = [colInfo.address, morphoAddr];
1143
1154
  const values = [0n, 0n];
1144
1155
  const datas = [
@@ -1213,9 +1224,20 @@ var MorphoClient = class {
1213
1224
  const borrowWei = import_ethers2.ethers.parseUnits(borrowUsdcAmount, 6);
1214
1225
  const morphoAddr = this.config.contracts.morphoBlue;
1215
1226
  const colToken = new import_ethers2.Contract(colInfo.address, ERC20_ABI, this._signer);
1216
- const transferTx = await colToken.transfer(acctAddr, colWei);
1217
- await transferTx.wait();
1218
- this._refreshSigner();
1227
+ const acctBalance = await colToken.balanceOf(acctAddr);
1228
+ if (acctBalance < colWei) {
1229
+ const shortfall = colWei - acctBalance;
1230
+ const eoaBalance = await colToken.balanceOf(await this.getSignerAddress());
1231
+ if (eoaBalance < shortfall) {
1232
+ throw new AgetherError(
1233
+ `Insufficient ${tokenSymbol}. Need ${collateralAmount}, AgentAccount has ${import_ethers2.ethers.formatUnits(acctBalance, colInfo.decimals)}, EOA has ${import_ethers2.ethers.formatUnits(eoaBalance, colInfo.decimals)}.`,
1234
+ "INSUFFICIENT_BALANCE"
1235
+ );
1236
+ }
1237
+ const transferTx = await colToken.transfer(acctAddr, shortfall);
1238
+ await transferTx.wait();
1239
+ this._refreshSigner();
1240
+ }
1219
1241
  const targets = [colInfo.address, morphoAddr, morphoAddr];
1220
1242
  const values = [0n, 0n, 0n];
1221
1243
  const datas = [
@@ -1529,7 +1551,9 @@ var MorphoClient = class {
1529
1551
  */
1530
1552
  async _submitUserOp(callData) {
1531
1553
  const sender = await this.getAccountAddress();
1532
- const nonce = await this.entryPoint.getNonce(sender, 0);
1554
+ const validatorAddr = this.config.contracts.erc8004ValidationModule;
1555
+ const nonceKey = BigInt(validatorAddr) << 32n;
1556
+ const nonce = await this.entryPoint.getNonce(sender, nonceKey);
1533
1557
  const feeData = await this.provider.getFeeData();
1534
1558
  const maxFeePerGas = feeData.maxFeePerGas ?? import_ethers2.ethers.parseUnits("0.5", "gwei");
1535
1559
  const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas ?? import_ethers2.ethers.parseUnits("0.1", "gwei");
@@ -1538,6 +1562,18 @@ var MorphoClient = class {
1538
1562
  const preVerificationGas = 100000n;
1539
1563
  const accountGasLimits = this._packUint128(verificationGasLimit, callGasLimit);
1540
1564
  const gasFees = this._packUint128(maxPriorityFeePerGas, maxFeePerGas);
1565
+ const requiredPrefund = (verificationGasLimit + callGasLimit + preVerificationGas) * maxFeePerGas;
1566
+ const accountBalance = await this.provider.getBalance(sender);
1567
+ if (accountBalance < requiredPrefund) {
1568
+ const topUp = requiredPrefund - accountBalance;
1569
+ const topUpWithBuffer = topUp * 120n / 100n;
1570
+ const fundTx = await this._signer.sendTransaction({
1571
+ to: sender,
1572
+ value: topUpWithBuffer
1573
+ });
1574
+ await fundTx.wait();
1575
+ this._refreshSigner();
1576
+ }
1541
1577
  const userOp = {
1542
1578
  sender,
1543
1579
  nonce,
package/dist/index.mjs CHANGED
@@ -1064,9 +1064,20 @@ var MorphoClient = class {
1064
1064
  const weiAmount = ethers2.parseUnits(amount, colInfo.decimals);
1065
1065
  const morphoAddr = this.config.contracts.morphoBlue;
1066
1066
  const colToken = new Contract2(colInfo.address, ERC20_ABI, this._signer);
1067
- const transferTx = await colToken.transfer(acctAddr, weiAmount);
1068
- await transferTx.wait();
1069
- this._refreshSigner();
1067
+ const acctBalance = await colToken.balanceOf(acctAddr);
1068
+ if (acctBalance < weiAmount) {
1069
+ const shortfall = weiAmount - acctBalance;
1070
+ const eoaBalance = await colToken.balanceOf(await this.getSignerAddress());
1071
+ if (eoaBalance < shortfall) {
1072
+ throw new AgetherError(
1073
+ `Insufficient ${tokenSymbol}. Need ${amount}, AgentAccount has ${ethers2.formatUnits(acctBalance, colInfo.decimals)}, EOA has ${ethers2.formatUnits(eoaBalance, colInfo.decimals)}.`,
1074
+ "INSUFFICIENT_BALANCE"
1075
+ );
1076
+ }
1077
+ const transferTx = await colToken.transfer(acctAddr, shortfall);
1078
+ await transferTx.wait();
1079
+ this._refreshSigner();
1080
+ }
1070
1081
  const targets = [colInfo.address, morphoAddr];
1071
1082
  const values = [0n, 0n];
1072
1083
  const datas = [
@@ -1141,9 +1152,20 @@ var MorphoClient = class {
1141
1152
  const borrowWei = ethers2.parseUnits(borrowUsdcAmount, 6);
1142
1153
  const morphoAddr = this.config.contracts.morphoBlue;
1143
1154
  const colToken = new Contract2(colInfo.address, ERC20_ABI, this._signer);
1144
- const transferTx = await colToken.transfer(acctAddr, colWei);
1145
- await transferTx.wait();
1146
- this._refreshSigner();
1155
+ const acctBalance = await colToken.balanceOf(acctAddr);
1156
+ if (acctBalance < colWei) {
1157
+ const shortfall = colWei - acctBalance;
1158
+ const eoaBalance = await colToken.balanceOf(await this.getSignerAddress());
1159
+ if (eoaBalance < shortfall) {
1160
+ throw new AgetherError(
1161
+ `Insufficient ${tokenSymbol}. Need ${collateralAmount}, AgentAccount has ${ethers2.formatUnits(acctBalance, colInfo.decimals)}, EOA has ${ethers2.formatUnits(eoaBalance, colInfo.decimals)}.`,
1162
+ "INSUFFICIENT_BALANCE"
1163
+ );
1164
+ }
1165
+ const transferTx = await colToken.transfer(acctAddr, shortfall);
1166
+ await transferTx.wait();
1167
+ this._refreshSigner();
1168
+ }
1147
1169
  const targets = [colInfo.address, morphoAddr, morphoAddr];
1148
1170
  const values = [0n, 0n, 0n];
1149
1171
  const datas = [
@@ -1457,7 +1479,9 @@ var MorphoClient = class {
1457
1479
  */
1458
1480
  async _submitUserOp(callData) {
1459
1481
  const sender = await this.getAccountAddress();
1460
- const nonce = await this.entryPoint.getNonce(sender, 0);
1482
+ const validatorAddr = this.config.contracts.erc8004ValidationModule;
1483
+ const nonceKey = BigInt(validatorAddr) << 32n;
1484
+ const nonce = await this.entryPoint.getNonce(sender, nonceKey);
1461
1485
  const feeData = await this.provider.getFeeData();
1462
1486
  const maxFeePerGas = feeData.maxFeePerGas ?? ethers2.parseUnits("0.5", "gwei");
1463
1487
  const maxPriorityFeePerGas = feeData.maxPriorityFeePerGas ?? ethers2.parseUnits("0.1", "gwei");
@@ -1466,6 +1490,18 @@ var MorphoClient = class {
1466
1490
  const preVerificationGas = 100000n;
1467
1491
  const accountGasLimits = this._packUint128(verificationGasLimit, callGasLimit);
1468
1492
  const gasFees = this._packUint128(maxPriorityFeePerGas, maxFeePerGas);
1493
+ const requiredPrefund = (verificationGasLimit + callGasLimit + preVerificationGas) * maxFeePerGas;
1494
+ const accountBalance = await this.provider.getBalance(sender);
1495
+ if (accountBalance < requiredPrefund) {
1496
+ const topUp = requiredPrefund - accountBalance;
1497
+ const topUpWithBuffer = topUp * 120n / 100n;
1498
+ const fundTx = await this._signer.sendTransaction({
1499
+ to: sender,
1500
+ value: topUpWithBuffer
1501
+ });
1502
+ await fundTx.wait();
1503
+ this._refreshSigner();
1504
+ }
1469
1505
  const userOp = {
1470
1506
  sender,
1471
1507
  nonce,
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agether/sdk",
3
- "version": "2.2.0",
3
+ "version": "2.3.1",
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",