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