Skip to content

Commit 2bf6c52

Browse files
committed
fix: StacksRpc internal improvements
1 parent 940f17d commit 2bf6c52

File tree

1 file changed

+12
-7
lines changed

1 file changed

+12
-7
lines changed

src/utils/stacks/rpc_client.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use clarity_repl::clarity::codec::{StacksMessageCodec, StacksTransaction};
22
use clarity_repl::clarity::types::Value;
33
use clarity_repl::clarity::util::hash::{bytes_to_hex, hex_bytes};
4+
use reqwest::blocking::Client;
45
use std::io::Cursor;
56

67
#[derive(Debug)]
@@ -10,6 +11,7 @@ pub enum RpcError {
1011

1112
pub struct StacksRpc {
1213
pub url: String,
14+
pub client: Client,
1315
}
1416

1517
pub struct PostTransactionResult {
@@ -48,17 +50,19 @@ struct Balance {
4850

4951
impl StacksRpc {
5052
pub fn new(url: &str) -> Self {
51-
Self { url: url.into() }
53+
Self {
54+
url: url.into(),
55+
client: Client::builder().build().unwrap(),
56+
}
5257
}
5358

5459
pub fn post_transaction(
5560
&self,
5661
transaction: StacksTransaction,
5762
) -> Result<PostTransactionResult, RpcError> {
5863
let tx = transaction.serialize_to_vec();
59-
let client = reqwest::blocking::Client::new();
6064
let path = format!("{}/v2/transactions", self.url);
61-
let res = client
65+
let res = self.client
6266
.post(&path)
6367
.header("Content-Type", "application/octet-stream")
6468
.body(tx)
@@ -78,7 +82,8 @@ impl StacksRpc {
7882
pub fn get_nonce(&self, address: &str) -> Result<u64, RpcError> {
7983
let request_url = format!("{}/v2/accounts/{addr}", self.url, addr = address,);
8084

81-
let res: Balance = reqwest::blocking::get(&request_url)
85+
let res: Balance = self.client.get(&request_url)
86+
.send()
8287
.expect("Unable to retrieve account")
8388
.json()
8489
.expect("Unable to parse contract");
@@ -89,7 +94,8 @@ impl StacksRpc {
8994
pub fn get_pox_info(&self) -> Result<PoxInfo, RpcError> {
9095
let request_url = format!("{}/v2/pox", self.url);
9196

92-
let res: PoxInfo = reqwest::blocking::get(&request_url)
97+
let res: PoxInfo = self.client.get(&request_url)
98+
.send()
9399
.expect("Unable to retrieve account")
94100
.json()
95101
.expect("Unable to parse contract");
@@ -104,7 +110,6 @@ impl StacksRpc {
104110
args: Vec<Value>,
105111
sender: &str,
106112
) -> Result<Value, RpcError> {
107-
let client = reqwest::blocking::Client::new();
108113
let path = format!(
109114
"{}/v2/contracts/call-read/{}/{}/{}",
110115
self.url, contract_addr, contract_name, method
@@ -114,7 +119,7 @@ impl StacksRpc {
114119
.iter()
115120
.map(|a| bytes_to_hex(&a.serialize_to_vec()))
116121
.collect::<Vec<_>>();
117-
let res = client
122+
let res = self.client
118123
.post(&path)
119124
.json(&json!({
120125
"sender": sender,

0 commit comments

Comments
 (0)