executeCreateStorageTransaction method

Future<({String digest, String storageObjectId})> executeCreateStorageTransaction({
  1. required int size,
  2. required int epochs,
  3. required SuiAccount signer,
  4. String? walCoinObjectId,
  5. String? owner,
})

Execute a transaction that creates a storage reservation.

Resolves WAL coins automatically from the signer's account if walCoinObjectId is not provided. Returns the transaction digest and the created Storage object ID.

Mirrors the TS SDK's executeCreateStorageTransaction().

Implementation

Future<({String digest, String storageObjectId})>
executeCreateStorageTransaction({
  required int size,
  required int epochs,
  required SuiAccount signer,
  String? walCoinObjectId,
  String? owner,
}) async {
  final txBuilder = await _ensureTxBuilder();
  final costs = await storageCost(size, epochs);
  final effectiveOwner = owner ?? signer.getAddress();

  // Resolve WAL coin.
  final walCoin =
      walCoinObjectId ?? await findWalCoin(effectiveOwner, costs.storageCost);

  if (walCoin == null) {
    throw InsufficientWalBalanceError(
      ownerAddress: effectiveOwner,
      requiredAmount: costs.storageCost,
      message:
          'No WAL coin with sufficient balance for storage reservation. '
          'Need ${costs.storageCost} WAL.',
    );
  }

  final systemState = await _stateReader.systemState();
  final encodedSize = encodedBlobLength(size, systemState.nShards);

  final tx = txBuilder.createStorageTransaction(
    encodedSize: encodedSize,
    epochs: epochs,
    walCoinObjectId: walCoin,
    storageCost: costs.storageCost,
    walType: await getWalType(),
    owner: effectiveOwner,
  );
  tx.setSender(signer.getAddress());

  final result = await suiClient.signAndExecuteTransactionBlock(
    signer,
    tx,
    responseOptions: SuiTransactionBlockResponseOptions(
      showEffects: true,
      showObjectChanges: true,
    ),
  );

  // Extract the created storage object ID from transaction effects.
  final storageObjectId = _extractCreatedObjectId(result, 'Storage');

  return (digest: result.digest, storageObjectId: storageObjectId);
}