@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.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,
@@ -3487,6 +3516,7 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3487
3516
  }
3488
3517
  let investmentValue = 0;
3489
3518
  let investmentCostBasis = 0;
3519
+ let trackedValue = 0;
3490
3520
  const trackedAmounts = {};
3491
3521
  const trackedCostBasis = {};
3492
3522
  const earningAssetSet = /* @__PURE__ */ new Set();
@@ -3510,6 +3540,7 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3510
3540
  if (asset === "SUI") {
3511
3541
  const actualSui = earningAssetSet.has("SUI") ? tracked : Math.min(tracked, bal.gasReserve.sui);
3512
3542
  investmentValue += actualSui * price;
3543
+ trackedValue += actualSui * price;
3513
3544
  if (actualSui < tracked && tracked > 0) {
3514
3545
  investmentCostBasis += costBasis * (actualSui / tracked);
3515
3546
  } else {
@@ -3523,11 +3554,12 @@ To access invested funds: t2000 invest sell ${params.amount} ${asset}`,
3523
3554
  const onChainAmount = bal.assets[asset] ?? 0;
3524
3555
  const effectiveAmount = Math.max(tracked, onChainAmount);
3525
3556
  investmentValue += effectiveAmount * price;
3557
+ trackedValue += tracked * price;
3526
3558
  investmentCostBasis += costBasis;
3527
3559
  }
3528
3560
  }
3529
3561
  bal.investment = investmentValue;
3530
- bal.investmentPnL = investmentValue - investmentCostBasis;
3562
+ bal.investmentPnL = trackedValue - investmentCostBasis;
3531
3563
  } catch {
3532
3564
  bal.investment = 0;
3533
3565
  bal.investmentPnL = 0;
@@ -4305,7 +4337,8 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4305
4337
  const quote = await swapAdapter.getQuote("USDC", params.asset, 1);
4306
4338
  const assetPrice = 1 / quote.expectedOutput;
4307
4339
  sellAmountAsset = params.usdAmount / assetPrice;
4308
- sellAmountAsset = Math.min(sellAmountAsset, pos.totalAmount);
4340
+ const maxPosition = params._strategyOnly ? maxSellable : pos.totalAmount;
4341
+ sellAmountAsset = Math.min(sellAmountAsset, maxPosition);
4309
4342
  if (sellAmountAsset > maxSellable) {
4310
4343
  throw new T2000Error(
4311
4344
  "INSUFFICIENT_INVESTMENT",
@@ -4335,7 +4368,7 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4335
4368
  tx: swapResult.tx,
4336
4369
  timestamp: (/* @__PURE__ */ new Date()).toISOString()
4337
4370
  });
4338
- if (params.usdAmount === "all") {
4371
+ if (params.usdAmount === "all" && !params._strategyOnly) {
4339
4372
  this.portfolio.closePosition(params.asset);
4340
4373
  }
4341
4374
  const updatedPos = this.portfolio.getPosition(params.asset);
@@ -4557,7 +4590,25 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4557
4590
  let gasMethod = "self-funded";
4558
4591
  for (const pos of stratPositions) {
4559
4592
  const fullAmount = pos.totalAmount;
4560
- 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
+ });
4561
4612
  const pnl = this.portfolio.recordStrategySell(params.strategy, {
4562
4613
  id: `strat_sell_${Date.now()}_${pos.asset}`,
4563
4614
  type: "sell",
@@ -4877,7 +4928,7 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4877
4928
  this.portfolio.getPositions().filter((p) => p.earning).map((p) => p.asset)
4878
4929
  );
4879
4930
  const savePositions = allPositions.flatMap(
4880
- (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) => ({
4881
4932
  protocolId: p.protocolId,
4882
4933
  protocol: p.protocol,
4883
4934
  asset: s.asset,
@@ -5156,19 +5207,18 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
5156
5207
  // -- Helpers --
5157
5208
  async getFreeBalance(asset) {
5158
5209
  if (!(asset in INVESTMENT_ASSETS)) return Infinity;
5159
- let walletInvested = 0;
5160
5210
  const pos = this.portfolio.getPosition(asset);
5161
- if (pos && pos.totalAmount > 0 && !pos.earning) {
5162
- walletInvested += pos.totalAmount;
5163
- }
5211
+ const directAmount = pos && pos.totalAmount > 0 && !pos.earning ? pos.totalAmount : 0;
5212
+ let strategyTotal = 0;
5164
5213
  for (const key of this.portfolio.getAllStrategyKeys()) {
5165
5214
  for (const sp of this.portfolio.getStrategyPositions(key)) {
5166
5215
  if (sp.asset === asset && sp.totalAmount > 0) {
5167
- walletInvested += sp.totalAmount;
5216
+ strategyTotal += sp.totalAmount;
5168
5217
  }
5169
5218
  }
5170
5219
  }
5171
- if (walletInvested <= 0 && (!pos || pos.totalAmount <= 0)) return Infinity;
5220
+ const walletInvested = Math.max(directAmount, strategyTotal);
5221
+ if (walletInvested <= 0) return Infinity;
5172
5222
  const assetInfo = SUPPORTED_ASSETS[asset];
5173
5223
  const balance = await this.client.getBalance({ owner: this._address, coinType: assetInfo.type });
5174
5224
  const walletAmount = Number(balance.totalBalance) / 10 ** assetInfo.decimals;