addContractCallToBatch method

bool addContractCallToBatch({
  1. required String contractAddress,
  2. required String contractAbi,
  3. required String functionName,
  4. required List args,
  5. BigInt? gasPrice,
  6. BigInt? gasLimit,
})

Adds a contract call to a batch of Ethereum transactions.

This function constructs a contract call transaction and adds it to a batch of transactions that can be sent to the network together.

Parameters:

  • contractAddress: The address of the Ethereum smart contract to interact with.
  • contractAbi: The JSON ABI (Application Binary Interface) of the smart contract, which defines the methods and data structures of the contract.
  • functionName: The name of the function within the smart contract that you want to call.
  • args: A list of dynamic arguments required for the function call, which should match the function's parameter types as specified in the contract's ABI.
  • gasPrice: (Optional) The price (in Wei) that you are willing to pay per unit of gas. If not provided, the default gas price will be used.
  • gasLimit: (Optional) The maximum amount of gas that can be consumed by the transaction. If not provided, the default gas limit will be used.

Returns:

  • true if the contract call is successfully added to the batch.
  • false if an error occurs while constructing the contract call or encoding the data.

Implementation

bool addContractCallToBatch({
  required String contractAddress,
  required String contractAbi,
  required String functionName,
  required List<dynamic> args,
  BigInt? gasPrice,
  BigInt? gasLimit,
}) {
  try {
    // Encode the function call and arguments into binary data.
    final List encodedData = getEncodedDataAsList(
      contractAddress: contractAddress,
      contractAbi: contractAbi,
      functionName: functionName,
      args: args,
    );

    // Create a ContractCall object and add it to the batch.
    this.batch[batch.length] = ContractCall(
        contractAddress: contractAddress,
        contractAbi: contractAbi,
        functionName: functionName,
        args: args,
        encodedData: encodedData as List<int>);

    // Return true to indicate success.
    return true;
  } catch (e) {
    // Print any error that occurs during the process and return false.
    print(e);
    return false;
  }
}