diff --git a/api/bin/chainflip-broker-api/src/main.rs b/api/bin/chainflip-broker-api/src/main.rs index 07f89a109d..5f20e9e0f5 100644 --- a/api/bin/chainflip-broker-api/src/main.rs +++ b/api/bin/chainflip-broker-api/src/main.rs @@ -1,4 +1,5 @@ use cf_utilities::{ + rpc::NumberOrHex, task_scope::{task_scope, Scope}, AnyhowRpcError, }; @@ -12,7 +13,6 @@ use clap::Parser; use futures::FutureExt; use jsonrpsee::{core::async_trait, proc_macros::rpc, server::ServerBuilder}; use serde::{Deserialize, Serialize}; -use sp_rpc::number::NumberOrHex; use std::path::PathBuf; use tracing::log; diff --git a/api/bin/chainflip-lp-api/src/main.rs b/api/bin/chainflip-lp-api/src/main.rs index bd3e859dd8..70a79eb1fe 100644 --- a/api/bin/chainflip-lp-api/src/main.rs +++ b/api/bin/chainflip-lp-api/src/main.rs @@ -1,4 +1,5 @@ use cf_utilities::{ + rpc::NumberOrHex, task_scope::{task_scope, Scope}, try_parse_number_or_hex, AnyhowRpcError, }; @@ -17,7 +18,6 @@ use futures::FutureExt; use jsonrpsee::{core::async_trait, proc_macros::rpc, server::ServerBuilder}; use pallet_cf_pools::{IncreaseOrDecrease, OrderId, RangeOrderSize}; use rpc_types::{OpenSwapChannels, OrderIdJson, RangeOrderSizeJson}; -use sp_rpc::number::NumberOrHex; use std::{collections::BTreeMap, ops::Range, path::PathBuf}; use tracing::log; @@ -25,10 +25,10 @@ use tracing::log; pub mod rpc_types { use super::*; use anyhow::anyhow; + use cf_utilities::rpc::NumberOrHex; use chainflip_api::queries::SwapChannelInfo; use pallet_cf_pools::AssetsMap; use serde::{Deserialize, Serialize}; - use sp_rpc::number::NumberOrHex; #[derive(Copy, Clone, Debug, Serialize, Deserialize)] pub struct OrderIdJson(NumberOrHex); diff --git a/state-chain/custom-rpc/src/lib.rs b/state-chain/custom-rpc/src/lib.rs index 77a2076837..0ed7c25159 100644 --- a/state-chain/custom-rpc/src/lib.rs +++ b/state-chain/custom-rpc/src/lib.rs @@ -730,13 +730,16 @@ where self.unwrap_or_best(at), from, to, - amount.try_into().and_then(|amount| { - if amount == 0 { - Err(anyhow::anyhow!("Swap input amount cannot be zero.")) - } else { - Ok(amount) - } - })?, + amount + .try_into() + .and_then(|amount| { + if amount == 0 { + Err("Swap input amount cannot be zero.") + } else { + Ok(amount) + } + }) + .map_err(|str| anyhow::anyhow!(str))?, ) .map_err(to_rpc_error) .and_then(|r| { diff --git a/utilities/src/with_std.rs b/utilities/src/with_std.rs index ef51b74e11..b9c65e0981 100644 --- a/utilities/src/with_std.rs +++ b/utilities/src/with_std.rs @@ -4,7 +4,7 @@ use futures::{stream, Stream}; use jsonrpsee::types::{error::CALL_EXECUTION_FAILED_CODE, ErrorObjectOwned}; #[doc(hidden)] pub use lazy_format::lazy_format as internal_lazy_format; -use sp_rpc::number::NumberOrHex; +use rpc::NumberOrHex; pub mod future_map; pub mod loop_select; @@ -54,7 +54,7 @@ pub fn clean_hex_address>>(address_str: &str) -> Result anyhow::Result { u128::try_from(amount).map_err(|_| { - anyhow!("Error parsing amount. Please use a valid number or hex string as input.") + anyhow!("Error parsing amount to u128. Please use a valid number or hex string as input.") }) } diff --git a/utilities/src/with_std/rpc.rs b/utilities/src/with_std/rpc.rs index 57ae093aee..a415b6e995 100644 --- a/utilities/src/with_std/rpc.rs +++ b/utilities/src/with_std/rpc.rs @@ -1,8 +1,7 @@ -use anyhow::anyhow; use serde::{Deserialize, Serialize}; use sp_core::U256; -#[derive(Debug, PartialEq, Eq, Deserialize)] +#[derive(Debug, PartialEq, Eq, Deserialize, Copy, Clone)] #[serde(untagged)] pub enum NumberOrHex { Number(u64), @@ -51,15 +50,24 @@ macro_rules! impl_safe_hex { impl_safe_hex!(u128, U256); -impl TryInto for NumberOrHex { - type Error = anyhow::Error; +impl TryFrom for u128 { + type Error = &'static str; - fn try_into(self) -> Result { - match self { - Self::Number(n) => Ok(n.into()), - Self::Hex(n) => n.try_into().map_err(|_| { - anyhow!("Error parsing amount. Please use a valid number or hex string as input.") - }), + fn try_from(value: NumberOrHex) -> Result { + match value { + NumberOrHex::Number(n) => Ok(n.into()), + NumberOrHex::Hex(n) => u128::try_from(n), + } + } +} + +impl TryFrom for u64 { + type Error = &'static str; + + fn try_from(value: NumberOrHex) -> Result { + match value { + NumberOrHex::Number(n) => Ok(n), + NumberOrHex::Hex(n) => u64::try_from(n), } } } @@ -88,4 +96,17 @@ mod test { assert_deser(r#"0"#, NumberOrHex::Number(0)); assert_deser(r#"1000000000000"#, NumberOrHex::Number(1000000000000)); } + + #[test] + fn test_conversions() { + assert_eq!(u128::try_from(NumberOrHex::Hex(u128::MAX.into())).unwrap(), u128::MAX); + assert!(u128::try_from(NumberOrHex::Hex(U256::MAX / 2)).is_err()); + + assert_eq!(u64::try_from(NumberOrHex::Hex(u64::MAX.into())).unwrap(), u64::MAX); + assert_eq!(u64::try_from(NumberOrHex::Number(u64::MAX)).unwrap(), u64::MAX); + assert!(u64::try_from(NumberOrHex::Hex((u128::MAX / 2).into())).is_err()); + + assert_eq!(u128::try_from(NumberOrHex::Number(0)).unwrap(), 0); + assert_eq!(u128::try_from(NumberOrHex::Hex(U256::zero())).unwrap(), 0); + } } diff --git a/utilities/src/with_std/serde_helpers/number_or_hex.rs b/utilities/src/with_std/serde_helpers/number_or_hex.rs index 3c20486d33..9253f132ef 100644 --- a/utilities/src/with_std/serde_helpers/number_or_hex.rs +++ b/utilities/src/with_std/serde_helpers/number_or_hex.rs @@ -12,8 +12,8 @@ //! bar: u128, //! } /// ``` +use crate::rpc::NumberOrHex; use serde::{de::Error, Deserialize, Deserializer, Serialize, Serializer}; -use sp_rpc::number::NumberOrHex; pub fn serialize(value: &T, serializer: S) -> Result where