@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/index.cjs CHANGED
@@ -275,6 +275,7 @@ var init_token_registry = __esm({
275
275
  // src/wallet/coinSelection.ts
276
276
  var coinSelection_exports = {};
277
277
  __export(coinSelection_exports, {
278
+ buildCoinToAddressBalanceMigration: () => buildCoinToAddressBalanceMigration,
278
279
  fetchAllCoins: () => fetchAllCoins,
279
280
  selectAndSplitCoin: () => selectAndSplitCoin,
280
281
  selectSuiCoin: () => selectSuiCoin
@@ -388,6 +389,32 @@ async function selectCoinObjectsOnly(tx, client, owner, coinType, amount, allowS
388
389
  });
389
390
  return { coin, effectiveAmount, swapAll };
390
391
  }
392
+ function buildCoinToAddressBalanceMigration(args) {
393
+ const { coins, coinType, owner, minAmount } = args;
394
+ const sorted = [...coins].filter((c) => c.balance > 0n).sort((a, b) => b.balance > a.balance ? 1 : b.balance < a.balance ? -1 : 0);
395
+ const selected = [];
396
+ let migratedRaw = 0n;
397
+ for (const c of sorted) {
398
+ if (migratedRaw >= minAmount) break;
399
+ selected.push(c);
400
+ migratedRaw += c.balance;
401
+ }
402
+ if (migratedRaw < minAmount) {
403
+ throw new exports.T2000Error("INSUFFICIENT_BALANCE", `Insufficient ${coinType} coin objects to migrate`, {
404
+ available: migratedRaw.toString(),
405
+ required: minAmount.toString()
406
+ });
407
+ }
408
+ const tx = new transactions.Transaction();
409
+ for (const c of selected) {
410
+ tx.moveCall({
411
+ target: "0x2::coin::send_funds",
412
+ typeArguments: [coinType],
413
+ arguments: [tx.object(c.objectId), tx.pure.address(owner)]
414
+ });
415
+ }
416
+ return { tx, migratedRaw };
417
+ }
391
418
  async function selectSuiCoin(tx, client, owner, amountMist, sponsoredContext, mergeCache) {
392
419
  if (sponsoredContext) {
393
420
  const { SUI_TYPE: SUI_TYPE2 } = await Promise.resolve().then(() => (init_token_registry(), token_registry_exports));
@@ -1166,40 +1193,26 @@ async function ensureAddressBalanceCovers(args) {
1166
1193
  required: amountRaw.toString()
1167
1194
  });
1168
1195
  }
1196
+ const coins = [];
1169
1197
  let coinSum = 0n;
1170
1198
  let cursor;
1171
1199
  let hasNext = true;
1172
1200
  while (hasNext) {
1173
1201
  const page = await client.core.listCoins({ owner, coinType: asset, cursor: cursor ?? void 0 });
1174
- for (const c of page.objects) coinSum += BigInt(c.balance);
1202
+ for (const c of page.objects) {
1203
+ coins.push({ objectId: c.objectId, balance: BigInt(c.balance) });
1204
+ coinSum += BigInt(c.balance);
1205
+ }
1175
1206
  cursor = page.cursor;
1176
1207
  hasNext = page.hasNextPage;
1177
1208
  }
1178
1209
  const addressBalance = total - coinSum;
1179
1210
  if (addressBalance >= amountRaw) return 0;
1180
1211
  const shortfall = amountRaw - addressBalance;
1181
- const { Transaction: Transaction6 } = await import('@mysten/sui/transactions');
1182
- const { selectAndSplitCoin: selectAndSplitCoin2 } = await Promise.resolve().then(() => (init_coinSelection(), coinSelection_exports));
1212
+ const { buildCoinToAddressBalanceMigration: buildCoinToAddressBalanceMigration2 } = await Promise.resolve().then(() => (init_coinSelection(), coinSelection_exports));
1183
1213
  const grpcClient = await makeGrpcBuildClient(client);
1184
- const migration = await executeTx(
1185
- client,
1186
- signer,
1187
- async () => {
1188
- const tx = new Transaction6();
1189
- const { coin } = await selectAndSplitCoin2(tx, client, owner, asset, shortfall, {
1190
- sponsoredContext: true,
1191
- // coin-objects-only — never the address balance
1192
- allowSwapAll: false
1193
- });
1194
- tx.moveCall({
1195
- target: "0x2::coin::send_funds",
1196
- typeArguments: [asset],
1197
- arguments: [coin, tx.pure.address(owner)]
1198
- });
1199
- return tx;
1200
- },
1201
- { buildClient: grpcClient }
1202
- );
1214
+ const { tx } = buildCoinToAddressBalanceMigration2({ coins, coinType: asset, owner, minAmount: shortfall });
1215
+ const migration = await executeTx(client, signer, () => tx, { buildClient: grpcClient });
1203
1216
  return migration.gasCostSui;
1204
1217
  }
1205
1218
  async function finalize(response, opts) {