@t2000/sdk 5.6.0 → 5.6.1

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