interactWithContract method

Future<String> interactWithContract({
  1. required String contractAddress,
  2. required LocalContractFunctionWithValues function,
  3. required String sender,
  4. required Uint8List seed,
  5. required EvmFeeInformation<EvmGasPrice>? feeInfo,
  6. BigInt? value,
})

Interact with Contract

Implementation

Future<String> interactWithContract({
  required String contractAddress,
  required LocalContractFunctionWithValues function,
  required String sender,
  required Uint8List seed,
  required EvmFeeInformation? feeInfo,
  BigInt? value,
}) async {
  final valid = switch ((function.stateMutability, value)) {
    (StateMutability.nonpayable, BigInt? value) => value == null ||
        value == BigInt.zero, // If nonpayable, value must be 0 or null
    (StateMutability.payable, BigInt? value) =>
      value != null && value != BigInt.zero, // If payable, value must be set
    _ => false,
  };
  assert(valid, "Invalid value for state mutability of function");

  final data = function.buildDataField();

  return await buildAndBroadcastTransaction(
    sender: sender,
    recipient: contractAddress,
    seed: seed,
    feeInfo: feeInfo,
    data: data,
    value: value ?? BigInt.zero,
  );
}