181 lines
5.3 KiB
Rust
Raw Normal View History

2025-08-07 14:07:34 +03:00
use super::rpc_primitives::requests::{
2025-07-30 14:14:46 +03:00
GetAccountBalanceRequest, GetAccountBalanceResponse, GetBlockDataRequest, GetBlockDataResponse,
GetGenesisIdRequest, GetGenesisIdResponse, GetInitialTestnetAccountsRequest,
2025-04-16 16:17:53 +03:00
};
2025-08-07 14:07:34 +03:00
use anyhow::Result;
2025-02-14 10:58:54 +02:00
use json::{SendTxRequest, SendTxResponse, SequencerRpcRequest, SequencerRpcResponse};
2024-12-05 13:05:58 +02:00
use reqwest::Client;
use serde_json::Value;
2025-08-21 15:58:31 +03:00
use crate::rpc_primitives::requests::{
GetAccountsNoncesRequest, GetAccountsNoncesResponse, GetTransactionByHashRequest,
GetTransactionByHashResponse,
};
2025-07-30 14:01:40 +03:00
use crate::sequencer_client::json::AccountInitialData;
2025-08-07 14:07:34 +03:00
use crate::{SequencerClientError, SequencerRpcError};
2024-12-03 09:32:35 +02:00
pub mod json;
2024-12-05 13:05:58 +02:00
#[derive(Clone)]
2024-12-03 09:32:35 +02:00
pub struct SequencerClient {
pub client: reqwest::Client,
2025-08-07 14:07:34 +03:00
pub sequencer_addr: String,
2024-12-03 09:32:35 +02:00
}
2024-12-05 13:05:58 +02:00
impl SequencerClient {
2025-08-07 14:07:34 +03:00
pub fn new(sequencer_addr: String) -> Result<Self> {
2024-12-05 13:05:58 +02:00
Ok(Self {
client: Client::builder()
//Add more fiedls if needed
.timeout(std::time::Duration::from_secs(60))
.build()?,
2025-08-07 14:07:34 +03:00
sequencer_addr,
2024-12-05 13:05:58 +02:00
})
}
pub async fn call_method_with_payload(
&self,
method: &str,
payload: Value,
) -> Result<Value, SequencerClientError> {
let request = SequencerRpcRequest::from_payload_version_2_0(method.to_string(), payload);
2025-08-07 14:07:34 +03:00
let call_builder = self.client.post(&self.sequencer_addr);
2024-12-05 13:05:58 +02:00
let call_res = call_builder.json(&request).send().await?;
let response_vall = call_res.json::<Value>().await?;
if let Ok(response) = serde_json::from_value::<SequencerRpcResponse>(response_vall.clone())
{
Ok(response.result)
} else {
let err_resp = serde_json::from_value::<SequencerRpcError>(response_vall)?;
2024-12-05 13:05:58 +02:00
Err(err_resp.into())
}
2024-12-05 13:05:58 +02:00
}
2025-08-07 14:07:34 +03:00
///Get block data at `block_id` from sequencer
2024-12-05 13:05:58 +02:00
pub async fn get_block(
&self,
block_id: u64,
) -> Result<GetBlockDataResponse, SequencerClientError> {
let block_req = GetBlockDataRequest { block_id };
let req = serde_json::to_value(block_req)?;
let resp = self.call_method_with_payload("get_block", req).await?;
let resp_deser = serde_json::from_value(resp)?;
Ok(resp_deser)
}
2025-08-07 14:07:34 +03:00
///Get account public balance for `address`. `address` must be a valid hex-string for 32 bytes.
2025-07-30 14:01:40 +03:00
pub async fn get_account_balance(
&self,
address: String,
) -> Result<GetAccountBalanceResponse, SequencerClientError> {
let block_req = GetAccountBalanceRequest { address };
let req = serde_json::to_value(block_req)?;
2025-07-30 14:14:46 +03:00
let resp = self
.call_method_with_payload("get_account_balance", req)
.await?;
2025-07-30 14:01:40 +03:00
let resp_deser = serde_json::from_value(resp)?;
Ok(resp_deser)
}
2025-08-21 15:58:31 +03:00
///Get accounts nonces for `addresses`. `addresses` must be a list of valid hex-strings for 32 bytes.
pub async fn get_accounts_nonces(
&self,
addresses: Vec<String>,
) -> Result<GetAccountsNoncesResponse, SequencerClientError> {
let block_req = GetAccountsNoncesRequest { addresses };
let req = serde_json::to_value(block_req)?;
let resp = self
.call_method_with_payload("get_accounts_nonces", req)
.await?;
let resp_deser = serde_json::from_value(resp)?;
Ok(resp_deser)
}
///Get transaction details for `hash`.
pub async fn get_transaction_by_hash(
&self,
hash: String,
) -> Result<GetTransactionByHashResponse, SequencerClientError> {
let block_req = GetTransactionByHashRequest { hash };
let req = serde_json::to_value(block_req)?;
let resp = self
.call_method_with_payload("get_transaction_by_hash", req)
.await?;
let resp_deser = serde_json::from_value(resp)?;
Ok(resp_deser)
}
2025-08-07 14:07:34 +03:00
///Send transaction to sequencer
2024-12-05 13:05:58 +02:00
pub async fn send_tx(
&self,
transaction: nssa::PublicTransaction,
2024-12-05 13:05:58 +02:00
) -> Result<SendTxResponse, SequencerClientError> {
2025-08-12 16:48:53 -03:00
let tx_req = SendTxRequest {
transaction: transaction.to_bytes(),
};
2024-12-05 13:05:58 +02:00
let req = serde_json::to_value(tx_req)?;
let resp = self.call_method_with_payload("send_tx", req).await?;
let resp_deser = serde_json::from_value(resp)?;
Ok(resp_deser)
}
2025-08-07 14:07:34 +03:00
///Get genesis id from sequencer
2024-12-05 13:05:58 +02:00
pub async fn get_genesis_id(&self) -> Result<GetGenesisIdResponse, SequencerClientError> {
let genesis_req = GetGenesisIdRequest {};
let req = serde_json::to_value(genesis_req).unwrap();
2024-12-05 13:05:58 +02:00
2024-12-30 09:10:04 +02:00
let resp = self
.call_method_with_payload("get_genesis", req)
.await
.unwrap();
2024-12-05 13:05:58 +02:00
let resp_deser = serde_json::from_value(resp).unwrap();
2024-12-05 13:05:58 +02:00
Ok(resp_deser)
}
2025-07-23 15:16:53 +03:00
2025-08-07 14:07:34 +03:00
///Get initial testnet accounts from sequencer
2025-07-23 15:16:53 +03:00
pub async fn get_initial_testnet_accounts(
&self,
2025-07-30 14:01:40 +03:00
) -> Result<Vec<AccountInitialData>, SequencerClientError> {
2025-07-23 15:16:53 +03:00
let acc_req = GetInitialTestnetAccountsRequest {};
let req = serde_json::to_value(acc_req).unwrap();
let resp = self
.call_method_with_payload("get_initial_testnet_accounts", req)
.await
.unwrap();
let resp_deser = serde_json::from_value(resp).unwrap();
Ok(resp_deser)
}
2024-12-05 13:05:58 +02:00
}