@agether/sdk 1.11.1 → 1.11.2
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 +2294 -0
- package/dist/index.js +59 -12
- package/dist/index.mjs +59 -12
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -854,7 +854,7 @@ var MorphoClient = class {
|
|
|
854
854
|
const mkt = await this.morphoBlue.market(m.uniqueKey);
|
|
855
855
|
const totalBorrowShares = BigInt(mkt.totalBorrowShares);
|
|
856
856
|
const totalBorrowAssets = BigInt(mkt.totalBorrowAssets);
|
|
857
|
-
debt = totalBorrowShares > 0n ? BigInt(pos.borrowShares) * totalBorrowAssets / totalBorrowShares : 0n;
|
|
857
|
+
debt = totalBorrowShares > 0n ? (BigInt(pos.borrowShares) * totalBorrowAssets + totalBorrowShares - 1n) / totalBorrowShares : 0n;
|
|
858
858
|
totalDebt += debt;
|
|
859
859
|
} catch (e) {
|
|
860
860
|
console.warn(`[agether] debt calc failed for market ${m.uniqueKey}:`, e instanceof Error ? e.message : e);
|
|
@@ -916,7 +916,7 @@ var MorphoClient = class {
|
|
|
916
916
|
const mktState = await this.morphoBlue.market(m.uniqueKey);
|
|
917
917
|
const totalBorrowShares = BigInt(mktState.totalBorrowShares);
|
|
918
918
|
const totalBorrowAssets = BigInt(mktState.totalBorrowAssets);
|
|
919
|
-
const currentDebt = totalBorrowShares > 0n ? BigInt(pos.borrowShares) * totalBorrowAssets / totalBorrowShares : 0n;
|
|
919
|
+
const currentDebt = totalBorrowShares > 0n ? (BigInt(pos.borrowShares) * totalBorrowAssets + totalBorrowShares - 1n) / totalBorrowShares : 0n;
|
|
920
920
|
let collateralValueInLoan;
|
|
921
921
|
try {
|
|
922
922
|
const oracleContract = new import_ethers2.Contract(m.oracle, [
|
|
@@ -1279,13 +1279,14 @@ var MorphoClient = class {
|
|
|
1279
1279
|
if (!colInfo) throw new AgetherError(`Unknown collateral: ${tokenSymbol}`, "UNKNOWN_COLLATERAL");
|
|
1280
1280
|
const params = marketParams ?? await this.findMarketForCollateral(tokenSymbol);
|
|
1281
1281
|
const morphoAddr = this.config.contracts.morphoBlue;
|
|
1282
|
+
const usdcAddr = this.config.contracts.usdc;
|
|
1282
1283
|
const dest = receiver || await this.getSignerAddress();
|
|
1283
1284
|
let weiAmount;
|
|
1285
|
+
const markets = await this.getMarkets();
|
|
1286
|
+
const market = markets.find(
|
|
1287
|
+
(m) => m.collateralAsset?.address.toLowerCase() === colInfo.address.toLowerCase()
|
|
1288
|
+
);
|
|
1284
1289
|
if (amount === "all") {
|
|
1285
|
-
const markets = await this.getMarkets();
|
|
1286
|
-
const market = markets.find(
|
|
1287
|
-
(m) => m.collateralAsset.address.toLowerCase() === colInfo.address.toLowerCase()
|
|
1288
|
-
);
|
|
1289
1290
|
if (!market) throw new AgetherError("Market not found", "MARKET_NOT_FOUND");
|
|
1290
1291
|
const pos = await this.morphoBlue.position(market.uniqueKey, acctAddr);
|
|
1291
1292
|
weiAmount = pos.collateral;
|
|
@@ -1293,19 +1294,65 @@ var MorphoClient = class {
|
|
|
1293
1294
|
} else {
|
|
1294
1295
|
weiAmount = import_ethers2.ethers.parseUnits(amount, colInfo.decimals);
|
|
1295
1296
|
}
|
|
1296
|
-
|
|
1297
|
+
let hasDustDebt = false;
|
|
1298
|
+
let dustBorrowShares = 0n;
|
|
1299
|
+
let dustApproveAmount = 0n;
|
|
1300
|
+
if (market) {
|
|
1301
|
+
try {
|
|
1302
|
+
const pos = await this.morphoBlue.position(market.uniqueKey, acctAddr);
|
|
1303
|
+
dustBorrowShares = BigInt(pos.borrowShares);
|
|
1304
|
+
if (dustBorrowShares > 0n) {
|
|
1305
|
+
hasDustDebt = true;
|
|
1306
|
+
const onChainMkt = await this.morphoBlue.market(market.uniqueKey);
|
|
1307
|
+
const totalBorrowAssets = BigInt(onChainMkt.totalBorrowAssets);
|
|
1308
|
+
const totalBorrowShares = BigInt(onChainMkt.totalBorrowShares);
|
|
1309
|
+
const estimated = totalBorrowShares > 0n ? dustBorrowShares * totalBorrowAssets / totalBorrowShares + 10n : 0n;
|
|
1310
|
+
dustApproveAmount = estimated > 0n ? estimated : import_ethers2.ethers.parseUnits("1", 6);
|
|
1311
|
+
console.log(`[agether] dust borrow shares detected: ${dustBorrowShares} shares \u2248 ${import_ethers2.ethers.formatUnits(dustApproveAmount, 6)} USDC \u2014 auto-repaying before withdraw`);
|
|
1312
|
+
}
|
|
1313
|
+
} catch (e) {
|
|
1314
|
+
console.warn("[agether] failed to check borrow shares before withdraw:", e instanceof Error ? e.message : e);
|
|
1315
|
+
}
|
|
1316
|
+
}
|
|
1317
|
+
const withdrawData = morphoIface.encodeFunctionData("withdrawCollateral", [
|
|
1297
1318
|
this._toTuple(params),
|
|
1298
1319
|
weiAmount,
|
|
1299
1320
|
acctAddr,
|
|
1300
1321
|
dest
|
|
1301
1322
|
]);
|
|
1302
|
-
|
|
1323
|
+
let receipt;
|
|
1324
|
+
if (hasDustDebt) {
|
|
1325
|
+
const usdcContract = new import_ethers2.Contract(usdcAddr, ERC20_ABI, this._signer);
|
|
1326
|
+
const acctBalance = await usdcContract.balanceOf(acctAddr);
|
|
1327
|
+
if (acctBalance < dustApproveAmount) {
|
|
1328
|
+
const shortfall = dustApproveAmount - acctBalance;
|
|
1329
|
+
const eoaBalance = await usdcContract.balanceOf(await this.getSignerAddress());
|
|
1330
|
+
if (eoaBalance >= shortfall) {
|
|
1331
|
+
console.log(`[agether] transferring ${import_ethers2.ethers.formatUnits(shortfall, 6)} USDC from EOA \u2192 AgentAccount for dust repay`);
|
|
1332
|
+
const transferTx = await usdcContract.transfer(acctAddr, shortfall);
|
|
1333
|
+
await transferTx.wait();
|
|
1334
|
+
this._refreshSigner();
|
|
1335
|
+
}
|
|
1336
|
+
}
|
|
1337
|
+
const targets = [usdcAddr, morphoAddr, morphoAddr];
|
|
1338
|
+
const values = [0n, 0n, 0n];
|
|
1339
|
+
const datas = [
|
|
1340
|
+
erc20Iface.encodeFunctionData("approve", [morphoAddr, dustApproveAmount]),
|
|
1341
|
+
morphoIface.encodeFunctionData("repay", [
|
|
1342
|
+
this._toTuple(params),
|
|
1343
|
+
0n,
|
|
1344
|
+
dustBorrowShares,
|
|
1345
|
+
acctAddr,
|
|
1346
|
+
"0x"
|
|
1347
|
+
]),
|
|
1348
|
+
withdrawData
|
|
1349
|
+
];
|
|
1350
|
+
receipt = await this.batch(targets, values, datas);
|
|
1351
|
+
} else {
|
|
1352
|
+
receipt = await this.exec(morphoAddr, withdrawData);
|
|
1353
|
+
}
|
|
1303
1354
|
let remainingCollateral = "0";
|
|
1304
1355
|
try {
|
|
1305
|
-
const markets = await this.getMarkets();
|
|
1306
|
-
const market = markets.find(
|
|
1307
|
-
(m) => m.collateralAsset.address.toLowerCase() === colInfo.address.toLowerCase()
|
|
1308
|
-
);
|
|
1309
1356
|
if (market) {
|
|
1310
1357
|
const pos = await this.morphoBlue.position(market.uniqueKey, acctAddr);
|
|
1311
1358
|
remainingCollateral = import_ethers2.ethers.formatUnits(pos.collateral, colInfo.decimals);
|
package/dist/index.mjs
CHANGED
|
@@ -789,7 +789,7 @@ var MorphoClient = class {
|
|
|
789
789
|
const mkt = await this.morphoBlue.market(m.uniqueKey);
|
|
790
790
|
const totalBorrowShares = BigInt(mkt.totalBorrowShares);
|
|
791
791
|
const totalBorrowAssets = BigInt(mkt.totalBorrowAssets);
|
|
792
|
-
debt = totalBorrowShares > 0n ? BigInt(pos.borrowShares) * totalBorrowAssets / totalBorrowShares : 0n;
|
|
792
|
+
debt = totalBorrowShares > 0n ? (BigInt(pos.borrowShares) * totalBorrowAssets + totalBorrowShares - 1n) / totalBorrowShares : 0n;
|
|
793
793
|
totalDebt += debt;
|
|
794
794
|
} catch (e) {
|
|
795
795
|
console.warn(`[agether] debt calc failed for market ${m.uniqueKey}:`, e instanceof Error ? e.message : e);
|
|
@@ -851,7 +851,7 @@ var MorphoClient = class {
|
|
|
851
851
|
const mktState = await this.morphoBlue.market(m.uniqueKey);
|
|
852
852
|
const totalBorrowShares = BigInt(mktState.totalBorrowShares);
|
|
853
853
|
const totalBorrowAssets = BigInt(mktState.totalBorrowAssets);
|
|
854
|
-
const currentDebt = totalBorrowShares > 0n ? BigInt(pos.borrowShares) * totalBorrowAssets / totalBorrowShares : 0n;
|
|
854
|
+
const currentDebt = totalBorrowShares > 0n ? (BigInt(pos.borrowShares) * totalBorrowAssets + totalBorrowShares - 1n) / totalBorrowShares : 0n;
|
|
855
855
|
let collateralValueInLoan;
|
|
856
856
|
try {
|
|
857
857
|
const oracleContract = new Contract2(m.oracle, [
|
|
@@ -1214,13 +1214,14 @@ var MorphoClient = class {
|
|
|
1214
1214
|
if (!colInfo) throw new AgetherError(`Unknown collateral: ${tokenSymbol}`, "UNKNOWN_COLLATERAL");
|
|
1215
1215
|
const params = marketParams ?? await this.findMarketForCollateral(tokenSymbol);
|
|
1216
1216
|
const morphoAddr = this.config.contracts.morphoBlue;
|
|
1217
|
+
const usdcAddr = this.config.contracts.usdc;
|
|
1217
1218
|
const dest = receiver || await this.getSignerAddress();
|
|
1218
1219
|
let weiAmount;
|
|
1220
|
+
const markets = await this.getMarkets();
|
|
1221
|
+
const market = markets.find(
|
|
1222
|
+
(m) => m.collateralAsset?.address.toLowerCase() === colInfo.address.toLowerCase()
|
|
1223
|
+
);
|
|
1219
1224
|
if (amount === "all") {
|
|
1220
|
-
const markets = await this.getMarkets();
|
|
1221
|
-
const market = markets.find(
|
|
1222
|
-
(m) => m.collateralAsset.address.toLowerCase() === colInfo.address.toLowerCase()
|
|
1223
|
-
);
|
|
1224
1225
|
if (!market) throw new AgetherError("Market not found", "MARKET_NOT_FOUND");
|
|
1225
1226
|
const pos = await this.morphoBlue.position(market.uniqueKey, acctAddr);
|
|
1226
1227
|
weiAmount = pos.collateral;
|
|
@@ -1228,19 +1229,65 @@ var MorphoClient = class {
|
|
|
1228
1229
|
} else {
|
|
1229
1230
|
weiAmount = ethers2.parseUnits(amount, colInfo.decimals);
|
|
1230
1231
|
}
|
|
1231
|
-
|
|
1232
|
+
let hasDustDebt = false;
|
|
1233
|
+
let dustBorrowShares = 0n;
|
|
1234
|
+
let dustApproveAmount = 0n;
|
|
1235
|
+
if (market) {
|
|
1236
|
+
try {
|
|
1237
|
+
const pos = await this.morphoBlue.position(market.uniqueKey, acctAddr);
|
|
1238
|
+
dustBorrowShares = BigInt(pos.borrowShares);
|
|
1239
|
+
if (dustBorrowShares > 0n) {
|
|
1240
|
+
hasDustDebt = true;
|
|
1241
|
+
const onChainMkt = await this.morphoBlue.market(market.uniqueKey);
|
|
1242
|
+
const totalBorrowAssets = BigInt(onChainMkt.totalBorrowAssets);
|
|
1243
|
+
const totalBorrowShares = BigInt(onChainMkt.totalBorrowShares);
|
|
1244
|
+
const estimated = totalBorrowShares > 0n ? dustBorrowShares * totalBorrowAssets / totalBorrowShares + 10n : 0n;
|
|
1245
|
+
dustApproveAmount = estimated > 0n ? estimated : ethers2.parseUnits("1", 6);
|
|
1246
|
+
console.log(`[agether] dust borrow shares detected: ${dustBorrowShares} shares \u2248 ${ethers2.formatUnits(dustApproveAmount, 6)} USDC \u2014 auto-repaying before withdraw`);
|
|
1247
|
+
}
|
|
1248
|
+
} catch (e) {
|
|
1249
|
+
console.warn("[agether] failed to check borrow shares before withdraw:", e instanceof Error ? e.message : e);
|
|
1250
|
+
}
|
|
1251
|
+
}
|
|
1252
|
+
const withdrawData = morphoIface.encodeFunctionData("withdrawCollateral", [
|
|
1232
1253
|
this._toTuple(params),
|
|
1233
1254
|
weiAmount,
|
|
1234
1255
|
acctAddr,
|
|
1235
1256
|
dest
|
|
1236
1257
|
]);
|
|
1237
|
-
|
|
1258
|
+
let receipt;
|
|
1259
|
+
if (hasDustDebt) {
|
|
1260
|
+
const usdcContract = new Contract2(usdcAddr, ERC20_ABI, this._signer);
|
|
1261
|
+
const acctBalance = await usdcContract.balanceOf(acctAddr);
|
|
1262
|
+
if (acctBalance < dustApproveAmount) {
|
|
1263
|
+
const shortfall = dustApproveAmount - acctBalance;
|
|
1264
|
+
const eoaBalance = await usdcContract.balanceOf(await this.getSignerAddress());
|
|
1265
|
+
if (eoaBalance >= shortfall) {
|
|
1266
|
+
console.log(`[agether] transferring ${ethers2.formatUnits(shortfall, 6)} USDC from EOA \u2192 AgentAccount for dust repay`);
|
|
1267
|
+
const transferTx = await usdcContract.transfer(acctAddr, shortfall);
|
|
1268
|
+
await transferTx.wait();
|
|
1269
|
+
this._refreshSigner();
|
|
1270
|
+
}
|
|
1271
|
+
}
|
|
1272
|
+
const targets = [usdcAddr, morphoAddr, morphoAddr];
|
|
1273
|
+
const values = [0n, 0n, 0n];
|
|
1274
|
+
const datas = [
|
|
1275
|
+
erc20Iface.encodeFunctionData("approve", [morphoAddr, dustApproveAmount]),
|
|
1276
|
+
morphoIface.encodeFunctionData("repay", [
|
|
1277
|
+
this._toTuple(params),
|
|
1278
|
+
0n,
|
|
1279
|
+
dustBorrowShares,
|
|
1280
|
+
acctAddr,
|
|
1281
|
+
"0x"
|
|
1282
|
+
]),
|
|
1283
|
+
withdrawData
|
|
1284
|
+
];
|
|
1285
|
+
receipt = await this.batch(targets, values, datas);
|
|
1286
|
+
} else {
|
|
1287
|
+
receipt = await this.exec(morphoAddr, withdrawData);
|
|
1288
|
+
}
|
|
1238
1289
|
let remainingCollateral = "0";
|
|
1239
1290
|
try {
|
|
1240
|
-
const markets = await this.getMarkets();
|
|
1241
|
-
const market = markets.find(
|
|
1242
|
-
(m) => m.collateralAsset.address.toLowerCase() === colInfo.address.toLowerCase()
|
|
1243
|
-
);
|
|
1244
1291
|
if (market) {
|
|
1245
1292
|
const pos = await this.morphoBlue.position(market.uniqueKey, acctAddr);
|
|
1246
1293
|
remainingCollateral = ethers2.formatUnits(pos.collateral, colInfo.decimals);
|