read method

Future<ExternalContractFunctionWithValuesAndOutputs> read({
  1. required ExternalContractFunctionWithValues function,
  2. BlockNum? atBlock,
  3. required List<FunctionParam>? outputs,
  4. StateMutability? stateMutability,
})

Implementation

Future<ExternalContractFunctionWithValuesAndOutputs> read({
  required ExternalContractFunctionWithValues function,
  BlockNum? atBlock,
  required List<FunctionParam>? outputs,
  StateMutability? stateMutability,
}) async {
  final _stateMutability = stateMutability ?? function.stateMutability;

  if (_stateMutability != StateMutability.pure &&
      _stateMutability != StateMutability.view) {
    throw ArgumentError('Function is not view or pure');
  }

  final _outputs = outputs ?? function.outputTypes;

  if (_outputs == null || _outputs.isEmpty) {
    throw ArgumentError('Outputs must be provided');
  }

  final data = function.buildDataField();

  final String result =
      await rpc.call(contractAddress: contractAddress, data: data);

  final resultBuffer = result.hexToBytesWithPrefix;

  return ExternalContractFunctionWithValuesAndOutputs.decode(
    data: resultBuffer,
    function: function,
    outputs: _outputs,
    stateMutability: stateMutability,
  );
}