@t2000/sdk 4.0.0 → 4.0.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.
package/dist/index.cjs CHANGED
@@ -677,6 +677,9 @@ async function fetchAllCoins(client, owner, coinType) {
677
677
  return { ids, totalBalance: BigInt(balance.totalBalance) };
678
678
  }
679
679
  async function selectAndSplitCoin(tx, client, owner, coinType, amount, options = {}) {
680
+ if (options.sponsoredContext) {
681
+ return selectCoinObjectsOnly(tx, client, owner, coinType, amount, options.allowSwapAll ?? true);
682
+ }
680
683
  const balanceResp = await client.getBalance({ owner, coinType });
681
684
  const totalBalance = BigInt(balanceResp.totalBalance);
682
685
  if (totalBalance === 0n) {
@@ -695,19 +698,51 @@ async function selectAndSplitCoin(tx, client, owner, coinType, amount, options =
695
698
  const coin = transactions.coinWithBalance({ type: coinType, balance: effectiveAmount })(tx);
696
699
  return { coin, effectiveAmount, swapAll };
697
700
  }
701
+ async function selectCoinObjectsOnly(tx, client, owner, coinType, amount, allowSwapAll) {
702
+ const objects = [];
703
+ let coinObjectTotal = 0n;
704
+ let cursor;
705
+ let hasNext = true;
706
+ while (hasNext) {
707
+ const page = await client.getCoins({ owner, coinType, cursor: cursor ?? void 0 });
708
+ for (const c of page.data) {
709
+ objects.push({ objectId: c.coinObjectId, balance: BigInt(c.balance) });
710
+ coinObjectTotal += BigInt(c.balance);
711
+ }
712
+ cursor = page.nextCursor;
713
+ hasNext = page.hasNextPage;
714
+ }
715
+ const unsponsorable = () => new exports.T2000Error(
716
+ "ADDRESS_BALANCE_UNSPONSORABLE",
717
+ `These funds are in your address balance, which sponsored transactions can't access yet. (Funds received via gasless transfers land there.) This will work once the gas sponsor adds address-balance support.`,
718
+ { coinObjectTotal: coinObjectTotal.toString(), coinType }
719
+ );
720
+ if (coinObjectTotal === 0n) {
721
+ throw unsponsorable();
722
+ }
723
+ const requested = amount === "all" ? coinObjectTotal : amount;
724
+ if (requested > coinObjectTotal) {
725
+ if (allowSwapAll && amount === "all") ; else {
726
+ throw unsponsorable();
727
+ }
728
+ }
729
+ const swapAll = amount === "all" || requested >= coinObjectTotal;
730
+ const effectiveAmount = swapAll ? coinObjectTotal : requested;
731
+ const [first, ...rest] = objects;
732
+ const primary = tx.object(first.objectId);
733
+ if (rest.length > 0) {
734
+ tx.mergeCoins(
735
+ primary,
736
+ rest.map((o) => tx.object(o.objectId))
737
+ );
738
+ }
739
+ const coin = swapAll ? primary : tx.splitCoins(primary, [effectiveAmount])[0];
740
+ return { coin, effectiveAmount, swapAll };
741
+ }
698
742
  async function selectSuiCoin(tx, client, owner, amountMist, sponsoredContext) {
699
743
  if (sponsoredContext) {
700
744
  const { SUI_TYPE: SUI_TYPE2 } = await Promise.resolve().then(() => (init_token_registry(), token_registry_exports));
701
- const balanceResp = await client.getBalance({ owner, coinType: SUI_TYPE2 });
702
- const totalBalance = BigInt(balanceResp.totalBalance);
703
- if (totalBalance < amountMist) {
704
- throw new exports.T2000Error("INSUFFICIENT_BALANCE", `Insufficient SUI balance`, {
705
- available: totalBalance.toString(),
706
- required: amountMist.toString()
707
- });
708
- }
709
- const coin2 = transactions.coinWithBalance({ type: SUI_TYPE2, balance: amountMist, useGasCoin: false })(tx);
710
- return { coin: coin2, effectiveAmount: amountMist, swapAll: false };
745
+ return selectCoinObjectsOnly(tx, client, owner, SUI_TYPE2, amountMist, false);
711
746
  }
712
747
  const [coin] = tx.splitCoins(tx.gas, [amountMist]);
713
748
  return { coin, effectiveAmount: amountMist, swapAll: false };
@@ -935,7 +970,9 @@ async function addSwapToTx(tx, client, address, input) {
935
970
  effectiveRaw = result.effectiveAmount;
936
971
  } else {
937
972
  const { selectAndSplitCoin: selectAndSplitCoin2 } = await Promise.resolve().then(() => (init_coinSelection(), coinSelection_exports));
938
- const result = await selectAndSplitCoin2(tx, client, address, fromType, requestedRaw);
973
+ const result = await selectAndSplitCoin2(tx, client, address, fromType, requestedRaw, {
974
+ sponsoredContext: input.sponsoredContext ?? false
975
+ });
939
976
  inputCoin = result.coin;
940
977
  effectiveRaw = result.effectiveAmount;
941
978
  }
@@ -1160,7 +1197,6 @@ var DEFAULT_RPC_URL = "https://fullnode.mainnet.sui.io:443";
1160
1197
  var DEFAULT_GRPC_URL = "https://fullnode.mainnet.sui.io:443";
1161
1198
  var DEFAULT_KEY_PATH = "~/.t2000/wallet.key";
1162
1199
  var GASLESS_MIN_STABLE_AMOUNT = 0.01;
1163
- process.env.T2000_API_URL ?? "https://api.t2000.ai";
1164
1200
  var CETUS_USDC_SUI_POOL = "0x51e883ba7c0b566a26cbc8a94cd33eb0abd418a77cc1e60ad22fd9b1f29cd2ab";
1165
1201
  var GAS_RESERVE_MIN = 0.05;
1166
1202
 
@@ -7902,7 +7938,9 @@ var WRITE_APPENDER_REGISTRY = {
7902
7938
  coin = ctx.chainedCoin;
7903
7939
  effectiveAmount = rawAmount;
7904
7940
  } else {
7905
- const r = await selectAndSplitCoin(tx, ctx.client, ctx.sender, assetInfo.type, rawAmount);
7941
+ const r = await selectAndSplitCoin(tx, ctx.client, ctx.sender, assetInfo.type, rawAmount, {
7942
+ sponsoredContext: ctx.sponsoredContext
7943
+ });
7906
7944
  coin = r.coin;
7907
7945
  effectiveAmount = r.effectiveAmount;
7908
7946
  }
@@ -7974,7 +8012,9 @@ var WRITE_APPENDER_REGISTRY = {
7974
8012
  coin = ctx.chainedCoin;
7975
8013
  effectiveAmount = rawAmount;
7976
8014
  } else {
7977
- const r = await selectAndSplitCoin(tx, ctx.client, ctx.sender, assetInfo.type, rawAmount);
8015
+ const r = await selectAndSplitCoin(tx, ctx.client, ctx.sender, assetInfo.type, rawAmount, {
8016
+ sponsoredContext: ctx.sponsoredContext
8017
+ });
7978
8018
  coin = r.coin;
7979
8019
  effectiveAmount = r.effectiveAmount;
7980
8020
  }