@t2000/cli 1.11.0 → 1.11.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.
@@ -17203,6 +17203,95 @@ var init_volo = __esm({
17203
17203
  VOLO_STATS_URL = "https://open-api.naviprotocol.io/api/volo/stats";
17204
17204
  }
17205
17205
  });
17206
+ var coinSelection_exports = {};
17207
+ __export(coinSelection_exports, {
17208
+ fetchAllCoins: () => fetchAllCoins,
17209
+ selectAndSplitCoin: () => selectAndSplitCoin,
17210
+ selectSuiCoin: () => selectSuiCoin
17211
+ });
17212
+ function getMergeCache(tx) {
17213
+ let cache = ptbMergeCache.get(tx);
17214
+ if (!cache) {
17215
+ cache = /* @__PURE__ */ new Map();
17216
+ ptbMergeCache.set(tx, cache);
17217
+ }
17218
+ return cache;
17219
+ }
17220
+ async function fetchAllCoins(client, owner, coinType) {
17221
+ const ids = [];
17222
+ let totalBalance = 0n;
17223
+ let cursor;
17224
+ let hasNext = true;
17225
+ while (hasNext) {
17226
+ const page = await client.getCoins({ owner, coinType, cursor: cursor ?? void 0 });
17227
+ for (const c of page.data) {
17228
+ ids.push(c.coinObjectId);
17229
+ totalBalance += BigInt(c.balance);
17230
+ }
17231
+ cursor = page.nextCursor;
17232
+ hasNext = page.hasNextPage;
17233
+ }
17234
+ return { ids, totalBalance };
17235
+ }
17236
+ async function selectAndSplitCoin(tx, client, owner, coinType, amount, options = {}) {
17237
+ const cache = getMergeCache(tx);
17238
+ const cacheKey = `${owner}|${coinType}`;
17239
+ const cached = cache.get(cacheKey);
17240
+ if (cached) {
17241
+ const allowSwapAll2 = options.allowSwapAll ?? true;
17242
+ if (amount !== "all" && amount > cached.remaining && !allowSwapAll2) {
17243
+ throw new T2000Error("INSUFFICIENT_BALANCE", `Insufficient balance for ${coinType}`, {
17244
+ available: cached.remaining.toString(),
17245
+ required: amount.toString()
17246
+ });
17247
+ }
17248
+ const requested2 = amount === "all" ? cached.remaining : amount;
17249
+ const swapAll2 = amount === "all" || requested2 >= cached.remaining;
17250
+ const effectiveAmount2 = swapAll2 ? cached.remaining : requested2;
17251
+ const coin2 = swapAll2 ? cached.primary : tx.splitCoins(cached.primary, [effectiveAmount2])[0];
17252
+ cached.remaining = swapAll2 ? 0n : cached.remaining - effectiveAmount2;
17253
+ return { coin: coin2, effectiveAmount: effectiveAmount2, swapAll: swapAll2 };
17254
+ }
17255
+ const { ids, totalBalance } = await fetchAllCoins(client, owner, coinType);
17256
+ if (ids.length === 0) {
17257
+ throw new T2000Error("INSUFFICIENT_BALANCE", `No coins found for ${coinType}`);
17258
+ }
17259
+ const allowSwapAll = options.allowSwapAll ?? true;
17260
+ if (amount !== "all" && amount > totalBalance && !allowSwapAll) {
17261
+ throw new T2000Error("INSUFFICIENT_BALANCE", `Insufficient balance for ${coinType}`, {
17262
+ available: totalBalance.toString(),
17263
+ required: amount.toString()
17264
+ });
17265
+ }
17266
+ const requested = amount === "all" ? totalBalance : amount;
17267
+ const swapAll = amount === "all" || requested >= totalBalance;
17268
+ const effectiveAmount = swapAll ? totalBalance : requested;
17269
+ const primary = tx.object(ids[0]);
17270
+ if (ids.length > 1) {
17271
+ tx.mergeCoins(primary, ids.slice(1).map((id) => tx.object(id)));
17272
+ }
17273
+ const coin = swapAll ? primary : tx.splitCoins(primary, [effectiveAmount])[0];
17274
+ cache.set(cacheKey, {
17275
+ primary,
17276
+ remaining: swapAll ? 0n : totalBalance - effectiveAmount
17277
+ });
17278
+ return { coin, effectiveAmount, swapAll };
17279
+ }
17280
+ async function selectSuiCoin(tx, client, owner, amountMist, sponsoredContext) {
17281
+ if (sponsoredContext) {
17282
+ const { SUI_TYPE: SUI_TYPE2 } = await Promise.resolve().then(() => (init_token_registry(), token_registry_exports));
17283
+ return selectAndSplitCoin(tx, client, owner, SUI_TYPE2, amountMist);
17284
+ }
17285
+ const [coin] = tx.splitCoins(tx.gas, [amountMist]);
17286
+ return { coin, effectiveAmount: amountMist, swapAll: false };
17287
+ }
17288
+ var ptbMergeCache;
17289
+ var init_coinSelection = __esm({
17290
+ "src/wallet/coinSelection.ts"() {
17291
+ init_errors();
17292
+ ptbMergeCache = /* @__PURE__ */ new WeakMap();
17293
+ }
17294
+ });
17206
17295
  var cetus_swap_exports = {};
