@t2000/sdk 0.18.0 → 0.18.4

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.cjs CHANGED
@@ -1674,15 +1674,20 @@ var ProtocolRegistry = class {
1674
1674
  }
1675
1675
  async allPositions(address) {
1676
1676
  const results = [];
1677
+ const errors = [];
1677
1678
  for (const adapter of this.lending.values()) {
1678
1679
  try {
1679
1680
  const positions = await adapter.getPositions(address);
1680
1681
  if (positions.supplies.length > 0 || positions.borrows.length > 0) {
1681
1682
  results.push({ protocol: adapter.name, protocolId: adapter.id, positions });
1682
1683
  }
1683
- } catch {
1684
+ } catch (err) {
1685
+ errors.push(`${adapter.name}: ${err instanceof Error ? err.message : String(err)}`);
1684
1686
  }
1685
1687
  }
1688
+ if (results.length === 0 && errors.length > 0) {
1689
+ throw new T2000Error("PROTOCOL_UNAVAILABLE", `Protocol queries failed (${errors.length}/${this.lending.size}): ${errors.join("; ")}`);
1690
+ }
1686
1691
  return results;
1687
1692
  }
1688
1693
  getLending(id) {
@@ -3848,7 +3853,7 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3848
3853
  );
3849
3854
  try {
3850
3855
  const positions = await this.positions();
3851
- const savings = positions.positions.filter((p) => p.type === "save").filter((p) => !earningAssets.has(p.asset)).reduce((sum, p) => sum + p.amount, 0);
3856
+ const savings = positions.positions.filter((p) => p.type === "save").filter((p) => !earningAssets.has(p.asset)).filter((p) => !(p.asset in INVESTMENT_ASSETS)).reduce((sum, p) => sum + p.amount, 0);
3852
3857
  const debt = positions.positions.filter((p) => p.type === "borrow").reduce((sum, p) => sum + p.amount, 0);
3853
3858
  bal.savings = savings;
3854
3859
  bal.debt = debt;
@@ -4184,7 +4189,8 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
4184
4189
  if (entries.length === 0) {
4185
4190
  throw new T2000Error("NO_COLLATERAL", "No savings to withdraw across any protocol");
4186
4191
  }
4187
- const swapAdapter = this.registry.listSwap()[0];
4192
+ const hasNonUsdc = entries.some((e) => e.asset !== "USDC");
4193
+ const swapAdapter = hasNonUsdc ? this.registry.listSwap()[0] : void 0;
4188
4194
  const canPTB = entries.every((e) => e.adapter.addWithdrawToTx) && (!swapAdapter || swapAdapter.addSwapToTx);
4189
4195
  let totalUsdcReceived = 0;
4190
4196
  const gasResult = await executeWithGas(this.client, this.keypair, async () => {
@@ -4192,7 +4198,6 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
4192
4198
  const tx = new transactions.Transaction();
4193
4199
  tx.setSender(this._address);
4194
4200
  const usdcCoins = [];
4195
- const nonUsdcCoins = [];
4196
4201
  for (const entry of entries) {
4197
4202
  const { coin, effectiveAmount } = await entry.adapter.addWithdrawToTx(
4198
4203
  tx,
@@ -4203,9 +4208,20 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
4203
4208
  if (entry.asset === "USDC") {
4204
4209
  totalUsdcReceived += effectiveAmount;
4205
4210
  usdcCoins.push(coin);
4211
+ } else if (swapAdapter?.addSwapToTx) {
4212
+ const { outputCoin, estimatedOut, toDecimals } = await swapAdapter.addSwapToTx(
4213
+ tx,
4214
+ this._address,
4215
+ coin,
4216
+ entry.asset,
4217
+ "USDC",
4218
+ effectiveAmount
4219
+ );
4220
+ totalUsdcReceived += estimatedOut / 10 ** toDecimals;
4221
+ usdcCoins.push(outputCoin);
4206
4222
  } else {
4207
4223
  totalUsdcReceived += effectiveAmount;
4208
- nonUsdcCoins.push(coin);
4224
+ tx.transferObjects([coin], this._address);
4209
4225
  }
4210
4226
  }
4211
4227
  if (usdcCoins.length > 1) {
@@ -4214,9 +4230,6 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
4214
4230
  if (usdcCoins.length > 0) {
4215
4231
  tx.transferObjects([usdcCoins[0]], this._address);
4216
4232
  }
4217
- for (const coin of nonUsdcCoins) {
4218
- tx.transferObjects([coin], this._address);
4219
- }
4220
4233
  return tx;
4221
4234
  }
4222
4235
  let lastTx;
@@ -4225,6 +4238,9 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
4225
4238
  totalUsdcReceived += built.effectiveAmount;
4226
4239
  lastTx = built.tx;
4227
4240
  }
4241
+ if (hasNonUsdc && swapAdapter) {
4242
+ await this._convertWalletStablesToUsdc(await queryBalance(this.client, this._address));
4243
+ }
4228
4244
  return lastTx;
4229
4245
  });
4230
4246
  if (totalUsdcReceived <= 0) {