@t2000/sdk 0.16.28 → 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,
@@ -4306,7 +4335,8 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4306
4335
  const quote = await swapAdapter.getQuote("USDC", params.asset, 1);
4307
4336
  const assetPrice = 1 / quote.expectedOutput;
4308
4337
  sellAmountAsset = params.usdAmount / assetPrice;
4309
- sellAmountAsset = Math.min(sellAmountAsset, pos.totalAmount);
4338
+ const maxPosition = params._strategyOnly ? maxSellable : pos.totalAmount;
4339
+ sellAmountAsset = Math.min(sellAmountAsset, maxPosition);
4310
4340
  if (sellAmountAsset > maxSellable) {
4311
4341
  throw new T2000Error(
4312
4342
  "INSUFFICIENT_INVESTMENT",
@@ -4336,7 +4366,7 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4336
4366
  tx: swapResult.tx,
4337
4367
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
4338
4368
  });
4339
- if (params.usdAmount === "all") {
4369
+ if (params.usdAmount === "all" && !params._strategyOnly) {
4340
4370
  this.portfolio.closePosition(params.asset);
4341
4371
  }
4342
4372
  const updatedPos = this.portfolio.getPosition(params.asset);
@@ -4558,7 +4588,25 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4558
4588
  let gasMethod = "self-funded";
4559
4589
  for (const pos of stratPositions) {
4560
4590
  const fullAmount = pos.totalAmount;
4561
- 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
+ });
4562
4610
  const pnl = this.portfolio.recordStrategySell(params.strategy, {
4563
4611
  id: `strat_sell_${Date.now()}_${pos.asset}`,
4564
4612
  type: "sell",
@@ -4878,7 +4926,7 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4878
4926
  this.portfolio.getPositions().filter((p) => p.earning).map((p) => p.asset)
4879
4927
  );
4880
4928
  const savePositions = allPositions.flatMap(
4881
- (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) => ({
4882
4930
  protocolId: p.protocolId,
4883
4931
  protocol: p.protocol,
4884
4932
  asset: s.asset,
@@ -5157,19 +5205,18 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
5157
5205
  // -- Helpers --
5158
5206
  async getFreeBalance(asset) {
5159
5207
  if (!(asset in INVESTMENT_ASSETS)) return Infinity;
5160
- let walletInvested = 0;
5161
5208
  const pos = this.portfolio.getPosition(asset);
5162
- if (pos && pos.totalAmount > 0 && !pos.earning) {
5163
- walletInvested += pos.totalAmount;
5164
- }
5209
+ const directAmount = pos && pos.totalAmount > 0 && !pos.earning ? pos.totalAmount : 0;
5210
+ let strategyTotal = 0;
5165
5211
  for (const key of this.portfolio.getAllStrategyKeys()) {
5166
5212
  for (const sp of this.portfolio.getStrategyPositions(key)) {
5167
5213
  if (sp.asset === asset && sp.totalAmount > 0) {
5168
- walletInvested += sp.totalAmount;
5214
+ strategyTotal += sp.totalAmount;
5169
5215
  }
5170
5216
  }
5171
5217
  }
5172
- if (walletInvested <= 0 && (!pos || pos.totalAmount <= 0)) return Infinity;
5218
+ const walletInvested = Math.max(directAmount, strategyTotal);
5219
+ if (walletInvested <= 0) return Infinity;
5173
5220
  const assetInfo = SUPPORTED_ASSETS[asset];
5174
5221
  const balance = await this.client.getBalance({ owner: this._address, coinType: assetInfo.type });
5175
5222
  const walletAmount = Number(balance.totalBalance) / 10 ** assetInfo.decimals;