17207
17296
  __export(cetus_swap_exports, {
17208
17297
  OVERLAY_FEE_RATE: () => OVERLAY_FEE_RATE,
@@ -17297,17 +17386,10 @@ async function addSwapToTx(tx, client, address, input) {
17297
17386
  inputCoin = input.inputCoin;
17298
17387
  effectiveRaw = requestedRaw;
17299
17388
  } else {
17300
- const { ids, totalBalance } = await fetchAllCoinsForSwap(client, address, fromType);
17301
- if (ids.length === 0) {
17302
- throw new T2000Error2("INSUFFICIENT_BALANCE", `No ${input.from} coins found in wallet`);
17303
- }
17304
- const swapAll = requestedRaw >= totalBalance;
17305
- effectiveRaw = swapAll ? totalBalance : requestedRaw;
17306
- const primary = tx.object(ids[0]);
17307
- if (ids.length > 1) {
17308
- tx.mergeCoins(primary, ids.slice(1).map((id) => tx.object(id)));
17309
- }
17310
- inputCoin = swapAll ? primary : tx.splitCoins(primary, [effectiveRaw])[0];
17389
+ const { selectAndSplitCoin: selectAndSplitCoin2 } = await Promise.resolve().then(() => (init_coinSelection(), coinSelection_exports));
17390
+ const result = await selectAndSplitCoin2(tx, client, address, fromType, requestedRaw);
17391
+ inputCoin = result.coin;
17392
+ effectiveRaw = result.effectiveAmount;
17311
17393
  }
17312
17394
  const route = await findSwapRoute({
17313
17395
  walletAddress: address,
@@ -17339,22 +17421,6 @@ async function addSwapToTx(tx, client, address, input) {
17339
17421
  route
17340
17422
  };
17341
17423
  }
17342
- async function fetchAllCoinsForSwap(client, owner, coinType) {
17343
- const ids = [];
17344
- let totalBalance = 0n;
17345
- let cursor;
17346
- let hasNext = true;
17347
- while (hasNext) {
17348
- const page = await client.getCoins({ owner, coinType, cursor: cursor ?? void 0 });
17349
- for (const c of page.data) {
17350
- ids.push(c.coinObjectId);
17351
- totalBalance += BigInt(c.balance);
17352
- }
17353
- cursor = page.nextCursor;
17354
- hasNext = page.hasNextPage;
17355
- }
17356
- return { ids, totalBalance };
17357
- }
17358
17424
  async function simulateSwap(params) {
17359
17425
  const client = getClient(params.walletAddress, params.overlayFee);
17360
17426
  try {
@@ -23695,55 +23761,10 @@ var T2000 = class _T2000 extends import_index.default {
23695
23761
  }
23696
23762
  };
23697
23763
  init_errors();
23698
- init_errors();
23699
- async function fetchAllCoins(client, owner, coinType) {
23700
- const ids = [];
23701
- let totalBalance = 0n;
23702
- let cursor;
23703
- let hasNext = true;
23704
- while (hasNext) {
23705
- const page = await client.getCoins({ owner, coinType, cursor: cursor ?? void 0 });
23706
- for (const c of page.data) {
23707
- ids.push(c.coinObjectId);
23708
- totalBalance += BigInt(c.balance);
23709
- }
23710
- cursor = page.nextCursor;
23711
- hasNext = page.hasNextPage;
23712
- }
23713
- return { ids, totalBalance };
23714
- }
23715
- async function selectAndSplitCoin(tx, client, owner, coinType, amount, options = {}) {
23716
- const { ids, totalBalance } = await fetchAllCoins(client, owner, coinType);
23717
- if (ids.length === 0) {
23718
- throw new T2000Error("INSUFFICIENT_BALANCE", `No coins found for ${coinType}`);
23719
- }
23720
- const allowSwapAll = options.allowSwapAll ?? true;
23721
- if (amount !== "all" && amount > totalBalance && !allowSwapAll) {
23722
- throw new T2000Error("INSUFFICIENT_BALANCE", `Insufficient balance for ${coinType}`, {
23723
- available: totalBalance.toString(),
23724
- required: amount.toString()
23725
- });
23726
- }
23727
- const requested = amount === "all" ? totalBalance : amount;
23728
- const swapAll = amount === "all" || requested >= totalBalance;
23729
- const effectiveAmount = swapAll ? totalBalance : requested;
23730
- const primary = tx.object(ids[0]);
23731
- if (ids.length > 1) {
23732
- tx.mergeCoins(primary, ids.slice(1).map((id) => tx.object(id)));
23733
- }
23734
- const coin = swapAll ? primary : tx.splitCoins(primary, [effectiveAmount])[0];
23735
- return { coin, effectiveAmount, swapAll };
23736
- }
23737
- async function selectSuiCoin(tx, client, owner, amountMist, sponsoredContext) {
23738
- if (sponsoredContext) {
23739
- const { SUI_TYPE: SUI_TYPE2 } = await Promise.resolve().then(() => (init_token_registry(), token_registry_exports));
23740
- return selectAndSplitCoin(tx, client, owner, SUI_TYPE2, amountMist);
23741
- }
23742
- const [coin] = tx.splitCoins(tx.gas, [amountMist]);
23743
- return { coin, effectiveAmount: amountMist, swapAll: false };
23744
- }
23764
+ init_coinSelection();
23745
23765
  init_cetus_swap();
23746
23766
  init_volo();
23767
+ init_coinSelection();
23747
23768
  init_token_registry();
23748
23769
  init_errors();
23749
23770
  var SPONSORED_PYTH_DEPENDENT_PROVIDERS = [
@@ -24227,6 +24248,9 @@ export {
24227
24248
  VOLO_POOL,
24228
24249
  VOLO_METADATA,
24229
24250
  VSUI_TYPE,
24251
+ fetchAllCoins,
24252
+ selectAndSplitCoin,
24253
+ selectSuiCoin,
24230
24254
  findSwapRoute,
24231
24255
  buildSwapTx,
24232
24256
  addSwapToTx,
@@ -24270,6 +24294,7 @@ export {
24270
24294
  formatUsd,
24271
24295
  formatSui,
24272
24296
  formatAssetAmount,
24297
+ normalizeAsset,
24273
24298
  buildSendTx,
24274
24299
  addSendToTx,
24275
24300
  KNOWN_TARGETS,
@@ -24298,9 +24323,6 @@ export {
24298
24323
  SafeguardEnforcer,
24299
24324
  ContactManager,
24300
24325
  T2000,
24301
- fetchAllCoins,
24302
- selectAndSplitCoin,
24303
- selectSuiCoin,
24304
24326
  WRITE_APPENDER_REGISTRY,
24305
24327
  deriveAllowedAddressesFromPtb,
24306
24328
  composeTx,
@@ -24336,4 +24358,4 @@ axios/dist/node/axios.cjs:
24336
24358
  @scure/bip39/index.js:
24337
24359
  (*! scure-bip39 - MIT License (c) 2022 Patricio Palladino, Paul Miller (paulmillr.com) *)
24338
24360
  */
24339
- //# sourceMappingURL=chunk-33M73WY4.js.map
24361
+ //# sourceMappingURL=chunk-VZAS6UYO.js.map