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