@t2000/sdk 0.16.0 → 0.16.2
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 +80 -30
- package/dist/index.cjs.map +1 -1
- package/dist/index.js +80 -30
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3095,15 +3095,14 @@ var StrategyManager = class {
|
|
|
3095
3095
|
}
|
|
3096
3096
|
}
|
|
3097
3097
|
validateMinAmount(allocations, totalUsd) {
|
|
3098
|
-
const
|
|
3099
|
-
|
|
3100
|
-
|
|
3101
|
-
|
|
3102
|
-
|
|
3103
|
-
|
|
3104
|
-
|
|
3105
|
-
|
|
3106
|
-
}
|
|
3098
|
+
const smallestPct = Math.min(...Object.values(allocations));
|
|
3099
|
+
const minRequired = Math.ceil(100 / smallestPct);
|
|
3100
|
+
if (totalUsd < minRequired) {
|
|
3101
|
+
const smallestAsset = Object.entries(allocations).find(([, p]) => p === smallestPct)?.[0] ?? "?";
|
|
3102
|
+
throw new T2000Error(
|
|
3103
|
+
"STRATEGY_MIN_AMOUNT",
|
|
3104
|
+
`Minimum $${minRequired} for this strategy (${smallestAsset} at ${smallestPct}% needs at least $1)`
|
|
3105
|
+
);
|
|
3107
3106
|
}
|
|
3108
3107
|
}
|
|
3109
3108
|
};
|
|
@@ -4339,20 +4338,22 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
|
|
|
4339
4338
|
if (!params.usdAmount || params.usdAmount <= 0) {
|
|
4340
4339
|
throw new T2000Error("INVALID_AMOUNT", "Strategy investment must be > $0");
|
|
4341
4340
|
}
|
|
4341
|
+
this.enforcer.check({ operation: "invest", amount: params.usdAmount });
|
|
4342
4342
|
const bal = await queryBalance(this.client, this._address);
|
|
4343
4343
|
if (bal.available < params.usdAmount) {
|
|
4344
4344
|
throw new T2000Error("INSUFFICIENT_BALANCE", `Insufficient balance. Available: $${bal.available.toFixed(2)}, requested: $${params.usdAmount.toFixed(2)}`);
|
|
4345
4345
|
}
|
|
4346
4346
|
const buys = [];
|
|
4347
|
+
const allocEntries = Object.entries(definition.allocations);
|
|
4347
4348
|
if (params.dryRun) {
|
|
4348
|
-
const
|
|
4349
|
-
for (const [asset, pct] of
|
|
4349
|
+
const swapAdapter2 = this.registry.listSwap()[0];
|
|
4350
|
+
for (const [asset, pct] of allocEntries) {
|
|
4350
4351
|
const assetUsd = params.usdAmount * (pct / 100);
|
|
4351
4352
|
let estAmount = 0;
|
|
4352
4353
|
let estPrice = 0;
|
|
4353
4354
|
try {
|
|
4354
|
-
if (
|
|
4355
|
-
const quote = await
|
|
4355
|
+
if (swapAdapter2) {
|
|
4356
|
+
const quote = await swapAdapter2.getQuote("USDC", asset, assetUsd);
|
|
4356
4357
|
estAmount = quote.expectedOutput;
|
|
4357
4358
|
estPrice = assetUsd / estAmount;
|
|
4358
4359
|
}
|
|
@@ -4362,27 +4363,76 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
|
|
|
4362
4363
|
}
|
|
4363
4364
|
return { success: true, strategy: params.strategy, totalInvested: params.usdAmount, buys, gasCost: 0, gasMethod: "self-funded" };
|
|
4364
4365
|
}
|
|
4365
|
-
|
|
4366
|
-
|
|
4367
|
-
|
|
4368
|
-
|
|
4369
|
-
|
|
4366
|
+
const swapAdapter = this.registry.listSwap()[0];
|
|
4367
|
+
if (!swapAdapter?.addSwapToTx) {
|
|
4368
|
+
throw new T2000Error("PROTOCOL_UNAVAILABLE", "Swap adapter does not support composable PTB");
|
|
4369
|
+
}
|
|
4370
|
+
const swapMetas = [];
|
|
4371
|
+
const gasResult = await executeWithGas(this.client, this.keypair, async () => {
|
|
4372
|
+
const tx = new Transaction();
|
|
4373
|
+
tx.setSender(this._address);
|
|
4374
|
+
const usdcCoins = await this._fetchCoins(SUPPORTED_ASSETS.USDC.type);
|
|
4375
|
+
if (usdcCoins.length === 0) throw new T2000Error("INSUFFICIENT_BALANCE", "No USDC coins found");
|
|
4376
|
+
const mergedUsdc = this._mergeCoinsInTx(tx, usdcCoins);
|
|
4377
|
+
const splitAmounts = allocEntries.map(
|
|
4378
|
+
([, pct]) => BigInt(Math.floor(params.usdAmount * (pct / 100) * 10 ** SUPPORTED_ASSETS.USDC.decimals))
|
|
4379
|
+
);
|
|
4380
|
+
const splitCoins = tx.splitCoins(mergedUsdc, splitAmounts);
|
|
4381
|
+
const outputCoins = [];
|
|
4382
|
+
for (let i = 0; i < allocEntries.length; i++) {
|
|
4383
|
+
const [asset] = allocEntries[i];
|
|
4384
|
+
const assetUsd = params.usdAmount * (allocEntries[i][1] / 100);
|
|
4385
|
+
const { outputCoin, estimatedOut, toDecimals } = await swapAdapter.addSwapToTx(
|
|
4386
|
+
tx,
|
|
4387
|
+
this._address,
|
|
4388
|
+
splitCoins[i],
|
|
4389
|
+
"USDC",
|
|
4390
|
+
asset,
|
|
4391
|
+
assetUsd
|
|
4392
|
+
);
|
|
4393
|
+
outputCoins.push(outputCoin);
|
|
4394
|
+
swapMetas.push({ asset, usdAmount: assetUsd, estimatedOut, toDecimals });
|
|
4395
|
+
}
|
|
4396
|
+
tx.transferObjects(outputCoins, this._address);
|
|
4397
|
+
return tx;
|
|
4398
|
+
});
|
|
4399
|
+
const digest = gasResult.digest;
|
|
4400
|
+
const now = (/* @__PURE__ */ new Date()).toISOString();
|
|
4401
|
+
for (const meta of swapMetas) {
|
|
4402
|
+
const amount = meta.estimatedOut / 10 ** meta.toDecimals;
|
|
4403
|
+
const price = meta.usdAmount / amount;
|
|
4404
|
+
this.portfolio.recordBuy({
|
|
4405
|
+
id: `inv_${Date.now()}_${meta.asset}`,
|
|
4406
|
+
type: "buy",
|
|
4407
|
+
asset: meta.asset,
|
|
4408
|
+
amount,
|
|
4409
|
+
price,
|
|
4410
|
+
usdValue: meta.usdAmount,
|
|
4411
|
+
fee: 0,
|
|
4412
|
+
tx: digest,
|
|
4413
|
+
timestamp: now
|
|
4414
|
+
});
|
|
4370
4415
|
this.portfolio.recordStrategyBuy(params.strategy, {
|
|
4371
|
-
id: `strat_${Date.now()}_${asset}`,
|
|
4416
|
+
id: `strat_${Date.now()}_${meta.asset}`,
|
|
4372
4417
|
type: "buy",
|
|
4373
|
-
asset,
|
|
4374
|
-
amount
|
|
4375
|
-
price
|
|
4376
|
-
usdValue:
|
|
4377
|
-
fee:
|
|
4378
|
-
tx:
|
|
4379
|
-
timestamp:
|
|
4418
|
+
asset: meta.asset,
|
|
4419
|
+
amount,
|
|
4420
|
+
price,
|
|
4421
|
+
usdValue: meta.usdAmount,
|
|
4422
|
+
fee: 0,
|
|
4423
|
+
tx: digest,
|
|
4424
|
+
timestamp: now
|
|
4380
4425
|
});
|
|
4381
|
-
buys.push({ asset, usdAmount:
|
|
4382
|
-
totalGas += result.gasCost;
|
|
4383
|
-
gasMethod = result.gasMethod;
|
|
4426
|
+
buys.push({ asset: meta.asset, usdAmount: meta.usdAmount, amount, price, tx: digest });
|
|
4384
4427
|
}
|
|
4385
|
-
return {
|
|
4428
|
+
return {
|
|
4429
|
+
success: true,
|
|
4430
|
+
strategy: params.strategy,
|
|
4431
|
+
totalInvested: params.usdAmount,
|
|
4432
|
+
buys,
|
|
4433
|
+
gasCost: gasResult.gasCostSui,
|
|
4434
|
+
gasMethod: gasResult.gasMethod
|
|
4435
|
+
};
|
|
4386
4436
|
}
|
|
4387
4437
|
async sellStrategy(params) {
|
|
4388
4438
|
this.enforcer.assertNotLocked();
|