@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.cjs CHANGED
@@ -1611,12 +1611,20 @@ async function buildSwapTx(params) {
1611
1611
  }
1612
1612
  const rawAmount = BigInt(Math.floor(amount * 10 ** fromInfo.decimals));
1613
1613
  const aggClient = createAggregatorClient(client, address);
1614
- const result = await aggClient.findRouters({
1615
- from: fromInfo.type,
1616
- target: toInfo.type,
1617
- amount: rawAmount,
1618
- byAmountIn: true
1619
- });
1614
+ const _origLog = console.log;
1615
+ console.log = () => {
1616
+ };
1617
+ let result;
1618
+ try {
1619
+ result = await aggClient.findRouters({
1620
+ from: fromInfo.type,
1621
+ target: toInfo.type,
1622
+ amount: rawAmount,
1623
+ byAmountIn: true
1624
+ });
1625
+ } finally {
1626
+ console.log = _origLog;
1627
+ }
1620
1628
  if (!result || result.insufficientLiquidity) {
1621
1629
  throw new T2000Error(
1622
1630
  "ASSET_NOT_SUPPORTED",
@@ -1625,11 +1633,17 @@ async function buildSwapTx(params) {
1625
1633
  }
1626
1634
  const tx = new transactions.Transaction();
1627
1635
  const slippage = maxSlippageBps / 1e4;
1628
- await aggClient.fastRouterSwap({
1629
- router: result,
1630
- txb: tx,
1631
- slippage
1632
- });
1636
+ console.log = () => {
1637
+ };
1638
+ try {
1639
+ await aggClient.fastRouterSwap({
1640
+ router: result,
1641
+ txb: tx,
1642
+ slippage
1643
+ });
1644
+ } finally {
1645
+ console.log = _origLog;
1646
+ }
1633
1647
  const estimatedOut = Number(result.amountOut.toString());
1634
1648
  return {
1635
1649
  tx,
@@ -1646,12 +1660,20 @@ async function addSwapToTx(params) {
1646
1660
  }
1647
1661
  const rawAmount = BigInt(Math.floor(amount * 10 ** fromInfo.decimals));
1648
1662
  const aggClient = createAggregatorClient(client, address);
1649
- const result = await aggClient.findRouters({
1650
- from: fromInfo.type,
1651
- target: toInfo.type,
1652
- amount: rawAmount,
1653
- byAmountIn: true
1654
- });
1663
+ const _origLog = console.log;
1664
+ console.log = () => {
1665
+ };
1666
+ let result;
1667
+ try {
1668
+ result = await aggClient.findRouters({
1669
+ from: fromInfo.type,
1670
+ target: toInfo.type,
1671
+ amount: rawAmount,
1672
+ byAmountIn: true
1673
+ });
1674
+ } finally {
1675
+ console.log = _origLog;
1676
+ }
1655
1677
  if (!result || result.insufficientLiquidity) {
1656
1678
  throw new T2000Error(
1657
1679
  "ASSET_NOT_SUPPORTED",
@@ -1659,12 +1681,19 @@ async function addSwapToTx(params) {
1659
1681
  );
1660
1682
  }
1661
1683
  const slippage = maxSlippageBps / 1e4;
1662
- const outputCoin = await aggClient.routerSwap({
1663
- router: result,
1664
- txb: tx,
1665
- inputCoin,
1666
- slippage
1667
- });
1684
+ console.log = () => {
1685
+ };
1686
+ let outputCoin;
1687
+ try {
1688
+ outputCoin = await aggClient.routerSwap({
1689
+ router: result,
1690
+ txb: tx,
1691
+ inputCoin,
1692
+ slippage
1693
+ });
1694
+ } finally {
1695
+ console.log = _origLog;
1696
+ }
1668
1697
  const estimatedOut = Number(result.amountOut.toString());
1669
1698
  return {
1670
1699
  outputCoin,
@@ -4308,7 +4337,8 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4308
4337
  const quote = await swapAdapter.getQuote("USDC", params.asset, 1);
4309
4338
  const assetPrice = 1 / quote.expectedOutput;
4310
4339
  sellAmountAsset = params.usdAmount / assetPrice;
4311
- sellAmountAsset = Math.min(sellAmountAsset, pos.totalAmount);
4340
+ const maxPosition = params._strategyOnly ? maxSellable : pos.totalAmount;
4341
+ sellAmountAsset = Math.min(sellAmountAsset, maxPosition);
4312
4342
  if (sellAmountAsset > maxSellable) {
4313
4343
  throw new T2000Error(
4314
4344
  "INSUFFICIENT_INVESTMENT",
@@ -4338,7 +4368,7 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4338
4368
  tx: swapResult.tx,
4339
4369
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
4340
4370
  });
4341
- if (params.usdAmount === "all") {
4371
+ if (params.usdAmount === "all" && !params._strategyOnly) {
4342
4372
  this.portfolio.closePosition(params.asset);
4343
4373
  }
4344
4374
  const updatedPos = this.portfolio.getPosition(params.asset);
@@ -4560,7 +4590,25 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4560
4590
  let gasMethod = "self-funded";
4561
4591
  for (const pos of stratPositions) {
4562
4592
  const fullAmount = pos.totalAmount;
4563
- const result = await this.investSell({ asset: pos.asset, usdAmount: "all" });
4593
+ const swapAdapter = this.registry.listSwap()[0];
4594
+ let assetPrice = 1;
4595
+ try {
4596
+ if (swapAdapter) {
4597
+ if (pos.asset === "SUI") {
4598
+ assetPrice = await swapAdapter.getPoolPrice();
4599
+ } else {
4600
+ const q = await swapAdapter.getQuote("USDC", pos.asset, 1);
4601
+ assetPrice = q.expectedOutput > 0 ? 1 / q.expectedOutput : 1;
4602
+ }
4603
+ }
4604
+ } catch {
4605
+ }
4606
+ const strategyUsdValue = fullAmount * assetPrice;
4607
+ const result = await this.investSell({
4608
+ asset: pos.asset,
4609
+ usdAmount: strategyUsdValue,
4610
+ _strategyOnly: true
4611
+ });
4564
4612
  const pnl = this.portfolio.recordStrategySell(params.strategy, {
4565
4613
  id: `strat_sell_${Date.now()}_${pos.asset}`,
4566
4614
  type: "sell",
@@ -4880,7 +4928,7 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4880
4928
  this.portfolio.getPositions().filter((p) => p.earning).map((p) => p.asset)
4881
4929
  );
4882
4930
  const savePositions = allPositions.flatMap(
4883
- (p) => p.positions.supplies.filter((s) => s.amount > 0.01).filter((s) => !earningAssets.has(s.asset)).map((s) => ({
4931
+ (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) => ({
4884
4932
  protocolId: p.protocolId,
4885
4933
  protocol: p.protocol,
4886
4934
  asset: s.asset,
@@ -5159,19 +5207,18 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
5159
5207
  // -- Helpers --
5160
5208
  async getFreeBalance(asset) {
5161
5209
  if (!(asset in INVESTMENT_ASSETS)) return Infinity;
5162
- let walletInvested = 0;
5163
5210
  const pos = this.portfolio.getPosition(asset);
5164
- if (pos && pos.totalAmount > 0 && !pos.earning) {
5165
- walletInvested += pos.totalAmount;
5166
- }
5211
+ const directAmount = pos && pos.totalAmount > 0 && !pos.earning ? pos.totalAmount : 0;
5212
+ let strategyTotal = 0;
5167
5213
  for (const key of this.portfolio.getAllStrategyKeys()) {
5168
5214
  for (const sp of this.portfolio.getStrategyPositions(key)) {
5169
5215
  if (sp.asset === asset && sp.totalAmount > 0) {
5170
- walletInvested += sp.totalAmount;
5216
+ strategyTotal += sp.totalAmount;
5171
5217
  }
5172
5218
  }
5173
5219
  }
5174
- if (walletInvested <= 0 && (!pos || pos.totalAmount <= 0)) return Infinity;
5220
+ const walletInvested = Math.max(directAmount, strategyTotal);
5221
+ if (walletInvested <= 0) return Infinity;
5175
5222
  const assetInfo = SUPPORTED_ASSETS[asset];
5176
5223
  const balance = await this.client.getBalance({ owner: this._address, coinType: assetInfo.type });
5177
5224
  const walletAmount = Number(balance.totalBalance) / 10 ** assetInfo.decimals;