@t2000/sdk 1.11.0 → 1.11.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
@@ -820,6 +820,97 @@ var init_volo = __esm({
820
820
  }
821
821
  });
822
822
 
823
+ // src/wallet/coinSelection.ts
824
+ var coinSelection_exports = {};
825
+ __export(coinSelection_exports, {
826
+ fetchAllCoins: () => fetchAllCoins,
827
+ selectAndSplitCoin: () => selectAndSplitCoin,
828
+ selectSuiCoin: () => selectSuiCoin
829
+ });
830
+ function getMergeCache(tx) {
831
+ let cache = ptbMergeCache.get(tx);
832
+ if (!cache) {
833
+ cache = /* @__PURE__ */ new Map();
834
+ ptbMergeCache.set(tx, cache);
835
+ }
836
+ return cache;
837
+ }
838
+ async function fetchAllCoins(client, owner, coinType) {
839
+ const ids = [];
840
+ let totalBalance = 0n;
841
+ let cursor;
842
+ let hasNext = true;
843
+ while (hasNext) {
844
+ const page = await client.getCoins({ owner, coinType, cursor: cursor ?? void 0 });
845
+ for (const c of page.data) {
846
+ ids.push(c.coinObjectId);
847
+ totalBalance += BigInt(c.balance);
848
+ }
849
+ cursor = page.nextCursor;
850
+ hasNext = page.hasNextPage;
851
+ }
852
+ return { ids, totalBalance };
853
+ }
854
+ async function selectAndSplitCoin(tx, client, owner, coinType, amount, options = {}) {
855
+ const cache = getMergeCache(tx);
856
+ const cacheKey = `${owner}|${coinType}`;
857
+ const cached = cache.get(cacheKey);
858
+ if (cached) {
859
+ const allowSwapAll2 = options.allowSwapAll ?? true;
860
+ if (amount !== "all" && amount > cached.remaining && !allowSwapAll2) {
861
+ throw new exports.T2000Error("INSUFFICIENT_BALANCE", `Insufficient balance for ${coinType}`, {
862
+ available: cached.remaining.toString(),
863
+ required: amount.toString()
864
+ });
865
+ }
866
+ const requested2 = amount === "all" ? cached.remaining : amount;
867
+ const swapAll2 = amount === "all" || requested2 >= cached.remaining;
868
+ const effectiveAmount2 = swapAll2 ? cached.remaining : requested2;
869
+ const coin2 = swapAll2 ? cached.primary : tx.splitCoins(cached.primary, [effectiveAmount2])[0];
870
+ cached.remaining = swapAll2 ? 0n : cached.remaining - effectiveAmount2;
871
+ return { coin: coin2, effectiveAmount: effectiveAmount2, swapAll: swapAll2 };
872
+ }
873
+ const { ids, totalBalance } = await fetchAllCoins(client, owner, coinType);
874
+ if (ids.length === 0) {
875
+ throw new exports.T2000Error("INSUFFICIENT_BALANCE", `No coins found for ${coinType}`);
876
+ }
877
+ const allowSwapAll = options.allowSwapAll ?? true;
878
+ if (amount !== "all" && amount > totalBalance && !allowSwapAll) {
879
+ throw new exports.T2000Error("INSUFFICIENT_BALANCE", `Insufficient balance for ${coinType}`, {
880
+ available: totalBalance.toString(),
881
+ required: amount.toString()
882
+ });
883
+ }
884
+ const requested = amount === "all" ? totalBalance : amount;
885
+ const swapAll = amount === "all" || requested >= totalBalance;
886
+ const effectiveAmount = swapAll ? totalBalance : requested;
887
+ const primary = tx.object(ids[0]);
888
+ if (ids.length > 1) {
889
+ tx.mergeCoins(primary, ids.slice(1).map((id) => tx.object(id)));
890
+ }
891
+ const coin = swapAll ? primary : tx.splitCoins(primary, [effectiveAmount])[0];
892
+ cache.set(cacheKey, {
893
+ primary,
894
+ remaining: swapAll ? 0n : totalBalance - effectiveAmount
895
+ });
896
+ return { coin, effectiveAmount, swapAll };
897
+ }
898
+ async function selectSuiCoin(tx, client, owner, amountMist, sponsoredContext) {
899
+ if (sponsoredContext) {
900
+ const { SUI_TYPE: SUI_TYPE2 } = await Promise.resolve().then(() => (init_token_registry(), token_registry_exports));
901
+ return selectAndSplitCoin(tx, client, owner, SUI_TYPE2, amountMist);
902
+ }
903
+ const [coin] = tx.splitCoins(tx.gas, [amountMist]);
904
+ return { coin, effectiveAmount: amountMist, swapAll: false };
905
+ }
906
+ var ptbMergeCache;
907
+ var init_coinSelection = __esm({
908
+ "src/wallet/coinSelection.ts"() {
909
+ init_errors();
910
+ ptbMergeCache = /* @__PURE__ */ new WeakMap();
911
+ }
912
+ });
913
+
823
914
  // src/protocols/cetus-swap.ts
824
915
  var cetus_swap_exports = {};
