Skip to content

Commit

Permalink
chore: move BalanceWrapper to primitives
Browse files Browse the repository at this point in the history
Signed-off-by: Gregory Hill <gregorydhill@outlook.com>
  • Loading branch information
gregdhill committed Jul 21, 2023
1 parent 874dca1 commit 313132a
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 33 deletions.
3 changes: 1 addition & 2 deletions Cargo.lock

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

7 changes: 4 additions & 3 deletions crates/oracle/rpc/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@ authors = ["Interlay Ltd"]
edition = "2021"

[dependencies]
serde = { version = "1.0.130", default-features = false, optional = true, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive", "max-encoded-len"] }
frame-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }
sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false }
scale-info = { version = "2.2.0", default-features = false, features = ["derive"] }

primitives = { package = "interbtc-primitives", path = "../../../../primitives", default-features = false }

[features]
default = ["std"]
std = [
"serde",
"codec/std",
"frame-support/std",
"sp-api/std",
"sp-std/std",

"primitives/std",
]
30 changes: 2 additions & 28 deletions crates/oracle/rpc/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,10 @@

#![cfg_attr(not(feature = "std"), no_std)]

use codec::{Codec, Decode, Encode};
use codec::Codec;
use frame_support::dispatch::DispatchError;
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};

#[derive(Eq, PartialEq, Encode, Decode, Default, TypeInfo)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
/// a wrapper around a balance, used in RPC to workaround a bug where using u128
/// in runtime-apis fails. See <https://github.com/paritytech/substrate/issues/4641>
pub struct BalanceWrapper<T> {
#[cfg_attr(feature = "std", serde(bound(serialize = "T: std::fmt::Display")))]
#[cfg_attr(feature = "std", serde(serialize_with = "serialize_as_string"))]
#[cfg_attr(feature = "std", serde(bound(deserialize = "T: std::str::FromStr")))]
#[cfg_attr(feature = "std", serde(deserialize_with = "deserialize_from_string"))]
pub amount: T,
}

#[cfg(feature = "std")]
fn serialize_as_string<S: Serializer, T: std::fmt::Display>(t: &T, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&t.to_string())
}

#[cfg(feature = "std")]
fn deserialize_from_string<'de, D: Deserializer<'de>, T: std::str::FromStr>(deserializer: D) -> Result<T, D::Error> {
let s = String::deserialize(deserializer)?;
s.parse::<T>()
.map_err(|_| serde::de::Error::custom("Parse from string failed"))
}
pub use primitives::BalanceWrapper;

sp_api::decl_runtime_apis! {
pub trait OracleApi<Balance, CurrencyId> where
Expand Down
13 changes: 13 additions & 0 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,19 @@ pub mod issue {
}
}

#[derive(Eq, PartialEq, Encode, Decode, Default, TypeInfo)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
/// a wrapper around a balance, used in RPC to workaround a bug where using u128
/// in runtime-apis fails. See <https://github.com/paritytech/substrate/issues/4641>
pub struct BalanceWrapper<T> {
#[cfg_attr(feature = "std", serde(bound(serialize = "T: std::fmt::Display")))]
#[cfg_attr(feature = "std", serde(serialize_with = "serialize_as_string"))]
#[cfg_attr(feature = "std", serde(bound(deserialize = "T: std::str::FromStr")))]
#[cfg_attr(feature = "std", serde(deserialize_with = "deserialize_from_string"))]
pub amount: T,
}

#[cfg(feature = "std")]
fn serialize_as_string<S: Serializer, T: std::fmt::Display>(t: &T, serializer: S) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&t.to_string())
Expand Down

0 comments on commit 313132a

Please sign in to comment.