@t2000/sdk 0.16.8 → 0.16.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
@@ -3441,8 +3441,22 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3441
3441
  for (const sp of this.portfolio.getStrategyPositions(key)) {
3442
3442
  if (!(sp.asset in INVESTMENT_ASSETS)) continue;
3443
3443
  const price = assetPrices[sp.asset] ?? 0;
3444
- investmentValue += sp.totalAmount * price;
3445
- investmentCostBasis += sp.costBasis;
3444
+ if (sp.asset === "SUI") {
3445
+ const actualHeld = Math.min(sp.totalAmount, bal.gasReserve.sui);
3446
+ investmentValue += actualHeld * price;
3447
+ if (actualHeld < sp.totalAmount && sp.totalAmount > 0) {
3448
+ investmentCostBasis += sp.costBasis * (actualHeld / sp.totalAmount);
3449
+ } else {
3450
+ investmentCostBasis += sp.costBasis;
3451
+ }
3452
+ bal.gasReserve = {
3453
+ sui: Math.max(0, bal.gasReserve.sui - sp.totalAmount),
3454
+ usdEquiv: Math.max(0, bal.gasReserve.sui - sp.totalAmount) * price
3455
+ };
3456
+ } else {
3457
+ investmentValue += sp.totalAmount * price;
3458
+ investmentCostBasis += sp.costBasis;
3459
+ }
3446
3460
  }
3447
3461
  }
3448
3462
  bal.investment = investmentValue;
@@ -3665,10 +3679,13 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3665
3679
  }
3666
3680
  async withdrawAllProtocols() {
3667
3681
  const allPositions = await this.registry.allPositions(this._address);
3682
+ const earningAssets = new Set(
3683
+ this.portfolio.getPositions().filter((p) => p.earning).map((p) => p.asset)
3684
+ );
3668
3685
  const withdrawable = [];
3669
3686
  for (const pos of allPositions) {
3670
3687
  for (const supply of pos.positions.supplies) {
3671
- if (supply.amount > 0.01) {
3688
+ if (supply.amount > 0.01 && !earningAssets.has(supply.asset)) {
3672
3689
  withdrawable.push({ protocolId: pos.protocolId, asset: supply.asset, amount: supply.amount });
3673
3690
  }
3674
3691
  }
@@ -4319,9 +4336,7 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4319
4336
  if (!adapter) {
4320
4337
  throw new T2000Error("PROTOCOL_UNAVAILABLE", `Lending protocol ${pos.earningProtocol} not found`);
4321
4338
  }
4322
- const positions = await adapter.getPositions(this._address);
4323
- const supply = positions.supplies.find((s) => s.asset === params.asset);
4324
- const withdrawAmount = supply?.amount ?? pos.totalAmount;
4339
+ const withdrawAmount = pos.totalAmount;
4325
4340
  const protocolName = adapter.name;
4326
4341
  let effectiveAmount = withdrawAmount;
4327
4342
  const gasResult = await executeWithGas(this.client, this.keypair, async () => {
@@ -5057,14 +5072,24 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
5057
5072
  // -- Helpers --
5058
5073
  async getFreeBalance(asset) {
5059
5074
  if (!(asset in INVESTMENT_ASSETS)) return Infinity;
5075
+ let walletInvested = 0;
5060
5076
  const pos = this.portfolio.getPosition(asset);
5061
- const invested = pos?.totalAmount ?? 0;
5062
- if (invested <= 0) return Infinity;
5077
+ if (pos && pos.totalAmount > 0 && !pos.earning) {
5078
+ walletInvested += pos.totalAmount;
5079
+ }
5080
+ for (const key of this.portfolio.getAllStrategyKeys()) {
5081
+ for (const sp of this.portfolio.getStrategyPositions(key)) {
5082
+ if (sp.asset === asset && sp.totalAmount > 0) {
5083
+ walletInvested += sp.totalAmount;
5084
+ }
5085
+ }
5086
+ }
5087
+ if (walletInvested <= 0 && (!pos || pos.totalAmount <= 0)) return Infinity;
5063
5088
  const assetInfo = SUPPORTED_ASSETS[asset];
5064
5089
  const balance = await this.client.getBalance({ owner: this._address, coinType: assetInfo.type });
5065
5090
  const walletAmount = Number(balance.totalBalance) / 10 ** assetInfo.decimals;
5066
5091
  const gasReserve = asset === "SUI" ? GAS_RESERVE_MIN : 0;
5067
- return Math.max(0, walletAmount - invested - gasReserve);
5092
+ return Math.max(0, walletAmount - walletInvested - gasReserve);
5068
5093
  }
5069
5094
  async resolveLending(protocol, asset, capability) {
5070
5095
  if (protocol) {