aftermath-ts-sdk 3.1.0-dev.c01aecd → 3.1.1-dev.3618bb3

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.d.ts CHANGED
@@ -14737,7 +14737,21 @@ declare class CoinApi {
14737
14737
  walletAddress: SuiAddress;
14738
14738
  coinType: CoinType;
14739
14739
  }) => Promise<CoinStruct[]>;
14740
- private static coinWithAmountTx;
14740
+ /**
14741
+ * Checks the wallet's TOTAL balance (owned coins + SIP-58 address balance)
14742
+ * covers `coinAmount`, throwing the canonical insufficient-balance error
14743
+ * otherwise. `coinWithBalance` can source from both, so this is the correct
14744
+ * spendability check for the non-sponsored path.
14745
+ */
14746
+ private assertSufficientTotalBalance;
14747
+ /**
14748
+ * Owned-coin arg for the sponsored path: merge the selected coins and split
14749
+ * the amount without touching `tx.gas` (that's the sponsor's coin) and via
14750
+ * raw commands only, since the tx is serialized unbuilt. `coinData` comes
14751
+ * from `fetchCoinsWithAtLeastAmount`, which guarantees it covers
14752
+ * `coinAmount`.
14753
+ */
14754
+ private static sponsoredCoinWithAmountTx;
14741
14755
  }
14742
14756
 
