@t2000/sdk 5.6.0 → 5.7.0

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/browser.cjs CHANGED
@@ -231,6 +231,7 @@ var init_token_registry = __esm({
231
231
  // src/wallet/coinSelection.ts
232
232
  var coinSelection_exports = {};
233
233
  __export(coinSelection_exports, {
234
+ buildCoinToAddressBalanceMigration: () => buildCoinToAddressBalanceMigration,
234
235
  fetchAllCoins: () => fetchAllCoins,
235
236
  selectAndSplitCoin: () => selectAndSplitCoin,
236
237
  selectSuiCoin: () => selectSuiCoin
@@ -344,6 +345,32 @@ async function selectCoinObjectsOnly(tx, client, owner, coinType, amount, allowS
344
345
  });
345
346
  return { coin, effectiveAmount, swapAll };
346
347
  }
348
+ function buildCoinToAddressBalanceMigration(args) {
349
+ const { coins, coinType, owner, minAmount } = args;
350
+ const sorted = [...coins].filter((c) => c.balance > 0n).sort((a, b) => b.balance > a.balance ? 1 : b.balance < a.balance ? -1 : 0);
351
+ const selected = [];
352
+ let migratedRaw = 0n;
353
+ for (const c of sorted) {
354
+ if (migratedRaw >= minAmount) break;
355
+ selected.push(c);
356
+ migratedRaw += c.balance;
357
+ }
358
+ if (migratedRaw < minAmount) {
359
+ throw new exports.T2000Error("INSUFFICIENT_BALANCE", `Insufficient ${coinType} coin objects to migrate`, {
360
+ available: migratedRaw.toString(),
361
+ required: minAmount.toString()
362
+ });
363
+ }
364
+ const tx = new transactions.Transaction();
365
+ for (const c of selected) {
366
+ tx.moveCall({
367
+ target: "0x2::coin::send_funds",
368
+ typeArguments: [coinType],
369
+ arguments: [tx.object(c.objectId), tx.pure.address(owner)]
370
+ });
371
+ }
372
+ return { tx, migratedRaw };
373
+ }
347
374
  async function selectSuiCoin(tx, client, owner, amountMist, sponsoredContext, mergeCache) {
348
375
  if (sponsoredContext) {
349
376
  const { SUI_TYPE: SUI_TYPE2 } = await Promise.resolve().then(() => (init_token_registry(), token_registry_exports));
@@ -575,40 +602,26 @@ async function ensureAddressBalanceCovers(args) {
575
602
  required: amountRaw.toString()
576
603
  });
577
604
  }
605
+ const coins = [];
578
606
  let coinSum = 0n;
579
607
  let cursor;
580
608
  let hasNext = true;
581
609
  while (hasNext) {
582
610
  const page = await client.core.listCoins({ owner, coinType: asset, cursor: cursor ?? void 0 });
583
- for (const c of page.objects) coinSum += BigInt(c.balance);
611
+ for (const c of page.objects) {
612
+ coins.push({ objectId: c.objectId, balance: BigInt(c.balance) });
613
+ coinSum += BigInt(c.balance);
614
+ }
584
615
  cursor = page.cursor;
585
616
  hasNext = page.hasNextPage;
586
617
  }
587
618
  const addressBalance = total - coinSum;
588
619
  if (addressBalance >= amountRaw) return 0;
589
620
  const shortfall = amountRaw - addressBalance;
590
- const { Transaction: Transaction3 } = await import('@mysten/sui/transactions');
591
- const { selectAndSplitCoin: selectAndSplitCoin2 } = await Promise.resolve().then(() => (init_coinSelection(), coinSelection_exports));
621
+ const { buildCoinToAddressBalanceMigration: buildCoinToAddressBalanceMigration2 } = await Promise.resolve().then(() => (init_coinSelection(), coinSelection_exports));
592
622
  const grpcClient = await makeGrpcBuildClient(client);
593
- const migration = await executeTx(
594
- client,
595
- signer,
596
- async () => {
597
- const tx = new Transaction3();
598
- const { coin } = await selectAndSplitCoin2(tx, client, owner, asset, shortfall, {
599
- sponsoredContext: true,
600
- // coin-objects-only — never the address balance
601
- allowSwapAll: false
602
- });
603
- tx.moveCall({
604
- target: "0x2::coin::send_funds",
605
- typeArguments: [asset],
606
- arguments: [coin, tx.pure.address(owner)]
607
- });
608
- return tx;
609
- },
610
- { buildClient: grpcClient }
611
- );
623
+ const { tx } = buildCoinToAddressBalanceMigration2({ coins, coinType: asset, owner, minAmount: shortfall });
624
+ const migration = await executeTx(client, signer, () => tx, { buildClient: grpcClient });
612
625
  return migration.gasCostSui;
613
626
  }
614
627
  async function finalize(response, opts) {