aftermath-ts-sdk 3.1.1-dev.3618bb3 → 3.1.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.d.ts CHANGED
@@ -14737,21 +14737,7 @@ declare class CoinApi {
14737
14737
  walletAddress: SuiAddress;
14738
14738
  coinType: CoinType;
14739
14739
  }) => Promise<CoinStruct[]>;
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;
14740
+ private static coinWithAmountTx;
14755
14741
  }
14756
14742
 
14757
14743
  declare class DcaApi {
package/dist/index.js CHANGED
@@ -15980,9 +15980,6 @@ 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";
15986
15983
  var MAX_COIN_FETCH_PAGES, _CoinApi, CoinApi;
15987
15984
  var init_coinApi = __esm({
15988
15985
  "src/packages/coin/api/coinApi.ts"() {
@@ -15990,6 +15987,7 @@ var init_coinApi = __esm({
15990
15987
  init_transactionsApiHelpers();
15991
15988
  init_grpcCasting();
15992
15989
  init_helpers();
15990
+ init_coin();
15993
15991
  MAX_COIN_FETCH_PAGES = 50;
15994
15992
  _CoinApi = class _CoinApi {
15995
15993
  // =========================================================================
@@ -16003,41 +16001,51 @@ var init_coinApi = __esm({
16003
16001
  this.fetchCoinWithAmountTx = async (inputs) => {
16004
16002
  const { tx, walletAddress, coinType, coinAmount, isSponsoredTx } = inputs;
16005
16003
  tx.setSender(walletAddress);
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);
16004
+ const coinData = await this.fetchCoinsWithAtLeastAmount(inputs);
16005
+ return _CoinApi.coinWithAmountTx({
16006
+ tx,
16007
+ coinData,
16008
+ coinAmount,
16009
+ coinType,
16010
+ isSponsoredTx
16011
+ });
16017
16012
  };
16018
16013
  this.fetchCoinsWithAmountTx = async (inputs) => {
16019
- const { coinTypes, coinAmounts } = inputs;
16020
- const coinArgs = [];
16021
- for (const [index, coinType] of coinTypes.entries()) {
16022
- coinArgs.push(
16023
- await this.fetchCoinWithAmountTx({
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({
16024
16019
  ...inputs,
16025
- coinType,
16026
- coinAmount: coinAmounts[index]
16020
+ coinAmount: coinAmounts[index],
16021
+ coinType
16027
16022
  })
16028
- );
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];
16029
16035
  }
16030
16036
  return coinArgs;
16031
16037
  };
16032
16038
  this.fetchCoinsWithAtLeastAmount = async (inputs) => {
16033
16039
  const allCoinData = [];
16034
16040
  let cursor;
16035
- for (let page = 0; page < MAX_COIN_FETCH_PAGES; page++) {
16041
+ let pages = 0;
16042
+ do {
16036
16043
  const paginatedCoins = await this.api.client.listCoins({
16037
16044
  coinType: inputs.coinType,
16038
16045
  owner: inputs.walletAddress,
16039
16046
  cursor
16040
16047
  });
16048
+ pages += 1;
16041
16049
  allCoinData.push(
16042
16050
  ...paginatedCoins.objects.map(GrpcCasting.coinStructFromGrpcCoin)
16043
16051
  );
@@ -16054,10 +16062,15 @@ var init_coinApi = __esm({
16054
16062
  if (paginatedCoins.objects.length === 0 || !paginatedCoins.hasNextPage || !paginatedCoins.cursor) {
16055
16063
  throw new Error("wallet does not have coins of sufficient balance");
16056
16064
  }
16065
+ if (pages >= MAX_COIN_FETCH_PAGES) {
16066
+ throw new Error(
16067
+ "wallet balance is spread across too many coin objects"
16068
+ );
16069
+ }
16057
16070
  cursor = paginatedCoins.cursor;
16058
- }
16059
- throw new Error("wallet balance is spread across too many coin objects");
16071
+ } while (true);
16060
16072
  };
16073
+ // fetchCoinsUntilAmountReachedOrEnd
16061
16074
  this.fetchAllCoins = async (inputs) => {
16062
16075
  const allCoinData = [];
16063
16076
  let cursor;
@@ -16070,63 +16083,67 @@ var init_coinApi = __esm({
16070
16083
  allCoinData.push(
16071
16084
  ...paginatedCoins.objects.map(GrpcCasting.coinStructFromGrpcCoin)
16072
16085
  );
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
- }
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);
16096
16093
  };
16097
16094
  }
16098
16095
  };
16099
16096
  // =========================================================================
16100
16097
  // Private Static Methods
16101
16098
  // =========================================================================
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;
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
+ }
16111
16122
  const coinObjectIds = coinData.map((data) => data.coinObjectId);
16112
16123
  const mergedCoinObjectId = coinObjectIds[0];
16113
16124
  if (coinObjectIds.length > 1) {
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
- });
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
+ }
16123
16140
  }
16124
- return TransactionsApiHelpers.splitCoinTx({
16141
+ return isSponsoredTx ? TransactionsApiHelpers.splitCoinTx({
16125
16142
  tx,
16126
16143
  coinId: mergedCoinObjectId,
16127
16144
  amount: coinAmount,
16128
16145
  coinType
16129
- });
16146
+ }) : tx.splitCoins(mergedCoinObjectId, [coinAmount]);
16130
16147
  };
16131
16148
  CoinApi = _CoinApi;
16132
16149
  }