use serde::{Deserialize, Serialize};
use serde_json::json;
use crate::rebased::RpcClient;
use crate::rebased::client::{RawRpcResponse, RpcResult};
use super::super::encoding::Base64;
use super::{DryRunTransactionBlockResponse, IotaTransactionBlockResponse, IotaTransactionBlockResponseOptions};
pub trait WriteApi {
async fn execute_transaction_block(
&self,
tx_bytes: Base64,
signatures: Vec<Base64>,
options: Option<IotaTransactionBlockResponseOptions>,
request_type: Option<ExecuteTransactionRequestType>,
) -> RpcResult<IotaTransactionBlockResponse>;
async fn dry_run_transaction_block(&self, tx_bytes: Base64) -> RpcResult<DryRunTransactionBlockResponse>;
}
#[derive(Serialize, Deserialize, Clone, Debug)]
pub enum ExecuteTransactionRequestType {
WaitForEffectsCert,
WaitForLocalExecution,
}
impl WriteApi for RpcClient {
async fn execute_transaction_block(
&self,
tx_bytes: Base64,
signatures: Vec<Base64>,
options: Option<IotaTransactionBlockResponseOptions>,
request_type: Option<ExecuteTransactionRequestType>,
) -> RpcResult<IotaTransactionBlockResponse> {
let request_body = json!({
"jsonrpc": "2.0",
"id": 1,
"method": "iota_executeTransactionBlock",
"params": [json!(tx_bytes), json!(signatures), json!(options), json!(request_type)]
});
let response = self.client.post(self.url.clone()).json(&request_body).send().await?;
let body: RawRpcResponse<IotaTransactionBlockResponse> = response.json().await?;
body.into_result()
}
async fn dry_run_transaction_block(&self, tx_bytes: Base64) -> RpcResult<DryRunTransactionBlockResponse> {
let request_body = json!({
"jsonrpc": "2.0",
"id": 1,
"method": "iota_dryRunTransactionBlock",
"params": [
json!(tx_bytes)
]
});
let response = self.client.post(self.url.clone()).json(&request_body).send().await?;
let body: RawRpcResponse<DryRunTransactionBlockResponse> = response.json().await?;
body.into_result()
}
}