use serde_json::json;
use crate::rebased::{
RpcClient,
client::{RawRpcResponse, RpcResult},
};
use serde::{Deserialize, Serialize};
use serde_with::serde_as;
use super::{
super::{IotaAddress, TransactionDigest},
IotaTransactionBlockResponse, IotaTransactionBlockResponseOptions,
};
pub trait IndexerApi {
#[rustfmt::skip]
async fn query_transaction_blocks(
&self,
query: IotaTransactionBlockResponseQuery,
cursor: Option<TransactionDigest>,
limit: Option<usize>,
descending_order: Option<bool>,
) -> RpcResult<TransactionBlocksPage>;
}
#[derive(Debug, Clone, Deserialize, Serialize, Default)]
#[serde(rename_all = "camelCase", rename = "TransactionBlockResponseQuery", default)]
pub struct IotaTransactionBlockResponseQuery {
pub filter: Option<TransactionFilter>,
pub options: Option<IotaTransactionBlockResponseOptions>,
}
#[serde_as]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub enum TransactionFilter {
FromAddress(IotaAddress),
ToAddress(IotaAddress),
}
pub type TransactionBlocksPage = super::Page<IotaTransactionBlockResponse, TransactionDigest>;
impl IndexerApi for RpcClient {
async fn query_transaction_blocks(
&self,
query: IotaTransactionBlockResponseQuery,
cursor: Option<TransactionDigest>,
limit: Option<usize>,
descending_order: Option<bool>,
) -> RpcResult<TransactionBlocksPage> {
let request_body = json!({
"jsonrpc": "2.0",
"id": 1,
"method": "iotax_queryTransactionBlocks",
"params": [
json!(query),
json!(cursor),
json!(limit),
json!(descending_order)
],
});
let response = self.client.post(self.url.clone()).json(&request_body).send().await?;
let body: RawRpcResponse<TransactionBlocksPage> = response.json().await?;
body.into_result()
}
}