@t2000/sdk 0.17.5 → 0.17.9
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 +207 -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 +207 -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,89 @@ 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 slippageBps = LOW_LIQUIDITY_ASSETS.has(pos.asset) ? 500 : 300;
|
|
4650
|
+
const { outputCoin, estimatedOut, toDecimals } = await swapAdapter.addSwapToTx(
|
|
4651
|
+
tx,
|
|
4652
|
+
this._address,
|
|
4653
|
+
splitCoin,
|
|
4654
|
+
pos.asset,
|
|
4655
|
+
"USDC",
|
|
4656
|
+
sellAmount,
|
|
4657
|
+
slippageBps
|
|
4658
|
+
);
|
|
4659
|
+
usdcOutputs.push(outputCoin);
|
|
4660
|
+
swapMetas.push({ asset: pos.asset, amount: sellAmount, estimatedOut, toDecimals });
|
|
4661
|
+
}
|
|
4662
|
+
if (usdcOutputs.length > 1) {
|
|
4663
|
+
tx.mergeCoins(usdcOutputs[0], usdcOutputs.slice(1));
|
|
4664
|
+
}
|
|
4665
|
+
tx.transferObjects([usdcOutputs[0]], this._address);
|
|
4666
|
+
return tx;
|
|
4667
|
+
});
|
|
4668
|
+
const digest = gasResult.digest;
|
|
4669
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
4628
4670
|
const sells = [];
|
|
4629
4671
|
let totalProceeds = 0;
|
|
4630
4672
|
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
|
-
});
|
|
4673
|
+
for (const meta of swapMetas) {
|
|
4674
|
+
const usdValue = meta.estimatedOut / 10 ** meta.toDecimals;
|
|
4675
|
+
const price = meta.amount > 0 ? usdValue / meta.amount : 0;
|
|
4654
4676
|
const pnl = this.portfolio.recordStrategySell(params.strategy, {
|
|
4655
|
-
id: `strat_sell_${Date.now()}_${
|
|
4677
|
+
id: `strat_sell_${Date.now()}_${meta.asset}`,
|
|
4656
4678
|
type: "sell",
|
|
4657
|
-
asset:
|
|
4658
|
-
amount:
|
|
4659
|
-
price
|
|
4660
|
-
usdValue
|
|
4661
|
-
fee:
|
|
4662
|
-
tx:
|
|
4663
|
-
timestamp:
|
|
4679
|
+
asset: meta.asset,
|
|
4680
|
+
amount: meta.amount,
|
|
4681
|
+
price,
|
|
4682
|
+
usdValue,
|
|
4683
|
+
fee: 0,
|
|
4684
|
+
tx: digest,
|
|
4685
|
+
timestamp: now
|
|
4686
|
+
});
|
|
4687
|
+
this.portfolio.recordSell({
|
|
4688
|
+
id: `inv_sell_${Date.now()}_${meta.asset}`,
|
|
4689
|
+
type: "sell",
|
|
4690
|
+
asset: meta.asset,
|
|
4691
|
+
amount: meta.amount,
|
|
4692
|
+
price,
|
|
4693
|
+
usdValue,
|
|
4694
|
+
fee: 0,
|
|
4695
|
+
tx: digest,
|
|
4696
|
+
timestamp: now
|
|
4664
4697
|
});
|
|
4665
|
-
sells.push({ asset:
|
|
4666
|
-
totalProceeds +=
|
|
4698
|
+
sells.push({ asset: meta.asset, amount: meta.amount, usdValue, realizedPnL: pnl, tx: digest });
|
|
4699
|
+
totalProceeds += usdValue;
|
|
4667
4700
|
totalPnL += pnl;
|
|
4668
|
-
totalGas += result.gasCost;
|
|
4669
|
-
gasMethod = result.gasMethod;
|
|
4670
4701
|
}
|
|
4671
|
-
return {
|
|
4702
|
+
return {
|
|
4703
|
+
success: true,
|
|
4704
|
+
strategy: params.strategy,
|
|
4705
|
+
totalProceeds,
|
|
4706
|
+
realizedPnL: totalPnL,
|
|
4707
|
+
sells,
|
|
4708
|
+
gasCost: gasResult.gasCostSui,
|
|
4709
|
+
gasMethod: gasResult.gasMethod
|
|
4710
|
+
};
|
|
4672
4711
|
}
|
|
4673
4712
|
async rebalanceStrategy(params) {
|
|
4674
4713
|
this.enforcer.assertNotLocked();
|
|
@@ -4702,8 +4741,9 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
|
|
|
4702
4741
|
currentWeights[pos.asset] = w;
|
|
4703
4742
|
beforeWeights[pos.asset] = w;
|
|
4704
4743
|
}
|
|
4705
|
-
const trades = [];
|
|
4706
4744
|
const threshold = 3;
|
|
4745
|
+
const sellOps = [];
|
|
4746
|
+
const buyOps = [];
|
|
4707
4747
|
for (const [asset, targetPct] of Object.entries(definition.allocations)) {
|
|
4708
4748
|
const currentPct = currentWeights[asset] ?? 0;
|
|
4709
4749
|
const diff = targetPct - currentPct;
|
|
@@ -4711,33 +4751,140 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
|
|
|
4711
4751
|
const usdDiff = totalValue * (Math.abs(diff) / 100);
|
|
4712
4752
|
if (usdDiff < 1) continue;
|
|
4713
4753
|
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 });
|
|
4754
|
+
buyOps.push({ asset, usdAmount: usdDiff });
|
|
4727
4755
|
} else {
|
|
4728
|
-
const
|
|
4756
|
+
const price = prices[asset] ?? 1;
|
|
4757
|
+
const assetAmount = price > 0 ? usdDiff / price : 0;
|
|
4758
|
+
sellOps.push({ asset, usdAmount: usdDiff, assetAmount });
|
|
4759
|
+
}
|
|
4760
|
+
}
|
|
4761
|
+
if (sellOps.length === 0 && buyOps.length === 0) {
|
|
4762
|
+
return { success: true, strategy: params.strategy, trades: [], beforeWeights, afterWeights: { ...beforeWeights }, targetWeights: { ...definition.allocations } };
|
|
4763
|
+
}
|
|
4764
|
+
if (!swapAdapter?.addSwapToTx) {
|
|
4765
|
+
throw new T2000Error("PROTOCOL_UNAVAILABLE", "Swap adapter does not support composable PTB");
|
|
4766
|
+
}
|
|
4767
|
+
const tradeMetas = [];
|
|
4768
|
+
const gasResult = await executeWithGas(this.client, this.keypair, async () => {
|
|
4769
|
+
tradeMetas.length = 0;
|
|
4770
|
+
const tx = new transactions.Transaction();
|
|
4771
|
+
tx.setSender(this._address);
|
|
4772
|
+
const usdcCoins = [];
|
|
4773
|
+
for (const sell of sellOps) {
|
|
4774
|
+
const assetInfo = SUPPORTED_ASSETS[sell.asset];
|
|
4775
|
+
const coins = await this._fetchCoins(assetInfo.type);
|
|
4776
|
+
if (coins.length === 0) continue;
|
|
4777
|
+
const merged = this._mergeCoinsInTx(tx, coins);
|
|
4778
|
+
const gasReserve = sell.asset === "SUI" ? GAS_RESERVE_MIN : 0;
|
|
4779
|
+
const sellAmount = Math.max(0, sell.assetAmount - gasReserve);
|
|
4780
|
+
const rawAmount = BigInt(Math.floor(sellAmount * 10 ** assetInfo.decimals));
|
|
4781
|
+
const [splitCoin] = tx.splitCoins(merged, [rawAmount]);
|
|
4782
|
+
const slippageBps = LOW_LIQUIDITY_ASSETS.has(sell.asset) ? 500 : 300;
|
|
4783
|
+
const { outputCoin, estimatedOut, toDecimals } = await swapAdapter.addSwapToTx(
|
|
4784
|
+
tx,
|
|
4785
|
+
this._address,
|
|
4786
|
+
splitCoin,
|
|
4787
|
+
sell.asset,
|
|
4788
|
+
"USDC",
|
|
4789
|
+
sellAmount,
|
|
4790
|
+
slippageBps
|
|
4791
|
+
);
|
|
4792
|
+
usdcCoins.push(outputCoin);
|
|
4793
|
+
tradeMetas.push({ action: "sell", asset: sell.asset, usdAmount: sell.usdAmount, estimatedOut, toDecimals });
|
|
4794
|
+
}
|
|
4795
|
+
if (buyOps.length > 0) {
|
|
4796
|
+
const walletUsdc = await this._fetchCoins(SUPPORTED_ASSETS.USDC.type);
|
|
4797
|
+
if (walletUsdc.length > 0) {
|
|
4798
|
+
usdcCoins.push(this._mergeCoinsInTx(tx, walletUsdc));
|
|
4799
|
+
}
|
|
4800
|
+
if (usdcCoins.length === 0) {
|
|
4801
|
+
throw new T2000Error("INSUFFICIENT_BALANCE", "No USDC available for rebalance buys");
|
|
4802
|
+
}
|
|
4803
|
+
if (usdcCoins.length > 1) {
|
|
4804
|
+
tx.mergeCoins(usdcCoins[0], usdcCoins.slice(1));
|
|
4805
|
+
}
|
|
4806
|
+
const mergedUsdc = usdcCoins[0];
|
|
4807
|
+
const splitAmounts = buyOps.map(
|
|
4808
|
+
(b) => BigInt(Math.floor(b.usdAmount * 10 ** SUPPORTED_ASSETS.USDC.decimals))
|
|
4809
|
+
);
|
|
4810
|
+
const splitCoins = tx.splitCoins(mergedUsdc, splitAmounts);
|
|
4811
|
+
const outputCoins = [];
|
|
4812
|
+
for (let i = 0; i < buyOps.length; i++) {
|
|
4813
|
+
const buy = buyOps[i];
|
|
4814
|
+
const slippageBps = LOW_LIQUIDITY_ASSETS.has(buy.asset) ? 500 : 300;
|
|
4815
|
+
const { outputCoin, estimatedOut, toDecimals } = await swapAdapter.addSwapToTx(
|
|
4816
|
+
tx,
|
|
4817
|
+
this._address,
|
|
4818
|
+
splitCoins[i],
|
|
4819
|
+
"USDC",
|
|
4820
|
+
buy.asset,
|
|
4821
|
+
buy.usdAmount,
|
|
4822
|
+
slippageBps
|
|
4823
|
+
);
|
|
4824
|
+
outputCoins.push(outputCoin);
|
|
4825
|
+
tradeMetas.push({ action: "buy", asset: buy.asset, usdAmount: buy.usdAmount, estimatedOut, toDecimals });
|
|
4826
|
+
}
|
|
4827
|
+
tx.transferObjects(outputCoins, this._address);
|
|
4828
|
+
}
|
|
4829
|
+
return tx;
|
|
4830
|
+
});
|
|
4831
|
+
const digest = gasResult.digest;
|
|
4832
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
4833
|
+
const trades = [];
|
|
4834
|
+
for (const meta of tradeMetas) {
|
|
4835
|
+
const rawAmount = meta.estimatedOut / 10 ** meta.toDecimals;
|
|
4836
|
+
if (meta.action === "sell") {
|
|
4837
|
+
const price = meta.usdAmount > 0 && rawAmount > 0 ? meta.usdAmount / rawAmount : prices[meta.asset] ?? 0;
|
|
4838
|
+
const assetAmount = prices[meta.asset] > 0 ? meta.usdAmount / prices[meta.asset] : 0;
|
|
4729
4839
|
this.portfolio.recordStrategySell(params.strategy, {
|
|
4730
|
-
id: `strat_rebal_${Date.now()}_${asset}`,
|
|
4840
|
+
id: `strat_rebal_${Date.now()}_${meta.asset}`,
|
|
4731
4841
|
type: "sell",
|
|
4732
|
-
asset,
|
|
4733
|
-
amount:
|
|
4734
|
-
price
|
|
4735
|
-
usdValue:
|
|
4736
|
-
fee:
|
|
4737
|
-
tx:
|
|
4738
|
-
timestamp:
|
|
4842
|
+
asset: meta.asset,
|
|
4843
|
+
amount: assetAmount,
|
|
4844
|
+
price,
|
|
4845
|
+
usdValue: meta.usdAmount,
|
|
4846
|
+
fee: 0,
|
|
4847
|
+
tx: digest,
|
|
4848
|
+
timestamp: now
|
|
4849
|
+
});
|
|
4850
|
+
this.portfolio.recordSell({
|
|
4851
|
+
id: `inv_rebal_${Date.now()}_${meta.asset}`,
|
|
4852
|
+
type: "sell",
|
|
4853
|
+
asset: meta.asset,
|
|
4854
|
+
amount: assetAmount,
|
|
4855
|
+
price,
|
|
4856
|
+
usdValue: meta.usdAmount,
|
|
4857
|
+
fee: 0,
|
|
4858
|
+
tx: digest,
|
|
4859
|
+
timestamp: now
|
|
4860
|
+
});
|
|
4861
|
+
trades.push({ action: "sell", asset: meta.asset, usdAmount: meta.usdAmount, amount: assetAmount, tx: digest });
|
|
4862
|
+
} else {
|
|
4863
|
+
const amount = rawAmount;
|
|
4864
|
+
const price = meta.usdAmount / amount;
|
|
4865
|
+
this.portfolio.recordBuy({
|
|
4866
|
+
id: `inv_rebal_${Date.now()}_${meta.asset}`,
|
|
4867
|
+
type: "buy",
|
|
4868
|
+
asset: meta.asset,
|
|
4869
|
+
amount,
|
|
4870
|
+
price,
|
|
4871
|
+
usdValue: meta.usdAmount,
|
|
4872
|
+
fee: 0,
|
|
4873
|
+
tx: digest,
|
|
4874
|
+
timestamp: now
|
|
4875
|
+
});
|
|
4876
|
+
this.portfolio.recordStrategyBuy(params.strategy, {
|
|
4877
|
+
id: `strat_rebal_${Date.now()}_${meta.asset}`,
|
|
4878
|
+
type: "buy",
|
|
4879
|
+
asset: meta.asset,
|
|
4880
|
+
amount,
|
|
4881
|
+
price,
|
|
4882
|
+
usdValue: meta.usdAmount,
|
|
4883
|
+
fee: 0,
|
|
4884
|
+
tx: digest,
|
|
4885
|
+
timestamp: now
|
|
4739
4886
|
});
|
|
4740
|
-
trades.push({ action: "
|
|
4887
|
+
trades.push({ action: "buy", asset: meta.asset, usdAmount: meta.usdAmount, amount, tx: digest });
|
|
4741
4888
|
}
|
|
4742
4889
|
}
|
|
4743
4890
|
const afterWeights = {};
|