Skip to content

Commit

Permalink
fix: StacksRpc internal improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
lgalabru committed Dec 2, 2021
1 parent 940f17d commit 2bf6c52
Showing 1 changed file with 12 additions and 7 deletions.
19 changes: 12 additions & 7 deletions src/utils/stacks/rpc_client.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clarity_repl::clarity::codec::{StacksMessageCodec, StacksTransaction};
use clarity_repl::clarity::types::Value;
use clarity_repl::clarity::util::hash::{bytes_to_hex, hex_bytes};
use reqwest::blocking::Client;
use std::io::Cursor;

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

pub struct StacksRpc {
pub url: String,
pub client: Client,
}

pub struct PostTransactionResult {
Expand Down Expand Up @@ -48,17 +50,19 @@ struct Balance {

impl StacksRpc {
pub fn new(url: &str) -> Self {
Self { url: url.into() }
Self {
url: url.into(),
client: Client::builder().build().unwrap(),
}
}

pub fn post_transaction(
&self,
transaction: StacksTransaction,
) -> Result<PostTransactionResult, RpcError> {
let tx = transaction.serialize_to_vec();
let client = reqwest::blocking::Client::new();
let path = format!("{}/v2/transactions", self.url);
let res = client
let res = self.client
.post(&path)
.header("Content-Type", "application/octet-stream")
.body(tx)
Expand All @@ -78,7 +82,8 @@ impl StacksRpc {
pub fn get_nonce(&self, address: &str) -> Result<u64, RpcError> {
let request_url = format!("{}/v2/accounts/{addr}", self.url, addr = address,);

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

let res: PoxInfo = reqwest::blocking::get(&request_url)
let res: PoxInfo = self.client.get(&request_url)
.send()
.expect("Unable to retrieve account")
.json()
.expect("Unable to parse contract");
Expand All @@ -104,7 +110,6 @@ impl StacksRpc {
args: Vec<Value>,
sender: &str,
) -> Result<Value, RpcError> {
let client = reqwest::blocking::Client::new();
let path = format!(
"{}/v2/contracts/call-read/{}/{}/{}",
self.url, contract_addr, contract_name, method
Expand All @@ -114,7 +119,7 @@ impl StacksRpc {
.iter()
.map(|a| bytes_to_hex(&a.serialize_to_vec()))
.collect::<Vec<_>>();
let res = client
let res = self.client
.post(&path)
.json(&json!({
"sender": sender,
Expand Down

0 comments on commit 2bf6c52

Please sign in to comment.