feat: add eth_sendRawTransaction (#5)

This commit is contained in:
Noah Citron 2022-09-01 17:07:30 -04:00 committed by GitHub
parent dc0e31cedd
commit 76a230446d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 1 deletions

View File

@ -110,6 +110,10 @@ impl Client {
}
}
pub async fn send_raw_transaction(&self, bytes: &Vec<u8>) -> Result<Vec<u8>> {
self.execution.send_raw_transaction(bytes).await
}
pub fn get_gas_price(&self) -> Result<U256> {
let payload = self.get_payload(&None)?;
let base_fee = U256::from_little_endian(&payload.base_fee_per_gas.to_bytes_le());

View File

@ -10,7 +10,7 @@ use jsonrpsee::{
};
use super::Client;
use common::utils::u64_to_hex_string;
use common::utils::{hex_str_to_bytes, u64_to_hex_string};
use execution::types::{CallOpts, ExecutionBlock};
pub struct Rpc {
@ -61,6 +61,8 @@ trait EthRpc {
async fn block_number(&self) -> Result<String, Error>;
#[method(name = "getBlockByNumber")]
async fn get_block_by_number(&self, num: &str, full_tx: bool) -> Result<ExecutionBlock, Error>;
#[method(name = "sendRawTransaction")]
async fn send_raw_transaction(&self, bytes: &str) -> Result<String, Error>;
}
#[rpc(client, server, namespace = "net")]
@ -154,6 +156,13 @@ impl EthRpcServer for RpcInner {
Ok(block)
}
async fn send_raw_transaction(&self, bytes: &str) -> Result<String, Error> {
let client = self.client.lock().await;
let bytes = convert_err(hex_str_to_bytes(bytes))?;
let tx_hash = convert_err(client.send_raw_transaction(&bytes).await)?;
Ok(hex::encode(tx_hash))
}
}
#[async_trait]

View File

@ -97,6 +97,10 @@ impl ExecutionClient {
Ok(code)
}
pub async fn send_raw_transaction(&self, bytes: &Vec<u8>) -> Result<Vec<u8>> {
self.rpc.send_raw_transaction(bytes).await
}
pub fn get_block(&self, payload: &ExecutionPayload) -> Result<ExecutionBlock> {
let empty_nonce = "0x0000000000000000".to_string();
let empty_uncle_hash = "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347";

View File

@ -44,6 +44,14 @@ impl Rpc {
hex_str_to_bytes(&code)
}
pub async fn send_raw_transaction(&self, bytes: &Vec<u8>) -> Result<Vec<u8>> {
let client = self.client()?;
let bytes_hex = format!("0x{}", hex::encode(bytes));
let params = rpc_params!(bytes_hex);
let tx_hash: String = client.request("eth_sendRawTransaction", params).await?;
hex_str_to_bytes(&tx_hash)
}
fn client(&self) -> Result<HttpClient> {
Ok(HttpClientBuilder::default().build(&self.rpc)?)
}