@t2000/sdk 0.16.27 → 0.16.29

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
@@ -419,6 +419,7 @@ declare class T2000 extends EventEmitter<T2000Events> {
419
419
  asset: InvestmentAsset;
420
420
  usdAmount: number | 'all';
421
421
  maxSlippage?: number;
422
+ _strategyOnly?: boolean;
422
423
  }): Promise<InvestResult>;
423
424
  investEarn(params: {
424
425
  asset: InvestmentAsset;
package/dist/index.d.ts CHANGED
@@ -419,6 +419,7 @@ declare class T2000 extends EventEmitter<T2000Events> {
419
419
  asset: InvestmentAsset;
420
420
  usdAmount: number | 'all';
421
421
  maxSlippage?: number;
422
+ _strategyOnly?: boolean;
422
423
  }): Promise<InvestResult>;
423
424
  investEarn(params: {
424
425
  asset: InvestmentAsset;
package/dist/index.js CHANGED
@@ -1609,12 +1609,20 @@ async function buildSwapTx(params) {
1609
1609
  }
1610
1610
  const rawAmount = BigInt(Math.floor(amount * 10 ** fromInfo.decimals));
1611
1611
  const aggClient = createAggregatorClient(client, address);
1612
- const result = await aggClient.findRouters({
1613
- from: fromInfo.type,
1614
- target: toInfo.type,
1615
- amount: rawAmount,
1616
- byAmountIn: true
1617
- });
1612
+ const _origLog = console.log;
1613
+ console.log = () => {
1614
+ };
1615
+ let result;
1616
+ try {
1617
+ result = await aggClient.findRouters({
1618
+ from: fromInfo.type,
1619
+ target: toInfo.type,
1620
+ amount: rawAmount,
1621
+ byAmountIn: true
1622
+ });
1623
+ } finally {
1624
+ console.log = _origLog;
1625
+ }
1618
1626
  if (!result || result.insufficientLiquidity) {
1619
1627
  throw new T2000Error(
1620
1628
  "ASSET_NOT_SUPPORTED",
@@ -1623,11 +1631,17 @@ async function buildSwapTx(params) {
1623
1631
  }
1624
1632
  const tx = new Transaction();
1625
1633
  const slippage = maxSlippageBps / 1e4;
1626
- await aggClient.fastRouterSwap({
1627
- router: result,
1628
- txb: tx,
1629
- slippage
1630
- });
1634
+ console.log = () => {
1635
+ };
1636
+ try {
1637
+ await aggClient.fastRouterSwap({
1638
+ router: result,
1639
+ txb: tx,
1640
+ slippage
1641
+ });
1642
+ } finally {
1643
+ console.log = _origLog;
1644
+ }
1631
1645
  const estimatedOut = Number(result.amountOut.toString());
1632
1646
  return {
1633
1647
  tx,
@@ -1644,12 +1658,20 @@ async function addSwapToTx(params) {
1644
1658
  }
1645
1659
  const rawAmount = BigInt(Math.floor(amount * 10 ** fromInfo.decimals));
1646
1660
  const aggClient = createAggregatorClient(client, address);
1647
- const result = await aggClient.findRouters({
1648
- from: fromInfo.type,
1649
- target: toInfo.type,
1650
- amount: rawAmount,
1651
- byAmountIn: true
1652
- });
1661
+ const _origLog = console.log;
1662
+ console.log = () => {
1663
+ };
1664
+ let result;
1665
+ try {
1666
+ result = await aggClient.findRouters({
1667
+ from: fromInfo.type,
1668
+ target: toInfo.type,
1669
+ amount: rawAmount,
1670
+ byAmountIn: true
1671
+ });
1672
+ } finally {
1673
+ console.log = _origLog;
1674
+ }
1653
1675
  if (!result || result.insufficientLiquidity) {
1654
1676
  throw new T2000Error(
1655
1677
  "ASSET_NOT_SUPPORTED",
@@ -1657,12 +1679,19 @@ async function addSwapToTx(params) {
1657
1679
  );
1658
1680
  }
1659
1681
  const slippage = maxSlippageBps / 1e4;
1660
- const outputCoin = await aggClient.routerSwap({
1661
- router: result,
1662
- txb: tx,
1663
- inputCoin,
1664
- slippage
1665
- });
1682
+ console.log = () => {
1683
+ };
1684
+ let outputCoin;
1685
+ try {
1686
+ outputCoin = await aggClient.routerSwap({
1687
+ router: result,
1688
+ txb: tx,
1689
+ inputCoin,
1690
+ slippage
1691
+ });
1692
+ } finally {
1693
+ console.log = _origLog;
1694
+ }
1666
1695
  const estimatedOut = Number(result.amountOut.toString());
1667
1696
  return {
1668
1697
  outputCoin,
@@ -3485,6 +3514,7 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3485
3514
  }
3486
3515
  let investmentValue = 0;
3487
3516
  let investmentCostBasis = 0;
3517
+ let trackedValue = 0;
3488
3518
  const trackedAmounts = {};
3489
3519
  const trackedCostBasis = {};
3490
3520
  const earningAssetSet = /* @__PURE__ */ new Set();
@@ -3508,6 +3538,7 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3508
3538
  if (asset === "SUI") {
3509
3539
  const actualSui = earningAssetSet.has("SUI") ? tracked : Math.min(tracked, bal.gasReserve.sui);
3510
3540
  investmentValue += actualSui * price;
3541
+ trackedValue += actualSui * price;
3511
3542
  if (actualSui < tracked && tracked > 0) {
3512
3543
  investmentCostBasis += costBasis * (actualSui / tracked);
3513
3544
  } else {
@@ -3521,11 +3552,12 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3521
3552
  const onChainAmount = bal.assets[asset] ?? 0;
3522
3553
  const effectiveAmount = Math.max(tracked, onChainAmount);
3523
3554
  investmentValue += effectiveAmount * price;
3555
+ trackedValue += tracked * price;
3524
3556
  investmentCostBasis += costBasis;
3525
3557
  }
3526
3558
  }
3527
3559
  bal.investment = investmentValue;
3528
- bal.investmentPnL = investmentValue - investmentCostBasis;
3560
+ bal.investmentPnL = trackedValue - investmentCostBasis;
3529
3561
  } catch {
3530
3562
  bal.investment = 0;
3531
3563
  bal.investmentPnL = 0;
@@ -4303,7 +4335,8 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4303
4335
  const quote = await swapAdapter.getQuote("USDC", params.asset, 1);
4304
4336
  const assetPrice = 1 / quote.expectedOutput;
4305
4337
  sellAmountAsset = params.usdAmount / assetPrice;
4306
- sellAmountAsset = Math.min(sellAmountAsset, pos.totalAmount);
4338
+ const maxPosition = params._strategyOnly ? maxSellable : pos.totalAmount;
4339
+ sellAmountAsset = Math.min(sellAmountAsset, maxPosition);
4307
4340
  if (sellAmountAsset > maxSellable) {
4308
4341
  throw new T2000Error(
4309
4342
  "INSUFFICIENT_INVESTMENT",
@@ -4333,7 +4366,7 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4333
4366
  tx: swapResult.tx,
4334
4367
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
4335
4368
  });
4336
- if (params.usdAmount === "all") {
4369
+ if (params.usdAmount === "all" && !params._strategyOnly) {
4337
4370
  this.portfolio.closePosition(params.asset);
4338
4371
  }
4339
4372
  const updatedPos = this.portfolio.getPosition(params.asset);
@@ -4555,7 +4588,25 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4555
4588
  let gasMethod = "self-funded";
4556
4589
  for (const pos of stratPositions) {
4557
4590
  const fullAmount = pos.totalAmount;
4558
- const result = await this.investSell({ asset: pos.asset, usdAmount: "all" });
4591
+ const swapAdapter = this.registry.listSwap()[0];
4592
+ let assetPrice = 1;
4593
+ try {
4594
+ if (swapAdapter) {
4595
+ if (pos.asset === "SUI") {
4596
+ assetPrice = await swapAdapter.getPoolPrice();
4597
+ } else {
4598
+ const q = await swapAdapter.getQuote("USDC", pos.asset, 1);
4599
+ assetPrice = q.expectedOutput > 0 ? 1 / q.expectedOutput : 1;
4600
+ }
4601
+ }
4602
+ } catch {
4603
+ }
4604
+ const strategyUsdValue = fullAmount * assetPrice;
4605
+ const result = await this.investSell({
4606
+ asset: pos.asset,
4607
+ usdAmount: strategyUsdValue,
4608
+ _strategyOnly: true
4609
+ });
4559
4610
  const pnl = this.portfolio.recordStrategySell(params.strategy, {
4560
4611
  id: `strat_sell_${Date.now()}_${pos.asset}`,
4561
4612
  type: "sell",
@@ -4875,7 +4926,7 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4875
4926
  this.portfolio.getPositions().filter((p) => p.earning).map((p) => p.asset)
4876
4927
  );
4877
4928
  const savePositions = allPositions.flatMap(
4878
- (p) => p.positions.supplies.filter((s) => s.amount > 0.01).filter((s) => !earningAssets.has(s.asset)).map((s) => ({
4929
+ (p) => p.positions.supplies.filter((s) => s.amount > 0.01).filter((s) => !earningAssets.has(s.asset)).filter((s) => !(s.asset in INVESTMENT_ASSETS)).map((s) => ({
4879
4930
  protocolId: p.protocolId,
4880
4931
  protocol: p.protocol,
4881
4932
  asset: s.asset,
@@ -5154,19 +5205,18 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
5154
5205
  // -- Helpers --
5155
5206
  async getFreeBalance(asset) {
5156
5207
  if (!(asset in INVESTMENT_ASSETS)) return Infinity;
5157
- let walletInvested = 0;
5158
5208
  const pos = this.portfolio.getPosition(asset);
5159
- if (pos && pos.totalAmount > 0 && !pos.earning) {
5160
- walletInvested += pos.totalAmount;
5161
- }
5209
+ const directAmount = pos && pos.totalAmount > 0 && !pos.earning ? pos.totalAmount : 0;
5210
+ let strategyTotal = 0;
5162
5211
  for (const key of this.portfolio.getAllStrategyKeys()) {
5163
5212
  for (const sp of this.portfolio.getStrategyPositions(key)) {
5164
5213
  if (sp.asset === asset && sp.totalAmount > 0) {
5165
- walletInvested += sp.totalAmount;
5214
+ strategyTotal += sp.totalAmount;
5166
5215
  }
5167
5216
  }
5168
5217
  }
5169
- if (walletInvested <= 0 && (!pos || pos.totalAmount <= 0)) return Infinity;
5218
+ const walletInvested = Math.max(directAmount, strategyTotal);
5219
+ if (walletInvested <= 0) return Infinity;
5170
5220
  const assetInfo = SUPPORTED_ASSETS[asset];
5171
5221
  const balance = await this.client.getBalance({ owner: this._address, coinType: assetInfo.type });
5172
5222
  const walletAmount = Number(balance.totalBalance) / 10 ** assetInfo.decimals;