|
| 1 | +use std::collections::HashMap; |
| 2 | +use std::future::Future; |
| 3 | +use std::pin::Pin; |
| 4 | + |
| 5 | +use anyhow::Context; |
| 6 | +use candid::{CandidType, Deserialize}; |
| 7 | +use ic_canister_client::CanisterClient; |
| 8 | +use jsonrpc_core::{Call, Request, Response}; |
| 9 | +use serde::Serialize; |
| 10 | +use serde_bytes::ByteBuf; |
| 11 | + |
| 12 | +use crate::{Client, ETH_SEND_RAW_TRANSACTION_METHOD}; |
| 13 | + |
| 14 | +impl<T: CanisterClient + Sync + 'static> Client for T { |
| 15 | + fn send_rpc_request( |
| 16 | + &self, |
| 17 | + request: Request, |
| 18 | + ) -> Pin<Box<dyn Future<Output = anyhow::Result<Response>> + Send>> { |
| 19 | + let client = self.clone(); |
| 20 | + |
| 21 | + Box::pin(async move { |
| 22 | + log::trace!("CanisterClient - sending 'http_request'. request: {request:?}"); |
| 23 | + |
| 24 | + let is_update_call = match &request { |
| 25 | + Request::Single(Call::MethodCall(call)) => is_update_call(&call.method), |
| 26 | + Request::Batch(calls) => calls.iter().any(|call| { |
| 27 | + if let Call::MethodCall(call) = call { |
| 28 | + is_update_call(&call.method) |
| 29 | + } else { |
| 30 | + false |
| 31 | + } |
| 32 | + }), |
| 33 | + _ => false, |
| 34 | + }; |
| 35 | + |
| 36 | + let args = HttpRequest::new(&request)?; |
| 37 | + |
| 38 | + let http_response: HttpResponse = if is_update_call { |
| 39 | + client.update("http_request_update", (args,)).await |
| 40 | + } else { |
| 41 | + client.query("http_request", (args,)).await |
| 42 | + } |
| 43 | + .context("failed to send RPC request")?; |
| 44 | + |
| 45 | + let response = serde_json::from_slice(&http_response.body) |
| 46 | + .context("failed to deserialize RPC request")?; |
| 47 | + |
| 48 | + log::trace!("response: {:?}", response); |
| 49 | + |
| 50 | + Ok(response) |
| 51 | + }) |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +/// The important components of an HTTP request. |
| 56 | +#[derive(Clone, Debug, CandidType)] |
| 57 | +struct HttpRequest { |
| 58 | + /// The HTTP method string. |
| 59 | + pub method: &'static str, |
| 60 | + /// The URL method string. |
| 61 | + pub url: &'static str, |
| 62 | + /// The request headers. |
| 63 | + pub headers: HashMap<&'static str, &'static str>, |
| 64 | + /// The request body. |
| 65 | + pub body: ByteBuf, |
| 66 | +} |
| 67 | + |
| 68 | +impl HttpRequest { |
| 69 | + pub fn new<T: ?Sized + Serialize>(data: &T) -> anyhow::Result<Self> { |
| 70 | + let mut headers = HashMap::new(); |
| 71 | + headers.insert("content-type", "application/json"); |
| 72 | + Ok(Self { |
| 73 | + method: "POST", |
| 74 | + headers, |
| 75 | + url: "", |
| 76 | + body: ByteBuf::from( |
| 77 | + serde_json::to_vec(data).context("failed to serialize RPC request")?, |
| 78 | + ), |
| 79 | + }) |
| 80 | + } |
| 81 | +} |
| 82 | + |
| 83 | +#[derive(Clone, Debug, CandidType, Deserialize)] |
| 84 | +pub struct HttpResponse { |
| 85 | + /// The HTTP status code. |
| 86 | + pub status_code: u16, |
| 87 | + /// The response header map. |
| 88 | + pub headers: HashMap<String, String>, |
| 89 | + /// The response body. |
| 90 | + pub body: ByteBuf, |
| 91 | +} |
| 92 | + |
| 93 | +#[inline] |
| 94 | +fn is_update_call(method: &str) -> bool { |
| 95 | + method.eq(ETH_SEND_RAW_TRANSACTION_METHOD) |
| 96 | +} |
| 97 | + |
| 98 | +#[cfg(test)] |
| 99 | +mod test { |
| 100 | + |
| 101 | + use super::*; |
| 102 | + use crate::ETH_CHAIN_ID_METHOD; |
| 103 | + |
| 104 | + #[test] |
| 105 | + fn test_is_update_call() { |
| 106 | + assert!(is_update_call(ETH_SEND_RAW_TRANSACTION_METHOD)); |
| 107 | + assert!(!is_update_call(ETH_CHAIN_ID_METHOD)); |
| 108 | + } |
| 109 | +} |
0 commit comments