@t2000/sdk 0.19.5 → 0.19.6

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
@@ -56770,6 +56770,45 @@ var PortfolioManager = class {
56770
56770
  this.data.positions[asset] = pos;
56771
56771
  this.save();
56772
56772
  }
56773
+ getStrategyAmountForAsset(asset) {
56774
+ this.load();
56775
+ let total = 0;
56776
+ for (const bucket of Object.values(this.data.strategies)) {
56777
+ const pos = bucket[asset];
56778
+ if (pos && pos.totalAmount > 0) total += pos.totalAmount;
56779
+ }
56780
+ return total;
56781
+ }
56782
+ getDirectAmount(asset) {
56783
+ this.load();
56784
+ const aggregate = this.data.positions[asset]?.totalAmount ?? 0;
56785
+ const strategyAmount = this.getStrategyAmountForAsset(asset);
56786
+ return Math.max(0, aggregate - strategyAmount);
56787
+ }
56788
+ deductFromStrategies(asset, amount) {
56789
+ this.load();
56790
+ let remaining = amount;
56791
+ for (const [stratKey, bucket] of Object.entries(this.data.strategies)) {
56792
+ if (remaining <= 0) break;
56793
+ const pos = bucket[asset];
56794
+ if (!pos || pos.totalAmount <= 0) continue;
56795
+ const deduct = Math.min(pos.totalAmount, remaining);
56796
+ const costDeduct = pos.avgPrice * deduct;
56797
+ pos.totalAmount -= deduct;
56798
+ pos.costBasis -= costDeduct;
56799
+ if (pos.totalAmount < 1e-6) {
56800
+ pos.totalAmount = 0;
56801
+ pos.costBasis = 0;
56802
+ pos.avgPrice = 0;
56803
+ }
56804
+ remaining -= deduct;
56805
+ const hasPositions = Object.values(bucket).some((p) => p.totalAmount > 0);
56806
+ if (!hasPositions) {
56807
+ delete this.data.strategies[stratKey];
56808
+ }
56809
+ }
56810
+ this.save();
56811
+ }
56773
56812
  closePosition(asset) {
56774
56813
  this.load();
56775
56814
  const pos = this.data.positions[asset];
@@ -58341,6 +58380,7 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
58341
58380
  }
58342
58381
  }
58343
58382
  const price = swapResult.toAmount / sellAmountAsset;
58383
+ const directAmountBeforeSell = this.portfolio.getDirectAmount(params.asset);
58344
58384
  let realizedPnL = 0;
58345
58385
  try {
58346
58386
  realizedPnL = this.portfolio.recordSell({
@@ -58358,6 +58398,12 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
58358
58398
  }
58359
58399
  if (params.usdAmount === "all" && !params._strategyOnly) {
58360
58400
  this.portfolio.closePosition(params.asset);
58401
+ this.portfolio.deductFromStrategies(params.asset, sellAmountAsset);
58402
+ } else if (!params._strategyOnly && sellAmountAsset > 0) {
58403
+ const overflowIntoStrategy = sellAmountAsset - directAmountBeforeSell;
58404
+ if (overflowIntoStrategy > 0) {
58405
+ this.portfolio.deductFromStrategies(params.asset, overflowIntoStrategy);
58406
+ }
58361
58407
  }
58362
58408
  const updatedPos = this.portfolio.getPosition(params.asset);
58363
58409
  const position = {