Skip to content

Commit

Permalink
chore(starknet_api): move rpc tx test util convertion fn
Browse files Browse the repository at this point in the history
commit-id:9275b3bb
  • Loading branch information
Itay-Tsabary-Starkware committed Nov 28, 2024
1 parent 822f957 commit ca28f2a
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 22 deletions.
21 changes: 0 additions & 21 deletions crates/mempool_test_utils/src/starknet_api_test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use blockifier::test_utils::contracts::FeatureContract;
use blockifier::test_utils::{create_trivial_calldata, CairoVersion};
use infra_utils::path::resolve_project_relative_path;
use pretty_assertions::assert_ne;
use serde_json::to_string_pretty;
use starknet_api::block::GasPrice;
use starknet_api::core::{ClassHash, CompiledClassHash, ContractAddress, Nonce};
use starknet_api::executable_transaction::AccountTransaction;
Expand Down Expand Up @@ -361,23 +360,3 @@ impl Contract {
}
}
}

pub fn rpc_tx_to_json(tx: &RpcTransaction) -> String {
let mut tx_json = serde_json::to_value(tx)
.unwrap_or_else(|tx| panic!("Failed to serialize transaction: {tx:?}"));

// Add type and version manually
let type_string = match tx {
RpcTransaction::Declare(_) => "DECLARE",
RpcTransaction::DeployAccount(_) => "DEPLOY_ACCOUNT",
RpcTransaction::Invoke(_) => "INVOKE",
};

tx_json
.as_object_mut()
.unwrap()
.extend([("type".to_string(), type_string.into()), ("version".to_string(), "0x3".into())]);

// Serialize back to pretty JSON string
to_string_pretty(&tx_json).expect("Failed to serialize transaction")
}
23 changes: 23 additions & 0 deletions crates/starknet_api/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use std::path::{Path, PathBuf};

use infra_utils::path::cargo_manifest_dir;
use serde::{Deserialize, Serialize};
use serde_json::to_string_pretty;
use starknet_types_core::felt::Felt;

use crate::block::BlockNumber;
use crate::core::{ChainId, ContractAddress, Nonce};
use crate::rpc_transaction::RpcTransaction;
use crate::transaction::{Transaction, TransactionHash};

pub mod declare;
Expand Down Expand Up @@ -90,3 +92,24 @@ macro_rules! compiled_class_hash {
$crate::core::CompiledClassHash(starknet_types_core::felt::Felt::from($s))
};
}

/// Converts a [`RpcTransaction`] to a JSON string.
pub fn rpc_tx_to_json(tx: &RpcTransaction) -> String {
let mut tx_json = serde_json::to_value(tx)
.unwrap_or_else(|tx| panic!("Failed to serialize transaction: {tx:?}"));

// Add type and version manually
let type_string = match tx {
RpcTransaction::Declare(_) => "DECLARE",
RpcTransaction::DeployAccount(_) => "DEPLOY_ACCOUNT",
RpcTransaction::Invoke(_) => "INVOKE",
};

tx_json
.as_object_mut()
.unwrap()
.extend([("type".to_string(), type_string.into()), ("version".to_string(), "0x3".into())]);

// Serialize back to pretty JSON string
to_string_pretty(&tx_json).expect("Failed to serialize transaction")
}
2 changes: 1 addition & 1 deletion crates/starknet_http_server/src/test_utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use std::net::SocketAddr;

use axum::body::Body;
use mempool_test_utils::starknet_api_test_utils::rpc_tx_to_json;
use reqwest::{Client, Response};
use starknet_api::rpc_transaction::RpcTransaction;
use starknet_api::test_utils::rpc_tx_to_json;
use starknet_api::transaction::TransactionHash;
use starknet_gateway_types::errors::GatewaySpecError;
use starknet_sequencer_infra::test_utils::get_available_socket;
Expand Down

0 comments on commit ca28f2a

Please sign in to comment.