request method
Implementation
@override
Future<dynamic> request({
required String chainId,
required SessionRequestParams request,
}) async {
await _checkInstalled();
final cid = chainId.contains(':') ? chainId.split(':').last : chainId;
_core.logger.i('[$runtimeType] request $chainId, ${request.toJson()}');
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 CoinbaseServiceException('$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;
}
_core.logger.i('[$runtimeType] request result $value');
return value;
} on CoinbaseServiceException catch (e) {
_core.logger.e('[$runtimeType] request CoinbaseServiceException $e');
onCoinbaseError.broadcast(CoinbaseErrorEvent(e.message));
rethrow;
} on PlatformException catch (e, s) {
_core.logger.e('[$runtimeType] request PlatformException $e');
final message = 'Coinbase Wallet Error: (${e.code}) ${e.message}';
onCoinbaseError.broadcast(CoinbaseErrorEvent(message));
throw CoinbaseServiceException(message, e, s);
}
}