Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(jsonrpc): extract config primitives #7063

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 5 additions & 3 deletions chain/jsonrpc-primitives/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ description = "This crate hosts structures for the NEAR JSON RPC Requests, Respo
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
uuid = { version = "~0.8", features = ["v4"] }
serde = { version = "1", features = ["derive"] }
serde_json = "1"
chrono = { version = "0.4.4", features = ["serde"] }
thiserror = "1.0"
uuid = { version = "~0.8", features = ["v4"] }
serde_json = "1"
num-rational = { version = "0.3", features = ["serde"] }

near-crypto = { path = "../../core/crypto" }
near-account-id = { path = "../../core/account-id" }
near-primitives = { path = "../../core/primitives" }
near-chain-configs = { path = "../../core/chain-configs" }
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🎉

near-rpc-error-macro = { path = "../../tools/rpctypegen/macro" }
near-client-primitives = { path = "../client-primitives" }

Expand Down
1 change: 1 addition & 0 deletions chain/jsonrpc-primitives/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod errors;
pub mod message;
mod serde_with;
pub mod types;
47 changes: 47 additions & 0 deletions chain/jsonrpc-primitives/src/serde_with.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
pub mod u128_dec_format {
use serde::de;
use serde::{Deserialize, Deserializer, Serializer};

pub fn serialize<S>(num: &u128, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_str(&num.to_string())
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<u128, D::Error>
where
D: Deserializer<'de>,
{
let s = String::deserialize(deserializer)?;
u128::from_str_radix(&s, 10).map_err(de::Error::custom)
}
}

pub mod u128_dec_format_compatible {
//! This in an extension to `u128_dec_format` that serves a compatibility layer role to
//! deserialize u128 from a "small" JSON number (u64).
//!
//! It is unfortunate that we cannot enable "arbitrary_precision" feature in
//! serde_json due to a bug: <https://github.com/serde-rs/json/issues/505>.
use serde::{de, Deserialize, Deserializer};

pub use super::u128_dec_format::serialize;

#[derive(Deserialize)]
#[serde(untagged)]
enum U128 {
Number(u64),
String(String),
}

pub fn deserialize<'de, D>(deserializer: D) -> Result<u128, D::Error>
where
D: Deserializer<'de>,
{
match U128::deserialize(deserializer)? {
U128::Number(value) => Ok(u128::from(value)),
U128::String(value) => u128::from_str_radix(&value, 10).map_err(de::Error::custom),
}
}
}
Loading