@t2000/sdk 0.4.1 → 0.4.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/adapters/index.cjs +44 -34
- package/dist/adapters/index.cjs.map +1 -1
- package/dist/adapters/index.d.cts +1 -1
- package/dist/adapters/index.d.ts +1 -1
- package/dist/adapters/index.js +44 -34
- package/dist/adapters/index.js.map +1 -1
- package/dist/{index-DYQv9Wxo.d.cts → index-BuaGAa6b.d.cts} +3 -0
- package/dist/{index-DYQv9Wxo.d.ts → index-BuaGAa6b.d.ts} +3 -0
- package/dist/index.cjs +624 -611
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +624 -611
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -1116,6 +1116,306 @@ var CetusAdapter = class {
|
|
|
1116
1116
|
return getPoolPrice(this.client);
|
|
1117
1117
|
}
|
|
1118
1118
|
};
|
|
1119
|
+
var USDC_TYPE2 = SUPPORTED_ASSETS.USDC.type;
|
|
1120
|
+
SUPPORTED_ASSETS.USDC.decimals;
|
|
1121
|
+
var WAD = 1e18;
|
|
1122
|
+
var MIN_HEALTH_FACTOR2 = 1.5;
|
|
1123
|
+
function interpolateRate(utilBreakpoints, aprBreakpoints, utilizationPct) {
|
|
1124
|
+
if (utilBreakpoints.length === 0) return 0;
|
|
1125
|
+
if (utilizationPct <= utilBreakpoints[0]) return aprBreakpoints[0];
|
|
1126
|
+
if (utilizationPct >= utilBreakpoints[utilBreakpoints.length - 1]) {
|
|
1127
|
+
return aprBreakpoints[aprBreakpoints.length - 1];
|
|
1128
|
+
}
|
|
1129
|
+
for (let i = 1; i < utilBreakpoints.length; i++) {
|
|
1130
|
+
if (utilizationPct <= utilBreakpoints[i]) {
|
|
1131
|
+
const t = (utilizationPct - utilBreakpoints[i - 1]) / (utilBreakpoints[i] - utilBreakpoints[i - 1]);
|
|
1132
|
+
return aprBreakpoints[i - 1] + t * (aprBreakpoints[i] - aprBreakpoints[i - 1]);
|
|
1133
|
+
}
|
|
1134
|
+
}
|
|
1135
|
+
return aprBreakpoints[aprBreakpoints.length - 1];
|
|
1136
|
+
}
|
|
1137
|
+
function computeRatesFromReserve(reserve) {
|
|
1138
|
+
const decimals = reserve.mintDecimals;
|
|
1139
|
+
const available = Number(reserve.availableAmount) / 10 ** decimals;
|
|
1140
|
+
const borrowed = Number(reserve.borrowedAmount.value) / WAD / 10 ** decimals;
|
|
1141
|
+
const totalDeposited = available + borrowed;
|
|
1142
|
+
const utilizationPct = totalDeposited > 0 ? borrowed / totalDeposited * 100 : 0;
|
|
1143
|
+
const config = reserve.config.element;
|
|
1144
|
+
if (!config) return { borrowAprPct: 0, depositAprPct: 0, utilizationPct: 0 };
|
|
1145
|
+
const utils = config.interestRateUtils.map(Number);
|
|
1146
|
+
const aprs = config.interestRateAprs.map((a) => Number(a) / 100);
|
|
1147
|
+
const borrowAprPct = interpolateRate(utils, aprs, utilizationPct);
|
|
1148
|
+
const spreadFeeBps = Number(config.spreadFeeBps);
|
|
1149
|
+
const depositAprPct = utilizationPct / 100 * (borrowAprPct / 100) * (1 - spreadFeeBps / 1e4) * 100;
|
|
1150
|
+
return { borrowAprPct, depositAprPct, utilizationPct };
|
|
1151
|
+
}
|
|
1152
|
+
function cTokenRatio(reserve) {
|
|
1153
|
+
if (reserve.ctokenSupply === 0n) return 1;
|
|
1154
|
+
const available = Number(reserve.availableAmount);
|
|
1155
|
+
const borrowed = Number(reserve.borrowedAmount.value) / WAD;
|
|
1156
|
+
const spreadFees = Number(reserve.unclaimedSpreadFees.value) / WAD;
|
|
1157
|
+
const totalSupply = available + borrowed - spreadFees;
|
|
1158
|
+
return totalSupply / Number(reserve.ctokenSupply);
|
|
1159
|
+
}
|
|
1160
|
+
var SuilendAdapter = class {
|
|
1161
|
+
id = "suilend";
|
|
1162
|
+
name = "Suilend";
|
|
1163
|
+
version = "1.0.0";
|
|
1164
|
+
capabilities = ["save", "withdraw"];
|
|
1165
|
+
supportedAssets = ["USDC"];
|
|
1166
|
+
supportsSameAssetBorrow = false;
|
|
1167
|
+
client;
|
|
1168
|
+
suilend;
|
|
1169
|
+
lendingMarketType;
|
|
1170
|
+
initialized = false;
|
|
1171
|
+
initPromise = null;
|
|
1172
|
+
async init(client) {
|
|
1173
|
+
this.client = client;
|
|
1174
|
+
await this.lazyInit();
|
|
1175
|
+
}
|
|
1176
|
+
initSync(client) {
|
|
1177
|
+
this.client = client;
|
|
1178
|
+
}
|
|
1179
|
+
async lazyInit() {
|
|
1180
|
+
if (this.initialized) return;
|
|
1181
|
+
if (this.initPromise) return this.initPromise;
|
|
1182
|
+
this.initPromise = (async () => {
|
|
1183
|
+
let sdk;
|
|
1184
|
+
try {
|
|
1185
|
+
sdk = await import('@suilend/sdk');
|
|
1186
|
+
} catch {
|
|
1187
|
+
throw new T2000Error(
|
|
1188
|
+
"PROTOCOL_UNAVAILABLE",
|
|
1189
|
+
"Suilend SDK not installed. Run: npm install @suilend/sdk@^1"
|
|
1190
|
+
);
|
|
1191
|
+
}
|
|
1192
|
+
this.lendingMarketType = sdk.LENDING_MARKET_TYPE;
|
|
1193
|
+
try {
|
|
1194
|
+
this.suilend = await sdk.SuilendClient.initialize(
|
|
1195
|
+
sdk.LENDING_MARKET_ID,
|
|
1196
|
+
sdk.LENDING_MARKET_TYPE,
|
|
1197
|
+
this.client
|
|
1198
|
+
);
|
|
1199
|
+
} catch (err) {
|
|
1200
|
+
this.initPromise = null;
|
|
1201
|
+
throw new T2000Error(
|
|
1202
|
+
"PROTOCOL_UNAVAILABLE",
|
|
1203
|
+
`Failed to initialize Suilend: ${err instanceof Error ? err.message : String(err)}`
|
|
1204
|
+
);
|
|
1205
|
+
}
|
|
1206
|
+
this.initialized = true;
|
|
1207
|
+
})();
|
|
1208
|
+
return this.initPromise;
|
|
1209
|
+
}
|
|
1210
|
+
async ensureInit() {
|
|
1211
|
+
if (!this.initialized) {
|
|
1212
|
+
await this.lazyInit();
|
|
1213
|
+
}
|
|
1214
|
+
}
|
|
1215
|
+
findReserve(asset) {
|
|
1216
|
+
const upper = asset.toUpperCase();
|
|
1217
|
+
let coinType;
|
|
1218
|
+
if (upper === "USDC") coinType = USDC_TYPE2;
|
|
1219
|
+
else if (upper === "SUI") coinType = "0x2::sui::SUI";
|
|
1220
|
+
else if (asset.includes("::")) coinType = asset;
|
|
1221
|
+
else return void 0;
|
|
1222
|
+
try {
|
|
1223
|
+
const normalized = utils.normalizeStructTag(coinType);
|
|
1224
|
+
return this.suilend.lendingMarket.reserves.find(
|
|
1225
|
+
(r) => utils.normalizeStructTag(r.coinType.name) === normalized
|
|
1226
|
+
);
|
|
1227
|
+
} catch {
|
|
1228
|
+
return void 0;
|
|
1229
|
+
}
|
|
1230
|
+
}
|
|
1231
|
+
async getObligationCaps(address) {
|
|
1232
|
+
const SuilendClientStatic = (await import('@suilend/sdk')).SuilendClient;
|
|
1233
|
+
return SuilendClientStatic.getObligationOwnerCaps(
|
|
1234
|
+
address,
|
|
1235
|
+
[this.lendingMarketType],
|
|
1236
|
+
this.client
|
|
1237
|
+
);
|
|
1238
|
+
}
|
|
1239
|
+
resolveSymbol(coinType) {
|
|
1240
|
+
const normalized = utils.normalizeStructTag(coinType);
|
|
1241
|
+
if (normalized === utils.normalizeStructTag(USDC_TYPE2)) return "USDC";
|
|
1242
|
+
if (normalized === utils.normalizeStructTag("0x2::sui::SUI")) return "SUI";
|
|
1243
|
+
const parts = coinType.split("::");
|
|
1244
|
+
return parts[parts.length - 1] || "UNKNOWN";
|
|
1245
|
+
}
|
|
1246
|
+
async getRates(asset) {
|
|
1247
|
+
await this.ensureInit();
|
|
1248
|
+
const reserve = this.findReserve(asset);
|
|
1249
|
+
if (!reserve) {
|
|
1250
|
+
throw new T2000Error("ASSET_NOT_SUPPORTED", `Suilend does not support ${asset}`);
|
|
1251
|
+
}
|
|
1252
|
+
const { borrowAprPct, depositAprPct } = computeRatesFromReserve(reserve);
|
|
1253
|
+
return {
|
|
1254
|
+
asset,
|
|
1255
|
+
saveApy: depositAprPct,
|
|
1256
|
+
borrowApy: borrowAprPct
|
|
1257
|
+
};
|
|
1258
|
+
}
|
|
1259
|
+
async getPositions(address) {
|
|
1260
|
+
await this.ensureInit();
|
|
1261
|
+
const supplies = [];
|
|
1262
|
+
const borrows = [];
|
|
1263
|
+
const caps = await this.getObligationCaps(address);
|
|
1264
|
+
if (caps.length === 0) return { supplies, borrows };
|
|
1265
|
+
const obligation = await this.suilend.getObligation(caps[0].obligationId);
|
|
1266
|
+
for (const deposit of obligation.deposits) {
|
|
1267
|
+
const coinType = utils.normalizeStructTag(deposit.coinType.name);
|
|
1268
|
+
const reserve = this.suilend.lendingMarket.reserves.find(
|
|
1269
|
+
(r) => utils.normalizeStructTag(r.coinType.name) === coinType
|
|
1270
|
+
);
|
|
1271
|
+
if (!reserve) continue;
|
|
1272
|
+
const ctokenAmount = Number(deposit.depositedCtokenAmount.toString());
|
|
1273
|
+
const ratio = cTokenRatio(reserve);
|
|
1274
|
+
const amount = ctokenAmount * ratio / 10 ** reserve.mintDecimals;
|
|
1275
|
+
const { depositAprPct } = computeRatesFromReserve(reserve);
|
|
1276
|
+
supplies.push({ asset: this.resolveSymbol(coinType), amount, apy: depositAprPct });
|
|
1277
|
+
}
|
|
1278
|
+
for (const borrow of obligation.borrows) {
|
|
1279
|
+
const coinType = utils.normalizeStructTag(borrow.coinType.name);
|
|
1280
|
+
const reserve = this.suilend.lendingMarket.reserves.find(
|
|
1281
|
+
(r) => utils.normalizeStructTag(r.coinType.name) === coinType
|
|
1282
|
+
);
|
|
1283
|
+
if (!reserve) continue;
|
|
1284
|
+
const rawBorrowed = Number(borrow.borrowedAmount.value.toString()) / WAD;
|
|
1285
|
+
const amount = rawBorrowed / 10 ** reserve.mintDecimals;
|
|
1286
|
+
const reserveRate = Number(reserve.cumulativeBorrowRate.value.toString()) / WAD;
|
|
1287
|
+
const posRate = Number(borrow.cumulativeBorrowRate.value.toString()) / WAD;
|
|
1288
|
+
const compounded = posRate > 0 ? amount * (reserveRate / posRate) : amount;
|
|
1289
|
+
const { borrowAprPct } = computeRatesFromReserve(reserve);
|
|
1290
|
+
borrows.push({ asset: this.resolveSymbol(coinType), amount: compounded, apy: borrowAprPct });
|
|
1291
|
+
}
|
|
1292
|
+
return { supplies, borrows };
|
|
1293
|
+
}
|
|
1294
|
+
async getHealth(address) {
|
|
1295
|
+
await this.ensureInit();
|
|
1296
|
+
const caps = await this.getObligationCaps(address);
|
|
1297
|
+
if (caps.length === 0) {
|
|
1298
|
+
return { healthFactor: Infinity, supplied: 0, borrowed: 0, maxBorrow: 0, liquidationThreshold: 0 };
|
|
1299
|
+
}
|
|
1300
|
+
const positions = await this.getPositions(address);
|
|
1301
|
+
const supplied = positions.supplies.reduce((s, p) => s + p.amount, 0);
|
|
1302
|
+
const borrowed = positions.borrows.reduce((s, p) => s + p.amount, 0);
|
|
1303
|
+
const reserve = this.findReserve("USDC");
|
|
1304
|
+
const closeLtv = reserve?.config?.element?.closeLtvPct ?? 75;
|
|
1305
|
+
const openLtv = reserve?.config?.element?.openLtvPct ?? 70;
|
|
1306
|
+
const liqThreshold = closeLtv / 100;
|
|
1307
|
+
const healthFactor = borrowed > 0 ? supplied * liqThreshold / borrowed : Infinity;
|
|
1308
|
+
const maxBorrow = Math.max(0, supplied * (openLtv / 100) - borrowed);
|
|
1309
|
+
return { healthFactor, supplied, borrowed, maxBorrow, liquidationThreshold: liqThreshold };
|
|
1310
|
+
}
|
|
1311
|
+
async buildSaveTx(address, amount, _asset, options) {
|
|
1312
|
+
await this.ensureInit();
|
|
1313
|
+
const rawAmount = usdcToRaw(amount).toString();
|
|
1314
|
+
const tx = new transactions.Transaction();
|
|
1315
|
+
tx.setSender(address);
|
|
1316
|
+
const caps = await this.getObligationCaps(address);
|
|
1317
|
+
let capRef;
|
|
1318
|
+
if (caps.length === 0) {
|
|
1319
|
+
const [newCap] = this.suilend.createObligation(tx);
|
|
1320
|
+
capRef = newCap;
|
|
1321
|
+
} else {
|
|
1322
|
+
capRef = caps[0].id;
|
|
1323
|
+
}
|
|
1324
|
+
const allCoins = await this.fetchAllCoins(address, USDC_TYPE2);
|
|
1325
|
+
if (allCoins.length === 0) {
|
|
1326
|
+
throw new T2000Error("INSUFFICIENT_BALANCE", "No USDC coins found");
|
|
1327
|
+
}
|
|
1328
|
+
const primaryCoinId = allCoins[0].coinObjectId;
|
|
1329
|
+
if (allCoins.length > 1) {
|
|
1330
|
+
tx.mergeCoins(
|
|
1331
|
+
tx.object(primaryCoinId),
|
|
1332
|
+
allCoins.slice(1).map((c) => tx.object(c.coinObjectId))
|
|
1333
|
+
);
|
|
1334
|
+
}
|
|
1335
|
+
const [depositCoin] = tx.splitCoins(tx.object(primaryCoinId), [rawAmount]);
|
|
1336
|
+
if (options?.collectFee) {
|
|
1337
|
+
addCollectFeeToTx(tx, depositCoin, "save");
|
|
1338
|
+
}
|
|
1339
|
+
this.suilend.deposit(depositCoin, USDC_TYPE2, capRef, tx);
|
|
1340
|
+
return { tx };
|
|
1341
|
+
}
|
|
1342
|
+
async buildWithdrawTx(address, amount, _asset) {
|
|
1343
|
+
await this.ensureInit();
|
|
1344
|
+
const caps = await this.getObligationCaps(address);
|
|
1345
|
+
if (caps.length === 0) {
|
|
1346
|
+
throw new T2000Error("NO_COLLATERAL", "No Suilend position found");
|
|
1347
|
+
}
|
|
1348
|
+
const positions = await this.getPositions(address);
|
|
1349
|
+
const usdcSupply = positions.supplies.find((s) => s.asset === "USDC");
|
|
1350
|
+
const deposited = usdcSupply?.amount ?? 0;
|
|
1351
|
+
const effectiveAmount = Math.min(amount, deposited);
|
|
1352
|
+
if (effectiveAmount <= 0) {
|
|
1353
|
+
throw new T2000Error("NO_COLLATERAL", "Nothing to withdraw from Suilend");
|
|
1354
|
+
}
|
|
1355
|
+
const rawAmount = usdcToRaw(effectiveAmount).toString();
|
|
1356
|
+
const tx = new transactions.Transaction();
|
|
1357
|
+
tx.setSender(address);
|
|
1358
|
+
await this.suilend.withdrawAndSendToUser(
|
|
1359
|
+
address,
|
|
1360
|
+
caps[0].id,
|
|
1361
|
+
caps[0].obligationId,
|
|
1362
|
+
USDC_TYPE2,
|
|
1363
|
+
rawAmount,
|
|
1364
|
+
tx
|
|
1365
|
+
);
|
|
1366
|
+
return { tx, effectiveAmount };
|
|
1367
|
+
}
|
|
1368
|
+
async buildBorrowTx(_address, _amount, _asset, _options) {
|
|
1369
|
+
throw new T2000Error(
|
|
1370
|
+
"ASSET_NOT_SUPPORTED",
|
|
1371
|
+
"SuilendAdapter.buildBorrowTx() not available \u2014 Suilend requires different collateral/borrow assets. Deferred to Phase 10."
|
|
1372
|
+
);
|
|
1373
|
+
}
|
|
1374
|
+
async buildRepayTx(_address, _amount, _asset) {
|
|
1375
|
+
throw new T2000Error(
|
|
1376
|
+
"ASSET_NOT_SUPPORTED",
|
|
1377
|
+
"SuilendAdapter.buildRepayTx() not available \u2014 deferred to Phase 10."
|
|
1378
|
+
);
|
|
1379
|
+
}
|
|
1380
|
+
async maxWithdraw(address, _asset) {
|
|
1381
|
+
await this.ensureInit();
|
|
1382
|
+
const health = await this.getHealth(address);
|
|
1383
|
+
let maxAmount;
|
|
1384
|
+
if (health.borrowed === 0) {
|
|
1385
|
+
maxAmount = health.supplied;
|
|
1386
|
+
} else {
|
|
1387
|
+
maxAmount = Math.max(
|
|
1388
|
+
0,
|
|
1389
|
+
health.supplied - health.borrowed * MIN_HEALTH_FACTOR2 / health.liquidationThreshold
|
|
1390
|
+
);
|
|
1391
|
+
}
|
|
1392
|
+
const remainingSupply = health.supplied - maxAmount;
|
|
1393
|
+
const hfAfter = health.borrowed > 0 ? remainingSupply * health.liquidationThreshold / health.borrowed : Infinity;
|
|
1394
|
+
return {
|
|
1395
|
+
maxAmount,
|
|
1396
|
+
healthFactorAfter: hfAfter,
|
|
1397
|
+
currentHF: health.healthFactor
|
|
1398
|
+
};
|
|
1399
|
+
}
|
|
1400
|
+
async maxBorrow(_address, _asset) {
|
|
1401
|
+
throw new T2000Error(
|
|
1402
|
+
"ASSET_NOT_SUPPORTED",
|
|
1403
|
+
"SuilendAdapter.maxBorrow() not available \u2014 deferred to Phase 10."
|
|
1404
|
+
);
|
|
1405
|
+
}
|
|
1406
|
+
async fetchAllCoins(owner, coinType) {
|
|
1407
|
+
const all = [];
|
|
1408
|
+
let cursor = null;
|
|
1409
|
+
let hasNext = true;
|
|
1410
|
+
while (hasNext) {
|
|
1411
|
+
const page = await this.client.getCoins({ owner, coinType, cursor: cursor ?? void 0 });
|
|
1412
|
+
all.push(...page.data.map((c) => ({ coinObjectId: c.coinObjectId, balance: c.balance })));
|
|
1413
|
+
cursor = page.nextCursor;
|
|
1414
|
+
hasNext = page.hasNextPage;
|
|
1415
|
+
}
|
|
1416
|
+
return all;
|
|
1417
|
+
}
|
|
1418
|
+
};
|
|
1119
1419
|
function hasLeadingZeroBits(hash, bits) {
|
|
1120
1420
|
const fullBytes = Math.floor(bits / 8);
|
|
1121
1421
|
const remainingBits = bits % 8;
|
|
@@ -1364,6 +1664,9 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
|
|
|
1364
1664
|
const cetusAdapter = new CetusAdapter();
|
|
1365
1665
|
cetusAdapter.initSync(client);
|
|
1366
1666
|
registry.registerSwap(cetusAdapter);
|
|
1667
|
+
const suilendAdapter = new SuilendAdapter();
|
|
1668
|
+
suilendAdapter.initSync(client);
|
|
1669
|
+
registry.registerLending(suilendAdapter);
|
|
1367
1670
|
return registry;
|
|
1368
1671
|
}
|
|
1369
1672
|
static async create(options = {}) {
|
|
@@ -1540,666 +1843,376 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
|
|
|
1540
1843
|
async withdraw(params) {
|
|
1541
1844
|
const asset = (params.asset ?? "USDC").toUpperCase();
|
|
1542
1845
|
if (asset !== "USDC") {
|
|
1543
|
-
throw new T2000Error("ASSET_NOT_SUPPORTED", `Only USDC is supported for withdraw. Got: ${asset}`);
|
|
1544
|
-
}
|
|
1545
|
-
const adapter = await this.resolveLending(params.protocol, asset, "withdraw");
|
|
1546
|
-
let amount;
|
|
1547
|
-
if (params.amount === "all") {
|
|
1548
|
-
const maxResult = await adapter.maxWithdraw(this._address, asset);
|
|
1549
|
-
amount = maxResult.maxAmount;
|
|
1550
|
-
if (amount <= 0) {
|
|
1551
|
-
throw new T2000Error("NO_COLLATERAL", "No savings to withdraw");
|
|
1552
|
-
}
|
|
1553
|
-
} else {
|
|
1554
|
-
amount = params.amount;
|
|
1555
|
-
const hf = await adapter.getHealth(this._address);
|
|
1556
|
-
if (hf.borrowed > 0) {
|
|
1557
|
-
const maxResult = await adapter.maxWithdraw(this._address, asset);
|
|
1558
|
-
if (amount > maxResult.maxAmount) {
|
|
1559
|
-
throw new T2000Error(
|
|
1560
|
-
"WITHDRAW_WOULD_LIQUIDATE",
|
|
1561
|
-
`Withdrawing $${amount.toFixed(2)} would drop health factor below 1.5`,
|
|
1562
|
-
{
|
|
1563
|
-
safeWithdrawAmount: maxResult.maxAmount,
|
|
1564
|
-
currentHF: maxResult.currentHF,
|
|
1565
|
-
projectedHF: maxResult.healthFactorAfter
|
|
1566
|
-
}
|
|
1567
|
-
);
|
|
1568
|
-
}
|
|
1569
|
-
}
|
|
1570
|
-
}
|
|
1571
|
-
const withdrawAmount = amount;
|
|
1572
|
-
let effectiveAmount = withdrawAmount;
|
|
1573
|
-
const gasResult = await executeWithGas(this.client, this.keypair, async () => {
|
|
1574
|
-
const built = await adapter.buildWithdrawTx(this._address, withdrawAmount, asset);
|
|
1575
|
-
effectiveAmount = built.effectiveAmount;
|
|
1576
|
-
return built.tx;
|
|
1577
|
-
});
|
|
1578
|
-
this.emitBalanceChange("USDC", effectiveAmount, "withdraw", gasResult.digest);
|
|
1579
|
-
return {
|
|
1580
|
-
success: true,
|
|
1581
|
-
tx: gasResult.digest,
|
|
1582
|
-
amount: effectiveAmount,
|
|
1583
|
-
gasCost: gasResult.gasCostSui,
|
|
1584
|
-
gasMethod: gasResult.gasMethod
|
|
1585
|
-
};
|
|
1586
|
-
}
|
|
1587
|
-
async maxWithdraw() {
|
|
1588
|
-
const adapter = await this.resolveLending(void 0, "USDC", "withdraw");
|
|
1589
|
-
return adapter.maxWithdraw(this._address, "USDC");
|
|
1590
|
-
}
|
|
1591
|
-
// -- Borrowing --
|
|
1592
|
-
async borrow(params) {
|
|
1593
|
-
const asset = (params.asset ?? "USDC").toUpperCase();
|
|
1594
|
-
if (asset !== "USDC") {
|
|
1595
|
-
throw new T2000Error("ASSET_NOT_SUPPORTED", `Only USDC is supported for borrow. Got: ${asset}`);
|
|
1596
|
-
}
|
|
1597
|
-
const adapter = await this.resolveLending(params.protocol, asset, "borrow");
|
|
1598
|
-
const maxResult = await adapter.maxBorrow(this._address, asset);
|
|
1599
|
-
if (params.amount > maxResult.maxAmount) {
|
|
1600
|
-
throw new T2000Error("HEALTH_FACTOR_TOO_LOW", `Max safe borrow: $${maxResult.maxAmount.toFixed(2)}`, {
|
|
1601
|
-
maxBorrow: maxResult.maxAmount,
|
|
1602
|
-
currentHF: maxResult.currentHF
|
|
1603
|
-
});
|
|
1604
|
-
}
|
|
1605
|
-
const fee = calculateFee("borrow", params.amount);
|
|
1606
|
-
const borrowAmount = params.amount;
|
|
1607
|
-
const gasResult = await executeWithGas(this.client, this.keypair, async () => {
|
|
1608
|
-
const { tx } = await adapter.buildBorrowTx(this._address, borrowAmount, asset, { collectFee: true });
|
|
1609
|
-
return tx;
|
|
1610
|
-
});
|
|
1611
|
-
const hf = await adapter.getHealth(this._address);
|
|
1612
|
-
reportFee(this._address, "borrow", fee.amount, fee.rate, gasResult.digest);
|
|
1613
|
-
this.emitBalanceChange("USDC", borrowAmount, "borrow", gasResult.digest);
|
|
1614
|
-
return {
|
|
1615
|
-
success: true,
|
|
1616
|
-
tx: gasResult.digest,
|
|
1617
|
-
amount: borrowAmount,
|
|
1618
|
-
fee: fee.amount,
|
|
1619
|
-
healthFactor: hf.healthFactor,
|
|
1620
|
-
gasCost: gasResult.gasCostSui,
|
|
1621
|
-
gasMethod: gasResult.gasMethod
|
|
1622
|
-
};
|
|
1623
|
-
}
|
|
1624
|
-
async repay(params) {
|
|
1625
|
-
const asset = (params.asset ?? "USDC").toUpperCase();
|
|
1626
|
-
if (asset !== "USDC") {
|
|
1627
|
-
throw new T2000Error("ASSET_NOT_SUPPORTED", `Only USDC is supported for repay. Got: ${asset}`);
|
|
1628
|
-
}
|
|
1629
|
-
const adapter = await this.resolveLending(params.protocol, asset, "repay");
|
|
1630
|
-
let amount;
|
|
1631
|
-
if (params.amount === "all") {
|
|
1632
|
-
const hf2 = await adapter.getHealth(this._address);
|
|
1633
|
-
amount = hf2.borrowed;
|
|
1634
|
-
if (amount <= 0) {
|
|
1635
|
-
throw new T2000Error("NO_COLLATERAL", "No outstanding borrow to repay");
|
|
1636
|
-
}
|
|
1637
|
-
} else {
|
|
1638
|
-
amount = params.amount;
|
|
1639
|
-
}
|
|
1640
|
-
const repayAmount = amount;
|
|
1641
|
-
const gasResult = await executeWithGas(this.client, this.keypair, async () => {
|
|
1642
|
-
const { tx } = await adapter.buildRepayTx(this._address, repayAmount, asset);
|
|
1643
|
-
return tx;
|
|
1644
|
-
});
|
|
1645
|
-
const hf = await adapter.getHealth(this._address);
|
|
1646
|
-
this.emitBalanceChange("USDC", repayAmount, "repay", gasResult.digest);
|
|
1647
|
-
return {
|
|
1648
|
-
success: true,
|
|
1649
|
-
tx: gasResult.digest,
|
|
1650
|
-
amount: repayAmount,
|
|
1651
|
-
remainingDebt: hf.borrowed,
|
|
1652
|
-
gasCost: gasResult.gasCostSui,
|
|
1653
|
-
gasMethod: gasResult.gasMethod
|
|
1654
|
-
};
|
|
1655
|
-
}
|
|
1656
|
-
async maxBorrow() {
|
|
1657
|
-
const adapter = await this.resolveLending(void 0, "USDC", "borrow");
|
|
1658
|
-
return adapter.maxBorrow(this._address, "USDC");
|
|
1659
|
-
}
|
|
1660
|
-
async healthFactor() {
|
|
1661
|
-
const adapter = await this.resolveLending(void 0, "USDC", "save");
|
|
1662
|
-
const hf = await adapter.getHealth(this._address);
|
|
1663
|
-
if (hf.healthFactor < 1.2) {
|
|
1664
|
-
this.emit("healthCritical", { healthFactor: hf.healthFactor, threshold: 1.5, severity: "critical" });
|
|
1665
|
-
} else if (hf.healthFactor < 2) {
|
|
1666
|
-
this.emit("healthWarning", { healthFactor: hf.healthFactor, threshold: 2, severity: "warning" });
|
|
1667
|
-
}
|
|
1668
|
-
return hf;
|
|
1669
|
-
}
|
|
1670
|
-
// -- Swap --
|
|
1671
|
-
async swap(params) {
|
|
1672
|
-
const fromAsset = params.from.toUpperCase();
|
|
1673
|
-
const toAsset = params.to.toUpperCase();
|
|
1674
|
-
if (!(fromAsset in SUPPORTED_ASSETS) || !(toAsset in SUPPORTED_ASSETS)) {
|
|
1675
|
-
throw new T2000Error("ASSET_NOT_SUPPORTED", `Swap pair ${fromAsset}/${toAsset} is not supported`);
|
|
1676
|
-
}
|
|
1677
|
-
if (fromAsset === toAsset) {
|
|
1678
|
-
throw new T2000Error("INVALID_AMOUNT", "Cannot swap same asset");
|
|
1679
|
-
}
|
|
1680
|
-
let adapter;
|
|
1681
|
-
if (params.protocol) {
|
|
1682
|
-
const found = this.registry.getSwap(params.protocol);
|
|
1683
|
-
if (!found) throw new T2000Error("ASSET_NOT_SUPPORTED", `Swap adapter '${params.protocol}' not found`);
|
|
1684
|
-
adapter = found;
|
|
1846
|
+
throw new T2000Error("ASSET_NOT_SUPPORTED", `Only USDC is supported for withdraw. Got: ${asset}`);
|
|
1847
|
+
}
|
|
1848
|
+
const adapter = await this.resolveLending(params.protocol, asset, "withdraw");
|
|
1849
|
+
let amount;
|
|
1850
|
+
if (params.amount === "all") {
|
|
1851
|
+
const maxResult = await adapter.maxWithdraw(this._address, asset);
|
|
1852
|
+
amount = maxResult.maxAmount;
|
|
1853
|
+
if (amount <= 0) {
|
|
1854
|
+
throw new T2000Error("NO_COLLATERAL", "No savings to withdraw");
|
|
1855
|
+
}
|
|
1685
1856
|
} else {
|
|
1686
|
-
|
|
1687
|
-
|
|
1857
|
+
amount = params.amount;
|
|
1858
|
+
const hf = await adapter.getHealth(this._address);
|
|
1859
|
+
if (hf.borrowed > 0) {
|
|
1860
|
+
const maxResult = await adapter.maxWithdraw(this._address, asset);
|
|
1861
|
+
if (amount > maxResult.maxAmount) {
|
|
1862
|
+
throw new T2000Error(
|
|
1863
|
+
"WITHDRAW_WOULD_LIQUIDATE",
|
|
1864
|
+
`Withdrawing $${amount.toFixed(2)} would drop health factor below 1.5`,
|
|
1865
|
+
{
|
|
1866
|
+
safeWithdrawAmount: maxResult.maxAmount,
|
|
1867
|
+
currentHF: maxResult.currentHF,
|
|
1868
|
+
projectedHF: maxResult.healthFactorAfter
|
|
1869
|
+
}
|
|
1870
|
+
);
|
|
1871
|
+
}
|
|
1872
|
+
}
|
|
1688
1873
|
}
|
|
1689
|
-
const
|
|
1690
|
-
|
|
1691
|
-
const slippageBps = params.maxSlippage ? params.maxSlippage * 100 : void 0;
|
|
1692
|
-
let swapMeta = { estimatedOut: 0, toDecimals: 0 };
|
|
1874
|
+
const withdrawAmount = amount;
|
|
1875
|
+
let effectiveAmount = withdrawAmount;
|
|
1693
1876
|
const gasResult = await executeWithGas(this.client, this.keypair, async () => {
|
|
1694
|
-
const built = await adapter.
|
|
1695
|
-
|
|
1877
|
+
const built = await adapter.buildWithdrawTx(this._address, withdrawAmount, asset);
|
|
1878
|
+
effectiveAmount = built.effectiveAmount;
|
|
1696
1879
|
return built.tx;
|
|
1697
1880
|
});
|
|
1698
|
-
|
|
1699
|
-
await this.client.waitForTransaction({ digest: gasResult.digest });
|
|
1700
|
-
const txDetail = await this.client.getTransactionBlock({
|
|
1701
|
-
digest: gasResult.digest,
|
|
1702
|
-
options: { showBalanceChanges: true }
|
|
1703
|
-
});
|
|
1704
|
-
let actualReceived = 0;
|
|
1705
|
-
if (txDetail.balanceChanges) {
|
|
1706
|
-
for (const change of txDetail.balanceChanges) {
|
|
1707
|
-
if (change.coinType === toInfo.type && change.owner && typeof change.owner === "object" && "AddressOwner" in change.owner && change.owner.AddressOwner === this._address) {
|
|
1708
|
-
const amt = Number(change.amount) / 10 ** toInfo.decimals;
|
|
1709
|
-
if (amt > 0) actualReceived += amt;
|
|
1710
|
-
}
|
|
1711
|
-
}
|
|
1712
|
-
}
|
|
1713
|
-
const expectedOutput = swapMeta.estimatedOut / 10 ** swapMeta.toDecimals;
|
|
1714
|
-
if (actualReceived === 0) actualReceived = expectedOutput;
|
|
1715
|
-
const priceImpact = expectedOutput > 0 ? Math.abs(actualReceived - expectedOutput) / expectedOutput : 0;
|
|
1716
|
-
reportFee(this._address, "swap", fee.amount, fee.rate, gasResult.digest);
|
|
1717
|
-
this.emitBalanceChange(fromAsset, swapAmount, "swap", gasResult.digest);
|
|
1881
|
+
this.emitBalanceChange("USDC", effectiveAmount, "withdraw", gasResult.digest);
|
|
1718
1882
|
return {
|
|
1719
1883
|
success: true,
|
|
1720
1884
|
tx: gasResult.digest,
|
|
1721
|
-
|
|
1722
|
-
fromAsset,
|
|
1723
|
-
toAmount: actualReceived,
|
|
1724
|
-
toAsset,
|
|
1725
|
-
priceImpact,
|
|
1726
|
-
fee: fee.amount,
|
|
1885
|
+
amount: effectiveAmount,
|
|
1727
1886
|
gasCost: gasResult.gasCostSui,
|
|
1728
1887
|
gasMethod: gasResult.gasMethod
|
|
1729
1888
|
};
|
|
1730
1889
|
}
|
|
1731
|
-
async
|
|
1732
|
-
const
|
|
1733
|
-
|
|
1734
|
-
const best = await this.registry.bestSwapQuote(fromAsset, toAsset, params.amount);
|
|
1735
|
-
const fee = calculateFee("swap", params.amount);
|
|
1736
|
-
return { ...best.quote, fee: { amount: fee.amount, rate: fee.rate } };
|
|
1737
|
-
}
|
|
1738
|
-
// -- Info --
|
|
1739
|
-
async positions() {
|
|
1740
|
-
const allPositions = await this.registry.allPositions(this._address);
|
|
1741
|
-
const positions = allPositions.flatMap(
|
|
1742
|
-
(p) => [
|
|
1743
|
-
...p.positions.supplies.map((s) => ({
|
|
1744
|
-
protocol: p.protocolId,
|
|
1745
|
-
asset: s.asset,
|
|
1746
|
-
type: "save",
|
|
1747
|
-
amount: s.amount,
|
|
1748
|
-
apy: s.apy
|
|
1749
|
-
})),
|
|
1750
|
-
...p.positions.borrows.map((b) => ({
|
|
1751
|
-
protocol: p.protocolId,
|
|
1752
|
-
asset: b.asset,
|
|
1753
|
-
type: "borrow",
|
|
1754
|
-
amount: b.amount,
|
|
1755
|
-
apy: b.apy
|
|
1756
|
-
}))
|
|
1757
|
-
]
|
|
1758
|
-
);
|
|
1759
|
-
return { positions };
|
|
1760
|
-
}
|
|
1761
|
-
async rates() {
|
|
1762
|
-
return getRates(this.client);
|
|
1763
|
-
}
|
|
1764
|
-
async allRates(asset = "USDC") {
|
|
1765
|
-
return this.registry.allRates(asset);
|
|
1766
|
-
}
|
|
1767
|
-
async earnings() {
|
|
1768
|
-
const result = await getEarnings(this.client, this.keypair);
|
|
1769
|
-
if (result.totalYieldEarned > 0) {
|
|
1770
|
-
this.emit("yield", {
|
|
1771
|
-
earned: result.dailyEarning,
|
|
1772
|
-
total: result.totalYieldEarned,
|
|
1773
|
-
apy: result.currentApy / 100,
|
|
1774
|
-
timestamp: Date.now()
|
|
1775
|
-
});
|
|
1776
|
-
}
|
|
1777
|
-
return result;
|
|
1778
|
-
}
|
|
1779
|
-
async fundStatus() {
|
|
1780
|
-
return getFundStatus(this.client, this.keypair);
|
|
1781
|
-
}
|
|
1782
|
-
// -- Sentinel --
|
|
1783
|
-
async sentinelList() {
|
|
1784
|
-
return listSentinels();
|
|
1785
|
-
}
|
|
1786
|
-
async sentinelInfo(id) {
|
|
1787
|
-
return getSentinelInfo(this.client, id);
|
|
1788
|
-
}
|
|
1789
|
-
async sentinelAttack(id, prompt, fee) {
|
|
1790
|
-
return attack(this.client, this.keypair, id, prompt, fee);
|
|
1890
|
+
async maxWithdraw() {
|
|
1891
|
+
const adapter = await this.resolveLending(void 0, "USDC", "withdraw");
|
|
1892
|
+
return adapter.maxWithdraw(this._address, "USDC");
|
|
1791
1893
|
}
|
|
1792
|
-
// --
|
|
1793
|
-
async
|
|
1794
|
-
|
|
1795
|
-
|
|
1796
|
-
|
|
1797
|
-
return adapter;
|
|
1798
|
-
}
|
|
1799
|
-
if (capability === "save") {
|
|
1800
|
-
const { adapter } = await this.registry.bestSaveRate(asset);
|
|
1801
|
-
return adapter;
|
|
1802
|
-
}
|
|
1803
|
-
if (capability === "borrow" || capability === "repay") {
|
|
1804
|
-
const adapters2 = this.registry.listLending().filter(
|
|
1805
|
-
(a) => a.supportedAssets.includes(asset) && a.capabilities.includes(capability) && (capability !== "borrow" || a.supportsSameAssetBorrow)
|
|
1806
|
-
);
|
|
1807
|
-
if (adapters2.length === 0) throw new T2000Error("ASSET_NOT_SUPPORTED", `No adapter supports ${capability} ${asset}`);
|
|
1808
|
-
return adapters2[0];
|
|
1894
|
+
// -- Borrowing --
|
|
1895
|
+
async borrow(params) {
|
|
1896
|
+
const asset = (params.asset ?? "USDC").toUpperCase();
|
|
1897
|
+
if (asset !== "USDC") {
|
|
1898
|
+
throw new T2000Error("ASSET_NOT_SUPPORTED", `Only USDC is supported for borrow. Got: ${asset}`);
|
|
1809
1899
|
}
|
|
1810
|
-
const
|
|
1811
|
-
|
|
1812
|
-
)
|
|
1813
|
-
|
|
1814
|
-
|
|
1815
|
-
|
|
1816
|
-
emitBalanceChange(asset, amount, cause, tx) {
|
|
1817
|
-
this.emit("balanceChange", { asset, previous: 0, current: 0, cause, tx });
|
|
1818
|
-
}
|
|
1819
|
-
};
|
|
1820
|
-
async function callSponsorApi(address, name) {
|
|
1821
|
-
const res = await fetch(`${API_BASE_URL}/api/sponsor`, {
|
|
1822
|
-
method: "POST",
|
|
1823
|
-
headers: { "Content-Type": "application/json" },
|
|
1824
|
-
body: JSON.stringify({ address, name })
|
|
1825
|
-
});
|
|
1826
|
-
if (res.status === 429) {
|
|
1827
|
-
const data = await res.json();
|
|
1828
|
-
if (data.challenge) {
|
|
1829
|
-
const proof = solveHashcash(data.challenge);
|
|
1830
|
-
const retry = await fetch(`${API_BASE_URL}/api/sponsor`, {
|
|
1831
|
-
method: "POST",
|
|
1832
|
-
headers: { "Content-Type": "application/json" },
|
|
1833
|
-
body: JSON.stringify({ address, name, proof })
|
|
1900
|
+
const adapter = await this.resolveLending(params.protocol, asset, "borrow");
|
|
1901
|
+
const maxResult = await adapter.maxBorrow(this._address, asset);
|
|
1902
|
+
if (params.amount > maxResult.maxAmount) {
|
|
1903
|
+
throw new T2000Error("HEALTH_FACTOR_TOO_LOW", `Max safe borrow: $${maxResult.maxAmount.toFixed(2)}`, {
|
|
1904
|
+
maxBorrow: maxResult.maxAmount,
|
|
1905
|
+
currentHF: maxResult.currentHF
|
|
1834
1906
|
});
|
|
1835
|
-
if (!retry.ok) throw new T2000Error("SPONSOR_RATE_LIMITED", "Sponsor rate limited");
|
|
1836
|
-
return;
|
|
1837
1907
|
}
|
|
1838
|
-
|
|
1839
|
-
|
|
1840
|
-
|
|
1841
|
-
|
|
1842
|
-
|
|
1843
|
-
|
|
1844
|
-
// src/utils/simulate.ts
|
|
1845
|
-
async function simulateTransaction(client, tx, sender) {
|
|
1846
|
-
tx.setSender(sender);
|
|
1847
|
-
try {
|
|
1848
|
-
const txBytes = await tx.build({ client });
|
|
1849
|
-
const dryRun = await client.dryRunTransactionBlock({
|
|
1850
|
-
transactionBlock: Buffer.from(txBytes).toString("base64")
|
|
1908
|
+
const fee = calculateFee("borrow", params.amount);
|
|
1909
|
+
const borrowAmount = params.amount;
|
|
1910
|
+
const gasResult = await executeWithGas(this.client, this.keypair, async () => {
|
|
1911
|
+
const { tx } = await adapter.buildBorrowTx(this._address, borrowAmount, asset, { collectFee: true });
|
|
1912
|
+
return tx;
|
|
1851
1913
|
});
|
|
1852
|
-
const
|
|
1853
|
-
|
|
1854
|
-
|
|
1855
|
-
if (status?.status === "failure") {
|
|
1856
|
-
const rawError = status.error ?? "Unknown simulation error";
|
|
1857
|
-
const parsed = parseMoveAbort(rawError);
|
|
1858
|
-
return {
|
|
1859
|
-
success: false,
|
|
1860
|
-
gasEstimateSui,
|
|
1861
|
-
error: {
|
|
1862
|
-
moveAbortCode: parsed.abortCode,
|
|
1863
|
-
moveModule: parsed.module,
|
|
1864
|
-
reason: parsed.reason,
|
|
1865
|
-
rawError
|
|
1866
|
-
}
|
|
1867
|
-
};
|
|
1868
|
-
}
|
|
1869
|
-
return { success: true, gasEstimateSui };
|
|
1870
|
-
} catch (err) {
|
|
1871
|
-
const rawError = err instanceof Error ? err.message : String(err);
|
|
1914
|
+
const hf = await adapter.getHealth(this._address);
|
|
1915
|
+
reportFee(this._address, "borrow", fee.amount, fee.rate, gasResult.digest);
|
|
1916
|
+
this.emitBalanceChange("USDC", borrowAmount, "borrow", gasResult.digest);
|
|
1872
1917
|
return {
|
|
1873
|
-
success:
|
|
1874
|
-
|
|
1875
|
-
|
|
1876
|
-
|
|
1877
|
-
|
|
1878
|
-
|
|
1879
|
-
|
|
1880
|
-
|
|
1881
|
-
}
|
|
1882
|
-
function throwIfSimulationFailed(sim) {
|
|
1883
|
-
if (sim.success) return;
|
|
1884
|
-
throw new T2000Error(
|
|
1885
|
-
"SIMULATION_FAILED",
|
|
1886
|
-
sim.error?.reason ?? "Transaction simulation failed",
|
|
1887
|
-
{
|
|
1888
|
-
moveAbortCode: sim.error?.moveAbortCode,
|
|
1889
|
-
moveModule: sim.error?.moveModule,
|
|
1890
|
-
reason: sim.error?.reason,
|
|
1891
|
-
rawError: sim.error?.rawError
|
|
1892
|
-
}
|
|
1893
|
-
);
|
|
1894
|
-
}
|
|
1895
|
-
function parseMoveAbort(errorStr) {
|
|
1896
|
-
const abortMatch = errorStr.match(/MoveAbort\([^,]*,\s*(\d+)\)/);
|
|
1897
|
-
const moduleMatch = errorStr.match(/name:\s*Identifier\("([^"]+)"\)/);
|
|
1898
|
-
if (abortMatch) {
|
|
1899
|
-
const code = parseInt(abortMatch[1], 10);
|
|
1900
|
-
const module = moduleMatch?.[1];
|
|
1901
|
-
const reason = mapMoveAbortCode(code);
|
|
1902
|
-
return { abortCode: code, module, reason };
|
|
1918
|
+
success: true,
|
|
1919
|
+
tx: gasResult.digest,
|
|
1920
|
+
amount: borrowAmount,
|
|
1921
|
+
fee: fee.amount,
|
|
1922
|
+
healthFactor: hf.healthFactor,
|
|
1923
|
+
gasCost: gasResult.gasCostSui,
|
|
1924
|
+
gasMethod: gasResult.gasMethod
|
|
1925
|
+
};
|
|
1903
1926
|
}
|
|
1904
|
-
|
|
1905
|
-
const
|
|
1927
|
+
async repay(params) {
|
|
1928
|
+
const asset = (params.asset ?? "USDC").toUpperCase();
|
|
1929
|
+
if (asset !== "USDC") {
|
|
1930
|
+
throw new T2000Error("ASSET_NOT_SUPPORTED", `Only USDC is supported for repay. Got: ${asset}`);
|
|
1931
|
+
}
|
|
1932
|
+
const adapter = await this.resolveLending(params.protocol, asset, "repay");
|
|
1933
|
+
let amount;
|
|
1934
|
+
if (params.amount === "all") {
|
|
1935
|
+
const hf2 = await adapter.getHealth(this._address);
|
|
1936
|
+
amount = hf2.borrowed;
|
|
1937
|
+
if (amount <= 0) {
|
|
1938
|
+
throw new T2000Error("NO_COLLATERAL", "No outstanding borrow to repay");
|
|
1939
|
+
}
|
|
1940
|
+
} else {
|
|
1941
|
+
amount = params.amount;
|
|
1942
|
+
}
|
|
1943
|
+
const repayAmount = amount;
|
|
1944
|
+
const gasResult = await executeWithGas(this.client, this.keypair, async () => {
|
|
1945
|
+
const { tx } = await adapter.buildRepayTx(this._address, repayAmount, asset);
|
|
1946
|
+
return tx;
|
|
1947
|
+
});
|
|
1948
|
+
const hf = await adapter.getHealth(this._address);
|
|
1949
|
+
this.emitBalanceChange("USDC", repayAmount, "repay", gasResult.digest);
|
|
1906
1950
|
return {
|
|
1907
|
-
|
|
1908
|
-
|
|
1951
|
+
success: true,
|
|
1952
|
+
tx: gasResult.digest,
|
|
1953
|
+
amount: repayAmount,
|
|
1954
|
+
remainingDebt: hf.borrowed,
|
|
1955
|
+
gasCost: gasResult.gasCostSui,
|
|
1956
|
+
gasMethod: gasResult.gasMethod
|
|
1909
1957
|
};
|
|
1910
1958
|
}
|
|
1911
|
-
|
|
1912
|
-
|
|
1913
|
-
|
|
1914
|
-
SUPPORTED_ASSETS.USDC.decimals;
|
|
1915
|
-
var WAD = 1e18;
|
|
1916
|
-
var MIN_HEALTH_FACTOR2 = 1.5;
|
|
1917
|
-
function interpolateRate(utilBreakpoints, aprBreakpoints, utilizationPct) {
|
|
1918
|
-
if (utilBreakpoints.length === 0) return 0;
|
|
1919
|
-
if (utilizationPct <= utilBreakpoints[0]) return aprBreakpoints[0];
|
|
1920
|
-
if (utilizationPct >= utilBreakpoints[utilBreakpoints.length - 1]) {
|
|
1921
|
-
return aprBreakpoints[aprBreakpoints.length - 1];
|
|
1959
|
+
async maxBorrow() {
|
|
1960
|
+
const adapter = await this.resolveLending(void 0, "USDC", "borrow");
|
|
1961
|
+
return adapter.maxBorrow(this._address, "USDC");
|
|
1922
1962
|
}
|
|
1923
|
-
|
|
1924
|
-
|
|
1925
|
-
|
|
1926
|
-
|
|
1963
|
+
async healthFactor() {
|
|
1964
|
+
const adapter = await this.resolveLending(void 0, "USDC", "save");
|
|
1965
|
+
const hf = await adapter.getHealth(this._address);
|
|
1966
|
+
if (hf.healthFactor < 1.2) {
|
|
1967
|
+
this.emit("healthCritical", { healthFactor: hf.healthFactor, threshold: 1.5, severity: "critical" });
|
|
1968
|
+
} else if (hf.healthFactor < 2) {
|
|
1969
|
+
this.emit("healthWarning", { healthFactor: hf.healthFactor, threshold: 2, severity: "warning" });
|
|
1927
1970
|
}
|
|
1971
|
+
return hf;
|
|
1928
1972
|
}
|
|
1929
|
-
|
|
1930
|
-
|
|
1931
|
-
|
|
1932
|
-
|
|
1933
|
-
|
|
1934
|
-
|
|
1935
|
-
const totalDeposited = available + borrowed;
|
|
1936
|
-
const utilizationPct = totalDeposited > 0 ? borrowed / totalDeposited * 100 : 0;
|
|
1937
|
-
const config = reserve.config.element;
|
|
1938
|
-
if (!config) return { borrowAprPct: 0, depositAprPct: 0, utilizationPct: 0 };
|
|
1939
|
-
const utils = config.interestRateUtils.map(Number);
|
|
1940
|
-
const aprs = config.interestRateAprs.map((a) => Number(a) / 100);
|
|
1941
|
-
const borrowAprPct = interpolateRate(utils, aprs, utilizationPct);
|
|
1942
|
-
const spreadFeeBps = Number(config.spreadFeeBps);
|
|
1943
|
-
const depositAprPct = utilizationPct / 100 * (borrowAprPct / 100) * (1 - spreadFeeBps / 1e4) * 100;
|
|
1944
|
-
return { borrowAprPct, depositAprPct, utilizationPct };
|
|
1945
|
-
}
|
|
1946
|
-
function cTokenRatio(reserve) {
|
|
1947
|
-
if (reserve.ctokenSupply === 0n) return 1;
|
|
1948
|
-
const available = Number(reserve.availableAmount);
|
|
1949
|
-
const borrowed = Number(reserve.borrowedAmount.value) / WAD;
|
|
1950
|
-
const spreadFees = Number(reserve.unclaimedSpreadFees.value) / WAD;
|
|
1951
|
-
const totalSupply = available + borrowed - spreadFees;
|
|
1952
|
-
return totalSupply / Number(reserve.ctokenSupply);
|
|
1953
|
-
}
|
|
1954
|
-
var SuilendAdapter = class {
|
|
1955
|
-
id = "suilend";
|
|
1956
|
-
name = "Suilend";
|
|
1957
|
-
version = "1.0.0";
|
|
1958
|
-
capabilities = ["save", "withdraw"];
|
|
1959
|
-
supportedAssets = ["USDC"];
|
|
1960
|
-
supportsSameAssetBorrow = false;
|
|
1961
|
-
client;
|
|
1962
|
-
suilend;
|
|
1963
|
-
lendingMarketType;
|
|
1964
|
-
initialized = false;
|
|
1965
|
-
async init(client) {
|
|
1966
|
-
let sdk;
|
|
1967
|
-
try {
|
|
1968
|
-
sdk = await import('@suilend/sdk');
|
|
1969
|
-
} catch {
|
|
1970
|
-
throw new T2000Error(
|
|
1971
|
-
"PROTOCOL_UNAVAILABLE",
|
|
1972
|
-
"Suilend SDK not installed. Run: npm install @suilend/sdk@^1"
|
|
1973
|
-
);
|
|
1974
|
-
}
|
|
1975
|
-
this.client = client;
|
|
1976
|
-
this.lendingMarketType = sdk.LENDING_MARKET_TYPE;
|
|
1977
|
-
try {
|
|
1978
|
-
this.suilend = await sdk.SuilendClient.initialize(
|
|
1979
|
-
sdk.LENDING_MARKET_ID,
|
|
1980
|
-
sdk.LENDING_MARKET_TYPE,
|
|
1981
|
-
client
|
|
1982
|
-
);
|
|
1983
|
-
} catch (err) {
|
|
1984
|
-
throw new T2000Error(
|
|
1985
|
-
"PROTOCOL_UNAVAILABLE",
|
|
1986
|
-
`Failed to initialize Suilend: ${err instanceof Error ? err.message : String(err)}`
|
|
1987
|
-
);
|
|
1973
|
+
// -- Swap --
|
|
1974
|
+
async swap(params) {
|
|
1975
|
+
const fromAsset = params.from.toUpperCase();
|
|
1976
|
+
const toAsset = params.to.toUpperCase();
|
|
1977
|
+
if (!(fromAsset in SUPPORTED_ASSETS) || !(toAsset in SUPPORTED_ASSETS)) {
|
|
1978
|
+
throw new T2000Error("ASSET_NOT_SUPPORTED", `Swap pair ${fromAsset}/${toAsset} is not supported`);
|
|
1988
1979
|
}
|
|
1989
|
-
|
|
1990
|
-
|
|
1991
|
-
ensureInit() {
|
|
1992
|
-
if (!this.initialized) {
|
|
1993
|
-
throw new T2000Error(
|
|
1994
|
-
"PROTOCOL_UNAVAILABLE",
|
|
1995
|
-
"SuilendAdapter not initialized. Call init() first."
|
|
1996
|
-
);
|
|
1980
|
+
if (fromAsset === toAsset) {
|
|
1981
|
+
throw new T2000Error("INVALID_AMOUNT", "Cannot swap same asset");
|
|
1997
1982
|
}
|
|
1998
|
-
|
|
1999
|
-
|
|
2000
|
-
|
|
2001
|
-
|
|
2002
|
-
|
|
2003
|
-
else
|
|
2004
|
-
|
|
2005
|
-
|
|
2006
|
-
try {
|
|
2007
|
-
const normalized = utils.normalizeStructTag(coinType);
|
|
2008
|
-
return this.suilend.lendingMarket.reserves.find(
|
|
2009
|
-
(r) => utils.normalizeStructTag(r.coinType.name) === normalized
|
|
2010
|
-
);
|
|
2011
|
-
} catch {
|
|
2012
|
-
return void 0;
|
|
1983
|
+
let adapter;
|
|
1984
|
+
if (params.protocol) {
|
|
1985
|
+
const found = this.registry.getSwap(params.protocol);
|
|
1986
|
+
if (!found) throw new T2000Error("ASSET_NOT_SUPPORTED", `Swap adapter '${params.protocol}' not found`);
|
|
1987
|
+
adapter = found;
|
|
1988
|
+
} else {
|
|
1989
|
+
const best = await this.registry.bestSwapQuote(fromAsset, toAsset, params.amount);
|
|
1990
|
+
adapter = best.adapter;
|
|
2013
1991
|
}
|
|
2014
|
-
|
|
2015
|
-
|
|
2016
|
-
const
|
|
2017
|
-
|
|
2018
|
-
|
|
2019
|
-
|
|
2020
|
-
|
|
2021
|
-
|
|
2022
|
-
|
|
2023
|
-
|
|
2024
|
-
|
|
2025
|
-
|
|
2026
|
-
|
|
2027
|
-
|
|
2028
|
-
|
|
2029
|
-
|
|
2030
|
-
|
|
2031
|
-
|
|
2032
|
-
|
|
2033
|
-
|
|
2034
|
-
|
|
1992
|
+
const fee = calculateFee("swap", params.amount);
|
|
1993
|
+
const swapAmount = params.amount;
|
|
1994
|
+
const slippageBps = params.maxSlippage ? params.maxSlippage * 100 : void 0;
|
|
1995
|
+
let swapMeta = { estimatedOut: 0, toDecimals: 0 };
|
|
1996
|
+
const gasResult = await executeWithGas(this.client, this.keypair, async () => {
|
|
1997
|
+
const built = await adapter.buildSwapTx(this._address, fromAsset, toAsset, swapAmount, slippageBps);
|
|
1998
|
+
swapMeta = { estimatedOut: built.estimatedOut, toDecimals: built.toDecimals };
|
|
1999
|
+
return built.tx;
|
|
2000
|
+
});
|
|
2001
|
+
const toInfo = SUPPORTED_ASSETS[toAsset];
|
|
2002
|
+
await this.client.waitForTransaction({ digest: gasResult.digest });
|
|
2003
|
+
const txDetail = await this.client.getTransactionBlock({
|
|
2004
|
+
digest: gasResult.digest,
|
|
2005
|
+
options: { showBalanceChanges: true }
|
|
2006
|
+
});
|
|
2007
|
+
let actualReceived = 0;
|
|
2008
|
+
if (txDetail.balanceChanges) {
|
|
2009
|
+
for (const change of txDetail.balanceChanges) {
|
|
2010
|
+
if (change.coinType === toInfo.type && change.owner && typeof change.owner === "object" && "AddressOwner" in change.owner && change.owner.AddressOwner === this._address) {
|
|
2011
|
+
const amt = Number(change.amount) / 10 ** toInfo.decimals;
|
|
2012
|
+
if (amt > 0) actualReceived += amt;
|
|
2013
|
+
}
|
|
2014
|
+
}
|
|
2035
2015
|
}
|
|
2036
|
-
const
|
|
2016
|
+
const expectedOutput = swapMeta.estimatedOut / 10 ** swapMeta.toDecimals;
|
|
2017
|
+
if (actualReceived === 0) actualReceived = expectedOutput;
|
|
2018
|
+
const priceImpact = expectedOutput > 0 ? Math.abs(actualReceived - expectedOutput) / expectedOutput : 0;
|
|
2019
|
+
reportFee(this._address, "swap", fee.amount, fee.rate, gasResult.digest);
|
|
2020
|
+
this.emitBalanceChange(fromAsset, swapAmount, "swap", gasResult.digest);
|
|
2037
2021
|
return {
|
|
2038
|
-
|
|
2039
|
-
|
|
2040
|
-
|
|
2022
|
+
success: true,
|
|
2023
|
+
tx: gasResult.digest,
|
|
2024
|
+
fromAmount: swapAmount,
|
|
2025
|
+
fromAsset,
|
|
2026
|
+
toAmount: actualReceived,
|
|
2027
|
+
toAsset,
|
|
2028
|
+
priceImpact,
|
|
2029
|
+
fee: fee.amount,
|
|
2030
|
+
gasCost: gasResult.gasCostSui,
|
|
2031
|
+
gasMethod: gasResult.gasMethod
|
|
2041
2032
|
};
|
|
2042
2033
|
}
|
|
2043
|
-
async
|
|
2044
|
-
|
|
2045
|
-
const
|
|
2046
|
-
const
|
|
2047
|
-
const
|
|
2048
|
-
|
|
2049
|
-
|
|
2050
|
-
|
|
2051
|
-
|
|
2052
|
-
|
|
2053
|
-
|
|
2054
|
-
)
|
|
2055
|
-
|
|
2056
|
-
|
|
2057
|
-
|
|
2058
|
-
|
|
2059
|
-
|
|
2060
|
-
|
|
2061
|
-
|
|
2062
|
-
|
|
2063
|
-
|
|
2064
|
-
|
|
2065
|
-
|
|
2066
|
-
|
|
2067
|
-
|
|
2068
|
-
|
|
2069
|
-
|
|
2070
|
-
|
|
2071
|
-
|
|
2072
|
-
const compounded = posRate > 0 ? amount * (reserveRate / posRate) : amount;
|
|
2073
|
-
const { borrowAprPct } = computeRatesFromReserve(reserve);
|
|
2074
|
-
borrows.push({ asset: this.resolveSymbol(coinType), amount: compounded, apy: borrowAprPct });
|
|
2075
|
-
}
|
|
2076
|
-
return { supplies, borrows };
|
|
2034
|
+
async swapQuote(params) {
|
|
2035
|
+
const fromAsset = params.from.toUpperCase();
|
|
2036
|
+
const toAsset = params.to.toUpperCase();
|
|
2037
|
+
const best = await this.registry.bestSwapQuote(fromAsset, toAsset, params.amount);
|
|
2038
|
+
const fee = calculateFee("swap", params.amount);
|
|
2039
|
+
return { ...best.quote, fee: { amount: fee.amount, rate: fee.rate } };
|
|
2040
|
+
}
|
|
2041
|
+
// -- Info --
|
|
2042
|
+
async positions() {
|
|
2043
|
+
const allPositions = await this.registry.allPositions(this._address);
|
|
2044
|
+
const positions = allPositions.flatMap(
|
|
2045
|
+
(p) => [
|
|
2046
|
+
...p.positions.supplies.map((s) => ({
|
|
2047
|
+
protocol: p.protocolId,
|
|
2048
|
+
asset: s.asset,
|
|
2049
|
+
type: "save",
|
|
2050
|
+
amount: s.amount,
|
|
2051
|
+
apy: s.apy
|
|
2052
|
+
})),
|
|
2053
|
+
...p.positions.borrows.map((b) => ({
|
|
2054
|
+
protocol: p.protocolId,
|
|
2055
|
+
asset: b.asset,
|
|
2056
|
+
type: "borrow",
|
|
2057
|
+
amount: b.amount,
|
|
2058
|
+
apy: b.apy
|
|
2059
|
+
}))
|
|
2060
|
+
]
|
|
2061
|
+
);
|
|
2062
|
+
return { positions };
|
|
2077
2063
|
}
|
|
2078
|
-
async
|
|
2079
|
-
this.
|
|
2080
|
-
|
|
2081
|
-
|
|
2082
|
-
|
|
2064
|
+
async rates() {
|
|
2065
|
+
return getRates(this.client);
|
|
2066
|
+
}
|
|
2067
|
+
async allRates(asset = "USDC") {
|
|
2068
|
+
return this.registry.allRates(asset);
|
|
2069
|
+
}
|
|
2070
|
+
async earnings() {
|
|
2071
|
+
const result = await getEarnings(this.client, this.keypair);
|
|
2072
|
+
if (result.totalYieldEarned > 0) {
|
|
2073
|
+
this.emit("yield", {
|
|
2074
|
+
earned: result.dailyEarning,
|
|
2075
|
+
total: result.totalYieldEarned,
|
|
2076
|
+
apy: result.currentApy / 100,
|
|
2077
|
+
timestamp: Date.now()
|
|
2078
|
+
});
|
|
2083
2079
|
}
|
|
2084
|
-
|
|
2085
|
-
const supplied = positions.supplies.reduce((s, p) => s + p.amount, 0);
|
|
2086
|
-
const borrowed = positions.borrows.reduce((s, p) => s + p.amount, 0);
|
|
2087
|
-
const reserve = this.findReserve("USDC");
|
|
2088
|
-
const closeLtv = reserve?.config?.element?.closeLtvPct ?? 75;
|
|
2089
|
-
const openLtv = reserve?.config?.element?.openLtvPct ?? 70;
|
|
2090
|
-
const liqThreshold = closeLtv / 100;
|
|
2091
|
-
const healthFactor = borrowed > 0 ? supplied * liqThreshold / borrowed : Infinity;
|
|
2092
|
-
const maxBorrow = Math.max(0, supplied * (openLtv / 100) - borrowed);
|
|
2093
|
-
return { healthFactor, supplied, borrowed, maxBorrow, liquidationThreshold: liqThreshold };
|
|
2080
|
+
return result;
|
|
2094
2081
|
}
|
|
2095
|
-
async
|
|
2096
|
-
this.
|
|
2097
|
-
|
|
2098
|
-
|
|
2099
|
-
|
|
2100
|
-
|
|
2101
|
-
|
|
2102
|
-
|
|
2103
|
-
|
|
2104
|
-
|
|
2105
|
-
|
|
2106
|
-
|
|
2082
|
+
async fundStatus() {
|
|
2083
|
+
return getFundStatus(this.client, this.keypair);
|
|
2084
|
+
}
|
|
2085
|
+
// -- Sentinel --
|
|
2086
|
+
async sentinelList() {
|
|
2087
|
+
return listSentinels();
|
|
2088
|
+
}
|
|
2089
|
+
async sentinelInfo(id) {
|
|
2090
|
+
return getSentinelInfo(this.client, id);
|
|
2091
|
+
}
|
|
2092
|
+
async sentinelAttack(id, prompt, fee) {
|
|
2093
|
+
return attack(this.client, this.keypair, id, prompt, fee);
|
|
2094
|
+
}
|
|
2095
|
+
// -- Helpers --
|
|
2096
|
+
async resolveLending(protocol, asset, capability) {
|
|
2097
|
+
if (protocol) {
|
|
2098
|
+
const adapter = this.registry.getLending(protocol);
|
|
2099
|
+
if (!adapter) throw new T2000Error("ASSET_NOT_SUPPORTED", `Lending adapter '${protocol}' not found`);
|
|
2100
|
+
return adapter;
|
|
2107
2101
|
}
|
|
2108
|
-
|
|
2109
|
-
|
|
2110
|
-
|
|
2102
|
+
if (capability === "save") {
|
|
2103
|
+
const { adapter } = await this.registry.bestSaveRate(asset);
|
|
2104
|
+
return adapter;
|
|
2111
2105
|
}
|
|
2112
|
-
|
|
2113
|
-
|
|
2114
|
-
|
|
2115
|
-
tx.object(primaryCoinId),
|
|
2116
|
-
allCoins.slice(1).map((c) => tx.object(c.coinObjectId))
|
|
2106
|
+
if (capability === "borrow" || capability === "repay") {
|
|
2107
|
+
const adapters2 = this.registry.listLending().filter(
|
|
2108
|
+
(a) => a.supportedAssets.includes(asset) && a.capabilities.includes(capability) && (capability !== "borrow" || a.supportsSameAssetBorrow)
|
|
2117
2109
|
);
|
|
2110
|
+
if (adapters2.length === 0) throw new T2000Error("ASSET_NOT_SUPPORTED", `No adapter supports ${capability} ${asset}`);
|
|
2111
|
+
return adapters2[0];
|
|
2118
2112
|
}
|
|
2119
|
-
const
|
|
2120
|
-
|
|
2121
|
-
addCollectFeeToTx(tx, depositCoin, "save");
|
|
2122
|
-
}
|
|
2123
|
-
this.suilend.deposit(depositCoin, USDC_TYPE2, capRef, tx);
|
|
2124
|
-
return { tx };
|
|
2125
|
-
}
|
|
2126
|
-
async buildWithdrawTx(address, amount, _asset) {
|
|
2127
|
-
this.ensureInit();
|
|
2128
|
-
const caps = await this.getObligationCaps(address);
|
|
2129
|
-
if (caps.length === 0) {
|
|
2130
|
-
throw new T2000Error("NO_COLLATERAL", "No Suilend position found");
|
|
2131
|
-
}
|
|
2132
|
-
const positions = await this.getPositions(address);
|
|
2133
|
-
const usdcSupply = positions.supplies.find((s) => s.asset === "USDC");
|
|
2134
|
-
const deposited = usdcSupply?.amount ?? 0;
|
|
2135
|
-
const effectiveAmount = Math.min(amount, deposited);
|
|
2136
|
-
if (effectiveAmount <= 0) {
|
|
2137
|
-
throw new T2000Error("NO_COLLATERAL", "Nothing to withdraw from Suilend");
|
|
2138
|
-
}
|
|
2139
|
-
const rawAmount = usdcToRaw(effectiveAmount).toString();
|
|
2140
|
-
const tx = new transactions.Transaction();
|
|
2141
|
-
tx.setSender(address);
|
|
2142
|
-
await this.suilend.withdrawAndSendToUser(
|
|
2143
|
-
address,
|
|
2144
|
-
caps[0].id,
|
|
2145
|
-
caps[0].obligationId,
|
|
2146
|
-
USDC_TYPE2,
|
|
2147
|
-
rawAmount,
|
|
2148
|
-
tx
|
|
2113
|
+
const adapters = this.registry.listLending().filter(
|
|
2114
|
+
(a) => a.supportedAssets.includes(asset) && a.capabilities.includes(capability)
|
|
2149
2115
|
);
|
|
2150
|
-
|
|
2116
|
+
if (adapters.length === 0) throw new T2000Error("ASSET_NOT_SUPPORTED", `No adapter supports ${capability} ${asset}`);
|
|
2117
|
+
return adapters[0];
|
|
2151
2118
|
}
|
|
2152
|
-
|
|
2153
|
-
|
|
2154
|
-
"ASSET_NOT_SUPPORTED",
|
|
2155
|
-
"SuilendAdapter.buildBorrowTx() not available \u2014 Suilend requires different collateral/borrow assets. Deferred to Phase 10."
|
|
2156
|
-
);
|
|
2119
|
+
emitBalanceChange(asset, amount, cause, tx) {
|
|
2120
|
+
this.emit("balanceChange", { asset, previous: 0, current: 0, cause, tx });
|
|
2157
2121
|
}
|
|
2158
|
-
|
|
2159
|
-
|
|
2160
|
-
|
|
2161
|
-
|
|
2162
|
-
|
|
2122
|
+
};
|
|
2123
|
+
async function callSponsorApi(address, name) {
|
|
2124
|
+
const res = await fetch(`${API_BASE_URL}/api/sponsor`, {
|
|
2125
|
+
method: "POST",
|
|
2126
|
+
headers: { "Content-Type": "application/json" },
|
|
2127
|
+
body: JSON.stringify({ address, name })
|
|
2128
|
+
});
|
|
2129
|
+
if (res.status === 429) {
|
|
2130
|
+
const data = await res.json();
|
|
2131
|
+
if (data.challenge) {
|
|
2132
|
+
const proof = solveHashcash(data.challenge);
|
|
2133
|
+
const retry = await fetch(`${API_BASE_URL}/api/sponsor`, {
|
|
2134
|
+
method: "POST",
|
|
2135
|
+
headers: { "Content-Type": "application/json" },
|
|
2136
|
+
body: JSON.stringify({ address, name, proof })
|
|
2137
|
+
});
|
|
2138
|
+
if (!retry.ok) throw new T2000Error("SPONSOR_RATE_LIMITED", "Sponsor rate limited");
|
|
2139
|
+
return;
|
|
2140
|
+
}
|
|
2163
2141
|
}
|
|
2164
|
-
|
|
2165
|
-
|
|
2166
|
-
|
|
2167
|
-
|
|
2168
|
-
|
|
2169
|
-
|
|
2170
|
-
|
|
2171
|
-
|
|
2172
|
-
|
|
2173
|
-
|
|
2174
|
-
|
|
2142
|
+
if (!res.ok) {
|
|
2143
|
+
throw new T2000Error("SPONSOR_FAILED", "Sponsor API unavailable");
|
|
2144
|
+
}
|
|
2145
|
+
}
|
|
2146
|
+
|
|
2147
|
+
// src/utils/simulate.ts
|
|
2148
|
+
async function simulateTransaction(client, tx, sender) {
|
|
2149
|
+
tx.setSender(sender);
|
|
2150
|
+
try {
|
|
2151
|
+
const txBytes = await tx.build({ client });
|
|
2152
|
+
const dryRun = await client.dryRunTransactionBlock({
|
|
2153
|
+
transactionBlock: Buffer.from(txBytes).toString("base64")
|
|
2154
|
+
});
|
|
2155
|
+
const status = dryRun.effects?.status;
|
|
2156
|
+
const gasUsed = dryRun.effects?.gasUsed;
|
|
2157
|
+
const gasEstimateSui = gasUsed ? (Number(gasUsed.computationCost) + Number(gasUsed.storageCost) - Number(gasUsed.storageRebate)) / 1e9 : 0;
|
|
2158
|
+
if (status?.status === "failure") {
|
|
2159
|
+
const rawError = status.error ?? "Unknown simulation error";
|
|
2160
|
+
const parsed = parseMoveAbort(rawError);
|
|
2161
|
+
return {
|
|
2162
|
+
success: false,
|
|
2163
|
+
gasEstimateSui,
|
|
2164
|
+
error: {
|
|
2165
|
+
moveAbortCode: parsed.abortCode,
|
|
2166
|
+
moveModule: parsed.module,
|
|
2167
|
+
reason: parsed.reason,
|
|
2168
|
+
rawError
|
|
2169
|
+
}
|
|
2170
|
+
};
|
|
2175
2171
|
}
|
|
2176
|
-
|
|
2177
|
-
|
|
2172
|
+
return { success: true, gasEstimateSui };
|
|
2173
|
+
} catch (err) {
|
|
2174
|
+
const rawError = err instanceof Error ? err.message : String(err);
|
|
2178
2175
|
return {
|
|
2179
|
-
|
|
2180
|
-
|
|
2181
|
-
|
|
2176
|
+
success: false,
|
|
2177
|
+
gasEstimateSui: 0,
|
|
2178
|
+
error: {
|
|
2179
|
+
reason: "Simulation failed: " + rawError,
|
|
2180
|
+
rawError
|
|
2181
|
+
}
|
|
2182
2182
|
};
|
|
2183
2183
|
}
|
|
2184
|
-
|
|
2185
|
-
|
|
2186
|
-
|
|
2187
|
-
|
|
2188
|
-
|
|
2189
|
-
|
|
2190
|
-
|
|
2191
|
-
|
|
2192
|
-
|
|
2193
|
-
|
|
2194
|
-
|
|
2195
|
-
const page = await this.client.getCoins({ owner, coinType, cursor: cursor ?? void 0 });
|
|
2196
|
-
all.push(...page.data.map((c) => ({ coinObjectId: c.coinObjectId, balance: c.balance })));
|
|
2197
|
-
cursor = page.nextCursor;
|
|
2198
|
-
hasNext = page.hasNextPage;
|
|
2184
|
+
}
|
|
2185
|
+
function throwIfSimulationFailed(sim) {
|
|
2186
|
+
if (sim.success) return;
|
|
2187
|
+
throw new T2000Error(
|
|
2188
|
+
"SIMULATION_FAILED",
|
|
2189
|
+
sim.error?.reason ?? "Transaction simulation failed",
|
|
2190
|
+
{
|
|
2191
|
+
moveAbortCode: sim.error?.moveAbortCode,
|
|
2192
|
+
moveModule: sim.error?.moveModule,
|
|
2193
|
+
reason: sim.error?.reason,
|
|
2194
|
+
rawError: sim.error?.rawError
|
|
2199
2195
|
}
|
|
2200
|
-
|
|
2196
|
+
);
|
|
2197
|
+
}
|
|
2198
|
+
function parseMoveAbort(errorStr) {
|
|
2199
|
+
const abortMatch = errorStr.match(/MoveAbort\([^,]*,\s*(\d+)\)/);
|
|
2200
|
+
const moduleMatch = errorStr.match(/name:\s*Identifier\("([^"]+)"\)/);
|
|
2201
|
+
if (abortMatch) {
|
|
2202
|
+
const code = parseInt(abortMatch[1], 10);
|
|
2203
|
+
const module = moduleMatch?.[1];
|
|
2204
|
+
const reason = mapMoveAbortCode(code);
|
|
2205
|
+
return { abortCode: code, module, reason };
|
|
2201
2206
|
}
|
|
2202
|
-
|
|
2207
|
+
if (errorStr.includes("MovePrimitiveRuntimeError")) {
|
|
2208
|
+
const module = moduleMatch?.[1];
|
|
2209
|
+
return {
|
|
2210
|
+
module,
|
|
2211
|
+
reason: `Move runtime error in ${module ?? "unknown"} module`
|
|
2212
|
+
};
|
|
2213
|
+
}
|
|
2214
|
+
return { reason: errorStr };
|
|
2215
|
+
}
|
|
2203
2216
|
|
|
2204
2217
|
exports.BPS_DENOMINATOR = BPS_DENOMINATOR;
|
|
2205
2218
|
exports.CLOCK_ID = CLOCK_ID;
|