@t2000/sdk 0.7.0 → 0.7.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/index.d.cts CHANGED
@@ -110,6 +110,7 @@ declare class T2000 extends EventEmitter<T2000Events> {
110
110
  asset?: string;
111
111
  protocol?: string;
112
112
  }): Promise<WithdrawResult>;
113
+ private withdrawAllProtocols;
113
114
  maxWithdraw(): Promise<MaxWithdrawResult>;
114
115
  borrow(params: {
115
116
  amount: number;
package/dist/index.d.ts CHANGED
@@ -110,6 +110,7 @@ declare class T2000 extends EventEmitter<T2000Events> {
110
110
  asset?: string;
111
111
  protocol?: string;
112
112
  }): Promise<WithdrawResult>;
113
+ private withdrawAllProtocols;
113
114
  maxWithdraw(): Promise<MaxWithdrawResult>;
114
115
  borrow(params: {
115
116
  amount: number;
package/dist/index.js CHANGED
@@ -1374,7 +1374,7 @@ var LENDING_MARKET_ID = "0x84030d26d85eaa7035084a057f2f11f701b7e2e4eda87551becbc
1374
1374
  var LENDING_MARKET_TYPE = "0xf95b06141ed4a174f239417323bde3f209b972f5930d8521ea38a52aff3a6ddf::suilend::MAIN_POOL";
1375
1375
  var SUILEND_PACKAGE = "0xf95b06141ed4a174f239417323bde3f209b972f5930d8521ea38a52aff3a6ddf";
1376
1376
  var UPGRADE_CAP_ID = "0x3d4ef1859c3ee9fc72858f588b56a09da5466e64f8cc4e90a7b3b909fba8a7ae";
1377
- var FALLBACK_PUBLISHED_AT = "0xd2a67633ccb8de063163e25bcfca242929caf5cf1a26c2929dab519ee0b8f331";
1377
+ var FALLBACK_PUBLISHED_AT = "0x3d4353f3bd3565329655e6b77bc2abfd31e558b86662ebd078ae453d416bc10f";
1378
1378
  var descriptor4 = {
1379
1379
  id: "suilend",
1380
1380
  name: "Suilend",
@@ -2162,6 +2162,10 @@ var T2000 = class _T2000 extends EventEmitter {
2162
2162
  }
2163
2163
  } else {
2164
2164
  amount = params.amount;
2165
+ const bal = await queryBalance(this.client, this._address);
2166
+ if (amount > bal.available) {
2167
+ throw new T2000Error("INSUFFICIENT_BALANCE", `Insufficient USDC. Available: $${bal.available.toFixed(2)}, requested: $${amount.toFixed(2)}`);
2168
+ }
2165
2169
  }
2166
2170
  const fee = calculateFee("save", amount);
2167
2171
  const saveAmount = amount;
@@ -2195,6 +2199,9 @@ var T2000 = class _T2000 extends EventEmitter {
2195
2199
  if (asset !== "USDC") {
2196
2200
  throw new T2000Error("ASSET_NOT_SUPPORTED", `Only USDC is supported for withdraw. Got: ${asset}`);
2197
2201
  }
2202
+ if (params.amount === "all" && !params.protocol) {
2203
+ return this.withdrawAllProtocols(asset);
2204
+ }
2198
2205
  const adapter = await this.resolveLending(params.protocol, asset, "withdraw");
2199
2206
  let amount;
2200
2207
  if (params.amount === "all") {
@@ -2237,6 +2244,46 @@ var T2000 = class _T2000 extends EventEmitter {
2237
2244
  gasMethod: gasResult.gasMethod
2238
2245
  };
2239
2246
  }
2247
+ async withdrawAllProtocols(asset) {
2248
+ const allPositions = await this.registry.allPositions(this._address);
2249
+ const withSupply = allPositions.filter(
2250
+ (p) => p.positions.supplies.some((s) => s.asset === asset && s.amount > 1e-3)
2251
+ );
2252
+ if (withSupply.length === 0) {
2253
+ throw new T2000Error("NO_COLLATERAL", "No savings to withdraw across any protocol");
2254
+ }
2255
+ let totalWithdrawn = 0;
2256
+ let lastDigest = "";
2257
+ let totalGasCost = 0;
2258
+ let lastGasMethod = "self-funded";
2259
+ for (const pos of withSupply) {
2260
+ const adapter = this.registry.getLending(pos.protocolId);
2261
+ if (!adapter) continue;
2262
+ const maxResult = await adapter.maxWithdraw(this._address, asset);
2263
+ if (maxResult.maxAmount <= 1e-3) continue;
2264
+ let effectiveAmount = maxResult.maxAmount;
2265
+ const gasResult = await executeWithGas(this.client, this.keypair, async () => {
2266
+ const built = await adapter.buildWithdrawTx(this._address, maxResult.maxAmount, asset);
2267
+ effectiveAmount = built.effectiveAmount;
2268
+ return built.tx;
2269
+ });
2270
+ totalWithdrawn += effectiveAmount;
2271
+ lastDigest = gasResult.digest;
2272
+ totalGasCost += gasResult.gasCostSui;
2273
+ lastGasMethod = gasResult.gasMethod;
2274
+ this.emitBalanceChange("USDC", effectiveAmount, "withdraw", gasResult.digest);
2275
+ }
2276
+ if (totalWithdrawn <= 0) {
2277
+ throw new T2000Error("NO_COLLATERAL", "No savings to withdraw across any protocol");
2278
+ }
2279
+ return {
2280
+ success: true,
2281
+ tx: lastDigest,
2282
+ amount: totalWithdrawn,
2283
+ gasCost: totalGasCost,
2284
+ gasMethod: lastGasMethod
2285
+ };
2286
+ }
2240
2287
  async maxWithdraw() {
2241
2288
  const adapter = await this.resolveLending(void 0, "USDC", "withdraw");
2242
2289
  return adapter.maxWithdraw(this._address, "USDC");