pub trait RpcHandler: Sized {
// Required methods
fn parse(params: &Option<Vec<Value>>) -> Result<Self, RpcErr>;
async fn handle(&self, context: RpcApiContext) -> Result<Value, RpcErr>;
// Provided method
async fn call(
req: &RpcRequest,
context: RpcApiContext,
) -> Result<Value, RpcErr> { ... }
}Expand description
Trait for implementing JSON-RPC method handlers.
Each RPC method (e.g., eth_getBalance, engine_newPayloadV3) is implemented
as a struct that implements this trait. The trait provides a standard pattern
for parsing parameters and handling requests.
§Example
ⓘ
struct GetBalanceRequest {
address: Address,
block: BlockId,
}
impl RpcHandler for GetBalanceRequest {
fn parse(params: &Option<Vec<Value>>) -> Result<Self, RpcErr> {
let params = params.as_ref().ok_or(RpcErr::MissingParam("params"))?;
Ok(Self {
address: serde_json::from_value(params[0].clone())?,
block: serde_json::from_value(params[1].clone())?,
})
}
async fn handle(&self, context: RpcApiContext) -> Result<Value, RpcErr> {
let balance = context.storage.get_balance(self.address, self.block)?;
Ok(serde_json::to_value(balance)?)
}
}Required Methods§
Provided Methods§
Sourceasync fn call(req: &RpcRequest, context: RpcApiContext) -> Result<Value, RpcErr>
async fn call(req: &RpcRequest, context: RpcApiContext) -> Result<Value, RpcErr>
Entry point for handling an RPC request.
This method parses the request, records metrics, and delegates to handle().
Most implementations should not override this method.
Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety".