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