@t2000/sdk 0.16.1 → 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.js CHANGED
@@ -4338,20 +4338,22 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4338
4338
  if (!params.usdAmount || params.usdAmount <= 0) {
4339
4339
  throw new T2000Error("INVALID_AMOUNT", "Strategy investment must be > $0");
4340
4340
  }
4341
+ this.enforcer.check({ operation: "invest", amount: params.usdAmount });
4341
4342
  const bal = await queryBalance(this.client, this._address);
4342
4343
  if (bal.available < params.usdAmount) {
4343
4344
  throw new T2000Error("INSUFFICIENT_BALANCE", `Insufficient balance. Available: $${bal.available.toFixed(2)}, requested: $${params.usdAmount.toFixed(2)}`);
4344
4345
  }
4345
4346
  const buys = [];
4347
+ const allocEntries = Object.entries(definition.allocations);
4346
4348
  if (params.dryRun) {
4347
- const swapAdapter = this.registry.listSwap()[0];
4348
- for (const [asset, pct] of Object.entries(definition.allocations)) {
4349
+ const swapAdapter2 = this.registry.listSwap()[0];
4350
+ for (const [asset, pct] of allocEntries) {
4349
4351
  const assetUsd = params.usdAmount * (pct / 100);
4350
4352
  let estAmount = 0;
4351
4353
  let estPrice = 0;
4352
4354
  try {
4353
- if (swapAdapter) {
4354
- const quote = await swapAdapter.getQuote("USDC", asset, assetUsd);
4355
+ if (swapAdapter2) {
4356
+ const quote = await swapAdapter2.getQuote("USDC", asset, assetUsd);
4355
4357
  estAmount = quote.expectedOutput;
4356
4358
  estPrice = assetUsd / estAmount;
4357
4359
  }
@@ -4361,27 +4363,76 @@ To sell investment: t2000 invest sell ${params.amount} ${fromAsset}`,
4361
4363
  }
4362
4364
  return { success: true, strategy: params.strategy, totalInvested: params.usdAmount, buys, gasCost: 0, gasMethod: "self-funded" };
4363
4365
  }
4364
- let totalGas = 0;
4365
- let gasMethod = "self-funded";
4366
- for (const [asset, pct] of Object.entries(definition.allocations)) {
4367
- const assetUsd = params.usdAmount * (pct / 100);
4368
- const result = await this.investBuy({ asset, usdAmount: assetUsd });
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
+ });
4369
4415
  this.portfolio.recordStrategyBuy(params.strategy, {
4370
- id: `strat_${Date.now()}_${asset}`,
4416
+ id: `strat_${Date.now()}_${meta.asset}`,
4371
4417
  type: "buy",
4372
- asset,
4373
- amount: result.amount,
4374
- price: result.price,
4375
- usdValue: assetUsd,
4376
- fee: result.fee,
4377
- tx: result.tx,
4378
- timestamp: (/* @__PURE__ */ new Date()).toISOString()
4418
+ asset: meta.asset,
4419
+ amount,
4420
+ price,
4421
+ usdValue: meta.usdAmount,
4422
+ fee: 0,
4423
+ tx: digest,
4424
+ timestamp: now
4379
4425
  });
4380
- buys.push({ asset, usdAmount: assetUsd, amount: result.amount, price: result.price, tx: result.tx });
4381
- totalGas += result.gasCost;
4382
- gasMethod = result.gasMethod;
4426
+ buys.push({ asset: meta.asset, usdAmount: meta.usdAmount, amount, price, tx: digest });
4383
4427
  }
4384
- return { success: true, strategy: params.strategy, totalInvested: params.usdAmount, buys, gasCost: totalGas, gasMethod };
4428
+ return {
4429
+ success: true,
4430
+ strategy: params.strategy,
4431
+ totalInvested: params.usdAmount,
4432
+ buys,
4433
+ gasCost: gasResult.gasCostSui,
4434
+ gasMethod: gasResult.gasMethod
4435
+ };
4385
4436
  }
4386
4437
  async sellStrategy(params) {
4387
4438
  this.enforcer.assertNotLocked();