diff --git a/crates/starknet-types-core/Cargo.toml b/crates/starknet-types-core/Cargo.toml index 8ebcf930..054ab855 100644 --- a/crates/starknet-types-core/Cargo.toml +++ b/crates/starknet-types-core/Cargo.toml @@ -26,7 +26,7 @@ parity-scale-codec = { version = "3.6.9", default-features = false, optional = t lazy_static = { version = "1.4.0", default-features = false, optional = true } [features] -default = ["std", "serde", "curve", "num-traits"] +default = ["std", "serde", "curve", "num-traits", "hash"] std = [ "lambdaworks-math/std", "num-traits/std", @@ -57,4 +57,3 @@ lazy_static = { version = "1.4.0", default-features = false } [[bench]] name = "criterion_hashes" harness = false - diff --git a/crates/starknet-types-rpc/src/custom/block_id.rs b/crates/starknet-types-rpc/src/custom/block_id.rs index e5b92ebf..b0945e32 100644 --- a/crates/starknet-types-rpc/src/custom/block_id.rs +++ b/crates/starknet-types-rpc/src/custom/block_id.rs @@ -66,78 +66,64 @@ impl<'de, F: Deserialize<'de>> serde::Deserialize<'de> for BlockId { } } -#[test] -fn block_id_from_hash() { - pub use starknet_types_core::felt::Felt; - - let s = "{\"block_hash\":\"0x123\"}"; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Hash(Felt::from_hex("0x123").unwrap())); -} - -#[test] -fn block_id_from_number() { - pub use starknet_types_core::felt::Felt; - - let s = "{\"block_number\":123}"; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Number(123)); -} - -#[test] -fn block_id_from_latest() { - pub use starknet_types_core::felt::Felt; - - let s = "\"latest\""; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Tag(BlockTag::Latest)); -} - -#[test] -fn block_id_from_pending() { - pub use starknet_types_core::felt::Felt; - - let s = "\"pending\""; - let block_id: BlockId = serde_json::from_str(s).unwrap(); - assert_eq!(block_id, BlockId::Tag(BlockTag::Pending)); -} - #[cfg(test)] -#[test] -fn block_id_to_hash() { - pub use starknet_types_core::felt::Felt; +mod tests { + use super::{BlockId, BlockTag}; + use starknet_types_core::felt::Felt; + + #[test] + fn block_id_from_hash() { + let s = "{\"block_hash\":\"0x123\"}"; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Hash(Felt::from_hex("0x123").unwrap())); + } - let block_id = BlockId::Hash(Felt::from_hex("0x123").unwrap()); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "{\"block_hash\":\"0x123\"}"); -} + #[test] + fn block_id_from_number() { + let s = "{\"block_number\":123}"; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Number(123)); + } -#[cfg(test)] -#[test] -fn block_id_to_number() { - pub use starknet_types_core::felt::Felt; + #[test] + fn block_id_from_latest() { + let s = "\"latest\""; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Tag(BlockTag::Latest)); + } - let block_id = BlockId::::Number(123); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "{\"block_number\":123}"); -} + #[test] + fn block_id_from_pending() { + let s = "\"pending\""; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Tag(BlockTag::Pending)); + } -#[cfg(test)] -#[test] -fn block_id_to_latest() { - pub use starknet_types_core::felt::Felt; + #[test] + fn block_id_to_hash() { + let block_id = BlockId::Hash(Felt::from_hex("0x123").unwrap()); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "{\"block_hash\":\"0x123\"}"); + } - let block_id = BlockId::::Tag(BlockTag::Latest); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "\"latest\""); -} + #[test] + fn block_id_to_number() { + let block_id = BlockId::::Number(123); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "{\"block_number\":123}"); + } -#[cfg(test)] -#[test] -fn block_id_to_pending() { - pub use starknet_types_core::felt::Felt; + #[test] + fn block_id_to_latest() { + let block_id = BlockId::::Tag(BlockTag::Latest); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "\"latest\""); + } - let block_id = BlockId::::Tag(BlockTag::Pending); - let s = serde_json::to_string(&block_id).unwrap(); - assert_eq!(s, "\"pending\""); + #[test] + fn block_id_to_pending() { + let block_id = BlockId::::Tag(BlockTag::Pending); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "\"pending\""); + } } diff --git a/crates/starknet-types-rpc/src/custom/syncing_status.rs b/crates/starknet-types-rpc/src/custom/syncing_status.rs index d81b57f0..db200095 100644 --- a/crates/starknet-types-rpc/src/custom/syncing_status.rs +++ b/crates/starknet-types-rpc/src/custom/syncing_status.rs @@ -70,29 +70,27 @@ impl<'de, F: Deserialize<'de>> Deserialize<'de> for SyncingStatus { } #[cfg(test)] -#[test] -fn syncing_status_from_false() { +mod tests { + use super::SyncingStatus; pub use starknet_types_core::felt::Felt; - let s = "false"; - let syncing_status: SyncingStatus = serde_json::from_str(s).unwrap(); - assert!(matches!(syncing_status, SyncingStatus::NotSyncing)); -} - -#[cfg(test)] -#[test] -fn syncing_status_to_false() { - pub use starknet_types_core::felt::Felt; + #[test] + fn syncing_status_from_false() { + let s = "false"; + let syncing_status: SyncingStatus = serde_json::from_str(s).unwrap(); + assert!(matches!(syncing_status, SyncingStatus::NotSyncing)); + } - let syncing_status = SyncingStatus::::NotSyncing; - let s = serde_json::to_string(&syncing_status).unwrap(); - assert_eq!(s, "false"); -} + #[test] + fn syncing_status_to_false() { + let syncing_status = SyncingStatus::::NotSyncing; + let s = serde_json::to_string(&syncing_status).unwrap(); + assert_eq!(s, "false"); + } -#[cfg(test)] -#[test] -fn syncing_status_from_true() { - pub use starknet_types_core::felt::Felt; - let s = "true"; - assert!(serde_json::from_str::>(s).is_err()); + #[test] + fn syncing_status_from_true() { + let s = "true"; + assert!(serde_json::from_str::>(s).is_err()); + } } diff --git a/crates/starknet-types-rpc/src/custom_serde/num_as_hex.rs b/crates/starknet-types-rpc/src/custom_serde/num_as_hex.rs index a3fe274e..41ad4628 100644 --- a/crates/starknet-types-rpc/src/custom_serde/num_as_hex.rs +++ b/crates/starknet-types-rpc/src/custom_serde/num_as_hex.rs @@ -198,72 +198,66 @@ where } #[cfg(test)] -#[derive(serde::Serialize, serde::Deserialize)] -#[serde(transparent)] -struct Helper { - #[serde(with = "NumAsHex")] - num: u64, -} +mod tests { + use super::*; + + #[derive(serde::Serialize, serde::Deserialize)] + #[serde(transparent)] + struct Helper { + #[serde(with = "NumAsHex")] + num: u64, + } -#[cfg(test)] -fn serialize(num: u64) -> serde_json::Result { - let helper = Helper { num }; - serde_json::to_string(&helper) -} + fn serialize(num: u64) -> serde_json::Result { + let helper = Helper { num }; + serde_json::to_string(&helper) + } -#[cfg(test)] -fn deserialize(s: &str) -> serde_json::Result { - let helper: Helper = serde_json::from_str(s)?; - Ok(helper.num) -} + fn deserialize(s: &str) -> serde_json::Result { + let helper: Helper = serde_json::from_str(s)?; + Ok(helper.num) + } -#[test] -#[cfg(test)] -fn serialize_0_hex() { - assert_eq!(serialize(0x0).unwrap(), "\"0x0\""); -} + #[test] + fn serialize_0_hex() { + assert_eq!(serialize(0x0).unwrap(), "\"0x0\""); + } -#[test] -#[cfg(test)] -fn srialize_hex() { - assert_eq!(serialize(0x1234).unwrap(), "\"0x1234\""); -} + #[test] + fn serialize_hex() { + assert_eq!(serialize(0x1234).unwrap(), "\"0x1234\""); + } -#[test] -#[cfg(test)] -fn srialize_max() { - assert_eq!(serialize(u64::MAX).unwrap(), "\"0xffffffffffffffff\""); -} + #[test] + fn serialize_max() { + assert_eq!(serialize(u64::MAX).unwrap(), "\"0xffffffffffffffff\""); + } -#[test] -#[cfg(test)] -fn deserialize_zero() { - assert_eq!(deserialize("\"0x0\"").unwrap(), 0); -} + #[test] + fn deserialize_zero() { + assert_eq!(deserialize("\"0x0\"").unwrap(), 0); + } -#[test] -#[cfg(test)] -fn deserialize_zeros() { - assert_eq!(deserialize("\"0x00000\"").unwrap(), 0); -} + #[test] + fn deserialize_zeros() { + assert_eq!(deserialize("\"0x00000\"").unwrap(), 0); + } -#[test] -#[cfg(test)] -fn deserialize_max() { - assert_eq!(deserialize("\"0xFFFFFFFFFFFFFFFF\"").unwrap(), u64::MAX); -} + #[test] + fn deserialize_max() { + assert_eq!(deserialize("\"0xFFFFFFFFFFFFFFFF\"").unwrap(), u64::MAX); + } -#[test] -#[cfg(test)] -fn deserialize_big_one() { - assert_eq!( - deserialize("\"0x000000000000000000000000000001\"").unwrap(), - 1 - ); -} + #[test] + fn deserialize_big_one() { + assert_eq!( + deserialize("\"0x000000000000000000000000000001\"").unwrap(), + 1 + ); + } -#[test] -#[cfg(test)] -fn deserialize_hex() { - assert_eq!(deserialize("\"0x1234\"").unwrap(), 0x1234); + #[test] + fn deserialize_hex() { + assert_eq!(deserialize("\"0x1234\"").unwrap(), 0x1234); + } } diff --git a/crates/starknet-types-rpc/src/lib.rs b/crates/starknet-types-rpc/src/lib.rs index dfb4ddcd..7c6d8c73 100644 --- a/crates/starknet-types-rpc/src/lib.rs +++ b/crates/starknet-types-rpc/src/lib.rs @@ -24,6 +24,8 @@ mod custom_serde; // // Generated files. +pub mod v0_5_0; +pub mod v0_6_0; pub mod v0_7_1; pub use self::v0_7_1::*; diff --git a/crates/starknet-types-rpc/src/v0_5_0/block_id.rs b/crates/starknet-types-rpc/src/v0_5_0/block_id.rs new file mode 100644 index 00000000..f1e2a044 --- /dev/null +++ b/crates/starknet-types-rpc/src/v0_5_0/block_id.rs @@ -0,0 +1,127 @@ +use serde::{Deserialize, Deserializer, Serialize}; + +use super::{BlockHash, BlockNumber, BlockTag}; + +/// A hexadecimal number. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum BlockId { + /// The tag of the block. + Tag(BlockTag), + /// The hash of the block. + Hash(BlockHash), + /// The height of the block. + Number(BlockNumber), +} + +#[derive(Serialize, Deserialize)] +struct BlockHashHelper { + block_hash: BlockHash, +} + +#[derive(Serialize, Deserialize)] +struct BlockNumberHelper { + block_number: BlockNumber, +} + +#[derive(Deserialize)] +#[serde(untagged)] +enum BlockIdHelper { + Tag(BlockTag), + Hash(BlockHashHelper), + Number(BlockNumberHelper), +} + +impl serde::Serialize for BlockId { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + match *self { + BlockId::Tag(tag) => tag.serialize(serializer), + BlockId::Hash(block_hash) => { + let helper = BlockHashHelper { block_hash }; + helper.serialize(serializer) + } + BlockId::Number(block_number) => { + let helper = BlockNumberHelper { block_number }; + helper.serialize(serializer) + } + } + } +} + +impl<'de> serde::Deserialize<'de> for BlockId { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let helper = BlockIdHelper::deserialize(deserializer)?; + match helper { + BlockIdHelper::Tag(tag) => Ok(BlockId::Tag(tag)), + BlockIdHelper::Hash(helper) => Ok(BlockId::Hash(helper.block_hash)), + BlockIdHelper::Number(helper) => Ok(BlockId::Number(helper.block_number)), + } + } +} + +#[cfg(test)] +mod tests { + use super::{BlockId, BlockTag}; + use starknet_types_core::felt::Felt; + + #[test] + fn block_id_from_hash() { + let s = "{\"block_hash\":\"0x123\"}"; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Hash(Felt::from_hex("0x123").unwrap())); + } + + #[test] + fn block_id_from_number() { + let s = "{\"block_number\":123}"; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Number(123)); + } + + #[test] + fn block_id_from_latest() { + let s = "\"latest\""; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Tag(BlockTag::Latest)); + } + + #[test] + fn block_id_from_pending() { + let s = "\"pending\""; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Tag(BlockTag::Pending)); + } + + #[test] + fn block_id_to_hash() { + let block_id = BlockId::Hash(Felt::from_hex("0x123").unwrap()); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "{\"block_hash\":\"0x123\"}"); + } + + #[test] + fn block_id_to_number() { + let block_id = BlockId::Number(123); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "{\"block_number\":123}"); + } + + #[test] + fn block_id_to_latest() { + let block_id = BlockId::Tag(BlockTag::Latest); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "\"latest\""); + } + + #[test] + fn block_id_to_pending() { + let block_id = BlockId::Tag(BlockTag::Pending); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "\"pending\""); + } +} diff --git a/crates/starknet-types-rpc/src/v0_5_0/mod.rs b/crates/starknet-types-rpc/src/v0_5_0/mod.rs index 7a43d1bc..55f328d9 100644 --- a/crates/starknet-types-rpc/src/v0_5_0/mod.rs +++ b/crates/starknet-types-rpc/src/v0_5_0/mod.rs @@ -2,15 +2,16 @@ pub use starknet_types_core::felt::Felt; -pub use crate::custom::{ - BlockId, BroadcastedDeclareTxn, BroadcastedDeployAccountTxn, BroadcastedInvokeTxn, - SyncingStatus, -}; - +mod block_id; +mod query; mod starknet_api_openrpc; mod starknet_trace_api_openrpc; mod starknet_write_api; +mod syncing_status; +pub use self::block_id::*; +pub use self::query::*; pub use self::starknet_api_openrpc::*; pub use self::starknet_trace_api_openrpc::*; pub use self::starknet_write_api::*; +pub use self::syncing_status::*; diff --git a/crates/starknet-types-rpc/src/v0_5_0/query.rs b/crates/starknet-types-rpc/src/v0_5_0/query.rs new file mode 100644 index 00000000..e9dc1618 --- /dev/null +++ b/crates/starknet-types-rpc/src/v0_5_0/query.rs @@ -0,0 +1,48 @@ +// query offset: +// 0x0000000000000000000000000000000100000000000000000000000000000000 + +use serde::{Deserialize, Serialize}; + +use super::{ + BroadcastedDeclareTxnV1, BroadcastedDeclareTxnV2, DeployAccountTxnV1, InvokeTxnV0, InvokeTxnV1, +}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "version")] +pub enum BroadcastedDeclareTxn { + #[serde(rename = "0x1")] + V1(BroadcastedDeclareTxnV1), + #[serde(rename = "0x2")] + V2(BroadcastedDeclareTxnV2), + /// Query-only broadcasted declare transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000001")] + QueryV1(BroadcastedDeclareTxnV1), + /// Query-only broadcasted declare transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000002")] + QueryV2(BroadcastedDeclareTxnV2), +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "version")] +pub enum BroadcastedDeployAccountTxn { + #[serde(rename = "0x1")] + V1(DeployAccountTxnV1), + /// Query-only broadcasted deploy account transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000001")] + QueryV1(DeployAccountTxnV1), +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "version")] +pub enum BroadcastedInvokeTxn { + #[serde(rename = "0x0")] + V0(InvokeTxnV0), + #[serde(rename = "0x1")] + V1(InvokeTxnV1), + /// Query-only broadcasted invoke transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000000")] + QueryV0(InvokeTxnV0), + /// Query-only broadcasted invoke transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000001")] + QueryV1(InvokeTxnV1), +} diff --git a/crates/starknet-types-rpc/src/v0_5_0/starknet_api_openrpc.rs b/crates/starknet-types-rpc/src/v0_5_0/starknet_api_openrpc.rs index f840fa6e..349d9850 100644 --- a/crates/starknet-types-rpc/src/v0_5_0/starknet_api_openrpc.rs +++ b/crates/starknet-types-rpc/src/v0_5_0/starknet_api_openrpc.rs @@ -8,15 +8,16 @@ // https://github.com/nils-mathieu/openrpc-gen // -use super::{ - BlockId, BroadcastedDeclareTxn, BroadcastedDeployAccountTxn, BroadcastedInvokeTxn, Felt, -}; use crate::custom_serde::NumAsHex; use alloc::string::String; use alloc::vec::Vec; use serde::ser::SerializeMap; use serde::{Deserialize, Serialize}; +use super::{ + BlockId, BroadcastedDeclareTxn, BroadcastedDeployAccountTxn, BroadcastedInvokeTxn, Felt, +}; + pub type Address = Felt; #[derive(Debug, Clone, Serialize, Deserialize)] diff --git a/crates/starknet-types-rpc/src/v0_5_0/syncing_status.rs b/crates/starknet-types-rpc/src/v0_5_0/syncing_status.rs new file mode 100644 index 00000000..2a22ffcc --- /dev/null +++ b/crates/starknet-types-rpc/src/v0_5_0/syncing_status.rs @@ -0,0 +1,90 @@ +use serde::de::Visitor; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +use super::SyncStatus; + +/// The syncing status of a node. +#[derive(Clone, Debug)] +pub enum SyncingStatus { + /// The node is not syncing. + NotSyncing, + /// The node is syncing. + Syncing(SyncStatus), +} + +impl Serialize for SyncingStatus { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + match self { + SyncingStatus::NotSyncing => serializer.serialize_bool(false), + SyncingStatus::Syncing(status) => status.serialize(serializer), + } + } +} + +impl<'de> Deserialize<'de> for SyncingStatus { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + struct SyncingStatusVisitor; + + impl<'de> Visitor<'de> for SyncingStatusVisitor { + type Value = SyncingStatus; + + fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result { + writeln!(formatter, "a syncing status") + } + + fn visit_bool(self, v: bool) -> Result + where + E: serde::de::Error, + { + if v { + Err(serde::de::Error::custom("expected a syncing status")) + } else { + Ok(SyncingStatus::NotSyncing) + } + } + + fn visit_map(self, map: A) -> Result + where + A: serde::de::MapAccess<'de>, + { + let status = + SyncStatus::deserialize(serde::de::value::MapAccessDeserializer::new(map))?; + + Ok(SyncingStatus::Syncing(status)) + } + } + + deserializer.deserialize_any(SyncingStatusVisitor) + } +} + +#[cfg(test)] +mod tests { + use super::SyncingStatus; + + #[test] + fn syncing_status_from_false() { + let s = "false"; + let syncing_status: SyncingStatus = serde_json::from_str(s).unwrap(); + assert!(matches!(syncing_status, SyncingStatus::NotSyncing)); + } + + #[test] + fn syncing_status_to_false() { + let syncing_status = SyncingStatus::NotSyncing; + let s = serde_json::to_string(&syncing_status).unwrap(); + assert_eq!(s, "false"); + } + + #[test] + fn syncing_status_from_true() { + let s = "true"; + assert!(serde_json::from_str::(s).is_err()); + } +} diff --git a/crates/starknet-types-rpc/src/v0_6_0/block_id.rs b/crates/starknet-types-rpc/src/v0_6_0/block_id.rs new file mode 100644 index 00000000..f1e2a044 --- /dev/null +++ b/crates/starknet-types-rpc/src/v0_6_0/block_id.rs @@ -0,0 +1,127 @@ +use serde::{Deserialize, Deserializer, Serialize}; + +use super::{BlockHash, BlockNumber, BlockTag}; + +/// A hexadecimal number. +#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] +pub enum BlockId { + /// The tag of the block. + Tag(BlockTag), + /// The hash of the block. + Hash(BlockHash), + /// The height of the block. + Number(BlockNumber), +} + +#[derive(Serialize, Deserialize)] +struct BlockHashHelper { + block_hash: BlockHash, +} + +#[derive(Serialize, Deserialize)] +struct BlockNumberHelper { + block_number: BlockNumber, +} + +#[derive(Deserialize)] +#[serde(untagged)] +enum BlockIdHelper { + Tag(BlockTag), + Hash(BlockHashHelper), + Number(BlockNumberHelper), +} + +impl serde::Serialize for BlockId { + fn serialize(&self, serializer: S) -> Result + where + S: serde::Serializer, + { + match *self { + BlockId::Tag(tag) => tag.serialize(serializer), + BlockId::Hash(block_hash) => { + let helper = BlockHashHelper { block_hash }; + helper.serialize(serializer) + } + BlockId::Number(block_number) => { + let helper = BlockNumberHelper { block_number }; + helper.serialize(serializer) + } + } + } +} + +impl<'de> serde::Deserialize<'de> for BlockId { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let helper = BlockIdHelper::deserialize(deserializer)?; + match helper { + BlockIdHelper::Tag(tag) => Ok(BlockId::Tag(tag)), + BlockIdHelper::Hash(helper) => Ok(BlockId::Hash(helper.block_hash)), + BlockIdHelper::Number(helper) => Ok(BlockId::Number(helper.block_number)), + } + } +} + +#[cfg(test)] +mod tests { + use super::{BlockId, BlockTag}; + use starknet_types_core::felt::Felt; + + #[test] + fn block_id_from_hash() { + let s = "{\"block_hash\":\"0x123\"}"; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Hash(Felt::from_hex("0x123").unwrap())); + } + + #[test] + fn block_id_from_number() { + let s = "{\"block_number\":123}"; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Number(123)); + } + + #[test] + fn block_id_from_latest() { + let s = "\"latest\""; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Tag(BlockTag::Latest)); + } + + #[test] + fn block_id_from_pending() { + let s = "\"pending\""; + let block_id: BlockId = serde_json::from_str(s).unwrap(); + assert_eq!(block_id, BlockId::Tag(BlockTag::Pending)); + } + + #[test] + fn block_id_to_hash() { + let block_id = BlockId::Hash(Felt::from_hex("0x123").unwrap()); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "{\"block_hash\":\"0x123\"}"); + } + + #[test] + fn block_id_to_number() { + let block_id = BlockId::Number(123); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "{\"block_number\":123}"); + } + + #[test] + fn block_id_to_latest() { + let block_id = BlockId::Tag(BlockTag::Latest); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "\"latest\""); + } + + #[test] + fn block_id_to_pending() { + let block_id = BlockId::Tag(BlockTag::Pending); + let s = serde_json::to_string(&block_id).unwrap(); + assert_eq!(s, "\"pending\""); + } +} diff --git a/crates/starknet-types-rpc/src/v0_6_0/mod.rs b/crates/starknet-types-rpc/src/v0_6_0/mod.rs index 4570a01b..b0981ec8 100644 --- a/crates/starknet-types-rpc/src/v0_6_0/mod.rs +++ b/crates/starknet-types-rpc/src/v0_6_0/mod.rs @@ -2,15 +2,16 @@ pub use starknet_types_core::felt::Felt; -pub use crate::custom::{ - BlockId, BroadcastedDeclareTxn, BroadcastedDeployAccountTxn, BroadcastedInvokeTxn, - SyncingStatus, -}; - +mod block_id; +mod query; mod starknet_api_openrpc; mod starknet_trace_api_openrpc; mod starknet_write_api; +mod syncing_status; +pub use self::block_id::*; +pub use self::query::*; pub use self::starknet_api_openrpc::*; pub use self::starknet_trace_api_openrpc::*; pub use self::starknet_write_api::*; +pub use self::syncing_status::*; diff --git a/crates/starknet-types-rpc/src/v0_6_0/query.rs b/crates/starknet-types-rpc/src/v0_6_0/query.rs new file mode 100644 index 00000000..e9dc1618 --- /dev/null +++ b/crates/starknet-types-rpc/src/v0_6_0/query.rs @@ -0,0 +1,48 @@ +// query offset: +// 0x0000000000000000000000000000000100000000000000000000000000000000 + +use serde::{Deserialize, Serialize}; + +use super::{ + BroadcastedDeclareTxnV1, BroadcastedDeclareTxnV2, DeployAccountTxnV1, InvokeTxnV0, InvokeTxnV1, +}; + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "version")] +pub enum BroadcastedDeclareTxn { + #[serde(rename = "0x1")] + V1(BroadcastedDeclareTxnV1), + #[serde(rename = "0x2")] + V2(BroadcastedDeclareTxnV2), + /// Query-only broadcasted declare transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000001")] + QueryV1(BroadcastedDeclareTxnV1), + /// Query-only broadcasted declare transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000002")] + QueryV2(BroadcastedDeclareTxnV2), +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "version")] +pub enum BroadcastedDeployAccountTxn { + #[serde(rename = "0x1")] + V1(DeployAccountTxnV1), + /// Query-only broadcasted deploy account transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000001")] + QueryV1(DeployAccountTxnV1), +} + +#[derive(Debug, Clone, Serialize, Deserialize)] +#[serde(tag = "version")] +pub enum BroadcastedInvokeTxn { + #[serde(rename = "0x0")] + V0(InvokeTxnV0), + #[serde(rename = "0x1")] + V1(InvokeTxnV1), + /// Query-only broadcasted invoke transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000000")] + QueryV0(InvokeTxnV0), + /// Query-only broadcasted invoke transaction. + #[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000001")] + QueryV1(InvokeTxnV1), +} diff --git a/crates/starknet-types-rpc/src/v0_6_0/syncing_status.rs b/crates/starknet-types-rpc/src/v0_6_0/syncing_status.rs new file mode 100644 index 00000000..2a22ffcc --- /dev/null +++ b/crates/starknet-types-rpc/src/v0_6_0/syncing_status.rs @@ -0,0 +1,90 @@ +use serde::de::Visitor; +use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +use super::SyncStatus; + +/// The syncing status of a node. +#[derive(Clone, Debug)] +pub enum SyncingStatus { + /// The node is not syncing. + NotSyncing, + /// The node is syncing. + Syncing(SyncStatus), +} + +impl Serialize for SyncingStatus { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + match self { + SyncingStatus::NotSyncing => serializer.serialize_bool(false), + SyncingStatus::Syncing(status) => status.serialize(serializer), + } + } +} + +impl<'de> Deserialize<'de> for SyncingStatus { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + struct SyncingStatusVisitor; + + impl<'de> Visitor<'de> for SyncingStatusVisitor { + type Value = SyncingStatus; + + fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result { + writeln!(formatter, "a syncing status") + } + + fn visit_bool(self, v: bool) -> Result + where + E: serde::de::Error, + { + if v { + Err(serde::de::Error::custom("expected a syncing status")) + } else { + Ok(SyncingStatus::NotSyncing) + } + } + + fn visit_map(self, map: A) -> Result + where + A: serde::de::MapAccess<'de>, + { + let status = + SyncStatus::deserialize(serde::de::value::MapAccessDeserializer::new(map))?; + + Ok(SyncingStatus::Syncing(status)) + } + } + + deserializer.deserialize_any(SyncingStatusVisitor) + } +} + +#[cfg(test)] +mod tests { + use super::SyncingStatus; + + #[test] + fn syncing_status_from_false() { + let s = "false"; + let syncing_status: SyncingStatus = serde_json::from_str(s).unwrap(); + assert!(matches!(syncing_status, SyncingStatus::NotSyncing)); + } + + #[test] + fn syncing_status_to_false() { + let syncing_status = SyncingStatus::NotSyncing; + let s = serde_json::to_string(&syncing_status).unwrap(); + assert_eq!(s, "false"); + } + + #[test] + fn syncing_status_from_true() { + let s = "true"; + assert!(serde_json::from_str::(s).is_err()); + } +}