mirror of
https://github.com/serai-dex/serai.git
synced 2025-03-20 14:18:55 +00:00
3.1
This commit is contained in:
parent
5d9067b84d
commit
c878d38c60
1 changed files with 10 additions and 5 deletions
|
@ -5,6 +5,8 @@ use thiserror::Error;
|
||||||
use serde::{Deserialize, de::DeserializeOwned};
|
use serde::{Deserialize, de::DeserializeOwned};
|
||||||
use serde_json::json;
|
use serde_json::json;
|
||||||
|
|
||||||
|
use reqwest::Client;
|
||||||
|
|
||||||
use bitcoin::{
|
use bitcoin::{
|
||||||
hashes::{Hash, hex::FromHex},
|
hashes::{Hash, hex::FromHex},
|
||||||
consensus::encode,
|
consensus::encode,
|
||||||
|
@ -26,7 +28,10 @@ enum RpcResponse<T> {
|
||||||
|
|
||||||
/// A minimal asynchronous Bitcoin RPC client.
|
/// A minimal asynchronous Bitcoin RPC client.
|
||||||
#[derive(Clone, Debug)]
|
#[derive(Clone, Debug)]
|
||||||
pub struct Rpc(String);
|
pub struct Rpc {
|
||||||
|
client: Client,
|
||||||
|
url: String,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone, PartialEq, Eq, Debug, Error)]
|
#[derive(Clone, PartialEq, Eq, Debug, Error)]
|
||||||
pub enum RpcError {
|
pub enum RpcError {
|
||||||
|
@ -40,7 +45,7 @@ pub enum RpcError {
|
||||||
|
|
||||||
impl Rpc {
|
impl Rpc {
|
||||||
pub async fn new(url: String) -> Result<Rpc, RpcError> {
|
pub async fn new(url: String) -> Result<Rpc, RpcError> {
|
||||||
let rpc = Rpc(url);
|
let rpc = Rpc { client: Client::new(), url };
|
||||||
// Make an RPC request to verify the node is reachable and sane
|
// Make an RPC request to verify the node is reachable and sane
|
||||||
rpc.get_latest_block_number().await?;
|
rpc.get_latest_block_number().await?;
|
||||||
Ok(rpc)
|
Ok(rpc)
|
||||||
|
@ -52,9 +57,9 @@ impl Rpc {
|
||||||
method: &str,
|
method: &str,
|
||||||
params: serde_json::Value,
|
params: serde_json::Value,
|
||||||
) -> Result<Response, RpcError> {
|
) -> Result<Response, RpcError> {
|
||||||
let client = reqwest::Client::new();
|
let res = self
|
||||||
let res = client
|
.client
|
||||||
.post(&self.0)
|
.post(&self.url)
|
||||||
.json(&json!({ "jsonrpc": "2.0", "method": method, "params": params }))
|
.json(&json!({ "jsonrpc": "2.0", "method": method, "params": params }))
|
||||||
.send()
|
.send()
|
||||||
.await
|
.await
|
||||||
|
|
Loading…
Reference in a new issue