@t2000/sdk 0.18.41 → 0.18.42
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 +74 -42
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +74 -42
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.cjs
CHANGED
|
@@ -56766,6 +56766,19 @@ var PortfolioManager = class {
|
|
|
56766
56766
|
if (!bucket) return false;
|
|
56767
56767
|
return Object.values(bucket).some((p) => p.totalAmount > 0);
|
|
56768
56768
|
}
|
|
56769
|
+
closeStrategyPosition(strategyKey, asset) {
|
|
56770
|
+
this.load();
|
|
56771
|
+
const bucket = this.data.strategies[strategyKey];
|
|
56772
|
+
if (!bucket?.[asset]) return;
|
|
56773
|
+
bucket[asset].totalAmount = 0;
|
|
56774
|
+
bucket[asset].costBasis = 0;
|
|
56775
|
+
bucket[asset].avgPrice = 0;
|
|
56776
|
+
const hasPositions = Object.values(bucket).some((p) => p.totalAmount > 0);
|
|
56777
|
+
if (!hasPositions) {
|
|
56778
|
+
delete this.data.strategies[strategyKey];
|
|
56779
|
+
}
|
|
56780
|
+
this.save();
|
|
56781
|
+
}
|
|
56769
56782
|
};
|
|
56770
56783
|
function emptyData2() {
|
|
56771
56784
|
return { strategies: {} };
|
|
@@ -58116,16 +58129,14 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
|
|
|
58116
58129
|
if (!(params.asset in INVESTMENT_ASSETS)) {
|
|
58117
58130
|
throw new T2000Error("ASSET_NOT_SUPPORTED", `${params.asset} is not available for investment`);
|
|
58118
58131
|
}
|
|
58119
|
-
|
|
58120
|
-
|
|
58121
|
-
throw new T2000Error("INSUFFICIENT_INVESTMENT", `No ${params.asset} position to sell`);
|
|
58122
|
-
}
|
|
58123
|
-
const didAutoWithdraw = !!(pos.earning && pos.earningProtocol);
|
|
58132
|
+
let pos = this.portfolio.getPosition(params.asset);
|
|
58133
|
+
const didAutoWithdraw = !!(pos?.earning && pos.earningProtocol);
|
|
58124
58134
|
if (didAutoWithdraw) {
|
|
58125
58135
|
const unearnResult = await this.investUnearn({ asset: params.asset });
|
|
58126
58136
|
if (unearnResult.tx) {
|
|
58127
58137
|
await this.client.waitForTransaction({ digest: unearnResult.tx, options: { showEffects: true } });
|
|
58128
58138
|
}
|
|
58139
|
+
pos = this.portfolio.getPosition(params.asset);
|
|
58129
58140
|
}
|
|
58130
58141
|
const assetInfo = SUPPORTED_ASSETS[params.asset];
|
|
58131
58142
|
const gasReserve = params.asset === "SUI" ? GAS_RESERVE_MIN : 0;
|
|
@@ -58140,16 +58151,20 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
|
|
|
58140
58151
|
await new Promise((r) => setTimeout(r, 1500));
|
|
58141
58152
|
}
|
|
58142
58153
|
const maxSellable = Math.max(0, walletAmount - gasReserve);
|
|
58154
|
+
const trackedAmount = pos && pos.totalAmount > 0 ? pos.totalAmount : maxSellable;
|
|
58155
|
+
if (trackedAmount <= 0) {
|
|
58156
|
+
throw new T2000Error("INSUFFICIENT_INVESTMENT", `No ${params.asset} position to sell`);
|
|
58157
|
+
}
|
|
58143
58158
|
let sellAmountAsset;
|
|
58144
58159
|
if (params.usdAmount === "all") {
|
|
58145
|
-
sellAmountAsset = Math.min(
|
|
58160
|
+
sellAmountAsset = Math.min(trackedAmount, maxSellable);
|
|
58146
58161
|
} else {
|
|
58147
58162
|
const swapAdapter = this.registry.listSwap()[0];
|
|
58148
58163
|
if (!swapAdapter) throw new T2000Error("PROTOCOL_UNAVAILABLE", "No swap adapter available");
|
|
58149
58164
|
const quote = await swapAdapter.getQuote("USDC", params.asset, 1);
|
|
58150
58165
|
const assetPrice = 1 / quote.expectedOutput;
|
|
58151
58166
|
sellAmountAsset = params.usdAmount / assetPrice;
|
|
58152
|
-
const maxPosition = params._strategyOnly ? maxSellable :
|
|
58167
|
+
const maxPosition = params._strategyOnly ? maxSellable : trackedAmount;
|
|
58153
58168
|
sellAmountAsset = Math.min(sellAmountAsset, maxPosition);
|
|
58154
58169
|
if (sellAmountAsset > maxSellable) {
|
|
58155
58170
|
throw new T2000Error(
|
|
@@ -58184,17 +58199,21 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
|
|
|
58184
58199
|
}
|
|
58185
58200
|
}
|
|
58186
58201
|
const price = swapResult.toAmount / sellAmountAsset;
|
|
58187
|
-
|
|
58188
|
-
|
|
58189
|
-
|
|
58190
|
-
|
|
58191
|
-
|
|
58192
|
-
|
|
58193
|
-
|
|
58194
|
-
|
|
58195
|
-
|
|
58196
|
-
|
|
58197
|
-
|
|
58202
|
+
let realizedPnL = 0;
|
|
58203
|
+
try {
|
|
58204
|
+
realizedPnL = this.portfolio.recordSell({
|
|
58205
|
+
id: `inv_${Date.now()}`,
|
|
58206
|
+
type: "sell",
|
|
58207
|
+
asset: params.asset,
|
|
58208
|
+
amount: sellAmountAsset,
|
|
58209
|
+
price,
|
|
58210
|
+
usdValue: swapResult.toAmount,
|
|
58211
|
+
fee: swapResult.fee,
|
|
58212
|
+
tx: swapResult.tx,
|
|
58213
|
+
timestamp: (/* @__PURE__ */ new Date()).toISOString()
|
|
58214
|
+
});
|
|
58215
|
+
} catch {
|
|
58216
|
+
}
|
|
58198
58217
|
if (params.usdAmount === "all" && !params._strategyOnly) {
|
|
58199
58218
|
this.portfolio.closePosition(params.asset);
|
|
58200
58219
|
}
|
|
@@ -58697,34 +58716,47 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
|
|
|
58697
58716
|
for (const meta of swapMetas) {
|
|
58698
58717
|
const usdValue = meta.estimatedOut / 10 ** meta.toDecimals;
|
|
58699
58718
|
const price = meta.amount > 0 ? usdValue / meta.amount : 0;
|
|
58700
|
-
|
|
58701
|
-
|
|
58702
|
-
|
|
58703
|
-
|
|
58704
|
-
|
|
58705
|
-
|
|
58706
|
-
|
|
58707
|
-
|
|
58708
|
-
|
|
58709
|
-
|
|
58710
|
-
|
|
58711
|
-
|
|
58712
|
-
|
|
58713
|
-
|
|
58714
|
-
|
|
58715
|
-
|
|
58716
|
-
|
|
58717
|
-
|
|
58718
|
-
|
|
58719
|
-
|
|
58720
|
-
|
|
58721
|
-
|
|
58719
|
+
let pnl = 0;
|
|
58720
|
+
try {
|
|
58721
|
+
pnl = this.portfolio.recordStrategySell(params.strategy, {
|
|
58722
|
+
id: `strat_sell_${Date.now()}_${meta.asset}`,
|
|
58723
|
+
type: "sell",
|
|
58724
|
+
asset: meta.asset,
|
|
58725
|
+
amount: meta.amount,
|
|
58726
|
+
price,
|
|
58727
|
+
usdValue,
|
|
58728
|
+
fee: 0,
|
|
58729
|
+
tx: digest,
|
|
58730
|
+
timestamp: now
|
|
58731
|
+
});
|
|
58732
|
+
} catch {
|
|
58733
|
+
}
|
|
58734
|
+
try {
|
|
58735
|
+
this.portfolio.recordSell({
|
|
58736
|
+
id: `inv_sell_${Date.now()}_${meta.asset}`,
|
|
58737
|
+
type: "sell",
|
|
58738
|
+
asset: meta.asset,
|
|
58739
|
+
amount: meta.amount,
|
|
58740
|
+
price,
|
|
58741
|
+
usdValue,
|
|
58742
|
+
fee: 0,
|
|
58743
|
+
tx: digest,
|
|
58744
|
+
timestamp: now
|
|
58745
|
+
});
|
|
58746
|
+
} catch {
|
|
58747
|
+
}
|
|
58722
58748
|
sells.push({ asset: meta.asset, amount: meta.amount, usdValue, realizedPnL: pnl, tx: digest });
|
|
58723
58749
|
totalProceeds += usdValue;
|
|
58724
58750
|
totalPnL += pnl;
|
|
58725
58751
|
}
|
|
58726
|
-
if (
|
|
58727
|
-
|
|
58752
|
+
if (this.portfolio.hasStrategyPositions(params.strategy)) {
|
|
58753
|
+
if (unearnFailures.length === 0) {
|
|
58754
|
+
this.portfolio.clearStrategy(params.strategy);
|
|
58755
|
+
} else {
|
|
58756
|
+
for (const s of sells) {
|
|
58757
|
+
this.portfolio.closeStrategyPosition(params.strategy, s.asset);
|
|
58758
|
+
}
|
|
58759
|
+
}
|
|
58728
58760
|
}
|
|
58729
58761
|
const failed = unearnFailures.map((f) => ({ asset: f.asset, reason: f.error }));
|
|
58730
58762
|
return {
|