mergeWalCoins method

Future<String> mergeWalCoins(
  1. String ownerAddress,
  2. SuiAccount signer
)

Merge all WAL coins for an address into a single coin.

Returns the merged coin's object ID. Useful when no single coin has enough balance.

Implementation

Future<String> mergeWalCoins(String ownerAddress, SuiAccount signer) async {
  final walType = await getWalType();
  final coins = await suiClient.getCoins(ownerAddress, coinType: walType);

  if (coins.data.isEmpty) {
    throw InsufficientWalBalanceError(
      ownerAddress: ownerAddress,
      requiredAmount: BigInt.zero,
      message: 'No WAL coins found for $ownerAddress',
    );
  }

  if (coins.data.length == 1) {
    return coins.data.first.coinObjectId;
  }

  // Merge all coins into the first one.
  final tx = Transaction();
  tx.setSender(ownerAddress);

  final destination = tx.object(coins.data.first.coinObjectId);
  final sources = coins.data
      .skip(1)
      .map((c) => tx.object(c.coinObjectId))
      .toList();

  tx.mergeCoins(destination, sources);

  await suiClient.signAndExecuteTransactionBlock(signer, tx);

  return coins.data.first.coinObjectId;
}