Skip to content

Commit

Permalink
chore(rpc): generate latest spec (#60)
Browse files Browse the repository at this point in the history
Co-authored-by: Maciej Kamiński <[email protected]>
  • Loading branch information
0xLucqs and maciejka authored Jul 23, 2024
1 parent a6bcf4a commit f98f048
Show file tree
Hide file tree
Showing 17 changed files with 8,767 additions and 53 deletions.
2 changes: 1 addition & 1 deletion crates/starknet-types-rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "starknet-types-rpc"
version = "0.6.0"
version = "0.7.1"
edition = "2021"
license = "MIT"
homepage = "https://github.com/starknet-io/types-rs"
Expand Down
14 changes: 14 additions & 0 deletions crates/starknet-types-rpc/Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
SRC_DIR ?= src/v0_7_1

define generate_spec
echo "Generating types for $(1)";
openrpc-gen --config $(1).toml --document $(1).json --output $(1).rs ;
endef

BASE_NAMES := starknet_api_openrpc starknet_trace_api_openrpc starknet_write_api
# Default target that depends on all generated .rs files
all:
$(foreach file,$(BASE_NAMES),$(call generate_spec,$(SRC_DIR)/$(file)))

# Phony target to avoid conflicts with actual files
.PHONY: all
4 changes: 3 additions & 1 deletion crates/starknet-types-rpc/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ After having built `openrpc-gen`, you can use the following command to generate
Rust files:

```bash
openrpc-gen --config configs/v0.5.0.toml --document configs/spec_v0.5.0.json --output src/generated/v0.5.0.rs
make all
```

NOTE: Currently the `starknet_trace_api_openrpc` file requires a modification for `starknet_simulateTransactions` (nested `schema` in the result, see previous version for infos)

*Note that this first step is normally already done for you upon cloning the repository.*

### Building the generated files
Expand Down
54 changes: 34 additions & 20 deletions crates/starknet-types-rpc/src/custom/block_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@ use serde::{Deserialize, Deserializer, Serialize};
use crate::{BlockHash, BlockNumber, BlockTag};

/// A hexadecimal number.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum BlockId {
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
pub enum BlockId<F> {
/// The tag of the block.
Tag(BlockTag),
/// The hash of the block.
Hash(BlockHash),
Hash(BlockHash<F>),
/// The height of the block.
Number(BlockNumber),
}

#[derive(Serialize, Deserialize)]
struct BlockHashHelper {
block_hash: BlockHash,
struct BlockHashHelper<F> {
block_hash: BlockHash<F>,
}

#[derive(Serialize, Deserialize)]
Expand All @@ -25,32 +25,34 @@ struct BlockNumberHelper {

#[derive(Deserialize)]
#[serde(untagged)]
enum BlockIdHelper {
enum BlockIdHelper<F> {
Tag(BlockTag),
Hash(BlockHashHelper),
Hash(BlockHashHelper<F>),
Number(BlockNumberHelper),
}

impl serde::Serialize for BlockId {
impl<F: Serialize> serde::Serialize for BlockId<F> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
match *self {
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 };
let helper = BlockNumberHelper {
block_number: *block_number,
};
helper.serialize(serializer)
}
}
}
}

impl<'de> serde::Deserialize<'de> for BlockId {
impl<'de, F: Deserialize<'de>> serde::Deserialize<'de> for BlockId<F> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
Expand All @@ -66,38 +68,44 @@ impl<'de> serde::Deserialize<'de> for BlockId {

#[test]
fn block_id_from_hash() {
use crate::Felt;
pub use starknet_types_core::felt::Felt;

let s = "{\"block_hash\":\"0x123\"}";
let block_id: BlockId = serde_json::from_str(s).unwrap();
let block_id: BlockId<Felt> = 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();
let block_id: BlockId<Felt> = 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();
let block_id: BlockId<Felt> = 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();
let block_id: BlockId<Felt> = serde_json::from_str(s).unwrap();
assert_eq!(block_id, BlockId::Tag(BlockTag::Pending));
}

#[cfg(test)]
#[test]
fn block_id_to_hash() {
use crate::Felt;
pub use starknet_types_core::felt::Felt;

let block_id = BlockId::Hash(Felt::from_hex("0x123").unwrap());
let s = serde_json::to_string(&block_id).unwrap();
Expand All @@ -107,23 +115,29 @@ fn block_id_to_hash() {
#[cfg(test)]
#[test]
fn block_id_to_number() {
let block_id = BlockId::Number(123);
pub use starknet_types_core::felt::Felt;

let block_id = BlockId::<Felt>::Number(123);
let s = serde_json::to_string(&block_id).unwrap();
assert_eq!(s, "{\"block_number\":123}");
}

#[cfg(test)]
#[test]
fn block_id_to_latest() {
let block_id = BlockId::Tag(BlockTag::Latest);
pub use starknet_types_core::felt::Felt;

let block_id = BlockId::<Felt>::Tag(BlockTag::Latest);
let s = serde_json::to_string(&block_id).unwrap();
assert_eq!(s, "\"latest\"");
}

#[cfg(test)]
#[test]
fn block_id_to_pending() {
let block_id = BlockId::Tag(BlockTag::Pending);
pub use starknet_types_core::felt::Felt;

let block_id = BlockId::<Felt>::Tag(BlockTag::Pending);
let s = serde_json::to_string(&block_id).unwrap();
assert_eq!(s, "\"pending\"");
}
49 changes: 32 additions & 17 deletions crates/starknet-types-rpc/src/custom/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,45 +4,60 @@
use serde::{Deserialize, Serialize};

use crate::{
BroadcastedDeclareTxnV1, BroadcastedDeclareTxnV2, DeployAccountTxnV1, InvokeTxnV0, InvokeTxnV1,
BroadcastedDeclareTxnV1, BroadcastedDeclareTxnV2, BroadcastedDeclareTxnV3, DeployAccountTxnV1,
DeployAccountTxnV3, InvokeTxnV0, InvokeTxnV1, InvokeTxnV3,
};

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[serde(tag = "version")]
pub enum BroadcastedDeclareTxn {
pub enum BroadcastedDeclareTxn<F: Default> {
#[serde(rename = "0x1")]
V1(BroadcastedDeclareTxnV1),
V1(BroadcastedDeclareTxnV1<F>),
#[serde(rename = "0x2")]
V2(BroadcastedDeclareTxnV2),
V2(BroadcastedDeclareTxnV2<F>),
#[serde(rename = "0x3")]
V3(BroadcastedDeclareTxnV3<F>),
/// Query-only broadcasted declare transaction.
#[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000001")]
QueryV1(BroadcastedDeclareTxnV1),
QueryV1(BroadcastedDeclareTxnV1<F>),
/// Query-only broadcasted declare transaction.
#[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000002")]
QueryV2(BroadcastedDeclareTxnV2),
QueryV2(BroadcastedDeclareTxnV2<F>),
/// Query-only broadcasted declare transaction.
#[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000003")]
QueryV3(BroadcastedDeclareTxnV3<F>),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[serde(tag = "version")]
pub enum BroadcastedDeployAccountTxn {
pub enum BroadcastedDeployAccountTxn<F> {
#[serde(rename = "0x1")]
V1(DeployAccountTxnV1),
V1(DeployAccountTxnV1<F>),
/// Query-only broadcasted deploy account transaction.
#[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000001")]
QueryV1(DeployAccountTxnV1),
QueryV1(DeployAccountTxnV1<F>),
/// Query-only broadcasted deploy account transaction.
#[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000003")]
QueryV3(DeployAccountTxnV3<F>),
}

#[derive(Debug, Clone, Serialize, Deserialize)]
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[serde(tag = "version")]
pub enum BroadcastedInvokeTxn {
pub enum BroadcastedInvokeTxn<F> {
#[serde(rename = "0x0")]
V0(InvokeTxnV0),
V0(InvokeTxnV0<F>),
#[serde(rename = "0x1")]
V1(InvokeTxnV1),
V1(InvokeTxnV1<F>),
#[serde(rename = "0x3")]
V3(InvokeTxnV3<F>),

/// Query-only broadcasted invoke transaction.
#[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000000")]
QueryV0(InvokeTxnV0),
QueryV0(InvokeTxnV0<F>),
/// Query-only broadcasted invoke transaction.
#[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000001")]
QueryV1(InvokeTxnV1),
QueryV1(InvokeTxnV1<F>),
/// Query-only broadcasted invoke transaction.
#[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000003")]
QueryV3(InvokeTxnV3<F>),
}
32 changes: 21 additions & 11 deletions crates/starknet-types-rpc/src/custom/syncing_status.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
use core::marker::PhantomData;
use serde::de::Visitor;
use serde::{Deserialize, Deserializer, Serialize, Serializer};

use crate::SyncStatus;

/// The syncing status of a node.
#[derive(Clone, Debug)]
pub enum SyncingStatus {
pub enum SyncingStatus<F> {
/// The node is not syncing.
NotSyncing,
/// The node is syncing.
Syncing(SyncStatus),
Syncing(SyncStatus<F>),
}

impl Serialize for SyncingStatus {
impl<F: Serialize> Serialize for SyncingStatus<F> {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand All @@ -24,15 +25,17 @@ impl Serialize for SyncingStatus {
}
}

impl<'de> Deserialize<'de> for SyncingStatus {
impl<'de, F: Deserialize<'de>> Deserialize<'de> for SyncingStatus<F> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: Deserializer<'de>,
{
struct SyncingStatusVisitor;
struct SyncingStatusVisitor<F> {
marker: PhantomData<F>,
}

impl<'de> Visitor<'de> for SyncingStatusVisitor {
type Value = SyncingStatus;
impl<'de, F: Deserialize<'de>> Visitor<'de> for SyncingStatusVisitor<F> {
type Value = SyncingStatus<F>;

fn expecting(&self, formatter: &mut core::fmt::Formatter) -> core::fmt::Result {
writeln!(formatter, "a syncing status")
Expand Down Expand Up @@ -60,29 +63,36 @@ impl<'de> Deserialize<'de> for SyncingStatus {
}
}

deserializer.deserialize_any(SyncingStatusVisitor)
deserializer.deserialize_any(SyncingStatusVisitor::<F> {
marker: PhantomData,
})
}
}

#[cfg(test)]
#[test]
fn syncing_status_from_false() {
pub use starknet_types_core::felt::Felt;

let s = "false";
let syncing_status: SyncingStatus = serde_json::from_str(s).unwrap();
let syncing_status: SyncingStatus<Felt> = serde_json::from_str(s).unwrap();
assert!(matches!(syncing_status, SyncingStatus::NotSyncing));
}

#[cfg(test)]
#[test]
fn syncing_status_to_false() {
let syncing_status = SyncingStatus::NotSyncing;
pub use starknet_types_core::felt::Felt;

let syncing_status = SyncingStatus::<Felt>::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::<SyncingStatus>(s).is_err());
assert!(serde_json::from_str::<SyncingStatus<Felt>>(s).is_err());
}
6 changes: 3 additions & 3 deletions crates/starknet-types-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;
extern crate core;

mod custom;
mod custom_serde;

//
// Generated files.
//
pub mod v0_5_0;
pub mod v0_7_1;

pub use self::v0_5_0::*;
pub use self::v0_7_1::*;
13 changes: 13 additions & 0 deletions crates/starknet-types-rpc/src/v0_7_1/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
//! v0.7.1 of the API.
pub use crate::custom::{
BlockId, BroadcastedDeclareTxn, BroadcastedDeployAccountTxn, BroadcastedInvokeTxn,
SyncingStatus,
};

mod starknet_api_openrpc;
mod starknet_trace_api_openrpc;
mod starknet_write_api;

pub use self::starknet_api_openrpc::*;
pub use self::starknet_trace_api_openrpc::*;
pub use self::starknet_write_api::*;
Loading

0 comments on commit f98f048

Please sign in to comment.