@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.d.cts CHANGED
@@ -261,6 +261,9 @@ declare class PortfolioManager {
261
261
  } & StoredPosition>;
262
262
  recordEarn(asset: string, protocol: string, apy: number): void;
263
263
  recordUnearn(asset: string): void;
264
+ getStrategyAmountForAsset(asset: string): number;
265
+ getDirectAmount(asset: string): number;
266
+ deductFromStrategies(asset: string, amount: number): void;
264
267
  closePosition(asset: string): void;
265
268
  isEarning(asset: string): boolean;
266
269
  getRealizedPnL(): number;
package/dist/index.d.ts CHANGED
@@ -261,6 +261,9 @@ declare class PortfolioManager {
261
261
  } & StoredPosition>;
262
262
  recordEarn(asset: string, protocol: string, apy: number): void;
263
263
  recordUnearn(asset: string): void;
264
+ getStrategyAmountForAsset(asset: string): number;
265
+ getDirectAmount(asset: string): number;
266
+ deductFromStrategies(asset: string, amount: number): void;
264
267
  closePosition(asset: string): void;
265
268
  isEarning(asset: string): boolean;
266
269
  getRealizedPnL(): number;
package/dist/index.js CHANGED
@@ -56767,6 +56767,45 @@ var PortfolioManager = class {
56767
56767
  this.data.positions[asset] = pos;
56768
56768
  this.save();
56769
56769
  }
56770
+ getStrategyAmountForAsset(asset) {
56771
+ this.load();
56772
+ let total = 0;
56773
+ for (const bucket of Object.values(this.data.strategies)) {
56774
+ const pos = bucket[asset];
56775
+ if (pos && pos.totalAmount > 0) total += pos.totalAmount;
56776
+ }
56777
+ return total;
56778
+ }
56779
+ getDirectAmount(asset) {
56780
+ this.load();
56781
+ const aggregate = this.data.positions[asset]?.totalAmount ?? 0;
56782
+ const strategyAmount = this.getStrategyAmountForAsset(asset);
56783
+ return Math.max(0, aggregate - strategyAmount);
56784
+ }
56785
+ deductFromStrategies(asset, amount) {
56786
+ this.load();
56787
+ let remaining = amount;
56788
+ for (const [stratKey, bucket] of Object.entries(this.data.strategies)) {
56789
+ if (remaining <= 0) break;
56790
+ const pos = bucket[asset];
56791
+ if (!pos || pos.totalAmount <= 0) continue;
56792
+ const deduct = Math.min(pos.totalAmount, remaining);
56793
+ const costDeduct = pos.avgPrice * deduct;
56794
+ pos.totalAmount -= deduct;
56795
+ pos.costBasis -= costDeduct;
56796
+ if (pos.totalAmount < 1e-6) {
56797
+ pos.totalAmount = 0;
56798
+ pos.costBasis = 0;
56799
+ pos.avgPrice = 0;
56800
+ }
56801
+ remaining -= deduct;
56802
+ const hasPositions = Object.values(bucket).some((p) => p.totalAmount > 0);
56803
+ if (!hasPositions) {
56804
+ delete this.data.strategies[stratKey];
56805
+ }
56806
+ }
56807
+ this.save();
56808
+ }
56770
56809
  closePosition(asset) {
56771
56810
  this.load();
56772
56811
  const pos = this.data.positions[asset];
@@ -58338,6 +58377,7 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
58338
58377
  }
58339
58378
  }
58340
58379
  const price = swapResult.toAmount / sellAmountAsset;
58380
+ const directAmountBeforeSell = this.portfolio.getDirectAmount(params.asset);
58341
58381
  let realizedPnL = 0;
58342
58382
  try {
58343
58383
  realizedPnL = this.portfolio.recordSell({
@@ -58355,6 +58395,12 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
58355
58395
  }
58356
58396
  if (params.usdAmount === "all" && !params._strategyOnly) {
58357
58397
  this.portfolio.closePosition(params.asset);
58398
+ this.portfolio.deductFromStrategies(params.asset, sellAmountAsset);
58399
+ } else if (!params._strategyOnly && sellAmountAsset > 0) {
58400
+ const overflowIntoStrategy = sellAmountAsset - directAmountBeforeSell;
58401
+ if (overflowIntoStrategy > 0) {
58402
+ this.portfolio.deductFromStrategies(params.asset, overflowIntoStrategy);
58403
+ }
58358
58404
  }
58359
58405
  const updatedPos = this.portfolio.getPosition(params.asset);
58360
58406
  const position = {