From 874dca1fcaa6e44b86f2b25d2869987f69bb899f Mon Sep 17 00:00:00 2001 From: Gregory Hill Date: Fri, 21 Jul 2023 22:04:12 +0000 Subject: [PATCH 1/2] chore: feature gate sp-runtime in primitives Signed-off-by: Gregory Hill --- Cargo.lock | 4 +- crates/annuity/Cargo.toml | 1 - crates/currency/src/mock.rs | 2 +- crates/escrow/Cargo.toml | 2 - crates/farming/Cargo.toml | 2 - crates/farming/src/mock.rs | 2 +- crates/fee/Cargo.toml | 3 - crates/multi-transaction-payment/Cargo.toml | 1 - crates/oracle/src/mock.rs | 2 +- crates/reward/Cargo.toml | 4 +- crates/staking/Cargo.toml | 2 - crates/supply/Cargo.toml | 2 - crates/traits/Cargo.toml | 2 +- parachain/runtime/interlay/src/lib.rs | 8 +- parachain/runtime/kintsugi/src/lib.rs | 8 +- parachain/src/service.rs | 1 + primitives/Cargo.toml | 18 +- primitives/src/lib.rs | 173 +++++++++++--------- 18 files changed, 119 insertions(+), 118 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 388a4bdfc8..b3781d0854 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4386,14 +4386,12 @@ dependencies = [ "bitcoin", "bstringify", "parity-scale-codec", + "primitive-types", "scale-decode", "scale-encode", "scale-info", "serde", - "sp-core", "sp-runtime", - "sp-std", - "xcm", ] [[package]] diff --git a/crates/annuity/Cargo.toml b/crates/annuity/Cargo.toml index 6f68ea1128..3e5b48dbe1 100644 --- a/crates/annuity/Cargo.toml +++ b/crates/annuity/Cargo.toml @@ -31,7 +31,6 @@ frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false } reward = { path = "../reward", default-features = false } -primitives = { package = "interbtc-primitives", path = "../../primitives", default-features = false } [features] default = ["std"] diff --git a/crates/currency/src/mock.rs b/crates/currency/src/mock.rs index 4a4b2178d0..63073c6277 100644 --- a/crates/currency/src/mock.rs +++ b/crates/currency/src/mock.rs @@ -88,7 +88,7 @@ parameter_type_with_key! { impl orml_tokens::Config for Test { type RuntimeEvent = RuntimeEvent; type Balance = Balance; - type Amount = primitives::Amount; + type Amount = primitives::SignedBalance; type CurrencyId = CurrencyId; type WeightInfo = (); type ExistentialDeposits = ExistentialDeposits; diff --git a/crates/escrow/Cargo.toml b/crates/escrow/Cargo.toml index 03b7cb8747..33ad23596b 100644 --- a/crates/escrow/Cargo.toml +++ b/crates/escrow/Cargo.toml @@ -31,8 +31,6 @@ rand = "0.8.3" frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false } pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false } -primitives = { package = "interbtc-primitives", path = "../../primitives", default-features = false } - [features] default = ["std"] std = [ diff --git a/crates/farming/Cargo.toml b/crates/farming/Cargo.toml index 3e52498b35..26f9a00303 100644 --- a/crates/farming/Cargo.toml +++ b/crates/farming/Cargo.toml @@ -35,8 +35,6 @@ rand = "0.8.3" frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false } pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false } -primitives = { package = "interbtc-primitives", path = "../../primitives", default-features = false } - [features] default = ["std"] std = [ diff --git a/crates/farming/src/mock.rs b/crates/farming/src/mock.rs index 5a6cd3598c..add5686f41 100644 --- a/crates/farming/src/mock.rs +++ b/crates/farming/src/mock.rs @@ -83,7 +83,7 @@ parameter_type_with_key! { impl orml_tokens::Config for Test { type RuntimeEvent = RuntimeEvent; type Balance = Balance; - type Amount = primitives::Amount; + type Amount = primitives::SignedBalance; type CurrencyId = CurrencyId; type WeightInfo = (); type ExistentialDeposits = ExistentialDeposits; diff --git a/crates/fee/Cargo.toml b/crates/fee/Cargo.toml index d554554aaa..b1c91e3c7e 100644 --- a/crates/fee/Cargo.toml +++ b/crates/fee/Cargo.toml @@ -44,9 +44,6 @@ currency = { path = "../currency", features = ["testing-utils"] } orml-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "dc39cfddefb10ef0de23655e2c3dcdab66a19404", default-features = false } orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "dc39cfddefb10ef0de23655e2c3dcdab66a19404", default-features = false } -# Parachain dependencies -primitives = { package = "interbtc-primitives", path = "../../primitives"} - [features] default = ["std"] std = [ diff --git a/crates/multi-transaction-payment/Cargo.toml b/crates/multi-transaction-payment/Cargo.toml index b6363b2077..10d7b91b0e 100644 --- a/crates/multi-transaction-payment/Cargo.toml +++ b/crates/multi-transaction-payment/Cargo.toml @@ -35,7 +35,6 @@ sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot- sp-io = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false } sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false } sp-arithmetic = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false } -primitives = { package = "interbtc-primitives", path = "../../primitives", default-features = false } [features] default = ["std"] diff --git a/crates/oracle/src/mock.rs b/crates/oracle/src/mock.rs index 14928ba6f2..16a74ebbec 100644 --- a/crates/oracle/src/mock.rs +++ b/crates/oracle/src/mock.rs @@ -100,7 +100,7 @@ parameter_type_with_key! { impl orml_tokens::Config for Test { type RuntimeEvent = RuntimeEvent; type Balance = Balance; - type Amount = primitives::Amount; + type Amount = primitives::SignedBalance; type CurrencyId = CurrencyId; type WeightInfo = (); type ExistentialDeposits = ExistentialDeposits; diff --git a/crates/reward/Cargo.toml b/crates/reward/Cargo.toml index 3660cd2e3a..61cd3cb286 100644 --- a/crates/reward/Cargo.toml +++ b/crates/reward/Cargo.toml @@ -12,7 +12,7 @@ codec = { package = "parity-scale-codec", version = "3.1.5", default-features = scale-info = { version = "2.2.0", default-features = false, features = ["derive"] } # Parachain dependencies -primitives = { package = "interbtc-primitives", path = "../../primitives", default-features = false } +primitives = { package = "interbtc-primitives", path = "../../primitives", default-features = false, features = ["substrate-compat"] } # Substrate dependencies sp-arithmetic = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false } @@ -31,8 +31,6 @@ rand = "0.8.3" frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false } pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false } -primitives = { package = "interbtc-primitives", path = "../../primitives", default-features = false } - [features] default = ["std"] std = [ diff --git a/crates/staking/Cargo.toml b/crates/staking/Cargo.toml index cd6ae3b2de..bb5584d8b2 100644 --- a/crates/staking/Cargo.toml +++ b/crates/staking/Cargo.toml @@ -35,8 +35,6 @@ rand = "0.8.3" frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false } pallet-timestamp = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false } -primitives = { package = "interbtc-primitives", path = "../../primitives", default-features = false } - [features] default = ["std"] std = [ diff --git a/crates/supply/Cargo.toml b/crates/supply/Cargo.toml index 09f4a7afa5..dd39af06c3 100644 --- a/crates/supply/Cargo.toml +++ b/crates/supply/Cargo.toml @@ -30,8 +30,6 @@ rand = "0.8.3" frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false } pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false } -primitives = { package = "interbtc-primitives", path = "../../primitives", default-features = false } - [features] default = ["std"] std = [ diff --git a/crates/traits/Cargo.toml b/crates/traits/Cargo.toml index 9de6eff9a0..134bacfca4 100644 --- a/crates/traits/Cargo.toml +++ b/crates/traits/Cargo.toml @@ -30,7 +30,7 @@ frame-support = { git = "https://github.com/paritytech/substrate.git", branch = frame-system = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.31", default-features = false } # Parachain dependencies -primitives = { package = "interbtc-primitives", path = "../../primitives", default-features = false } +primitives = { package = "interbtc-primitives", path = "../../primitives", default-features = false, features = ["substrate-compat"] } [dev-dependencies] sp-core = { git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.31" } diff --git a/parachain/runtime/interlay/src/lib.rs b/parachain/runtime/interlay/src/lib.rs index 4b69067f88..14179ef55b 100644 --- a/parachain/runtime/interlay/src/lib.rs +++ b/parachain/runtime/interlay/src/lib.rs @@ -212,7 +212,7 @@ impl frame_system::Config for Runtime { /// The hashing algorithm used. type Hashing = BlakeTwo256; /// The header type. - type Header = generic::Header; + type Header = Header; /// The ubiquitous event type. type RuntimeEvent = RuntimeEvent; /// The ubiquitous origin type. @@ -692,7 +692,7 @@ where impl orml_tokens::Config for Runtime { type RuntimeEvent = RuntimeEvent; type Balance = Balance; - type Amount = primitives::Amount; + type Amount = primitives::SignedBalance; type CurrencyId = CurrencyId; type WeightInfo = weights::orml_tokens::WeightInfo; type ExistentialDeposits = ExistentialDeposits; @@ -1269,8 +1269,6 @@ construct_runtime! { } } -/// The address format for describing accounts. -pub type Address = AccountId; /// Block header type as expected by this runtime. pub type Header = generic::Header; /// Block type as expected by this runtime. @@ -1290,7 +1288,7 @@ pub type SignedExtra = ( pallet_transaction_payment::ChargeTransactionPayment, ); /// Unchecked extrinsic type as expected by this runtime. -pub type UncheckedExtrinsic = generic::UncheckedExtrinsic; +pub type UncheckedExtrinsic = generic::UncheckedExtrinsic; /// Extrinsic type that has already been checked. pub type CheckedExtrinsic = generic::CheckedExtrinsic; /// Executive: handles dispatch to the various modules. diff --git a/parachain/runtime/kintsugi/src/lib.rs b/parachain/runtime/kintsugi/src/lib.rs index b12da19fb9..2490bcadfb 100644 --- a/parachain/runtime/kintsugi/src/lib.rs +++ b/parachain/runtime/kintsugi/src/lib.rs @@ -210,7 +210,7 @@ impl frame_system::Config for Runtime { /// The hashing algorithm used. type Hashing = BlakeTwo256; /// The header type. - type Header = generic::Header; + type Header = Header; /// The ubiquitous event type. type RuntimeEvent = RuntimeEvent; /// The ubiquitous origin type. @@ -691,7 +691,7 @@ where impl orml_tokens::Config for Runtime { type RuntimeEvent = RuntimeEvent; type Balance = Balance; - type Amount = primitives::Amount; + type Amount = primitives::SignedBalance; type CurrencyId = CurrencyId; type WeightInfo = weights::orml_tokens::WeightInfo; type ExistentialDeposits = ExistentialDeposits; @@ -1269,8 +1269,6 @@ construct_runtime! { } } -/// The address format for describing accounts. -pub type Address = AccountId; /// Block header type as expected by this runtime. pub type Header = generic::Header; /// Block type as expected by this runtime. @@ -1290,7 +1288,7 @@ pub type SignedExtra = ( pallet_transaction_payment::ChargeTransactionPayment, ); /// Unchecked extrinsic type as expected by this runtime. -pub type UncheckedExtrinsic = generic::UncheckedExtrinsic; +pub type UncheckedExtrinsic = generic::UncheckedExtrinsic; /// Extrinsic type that has already been checked. pub type CheckedExtrinsic = generic::CheckedExtrinsic; /// Executive: handles dispatch to the various modules. diff --git a/parachain/src/service.rs b/parachain/src/service.rs index 37426d8e21..92f0a4de7a 100644 --- a/parachain/src/service.rs +++ b/parachain/src/service.rs @@ -25,6 +25,7 @@ use sp_consensus_aura::{ sr25519::{AuthorityId as AuraId, AuthorityPair as AuraPair}, SlotDuration, }; +use sp_core::H256; use sp_keystore::KeystorePtr; use sp_runtime::traits::BlakeTwo256; use std::{sync::Arc, time::Duration}; diff --git a/primitives/Cargo.toml b/primitives/Cargo.toml index f78c2d3719..16c7c432a8 100644 --- a/primitives/Cargo.toml +++ b/primitives/Cargo.toml @@ -8,30 +8,30 @@ version = "1.2.0" bstringify = "0.1.2" serde = { version = "1.0.130", optional = true } codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false } +primitive-types = { version = "0.12.1", default-features = false, features = ["codec", "scale-info"] } scale-info = { version = "2.2.0", default-features = false, features = ["derive"] } - -sp-core = { 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 } -sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false } -xcm = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.31", default-features = false } scale-decode = { version = "0.7.0", default-features = false, features = ["derive"], optional = true } scale-encode = { version = "0.3.0", default-features = false, features = ["derive"], optional = true } +# Substrate dependencies +sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31", default-features = false, optional = true } + # Parachain dependencies bitcoin = { path = "../crates/bitcoin", default-features = false } [features] -default = ["std"] +default = ["std", "substrate-compat"] std = [ "serde", "codec/std", + "primitive-types/std", + "primitive-types/serde", "scale-decode", "scale-encode", - "sp-core/std", - "sp-std/std", - "sp-runtime/std", + "sp-runtime?/std", "bitcoin/std", ] runtime-benchmarks = [] +substrate-compat = ["sp-runtime"] \ No newline at end of file diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index a9ff4a0bff..2da88406d4 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -1,8 +1,17 @@ #![cfg_attr(not(feature = "std"), no_std)] +#[cfg(not(feature = "std"))] +extern crate alloc; + +#[cfg(not(feature = "std"))] +use alloc::{vec, vec::Vec}; use bitcoin::{Address as BtcAddress, PublicKey as BtcPublicKey}; use bstringify::bstringify; use codec::{Decode, Encode, MaxEncodedLen}; +use core::convert::TryFrom; +#[cfg(any(feature = "runtime-benchmarks", feature = "substrate-compat"))] +use core::convert::TryInto; +use primitive_types::H256; #[cfg(feature = "std")] use scale_decode::DecodeAsType; #[cfg(feature = "std")] @@ -10,17 +19,6 @@ use scale_encode::EncodeAsType; use scale_info::TypeInfo; #[cfg(feature = "std")] use serde::{Deserialize, Deserializer, Serialize, Serializer}; -pub use sp_core::H256; -pub use sp_runtime::OpaqueExtrinsic as UncheckedExtrinsic; -use sp_runtime::{ - generic, - traits::{BlakeTwo256, IdentifyAccount, Verify}, - FixedI128, FixedPointNumber, FixedU128, MultiSignature, Permill, RuntimeDebug, -}; -use sp_std::{ - convert::{TryFrom, TryInto}, - prelude::*, -}; pub use bitcoin::types::H256Le; @@ -28,33 +26,54 @@ pub const BITCOIN_TESTNET: &str = "bitcoin-testnet"; pub const BITCOIN_MAINNET: &str = "bitcoin-mainnet"; pub const BITCOIN_REGTEST: &str = "bitcoin-regtest"; -pub trait BalanceToFixedPoint { - fn to_fixed(self) -> Option; -} +#[cfg(feature = "substrate-compat")] +pub use arithmetic::*; + +#[cfg(feature = "substrate-compat")] +mod arithmetic { + use super::*; + use sp_runtime::{FixedI128, FixedPointNumber, FixedU128}; + + /// The signed fixed point type. + pub type SignedFixedPoint = FixedI128; -impl BalanceToFixedPoint for Balance { - fn to_fixed(self) -> Option { - SignedFixedPoint::checked_from_integer( - TryInto::<::Inner>::try_into(self).ok()?, - ) + /// The `Inner` type of the `SignedFixedPoint`. + pub type SignedInner = ::Inner; + + /// The unsigned fixed point type. + pub type UnsignedFixedPoint = FixedU128; + + /// The `Inner` type of the `UnsignedFixedPoint`. + pub type UnsignedInner = ::Inner; + + pub trait BalanceToFixedPoint { + fn to_fixed(self) -> Option; } -} -pub trait TruncateFixedPointToInt: FixedPointNumber { - /// take a fixed point number and turns it into the truncated inner representation, - /// e.g. FixedU128(1.23) -> 1u128 - fn truncate_to_inner(&self) -> Option<::Inner>; -} + impl BalanceToFixedPoint for Balance { + fn to_fixed(self) -> Option { + SignedFixedPoint::checked_from_integer( + TryInto::<::Inner>::try_into(self).ok()?, + ) + } + } -impl TruncateFixedPointToInt for SignedFixedPoint { - fn truncate_to_inner(&self) -> Option { - self.into_inner().checked_div(SignedFixedPoint::accuracy()) + pub trait TruncateFixedPointToInt: FixedPointNumber { + /// take a fixed point number and turns it into the truncated inner representation, + /// e.g. FixedU128(1.23) -> 1u128 + fn truncate_to_inner(&self) -> Option<::Inner>; } -} -impl TruncateFixedPointToInt for UnsignedFixedPoint { - fn truncate_to_inner(&self) -> Option<::Inner> { - self.into_inner().checked_div(UnsignedFixedPoint::accuracy()) + impl TruncateFixedPointToInt for SignedFixedPoint { + fn truncate_to_inner(&self) -> Option { + self.into_inner().checked_div(SignedFixedPoint::accuracy()) + } + } + + impl TruncateFixedPointToInt for UnsignedFixedPoint { + fn truncate_to_inner(&self) -> Option<::Inner> { + self.into_inner().checked_div(UnsignedFixedPoint::accuracy()) + } } } @@ -321,19 +340,34 @@ pub mod oracle { } } -/// An index to a block. -pub type BlockNumber = u32; +#[cfg(feature = "substrate-compat")] +pub use runtime::*; + +#[cfg(feature = "substrate-compat")] +mod runtime { + use super::*; + use sp_runtime::{ + generic, + traits::{BlakeTwo256, IdentifyAccount, Verify}, + MultiSignature, OpaqueExtrinsic, + }; -/// Alias to 512-bit hash when used in the context of a transaction signature on the chain. -pub type Signature = MultiSignature; + /// Alias to 512-bit hash when used in the context of a transaction signature on the chain. + pub type Signature = MultiSignature; -/// Some way of identifying an account on the chain. We intentionally make it equivalent -/// to the public key of our transaction signing scheme. -pub type AccountId = <::Signer as IdentifyAccount>::AccountId; + /// Some way of identifying an account on the chain. We intentionally make it equivalent + /// to the public key of our transaction signing scheme. + pub type AccountId = <::Signer as IdentifyAccount>::AccountId; -/// The type for looking up accounts. We don't expect more than 4 billion of them, but you -/// never know... -pub type AccountIndex = u32; + /// Opaque block header type. + pub type Header = generic::Header; + + /// Opaque block type. + pub type Block = generic::Block; +} + +/// An index to a block. +pub type BlockNumber = u32; /// Index of a transaction in the chain. 32-bit should be plenty. pub type Nonce = u32; @@ -342,48 +376,35 @@ pub type Nonce = u32; pub type Balance = u128; /// Signed version of Balance -pub type Amount = i128; +pub type SignedBalance = i128; /// Index of a transaction in the chain. pub type Index = u32; /// A hash of some data used by the chain. -pub type Hash = sp_core::H256; +pub type Hash = H256; /// An instant or duration in time. pub type Moment = u64; -/// Opaque block header type. -pub type Header = generic::Header; - -/// Opaque block type. -pub type Block = generic::Block; - -/// Opaque block identifier type. -pub type BlockId = generic::BlockId; - -/// The signed fixed point type. -pub type SignedFixedPoint = FixedI128; - -/// The `Inner` type of the `SignedFixedPoint`. -pub type SignedInner = i128; - -/// The unsigned fixed point type. -pub type UnsignedFixedPoint = FixedU128; - -/// The `Inner` type of the `UnsignedFixedPoint`. -pub type UnsignedInner = u128; - /// Loans pallet types +#[cfg(feature = "substrate-compat")] +pub use loans::*; -pub type Price = FixedU128; -pub type Timestamp = Moment; -pub type PriceDetail = (Price, Timestamp); -pub type Rate = FixedU128; -pub type Ratio = Permill; -pub type Shortfall = FixedU128; -pub type Liquidity = FixedU128; -pub const SECONDS_PER_YEAR: Timestamp = 365 * 24 * 60 * 60; +#[cfg(feature = "substrate-compat")] +mod loans { + use super::*; + use sp_runtime::{FixedU128, Permill}; + + pub type Price = FixedU128; + pub type Timestamp = Moment; + pub type PriceDetail = (Price, Timestamp); + pub type Rate = FixedU128; + pub type Ratio = Permill; + pub type Shortfall = FixedU128; + pub type Liquidity = FixedU128; + pub const SECONDS_PER_YEAR: Timestamp = 365 * 24 * 60 * 60; +} pub trait CurrencyInfo { fn name(&self) -> &str; @@ -469,7 +490,7 @@ macro_rules! create_currency_id { } create_currency_id! { - #[derive(Encode, Decode, Eq, Hash, PartialEq, Copy, Clone, RuntimeDebug, PartialOrd, Ord, TypeInfo, MaxEncodedLen)] + #[derive(Encode, Decode, Eq, Hash, PartialEq, Copy, Clone, Debug, PartialOrd, Ord, TypeInfo, MaxEncodedLen)] #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] #[cfg_attr(feature = "std", derive(EncodeAsType,DecodeAsType))] #[repr(u8)] @@ -484,7 +505,7 @@ create_currency_id! { } } -#[derive(Encode, Decode, Eq, Hash, PartialEq, Copy, Clone, RuntimeDebug, PartialOrd, Ord, TypeInfo, MaxEncodedLen)] +#[derive(Encode, Decode, Eq, Hash, PartialEq, Copy, Clone, Debug, PartialOrd, Ord, TypeInfo, MaxEncodedLen)] #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] #[cfg_attr(feature = "std", serde(rename_all = "camelCase"))] #[cfg_attr(feature = "std", derive(EncodeAsType, DecodeAsType))] @@ -494,7 +515,7 @@ pub enum LpToken { StableLpToken(StablePoolId), } -#[derive(Encode, Decode, Eq, Hash, PartialEq, Copy, Clone, RuntimeDebug, PartialOrd, Ord, TypeInfo, MaxEncodedLen)] +#[derive(Encode, Decode, Eq, Hash, PartialEq, Copy, Clone, Debug, PartialOrd, Ord, TypeInfo, MaxEncodedLen)] #[cfg_attr(feature = "std", derive(Serialize, Deserialize))] #[cfg_attr(feature = "std", serde(rename_all = "camelCase"))] #[cfg_attr(feature = "std", derive(EncodeAsType, DecodeAsType))] From 313132a608c7cbedf6068432cb61b88cc0d676a2 Mon Sep 17 00:00:00 2001 From: Gregory Hill Date: Fri, 21 Jul 2023 22:28:59 +0000 Subject: [PATCH 2/2] chore: move BalanceWrapper to primitives Signed-off-by: Gregory Hill --- Cargo.lock | 3 +-- crates/oracle/rpc/runtime-api/Cargo.toml | 7 +++--- crates/oracle/rpc/runtime-api/src/lib.rs | 30 ++---------------------- primitives/src/lib.rs | 13 ++++++++++ 4 files changed, 20 insertions(+), 33 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index b3781d0854..e49b26a080 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -6643,9 +6643,8 @@ name = "oracle-rpc-runtime-api" version = "1.2.0" dependencies = [ "frame-support", + "interbtc-primitives", "parity-scale-codec", - "scale-info", - "serde", "sp-api", "sp-std", ] diff --git a/crates/oracle/rpc/runtime-api/Cargo.toml b/crates/oracle/rpc/runtime-api/Cargo.toml index 2e2f2d2b0d..d4f9af7f02 100644 --- a/crates/oracle/rpc/runtime-api/Cargo.toml +++ b/crates/oracle/rpc/runtime-api/Cargo.toml @@ -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", ] diff --git a/crates/oracle/rpc/runtime-api/src/lib.rs b/crates/oracle/rpc/runtime-api/src/lib.rs index 4e44e8bd06..d5066104ef 100644 --- a/crates/oracle/rpc/runtime-api/src/lib.rs +++ b/crates/oracle/rpc/runtime-api/src/lib.rs @@ -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 -pub struct BalanceWrapper { - #[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(t: &T, serializer: S) -> Result { - serializer.serialize_str(&t.to_string()) -} - -#[cfg(feature = "std")] -fn deserialize_from_string<'de, D: Deserializer<'de>, T: std::str::FromStr>(deserializer: D) -> Result { - let s = String::deserialize(deserializer)?; - s.parse::() - .map_err(|_| serde::de::Error::custom("Parse from string failed")) -} +pub use primitives::BalanceWrapper; sp_api::decl_runtime_apis! { pub trait OracleApi where diff --git a/primitives/src/lib.rs b/primitives/src/lib.rs index 2da88406d4..293e2e10ad 100644 --- a/primitives/src/lib.rs +++ b/primitives/src/lib.rs @@ -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 +pub struct BalanceWrapper { + #[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(t: &T, serializer: S) -> Result { serializer.serialize_str(&t.to_string())