14743
14757
  declare class DcaApi {
package/dist/index.js CHANGED
@@ -15980,6 +15980,9 @@ var init_wallet = __esm({
15980
15980
  });
15981
15981
 
15982
15982
  // src/packages/coin/api/coinApi.ts
15983
+ import {
15984
+ coinWithBalance
15985
+ } from "@mysten/sui/transactions";
15983
15986
  var MAX_COIN_FETCH_PAGES, _CoinApi, CoinApi;
15984
15987
  var init_coinApi = __esm({
15985
15988
  "src/packages/coin/api/coinApi.ts"() {
@@ -15987,7 +15990,6 @@ var init_coinApi = __esm({
15987
15990
  init_transactionsApiHelpers();
15988
15991
  init_grpcCasting();
15989
15992
  init_helpers();
15990
- init_coin();
15991
15993
  MAX_COIN_FETCH_PAGES = 50;
15992
15994
  _CoinApi = class _CoinApi {
15993
15995
  // =========================================================================
@@ -16001,51 +16003,41 @@ var init_coinApi = __esm({
16001
16003
  this.fetchCoinWithAmountTx = async (inputs) => {
16002
16004
  const { tx, walletAddress, coinType, coinAmount, isSponsoredTx } = inputs;
16003
16005
  tx.setSender(walletAddress);
16004
- const coinData = await this.fetchCoinsWithAtLeastAmount(inputs);
16005
- return _CoinApi.coinWithAmountTx({
16006
- tx,
16007
- coinData,
16008
- coinAmount,
16009
- coinType,
16010
- isSponsoredTx
16011
- });
16006
+ if (isSponsoredTx) {
16007
+ const coinData = await this.fetchCoinsWithAtLeastAmount(inputs);
16008
+ return _CoinApi.sponsoredCoinWithAmountTx({
16009
+ tx,
16010
+ coinData,
16011
+ coinAmount,
16012
+ coinType
16013
+ });
16014
+ }
16015
+ await this.assertSufficientTotalBalance(inputs);
16016
+ return coinWithBalance({ type: coinType, balance: coinAmount })(tx);
16012
16017
  };
16013
16018
  this.fetchCoinsWithAmountTx = async (inputs) => {
16014
- const { tx, walletAddress, coinTypes, coinAmounts, isSponsoredTx } = inputs;
16015
- tx.setSender(walletAddress);
16016
- const allCoinsData = await Promise.all(
16017
- coinTypes.map(
16018
- async (coinType, index) => this.fetchCoinsWithAtLeastAmount({
16019
+ const { coinTypes, coinAmounts } = inputs;
16020
+ const coinArgs = [];
16021
+ for (const [index, coinType] of coinTypes.entries()) {
16022
+ coinArgs.push(
16023
+ await this.fetchCoinWithAmountTx({
16019
16024
  ...inputs,
16020
- coinAmount: coinAmounts[index],
16021
- coinType
16025
+ coinType,
16026
+ coinAmount: coinAmounts[index]
16022
16027
  })
16023
- )
16024
- );
16025
- let coinArgs = [];
16026
- for (const [index, coinData] of allCoinsData.entries()) {
16027
- const coinArg = _CoinApi.coinWithAmountTx({
16028
- tx,
16029
- coinData,
16030
- coinAmount: coinAmounts[index],
16031
- coinType: coinTypes[index],
16032
- isSponsoredTx
16033
- });
16034
- coinArgs = [...coinArgs, coinArg];
16028
+ );
16035
16029
  }
16036
16030
  return coinArgs;
16037
16031
  };
16038
16032
  this.fetchCoinsWithAtLeastAmount = async (inputs) => {
16039
16033
  const allCoinData = [];
16040
16034
  let cursor;
16041
- let pages = 0;
16042
- do {
16035
+ for (let page = 0; page < MAX_COIN_FETCH_PAGES; page++) {
16043
16036
  const paginatedCoins = await this.api.client.listCoins({
16044
16037
  coinType: inputs.coinType,
16045
16038
  owner: inputs.walletAddress,
16046
16039
  cursor
16047
16040
  });
16048
- pages += 1;
16049
16041
  allCoinData.push(
16050
16042
  ...paginatedCoins.objects.map(GrpcCasting.coinStructFromGrpcCoin)
16051
16043
  );
@@ -16062,15 +16054,10 @@ var init_coinApi = __esm({
16062
16054
  if (paginatedCoins.objects.length === 0 || !paginatedCoins.hasNextPage || !paginatedCoins.cursor) {
16063
16055
  throw new Error("wallet does not have coins of sufficient balance");
16064
16056
  }
16065
- if (pages >= MAX_COIN_FETCH_PAGES) {
16066
- throw new Error(
16067
- "wallet balance is spread across too many coin objects"
16068
- );
16069
- }
16070
16057
  cursor = paginatedCoins.cursor;
16071
- } while (true);
16058
+ }
16059
+ throw new Error("wallet balance is spread across too many coin objects");
16072
16060
  };
16073
- // fetchCoinsUntilAmountReachedOrEnd
16074
16061
  this.fetchAllCoins = async (inputs) => {
16075
16062
  const allCoinData = [];
16076
16063
  let cursor;
@@ -16083,67 +16070,63 @@ var init_coinApi = __esm({
16083
16070
  allCoinData.push(
16084
16071
  ...paginatedCoins.objects.map(GrpcCasting.coinStructFromGrpcCoin)
16085
16072
  );
16086
- if (paginatedCoins.objects.length === 0 || !paginatedCoins.hasNextPage || !paginatedCoins.cursor) {
16087
- return allCoinData.sort(
16088
- (b, a) => Number(BigInt(b.coinObjectId) - BigInt(a.coinObjectId))
16089
- );
16090
- }
16091
- cursor = paginatedCoins.cursor;
16092
- } while (true);
16073
+ cursor = paginatedCoins.objects.length > 0 && paginatedCoins.hasNextPage && paginatedCoins.cursor ? paginatedCoins.cursor : void 0;
16074
+ } while (cursor !== void 0);
16075
+ return allCoinData.sort(
16076
+ (b, a) => Number(BigInt(b.coinObjectId) - BigInt(a.coinObjectId))
16077
+ );
16078
+ };
16079
+ // =========================================================================
16080
+ // Private Helpers
16081
+ // =========================================================================
16082
+ /**
16083
+ * Checks the wallet's TOTAL balance (owned coins + SIP-58 address balance)
16084
+ * covers `coinAmount`, throwing the canonical insufficient-balance error
16085
+ * otherwise. `coinWithBalance` can source from both, so this is the correct
16086
+ * spendability check for the non-sponsored path.
16087
+ */
16088
+ this.assertSufficientTotalBalance = async (inputs) => {
16089
+ const { balance } = await this.api.client.getBalance({
16090
+ owner: inputs.walletAddress,
16091
+ coinType: Helpers.stripLeadingZeroesFromType(inputs.coinType)
16092
+ });
16093
+ if (BigInt(balance.balance) < inputs.coinAmount) {
16094
+ throw new Error("wallet does not have coins of sufficient balance");
16095
+ }
16093
16096
  };
16094
16097
  }
16095
16098
  };
16096
16099
  // =========================================================================
16097
16100
  // Private Static Methods
16098
16101
  // =========================================================================
16099
- _CoinApi.coinWithAmountTx = (inputs) => {
16100
- const { tx, coinData, coinAmount, coinType, isSponsoredTx } = inputs;
16101
- if (coinData.length <= 0) {
16102
- throw new Error("wallet does not have coins of sufficient balance");
16103
- }
16104
- const isSuiCoin = Coin.isSuiCoin(coinData[0].coinType);
16105
- const totalCoinBalance = Helpers.sumBigInt(
16106
- coinData.map((data) => BigInt(data.balance))
16107
- );
16108
- if (totalCoinBalance < coinAmount) {
16109
- throw new Error("wallet does not have coins of sufficient balance");
16110
- }
16111
- if (!isSponsoredTx && isSuiCoin) {
16112
- tx.setGasPayment(
16113
- coinData.map((obj) => {
16114
- return {
16115
- ...obj,
16116
- objectId: obj.coinObjectId
16117
- };
16118
- })
16119
- );
16120
- return tx.splitCoins(tx.gas, [coinAmount]);
16121
- }
16102
+ /**
16103
+ * Owned-coin arg for the sponsored path: merge the selected coins and split
16104
+ * the amount without touching `tx.gas` (that's the sponsor's coin) and via
16105
+ * raw commands only, since the tx is serialized unbuilt. `coinData` comes
16106
+ * from `fetchCoinsWithAtLeastAmount`, which guarantees it covers
16107
+ * `coinAmount`.
16108
+ */
16109
+ _CoinApi.sponsoredCoinWithAmountTx = (inputs) => {
16110
+ const { tx, coinData, coinAmount, coinType } = inputs;
16122
16111
  const coinObjectIds = coinData.map((data) => data.coinObjectId);
16123
16112
  const mergedCoinObjectId = coinObjectIds[0];
16124
16113
  if (coinObjectIds.length > 1) {
16125
- if (isSponsoredTx) {
16126
- tx.add({
16127
- $kind: "MergeCoins",
16128
- MergeCoins: {
16129
- destination: tx.object(mergedCoinObjectId),
16130
- sources: [
16131
- ...coinObjectIds.slice(1).map((coinId) => tx.object(coinId))
16132
- ]
16133
- }
16134
- });
16135
- } else {
16136
- tx.mergeCoins(tx.object(mergedCoinObjectId), [
16137
- ...coinObjectIds.slice(1).map((coinId) => tx.object(coinId))
16138
- ]);
16139
- }
16114
+ tx.add({
16115
+ $kind: "MergeCoins",
16116
+ MergeCoins: {
16117
+ destination: tx.object(mergedCoinObjectId),
16118
+ sources: [
16119
+ ...coinObjectIds.slice(1).map((coinId) => tx.object(coinId))
16120
+ ]
16121
+ }
16122
+ });
16140
16123
  }
16141
- return isSponsoredTx ? TransactionsApiHelpers.splitCoinTx({
16124
+ return TransactionsApiHelpers.splitCoinTx({
16142
16125
  tx,
16143
16126
  coinId: mergedCoinObjectId,
16144
16127
  amount: coinAmount,
16145
16128
  coinType
16146
- }) : tx.splitCoins(mergedCoinObjectId, [coinAmount]);
16129
+ });
16147
16130
  };
16148
16131
  CoinApi = _CoinApi;
16149
16132
  }