createStorageTransaction method

Transaction createStorageTransaction({
  1. required int encodedSize,
  2. required int epochs,
  3. required String walCoinObjectId,
  4. required BigInt storageCost,
  5. String? walType,
  6. String? owner,
  7. Transaction? transaction,
})

Build a transaction that creates a storage reservation.

Splits storageCost from the WAL coin and calls reserve_space. The resulting Storage object can be used with register_blob.

Mirrors TS SDK's createStorage().

Implementation

Transaction createStorageTransaction({
  required int encodedSize,
  required int epochs,
  required String walCoinObjectId,
  required BigInt storageCost,
  String? walType,
  String? owner,
  Transaction? transaction,
}) {
  final tx = transaction ?? Transaction();

  final walCoin = tx.object(walCoinObjectId);
  final payment = tx.splitCoins(walCoin, [tx.pure.u64(storageCost)]);

  final storage = tx.moveCall(
    '$walrusPackageId::system::reserve_space',
    arguments: [
      tx.object(packageConfig.systemObjectId), // self
      tx.pure.u64(BigInt.from(encodedSize)), // storageAmount
      tx.pure.u32(epochs), // epochsAhead
      payment, // payment: Coin<WAL>
    ],
  );

  // Destroy the zero-balance payment coin (Coin<WAL> lacks `drop`).
  // reserve_space takes &mut Coin<WAL>, deducting cost and leaving 0.
  // Matches TS SDK's #withWal → coin::destroy_zero pattern.
  if (walType != null) {
    tx.moveCall(
      '0x2::coin::destroy_zero',
      typeArguments: [walType],
      arguments: [payment],
    );
  }

  if (owner != null) {
    tx.transferObjects([storage], tx.pure.address(owner));
  }

  return tx;
}