Skip to content

Commit

Permalink
add V3 transaction variants, generate with updated openrpc
Browse files Browse the repository at this point in the history
  • Loading branch information
maciejka committed Jul 3, 2024
1 parent 11e26aa commit dd4c1c1
Show file tree
Hide file tree
Showing 11 changed files with 283 additions and 220 deletions.
24 changes: 12 additions & 12 deletions crates/starknet-types-rpc/src/custom/block_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use serde::{Deserialize, Deserializer, Serialize};
use crate::{BlockHash, BlockNumber, BlockTag};

/// A hexadecimal number.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[derive(Debug, Clone, Eq, Hash, PartialEq)]
pub enum BlockId<F> {
/// The tag of the block.
Tag(BlockTag),
Expand Down Expand Up @@ -31,19 +31,19 @@ enum BlockIdHelper<F> {
Number(BlockNumberHelper),
}

impl<F: Copy + Serialize> serde::Serialize for BlockId<F> {
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)
}
}
Expand All @@ -66,7 +66,7 @@ impl<'de, F: Deserialize<'de>> serde::Deserialize<'de> for BlockId<F> {

#[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<Felt> = serde_json::from_str(s).unwrap();
Expand All @@ -75,7 +75,7 @@ fn block_id_from_hash() {

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

let s = "{\"block_number\":123}";
let block_id: BlockId<Felt> = serde_json::from_str(s).unwrap();
Expand All @@ -84,7 +84,7 @@ fn block_id_from_number() {

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

let s = "\"latest\"";
let block_id: BlockId<Felt> = serde_json::from_str(s).unwrap();
Expand All @@ -93,7 +93,7 @@ fn block_id_from_latest() {

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

let s = "\"pending\"";
let block_id: BlockId<Felt> = serde_json::from_str(s).unwrap();
Expand All @@ -103,7 +103,7 @@ fn block_id_from_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 @@ -113,7 +113,7 @@ fn block_id_to_hash() {
#[cfg(test)]
#[test]
fn block_id_to_number() {
use crate::Felt;
pub use starknet_types_core::felt::Felt;

let block_id = BlockId::<Felt>::Number(123);
let s = serde_json::to_string(&block_id).unwrap();
Expand All @@ -123,7 +123,7 @@ fn block_id_to_number() {
#[cfg(test)]
#[test]
fn block_id_to_latest() {
use crate::Felt;
pub use starknet_types_core::felt::Felt;

let block_id = BlockId::<Felt>::Tag(BlockTag::Latest);
let s = serde_json::to_string(&block_id).unwrap();
Expand All @@ -133,7 +133,7 @@ fn block_id_to_latest() {
#[cfg(test)]
#[test]
fn block_id_to_pending() {
use crate::Felt;
pub use starknet_types_core::felt::Felt;

let block_id = BlockId::<Felt>::Tag(BlockTag::Pending);
let s = serde_json::to_string(&block_id).unwrap();
Expand Down
25 changes: 19 additions & 6 deletions crates/starknet-types-rpc/src/custom/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,46 +3,59 @@

use serde::{Deserialize, Serialize};

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

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

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

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

/// Query-only broadcasted invoke transaction.
#[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000000")]
QueryV0(InvokeTxnV0<F>),
/// Query-only broadcasted invoke transaction.
#[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000001")]
QueryV1(InvokeTxnV1<F>),
/// Query-only broadcasted invoke transaction.
#[serde(rename = "0x0000000000000000000000000000000100000000000000000000000000000003")]
QueryV3(InvokeTxnV3<F>),
}
6 changes: 3 additions & 3 deletions crates/starknet-types-rpc/src/custom/syncing_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl<'de, F: Deserialize<'de>> Deserialize<'de> for SyncingStatus<F> {
#[cfg(test)]
#[test]
fn syncing_status_from_false() {
use crate::Felt;
pub use starknet_types_core::felt::Felt;

let s = "false";
let syncing_status: SyncingStatus<Felt> = serde_json::from_str(s).unwrap();
Expand All @@ -82,7 +82,7 @@ fn syncing_status_from_false() {
#[cfg(test)]
#[test]
fn syncing_status_to_false() {
use crate::Felt;
pub use starknet_types_core::felt::Felt;

let syncing_status = SyncingStatus::<Felt>::NotSyncing;
let s = serde_json::to_string(&syncing_status).unwrap();
Expand All @@ -92,7 +92,7 @@ fn syncing_status_to_false() {
#[cfg(test)]
#[test]
fn syncing_status_from_true() {
use crate::Felt;
pub use starknet_types_core::felt::Felt;
let s = "true";
assert!(serde_json::from_str::<SyncingStatus<Felt>>(s).is_err());
}
1 change: 1 addition & 0 deletions crates/starknet-types-rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#![cfg_attr(not(feature = "std"), no_std)]

extern crate alloc;
extern crate core;

mod custom;
mod custom_serde;
Expand Down
3 changes: 0 additions & 3 deletions crates/starknet-types-rpc/src/v0_7_1/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
//! v0.7.1 of the API.

pub use starknet_types_core::felt::Felt;

pub use crate::custom::{
BlockId, BroadcastedDeclareTxn, BroadcastedDeployAccountTxn, BroadcastedInvokeTxn,
SyncingStatus,
Expand Down
Loading

0 comments on commit dd4c1c1

Please sign in to comment.