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