requestReadContract method

  1. @override
Future<List> requestReadContract({
  1. required String? topic,
  2. required String chainId,
  3. required DeployedContract deployedContract,
  4. required String functionName,
  5. List parameters = const [],
})
override

Implementation

@override
Future<List<dynamic>> requestReadContract({
  required String? topic,
  required String chainId,
  required DeployedContract deployedContract,
  required String functionName,
  List parameters = const [],
}) async {
  if (_currentSession == null) {
    throw ReownAppKitModalException('Session is null');
  }
  if (!NamespaceUtils.isValidChainId(chainId)) {
    throw Errors.getSdkError(
      Errors.UNSUPPORTED_CHAINS,
      context: 'chainId should conform to "CAIP-2" format',
    ).toSignError();
  }
  if (selectedChain == null) {
    throw Errors.getSdkError(
      Errors.MALFORMED_REQUEST_PARAMS,
      context: 'You must select a chain before reading a contract',
    ).toSignError();
  }
  //
  _appKit.core.logger.i(
    '[$runtimeType] requestReadContract, chainId: $chainId',
  );

  try {
    final data = deployedContract
        .function(functionName)
        .encodeCall(parameters);
    final params = {
      'from': _currentSession!.getAddress('eip155'),
      'to': deployedContract.address.hex,
      'data': '0x${_bytesToHex(data)}',
    };

    final rawCallResponse = await _blockchainService.rawCall(
      chainId: chainId,
      params: params,
    );
    return deployedContract
        .function(functionName)
        .decodeReturnValues(rawCallResponse);
  } catch (e, s) {
    _appKit.core.logger.e(
      '[$runtimeType] requestReadContract, error: $e, $s',
    );
    rethrow;
  }
}