@t2000/sdk 0.18.8 → 0.18.10

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
@@ -4100,7 +4100,12 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
4100
4100
  if (supplies.length === 0) {
4101
4101
  throw new T2000Error("NO_COLLATERAL", "No savings to withdraw");
4102
4102
  }
4103
- supplies.sort((a, b) => a.apy - b.apy);
4103
+ supplies.sort((a, b) => {
4104
+ const aIsUsdc = a.asset === "USDC" ? 0 : 1;
4105
+ const bIsUsdc = b.asset === "USDC" ? 0 : 1;
4106
+ if (aIsUsdc !== bIsUsdc) return aIsUsdc - bIsUsdc;
4107
+ return a.apy - b.apy;
4108
+ });
4104
4109
  const target = supplies[0];
4105
4110
  const adapter = this.registry.getLending(target.protocolId);
4106
4111
  if (!adapter) throw new T2000Error("PROTOCOL_UNAVAILABLE", `Protocol ${target.protocolId} not found`);
@@ -4146,7 +4151,8 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
4146
4151
  coin,
4147
4152
  target.asset,
4148
4153
  "USDC",
4149
- effectiveAmount
4154
+ effectiveAmount,
4155
+ 500
4150
4156
  );
4151
4157
  finalAmount = estimatedOut / 10 ** toDecimals;
4152
4158
  tx.transferObjects([outputCoin], this._address);
@@ -4203,16 +4209,19 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
4203
4209
  if (entries.length === 0) {
4204
4210
  throw new T2000Error("NO_COLLATERAL", "No savings to withdraw across any protocol");
4205
4211
  }
4206
- const hasNonUsdc = entries.some((e) => e.asset !== "USDC");
4212
+ const DUST_SWAP_THRESHOLD = 1;
4213
+ const swappableEntries = entries.filter((e) => e.asset === "USDC" || e.maxAmount >= DUST_SWAP_THRESHOLD);
4214
+ const dustEntries = entries.filter((e) => e.asset !== "USDC" && e.maxAmount < DUST_SWAP_THRESHOLD);
4215
+ const hasNonUsdc = swappableEntries.some((e) => e.asset !== "USDC");
4207
4216
  const swapAdapter = hasNonUsdc ? this.registry.listSwap()[0] : void 0;
4208
- const canPTB = entries.every((e) => e.adapter.addWithdrawToTx) && (!swapAdapter || swapAdapter.addSwapToTx);
4217
+ const canPTB = swappableEntries.every((e) => e.adapter.addWithdrawToTx) && (!swapAdapter || swapAdapter.addSwapToTx);
4209
4218
  let totalUsdcReceived = 0;
4210
4219
  const gasResult = await executeWithGas(this.client, this.keypair, async () => {
4211
4220
  if (canPTB) {
4212
4221
  const tx = new transactions.Transaction();
4213
4222
  tx.setSender(this._address);
4214
4223
  const usdcCoins = [];
4215
- for (const entry of entries) {
4224
+ for (const entry of swappableEntries) {
4216
4225
  const { coin, effectiveAmount } = await entry.adapter.addWithdrawToTx(
4217
4226
  tx,
4218
4227
  this._address,
@@ -4229,7 +4238,8 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
4229
4238
  coin,
4230
4239
  entry.asset,
4231
4240
  "USDC",
4232
- effectiveAmount
4241
+ effectiveAmount,
4242
+ 500
4233
4243
  );
4234
4244
  totalUsdcReceived += estimatedOut / 10 ** toDecimals;
4235
4245
  usdcCoins.push(outputCoin);
@@ -4238,6 +4248,18 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
4238
4248
  tx.transferObjects([coin], this._address);
4239
4249
  }
4240
4250
  }
4251
+ for (const dust of dustEntries) {
4252
+ if (dust.adapter.addWithdrawToTx) {
4253
+ const { coin, effectiveAmount } = await dust.adapter.addWithdrawToTx(
4254
+ tx,
4255
+ this._address,
4256
+ dust.maxAmount,
4257
+ dust.asset
4258
+ );
4259
+ totalUsdcReceived += effectiveAmount;
4260
+ tx.transferObjects([coin], this._address);
4261
+ }
4262
+ }
4241
4263
  if (usdcCoins.length > 1) {
4242
4264
  tx.mergeCoins(usdcCoins[0], usdcCoins.slice(1));
4243
4265
  }