request method

  1. @override
Future request({
  1. required String chainId,
  2. required SessionRequestParams request,
})
override

Implementation

@override
Future<dynamic> request({
  required String chainId,
  required SessionRequestParams request,
}) async {
  await _checkInstalled();
  final cid = chainId.contains(':') ? chainId.split(':').last : chainId;
  try {
    final req = Request(actions: [request.toCoinbaseRequest(cid)]);
    final result = (await CoinbaseWalletSDK.shared.makeRequest(req)).first;
    if (result.error != null) {
      final errorCode = result.error?.code;
      final errorMessage = result.error!.message;
      onCoinbaseError.broadcast(CoinbaseErrorEvent(errorMessage));
      throw W3MCoinbaseException('$errorMessage ($errorCode)');
    }
    final value = result.value?.replaceAll('"', '');
    switch (req.actions.first.method) {
      case 'wallet_switchEthereumChain':
      case 'wallet_addEthereumChain':
        final event = CoinbaseSessionEvent(chainId: cid);
        onCoinbaseSessionUpdate.broadcast(event);
        break;
      case 'eth_requestAccounts':
        final json = jsonDecode(value!);
        final data = CoinbaseData.fromJson(json).copytWith(
          peer: metadata.copyWith(
            publicKey: await peerPublicKey,
          ),
          self: ConnectionMetadata(
            metadata: _metadata,
            publicKey: await ownPublicKey,
          ),
        );
        onCoinbaseConnect.broadcast(CoinbaseConnectEvent(data));
        break;
      default:
        onCoinbaseResponse.broadcast(CoinbaseResponseEvent(data: value));
        break;
    }
    return value;
  } on W3MCoinbaseException catch (e) {
    onCoinbaseError.broadcast(CoinbaseErrorEvent(e.message));
    rethrow;
  } on PlatformException catch (e, s) {
    final message = 'Coinbase Wallet Error: (${e.code}) ${e.message}';
    onCoinbaseError.broadcast(CoinbaseErrorEvent(message));
    throw W3MCoinbaseException(message, e, s);
  }
}