825
916
  __export(cetus_swap_exports, {
@@ -915,17 +1006,10 @@ async function addSwapToTx(tx, client, address, input) {
915
1006
  inputCoin = input.inputCoin;
916
1007
  effectiveRaw = requestedRaw;
917
1008
  } else {
918
- const { ids, totalBalance } = await fetchAllCoinsForSwap(client, address, fromType);
919
- if (ids.length === 0) {
920
- throw new T2000Error2("INSUFFICIENT_BALANCE", `No ${input.from} coins found in wallet`);
921
- }
922
- const swapAll = requestedRaw >= totalBalance;
923
- effectiveRaw = swapAll ? totalBalance : requestedRaw;
924
- const primary = tx.object(ids[0]);
925
- if (ids.length > 1) {
926
- tx.mergeCoins(primary, ids.slice(1).map((id) => tx.object(id)));
927
- }
928
- inputCoin = swapAll ? primary : tx.splitCoins(primary, [effectiveRaw])[0];
1009
+ const { selectAndSplitCoin: selectAndSplitCoin2 } = await Promise.resolve().then(() => (init_coinSelection(), coinSelection_exports));
1010
+ const result = await selectAndSplitCoin2(tx, client, address, fromType, requestedRaw);
1011
+ inputCoin = result.coin;
1012
+ effectiveRaw = result.effectiveAmount;
929
1013
  }
930
1014
  const route = await findSwapRoute({
931
1015
  walletAddress: address,
@@ -957,22 +1041,6 @@ async function addSwapToTx(tx, client, address, input) {
957
1041
  route
958
1042
  };
959
1043
  }
960
- async function fetchAllCoinsForSwap(client, owner, coinType) {
961
- const ids = [];
962
- let totalBalance = 0n;
963
- let cursor;
964
- let hasNext = true;
965
- while (hasNext) {
966
- const page = await client.getCoins({ owner, coinType, cursor: cursor ?? void 0 });
967
- for (const c of page.data) {
968
- ids.push(c.coinObjectId);
969
- totalBalance += BigInt(c.balance);
970
- }
971
- cursor = page.nextCursor;
972
- hasNext = page.hasNextPage;
973
- }
974
- return { ids, totalBalance };
975
- }
976
1044
  async function simulateSwap(params) {
977
1045
  const client = getClient(params.walletAddress, params.overlayFee);
978
1046
  try {
@@ -7380,57 +7448,10 @@ var T2000 = class _T2000 extends eventemitter3.EventEmitter {
7380
7448
 
7381
7449
  // src/index.ts
7382
7450
  init_errors();
7383
-
7384
- // src/wallet/coinSelection.ts
7385
- init_errors();
7386
- async function fetchAllCoins(client, owner, coinType) {
7387
- const ids = [];
7388
- let totalBalance = 0n;
7389
- let cursor;
7390
- let hasNext = true;
7391
- while (hasNext) {
7392
- const page = await client.getCoins({ owner, coinType, cursor: cursor ?? void 0 });
7393
- for (const c of page.data) {
7394
- ids.push(c.coinObjectId);
7395
- totalBalance += BigInt(c.balance);
7396
- }
7397
- cursor = page.nextCursor;
7398
- hasNext = page.hasNextPage;
7399
- }
7400
- return { ids, totalBalance };
7401
- }
7402
- async function selectAndSplitCoin(tx, client, owner, coinType, amount, options = {}) {
7403
- const { ids, totalBalance } = await fetchAllCoins(client, owner, coinType);
7404
- if (ids.length === 0) {
7405
- throw new exports.T2000Error("INSUFFICIENT_BALANCE", `No coins found for ${coinType}`);
7406
- }
7407
- const allowSwapAll = options.allowSwapAll ?? true;
7408
- if (amount !== "all" && amount > totalBalance && !allowSwapAll) {
7409
- throw new exports.T2000Error("INSUFFICIENT_BALANCE", `Insufficient balance for ${coinType}`, {
7410
- available: totalBalance.toString(),
7411
- required: amount.toString()
7412
- });
7413
- }
7414
- const requested = amount === "all" ? totalBalance : amount;
7415
- const swapAll = amount === "all" || requested >= totalBalance;
7416
- const effectiveAmount = swapAll ? totalBalance : requested;
7417
- const primary = tx.object(ids[0]);
7418
- if (ids.length > 1) {
7419
- tx.mergeCoins(primary, ids.slice(1).map((id) => tx.object(id)));
7420
- }
7421
- const coin = swapAll ? primary : tx.splitCoins(primary, [effectiveAmount])[0];
7422
- return { coin, effectiveAmount, swapAll };
7423
- }
7424
- async function selectSuiCoin(tx, client, owner, amountMist, sponsoredContext) {
7425
- if (sponsoredContext) {
7426
- const { SUI_TYPE: SUI_TYPE2 } = await Promise.resolve().then(() => (init_token_registry(), token_registry_exports));
7427
- return selectAndSplitCoin(tx, client, owner, SUI_TYPE2, amountMist);
7428
- }
7429
- const [coin] = tx.splitCoins(tx.gas, [amountMist]);
7430
- return { coin, effectiveAmount: amountMist, swapAll: false };
7431
- }
7451
+ init_coinSelection();
7432
7452
  init_cetus_swap();
7433
7453
  init_volo();
7454
+ init_coinSelection();
7434
7455
  init_token_registry();
7435
7456
  init_errors();
7436
7457
  var SPONSORED_PYTH_DEPENDENT_PROVIDERS = [