From 2c478e8b6190a337fefa62f0f1c24fa050827a83 Mon Sep 17 00:00:00 2001 From: Bryan Stitt Date: Mon, 30 May 2022 18:23:55 +0000 Subject: [PATCH] proper result --- web3-proxy/src/app.rs | 26 ++++++++++++++++---------- 1 file changed, 16 insertions(+), 10 deletions(-) diff --git a/web3-proxy/src/app.rs b/web3-proxy/src/app.rs index ecb3edc8..9212ee6c 100644 --- a/web3-proxy/src/app.rs +++ b/web3-proxy/src/app.rs @@ -12,6 +12,7 @@ use futures::future::{join_all, AbortHandle}; use futures::stream::StreamExt; use linkedhashmap::LinkedHashMap; use parking_lot::RwLock; +use serde_json::json; use serde_json::value::RawValue; use std::fmt; use std::sync::Arc; @@ -144,9 +145,12 @@ impl Web3ProxyApp { ) -> anyhow::Result<(AbortHandle, JsonRpcForwardedResponse)> { let (subscription_handle, subscription_registration) = AbortHandle::new_pair(); + // TODO: generate subscription_id as needed. atomic u16? + let subscription_id = "0xcd0c3e8af590364c09d0fa6a1210faf5".to_string(); + let f = { let head_block_receiver = self.head_block_receiver.clone(); - let id = id.clone(); + let subscription_id = subscription_id.clone(); if payload.params.as_deref().unwrap().to_string() == r#"["newHeads"]"# { info!("received new heads subscription"); @@ -158,15 +162,20 @@ impl Web3ProxyApp { while let Some(new_head) = head_block_receiver.next().await { // TODO: this String to RawValue probably not efficient, but it works for now - let msg = JsonRpcForwardedResponse::from_string( - serde_json::to_string(&new_head).unwrap(), - id.clone(), - ); + // TODO: make a struct for this? + let msg = json!({ + "jsonrpc": "2.0", + "method":"eth_subscription", + "params": { + "subscription": subscription_id, + "result": new_head, + }, + }); + + // let msg = JsonRpcForwardedResponse::from_json(&msg, id.clone()); let msg = Message::Text(serde_json::to_string(&msg).unwrap()); - info!(?msg); - subscription_tx.send_async(msg).await.unwrap(); } } @@ -177,9 +186,6 @@ impl Web3ProxyApp { tokio::spawn(f); - // TODO: generate subscription_id as needed. atomic u16? - let subscription_id = r#""0xcd0c3e8af590364c09d0fa6a1210faf5""#.to_string(); - let response = JsonRpcForwardedResponse::from_string(subscription_id, id); Ok((subscription_handle, response))