prepareUnsigned method

Future<Future<Unsigned7702Tx> Function(BigInt?, Uint8List?, int)> prepareUnsigned(
  1. HexString sender,
  2. HexString to, [
  3. BigInt? nonceOverride
])

Prepares the base components of an unsigned EIP-7702 transaction and returns a closure that finalizes the transaction once value and data are provided.

This method performs the initial transaction setup:

  • Resolves the sender’s nonce using getNonce, unless nonceOverride is provided.
  • Fetches EIP-1559 fee parameters using getFeeData.
  • Constructs a partially-built transaction template containing fixed fields (from, to, nonce, maxFeePerGas, maxPriorityFeePerGas).

Instead of immediately returning an Unsigned7702Tx, this method returns a function that completes the transaction when given:

  • an optional EtherAmount value, and
  • optional calldata data.

Why return a closure?

This design enables callers to resolve nonce and fee data once, while deferring gas estimation until value and data are known. Builders like buildUnsigned use this to assemble transactions in a staged, efficient manner.

Example

final prepare = await builder.prepareUnsigned(sender, to);
final unsigned = await prepare(EtherAmount.zero(), callData);

Implementation

Future<Future<Unsigned7702Tx> Function(BigInt?, Uint8List?, int)>
prepareUnsigned(
  HexString sender,
  HexString to, [
  BigInt? nonceOverride,
]) async {
  final [nonce, fees] = await Future.wait<dynamic>([
    resolveNonce(sender.ethAddress, null, nonceOverride),
    getFeeData(),
  ]);
  final maxFeePerGas = EtherAmount.inWei(fees.maxFeePerGas);
  final maxPriorityFeePerGas = EtherAmount.inWei(fees.maxPriorityFeePerGas);

  return (BigInt? value, Uint8List? data, int noOfAuths) async {
    final valueEtherAmount = EtherAmount.inWei(value ?? BigInt.zero);

    final gasLimit = await ctx.web3Client.estimateGas(
      sender: sender.ethAddress,
      to: to.ethAddress,
      data: data,
      value: valueEtherAmount,
      maxPriorityFeePerGas: maxPriorityFeePerGas,
      maxFeePerGas: maxFeePerGas,
    );

    final baseCost = baseAuthCost * BigInt.from(noOfAuths);
    final totalGas = gasLimit + baseCost;

    return Unsigned7702Tx(
      from: sender.ethAddress,
      to: to.ethAddress,
      gasLimit: ctx.transformer?.call(totalGas) ?? totalGas,
      nonce: nonce.toInt(),
      value: valueEtherAmount,
      data: data ?? Uint8List(0),
      maxFeePerGas: maxFeePerGas,
      maxPriorityFeePerGas: maxPriorityFeePerGas,
    );
  };
}