@t2000/sdk 0.17.5 → 0.17.8
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/adapters/index.d.cts +1 -1
- package/dist/adapters/index.d.ts +1 -1
- package/dist/{index-BzQsrfrc.d.cts → index-D4cFY__D.d.cts} +4 -0
- package/dist/{index-BzQsrfrc.d.ts → index-D4cFY__D.d.ts} +4 -0
- package/dist/index.cjs +208 -60
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +2 -2
- package/dist/index.d.ts +2 -2
- package/dist/index.js +208 -60
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { w as AdapterCapability, x as AdapterPositions, y as AdapterTxResult, C as CetusAdapter, K as HealthInfo, L as LendingAdapter, s as LendingRates, N as NaviAdapter, O as PerpsAdapter, X as ProtocolDescriptor, Y as ProtocolRegistry, $ as SuilendAdapter, d as SwapAdapter, a0 as SwapQuote, a3 as allDescriptors, a4 as cetusDescriptor, a7 as naviDescriptor, aa as sentinelDescriptor, ad as suilendDescriptor } from '../index-
|
|
1
|
+
export { w as AdapterCapability, x as AdapterPositions, y as AdapterTxResult, C as CetusAdapter, K as HealthInfo, L as LendingAdapter, s as LendingRates, N as NaviAdapter, O as PerpsAdapter, X as ProtocolDescriptor, Y as ProtocolRegistry, $ as SuilendAdapter, d as SwapAdapter, a0 as SwapQuote, a3 as allDescriptors, a4 as cetusDescriptor, a7 as naviDescriptor, aa as sentinelDescriptor, ad as suilendDescriptor } from '../index-D4cFY__D.cjs';
|
|
2
2
|
import '@mysten/sui/transactions';
|
|
3
3
|
import '@mysten/sui/jsonRpc';
|
|
4
4
|
import '@mysten/sui/keypairs/ed25519';
|
package/dist/adapters/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
export { w as AdapterCapability, x as AdapterPositions, y as AdapterTxResult, C as CetusAdapter, K as HealthInfo, L as LendingAdapter, s as LendingRates, N as NaviAdapter, O as PerpsAdapter, X as ProtocolDescriptor, Y as ProtocolRegistry, $ as SuilendAdapter, d as SwapAdapter, a0 as SwapQuote, a3 as allDescriptors, a4 as cetusDescriptor, a7 as naviDescriptor, aa as sentinelDescriptor, ad as suilendDescriptor } from '../index-
|
|
1
|
+
export { w as AdapterCapability, x as AdapterPositions, y as AdapterTxResult, C as CetusAdapter, K as HealthInfo, L as LendingAdapter, s as LendingRates, N as NaviAdapter, O as PerpsAdapter, X as ProtocolDescriptor, Y as ProtocolRegistry, $ as SuilendAdapter, d as SwapAdapter, a0 as SwapQuote, a3 as allDescriptors, a4 as cetusDescriptor, a7 as naviDescriptor, aa as sentinelDescriptor, ad as suilendDescriptor } from '../index-D4cFY__D.js';
|
|
2
2
|
import '@mysten/sui/transactions';
|
|
3
3
|
import '@mysten/sui/jsonRpc';
|
|
4
4
|
import '@mysten/sui/keypairs/ed25519';
|
package/dist/index.cjs
CHANGED
|
@@ -4625,50 +4625,90 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
|
|
|
4625
4625
|
if (stratPositions.length === 0) {
|
|
4626
4626
|
throw new T2000Error("INSUFFICIENT_INVESTMENT", `No positions in strategy '${params.strategy}'`);
|
|
4627
4627
|
}
|
|
4628
|
+
const swapAdapter = this.registry.listSwap()[0];
|
|
4629
|
+
if (!swapAdapter?.addSwapToTx) {
|
|
4630
|
+
throw new T2000Error("PROTOCOL_UNAVAILABLE", "Swap adapter does not support composable PTB");
|
|
4631
|
+
}
|
|
4632
|
+
let swapMetas = [];
|
|
4633
|
+
const gasResult = await executeWithGas(this.client, this.keypair, async () => {
|
|
4634
|
+
swapMetas = [];
|
|
4635
|
+
const tx = new transactions.Transaction();
|
|
4636
|
+
tx.setSender(this._address);
|
|
4637
|
+
const usdcOutputs = [];
|
|
4638
|
+
for (const pos of stratPositions) {
|
|
4639
|
+
const assetInfo = SUPPORTED_ASSETS[pos.asset];
|
|
4640
|
+
const coins = await this._fetchCoins(assetInfo.type);
|
|
4641
|
+
if (coins.length === 0) {
|
|
4642
|
+
throw new T2000Error("INSUFFICIENT_BALANCE", `No ${pos.asset} coins in wallet`);
|
|
4643
|
+
}
|
|
4644
|
+
const merged = this._mergeCoinsInTx(tx, coins);
|
|
4645
|
+
const gasReserve = pos.asset === "SUI" ? GAS_RESERVE_MIN : 0;
|
|
4646
|
+
const sellAmount = Math.max(0, pos.totalAmount - gasReserve);
|
|
4647
|
+
const rawAmount = BigInt(Math.floor(sellAmount * 10 ** assetInfo.decimals));
|
|
4648
|
+
const [splitCoin] = tx.splitCoins(merged, [rawAmount]);
|
|
4649
|
+
const assetUsd = sellAmount * (pos.avgPrice || 1);
|
|
4650
|
+
const slippageBps = LOW_LIQUIDITY_ASSETS.has(pos.asset) ? 500 : 300;
|
|
4651
|
+
const { outputCoin, estimatedOut, toDecimals } = await swapAdapter.addSwapToTx(
|
|
4652
|
+
tx,
|
|
4653
|
+
this._address,
|
|
4654
|
+
splitCoin,
|
|
4655
|
+
pos.asset,
|
|
4656
|
+
"USDC",
|
|
4657
|
+
assetUsd,
|
|
4658
|
+
slippageBps
|
|
4659
|
+
);
|
|
4660
|
+
usdcOutputs.push(outputCoin);
|
|
4661
|
+
swapMetas.push({ asset: pos.asset, amount: sellAmount, estimatedOut, toDecimals });
|
|
4662
|
+
}
|
|
4663
|
+
if (usdcOutputs.length > 1) {
|
|
4664
|
+
tx.mergeCoins(usdcOutputs[0], usdcOutputs.slice(1));
|
|
4665
|
+
}
|
|
4666
|
+
tx.transferObjects([usdcOutputs[0]], this._address);
|
|
4667
|
+
return tx;
|
|
4668
|
+
});
|
|
4669
|
+
const digest = gasResult.digest;
|
|
4670
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
4628
4671
|
const sells = [];
|
|
4629
4672
|
let totalProceeds = 0;
|
|
4630
4673
|
let totalPnL = 0;
|
|
4631
|
-
|
|
4632
|
-
|
|
4633
|
-
|
|
4634
|
-
const fullAmount = pos.totalAmount;
|
|
4635
|
-
const swapAdapter = this.registry.listSwap()[0];
|
|
4636
|
-
let assetPrice = 1;
|
|
4637
|
-
try {
|
|
4638
|
-
if (swapAdapter) {
|
|
4639
|
-
if (pos.asset === "SUI") {
|
|
4640
|
-
assetPrice = await swapAdapter.getPoolPrice();
|
|
4641
|
-
} else {
|
|
4642
|
-
const q = await swapAdapter.getQuote("USDC", pos.asset, 1);
|
|
4643
|
-
assetPrice = q.expectedOutput > 0 ? 1 / q.expectedOutput : 1;
|
|
4644
|
-
}
|
|
4645
|
-
}
|
|
4646
|
-
} catch {
|
|
4647
|
-
}
|
|
4648
|
-
const strategyUsdValue = fullAmount * assetPrice;
|
|
4649
|
-
const result = await this.investSell({
|
|
4650
|
-
asset: pos.asset,
|
|
4651
|
-
usdAmount: strategyUsdValue,
|
|
4652
|
-
_strategyOnly: true
|
|
4653
|
-
});
|
|
4674
|
+
for (const meta of swapMetas) {
|
|
4675
|
+
const usdValue = meta.estimatedOut / 10 ** meta.toDecimals;
|
|
4676
|
+
const price = meta.amount > 0 ? usdValue / meta.amount : 0;
|
|
4654
4677
|
const pnl = this.portfolio.recordStrategySell(params.strategy, {
|
|
4655
|
-
id: `strat_sell_${Date.now()}_${
|
|
4678
|
+
id: `strat_sell_${Date.now()}_${meta.asset}`,
|
|
4656
4679
|
type: "sell",
|
|
4657
|
-
asset:
|
|
4658
|
-
amount:
|
|
4659
|
-
price
|
|
4660
|
-
usdValue
|
|
4661
|
-
fee:
|
|
4662
|
-
tx:
|
|
4663
|
-
timestamp:
|
|
4680
|
+
asset: meta.asset,
|
|
4681
|
+
amount: meta.amount,
|
|
4682
|
+
price,
|
|
4683
|
+
usdValue,
|
|
4684
|
+
fee: 0,
|
|
4685
|
+
tx: digest,
|
|
4686
|
+
timestamp: now
|
|
4687
|
+
});
|
|
4688
|
+
this.portfolio.recordSell({
|
|
4689
|
+
id: `inv_sell_${Date.now()}_${meta.asset}`,
|
|
4690
|
+
type: "sell",
|
|
4691
|
+
asset: meta.asset,
|
|
4692
|
+
amount: meta.amount,
|
|
4693
|
+
price,
|
|
4694
|
+
usdValue,
|
|
4695
|
+
fee: 0,
|
|
4696
|
+
tx: digest,
|
|
4697
|
+
timestamp: now
|
|
4664
4698
|
});
|
|
4665
|
-
sells.push({ asset:
|
|
4666
|
-
totalProceeds +=
|
|
4699
|
+
sells.push({ asset: meta.asset, amount: meta.amount, usdValue, realizedPnL: pnl, tx: digest });
|
|
4700
|
+
totalProceeds += usdValue;
|
|
4667
4701
|
totalPnL += pnl;
|
|
4668
|
-
totalGas += result.gasCost;
|
|
4669
|
-
gasMethod = result.gasMethod;
|
|
4670
4702
|
}
|
|
4671
|
-
return {
|
|
4703
|
+
return {
|
|
4704
|
+
success: true,
|
|
4705
|
+
strategy: params.strategy,
|
|
4706
|
+
totalProceeds,
|
|
4707
|
+
realizedPnL: totalPnL,
|
|
4708
|
+
sells,
|
|
4709
|
+
gasCost: gasResult.gasCostSui,
|
|
4710
|
+
gasMethod: gasResult.gasMethod
|
|
4711
|
+
};
|
|
4672
4712
|
}
|
|
4673
4713
|
async rebalanceStrategy(params) {
|
|
4674
4714
|
this.enforcer.assertNotLocked();
|
|
@@ -4702,8 +4742,9 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
|
|
|
4702
4742
|
currentWeights[pos.asset] = w;
|
|
4703
4743
|
beforeWeights[pos.asset] = w;
|
|
4704
4744
|
}
|
|
4705
|
-
const trades = [];
|
|
4706
4745
|
const threshold = 3;
|
|
4746
|
+
const sellOps = [];
|
|
4747
|
+
const buyOps = [];
|
|
4707
4748
|
for (const [asset, targetPct] of Object.entries(definition.allocations)) {
|
|
4708
4749
|
const currentPct = currentWeights[asset] ?? 0;
|
|
4709
4750
|
const diff = targetPct - currentPct;
|
|
@@ -4711,33 +4752,140 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
|
|
|
4711
4752
|
const usdDiff = totalValue * (Math.abs(diff) / 100);
|
|
4712
4753
|
if (usdDiff < 1) continue;
|
|
4713
4754
|
if (diff > 0) {
|
|
4714
|
-
|
|
4715
|
-
this.portfolio.recordStrategyBuy(params.strategy, {
|
|
4716
|
-
id: `strat_rebal_${Date.now()}_${asset}`,
|
|
4717
|
-
type: "buy",
|
|
4718
|
-
asset,
|
|
4719
|
-
amount: result.amount,
|
|
4720
|
-
price: result.price,
|
|
4721
|
-
usdValue: usdDiff,
|
|
4722
|
-
fee: result.fee,
|
|
4723
|
-
tx: result.tx,
|
|
4724
|
-
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
4725
|
-
});
|
|
4726
|
-
trades.push({ action: "buy", asset, usdAmount: usdDiff, amount: result.amount, tx: result.tx });
|
|
4755
|
+
buyOps.push({ asset, usdAmount: usdDiff });
|
|
4727
4756
|
} else {
|
|
4728
|
-
const
|
|
4757
|
+
const price = prices[asset] ?? 1;
|
|
4758
|
+
const assetAmount = price > 0 ? usdDiff / price : 0;
|
|
4759
|
+
sellOps.push({ asset, usdAmount: usdDiff, assetAmount });
|
|
4760
|
+
}
|
|
4761
|
+
}
|
|
4762
|
+
if (sellOps.length === 0 && buyOps.length === 0) {
|
|
4763
|
+
return { success: true, strategy: params.strategy, trades: [], beforeWeights, afterWeights: { ...beforeWeights }, targetWeights: { ...definition.allocations } };
|
|
4764
|
+
}
|
|
4765
|
+
if (!swapAdapter?.addSwapToTx) {
|
|
4766
|
+
throw new T2000Error("PROTOCOL_UNAVAILABLE", "Swap adapter does not support composable PTB");
|
|
4767
|
+
}
|
|
4768
|
+
const tradeMetas = [];
|
|
4769
|
+
const gasResult = await executeWithGas(this.client, this.keypair, async () => {
|
|
4770
|
+
tradeMetas.length = 0;
|
|
4771
|
+
const tx = new transactions.Transaction();
|
|
4772
|
+
tx.setSender(this._address);
|
|
4773
|
+
const usdcCoins = [];
|
|
4774
|
+
for (const sell of sellOps) {
|
|
4775
|
+
const assetInfo = SUPPORTED_ASSETS[sell.asset];
|
|
4776
|
+
const coins = await this._fetchCoins(assetInfo.type);
|
|
4777
|
+
if (coins.length === 0) continue;
|
|
4778
|
+
const merged = this._mergeCoinsInTx(tx, coins);
|
|
4779
|
+
const gasReserve = sell.asset === "SUI" ? GAS_RESERVE_MIN : 0;
|
|
4780
|
+
const sellAmount = Math.max(0, sell.assetAmount - gasReserve);
|
|
4781
|
+
const rawAmount = BigInt(Math.floor(sellAmount * 10 ** assetInfo.decimals));
|
|
4782
|
+
const [splitCoin] = tx.splitCoins(merged, [rawAmount]);
|
|
4783
|
+
const slippageBps = LOW_LIQUIDITY_ASSETS.has(sell.asset) ? 500 : 300;
|
|
4784
|
+
const { outputCoin, estimatedOut, toDecimals } = await swapAdapter.addSwapToTx(
|
|
4785
|
+
tx,
|
|
4786
|
+
this._address,
|
|
4787
|
+
splitCoin,
|
|
4788
|
+
sell.asset,
|
|
4789
|
+
"USDC",
|
|
4790
|
+
sell.usdAmount,
|
|
4791
|
+
slippageBps
|
|
4792
|
+
);
|
|
4793
|
+
usdcCoins.push(outputCoin);
|
|
4794
|
+
tradeMetas.push({ action: "sell", asset: sell.asset, usdAmount: sell.usdAmount, estimatedOut, toDecimals });
|
|
4795
|
+
}
|
|
4796
|
+
if (buyOps.length > 0) {
|
|
4797
|
+
const walletUsdc = await this._fetchCoins(SUPPORTED_ASSETS.USDC.type);
|
|
4798
|
+
if (walletUsdc.length > 0) {
|
|
4799
|
+
usdcCoins.push(this._mergeCoinsInTx(tx, walletUsdc));
|
|
4800
|
+
}
|
|
4801
|
+
if (usdcCoins.length === 0) {
|
|
4802
|
+
throw new T2000Error("INSUFFICIENT_BALANCE", "No USDC available for rebalance buys");
|
|
4803
|
+
}
|
|
4804
|
+
if (usdcCoins.length > 1) {
|
|
4805
|
+
tx.mergeCoins(usdcCoins[0], usdcCoins.slice(1));
|
|
4806
|
+
}
|
|
4807
|
+
const mergedUsdc = usdcCoins[0];
|
|
4808
|
+
const splitAmounts = buyOps.map(
|
|
4809
|
+
(b) => BigInt(Math.floor(b.usdAmount * 10 ** SUPPORTED_ASSETS.USDC.decimals))
|
|
4810
|
+
);
|
|
4811
|
+
const splitCoins = tx.splitCoins(mergedUsdc, splitAmounts);
|
|
4812
|
+
const outputCoins = [];
|
|
4813
|
+
for (let i = 0; i < buyOps.length; i++) {
|
|
4814
|
+
const buy = buyOps[i];
|
|
4815
|
+
const slippageBps = LOW_LIQUIDITY_ASSETS.has(buy.asset) ? 500 : 300;
|
|
4816
|
+
const { outputCoin, estimatedOut, toDecimals } = await swapAdapter.addSwapToTx(
|
|
4817
|
+
tx,
|
|
4818
|
+
this._address,
|
|
4819
|
+
splitCoins[i],
|
|
4820
|
+
"USDC",
|
|
4821
|
+
buy.asset,
|
|
4822
|
+
buy.usdAmount,
|
|
4823
|
+
slippageBps
|
|
4824
|
+
);
|
|
4825
|
+
outputCoins.push(outputCoin);
|
|
4826
|
+
tradeMetas.push({ action: "buy", asset: buy.asset, usdAmount: buy.usdAmount, estimatedOut, toDecimals });
|
|
4827
|
+
}
|
|
4828
|
+
tx.transferObjects(outputCoins, this._address);
|
|
4829
|
+
}
|
|
4830
|
+
return tx;
|
|
4831
|
+
});
|
|
4832
|
+
const digest = gasResult.digest;
|
|
4833
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
4834
|
+
const trades = [];
|
|
4835
|
+
for (const meta of tradeMetas) {
|
|
4836
|
+
const rawAmount = meta.estimatedOut / 10 ** meta.toDecimals;
|
|
4837
|
+
if (meta.action === "sell") {
|
|
4838
|
+
const price = meta.usdAmount > 0 && rawAmount > 0 ? meta.usdAmount / rawAmount : prices[meta.asset] ?? 0;
|
|
4839
|
+
const assetAmount = prices[meta.asset] > 0 ? meta.usdAmount / prices[meta.asset] : 0;
|
|
4729
4840
|
this.portfolio.recordStrategySell(params.strategy, {
|
|
4730
|
-
id: `strat_rebal_${Date.now()}_${asset}`,
|
|
4841
|
+
id: `strat_rebal_${Date.now()}_${meta.asset}`,
|
|
4731
4842
|
type: "sell",
|
|
4732
|
-
asset,
|
|
4733
|
-
amount:
|
|
4734
|
-
price
|
|
4735
|
-
usdValue:
|
|
4736
|
-
fee:
|
|
4737
|
-
tx:
|
|
4738
|
-
timestamp:
|
|
4843
|
+
asset: meta.asset,
|
|
4844
|
+
amount: assetAmount,
|
|
4845
|
+
price,
|
|
4846
|
+
usdValue: meta.usdAmount,
|
|
4847
|
+
fee: 0,
|
|
4848
|
+
tx: digest,
|
|
4849
|
+
timestamp: now
|
|
4850
|
+
});
|
|
4851
|
+
this.portfolio.recordSell({
|
|
4852
|
+
id: `inv_rebal_${Date.now()}_${meta.asset}`,
|
|
4853
|
+
type: "sell",
|
|
4854
|
+
asset: meta.asset,
|
|
4855
|
+
amount: assetAmount,
|
|
4856
|
+
price,
|
|
4857
|
+
usdValue: meta.usdAmount,
|
|
4858
|
+
fee: 0,
|
|
4859
|
+
tx: digest,
|
|
4860
|
+
timestamp: now
|
|
4861
|
+
});
|
|
4862
|
+
trades.push({ action: "sell", asset: meta.asset, usdAmount: meta.usdAmount, amount: assetAmount, tx: digest });
|
|
4863
|
+
} else {
|
|
4864
|
+
const amount = rawAmount;
|
|
4865
|
+
const price = meta.usdAmount / amount;
|
|
4866
|
+
this.portfolio.recordBuy({
|
|
4867
|
+
id: `inv_rebal_${Date.now()}_${meta.asset}`,
|
|
4868
|
+
type: "buy",
|
|
4869
|
+
asset: meta.asset,
|
|
4870
|
+
amount,
|
|
4871
|
+
price,
|
|
4872
|
+
usdValue: meta.usdAmount,
|
|
4873
|
+
fee: 0,
|
|
4874
|
+
tx: digest,
|
|
4875
|
+
timestamp: now
|
|
4876
|
+
});
|
|
4877
|
+
this.portfolio.recordStrategyBuy(params.strategy, {
|
|
4878
|
+
id: `strat_rebal_${Date.now()}_${meta.asset}`,
|
|
4879
|
+
type: "buy",
|
|
4880
|
+
asset: meta.asset,
|
|
4881
|
+
amount,
|
|
4882
|
+
price,
|
|
4883
|
+
usdValue: meta.usdAmount,
|
|
4884
|
+
fee: 0,
|
|
4885
|
+
tx: digest,
|
|
4886
|
+
timestamp: now
|
|
4739
4887
|
});
|
|
4740
|
-
trades.push({ action: "
|
|
4888
|
+
trades.push({ action: "buy", asset: meta.asset, usdAmount: meta.usdAmount, amount, tx: digest });
|
|
4741
4889
|
}
|
|
4742
4890
|
}
|
|
4743
4891
|
const afterWeights = {};
|