Skip to content

Commit

Permalink
fix: remove std feature gate for serde.
Browse files Browse the repository at this point in the history
  • Loading branch information
nakul1010 committed Sep 6, 2023
1 parent a262f15 commit f50fc32
Show file tree
Hide file tree
Showing 8 changed files with 139 additions and 56 deletions.
9 changes: 5 additions & 4 deletions crates/bitcoin/src/address.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use bitcoin_hashes::{hash160::Hash as Hash160, Hash};
use codec::{Decode, Encode, MaxEncodedLen};
use primitive_types::{H160, H256};
use scale_info::TypeInfo;
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};

#[cfg(not(feature = "std"))]
Expand All @@ -13,8 +14,10 @@ use secp256k1::{constants::PUBLIC_KEY_SIZE, Error as Secp256k1Error, PublicKey a
/// A Bitcoin address is a serialized identifier that represents the destination for a payment.
/// Address prefixes are used to indicate the network as well as the format. Since the Parachain
/// follows SPV assumptions we do not need to know which network a payment is included in.
#[derive(Encode, Decode, Clone, Ord, PartialOrd, PartialEq, Eq, Debug, Copy, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize, std::hash::Hash))]
#[derive(
Serialize, Deserialize, Encode, Decode, Clone, Ord, PartialOrd, PartialEq, Eq, Debug, Copy, TypeInfo, MaxEncodedLen,
)]
#[cfg_attr(feature = "std", derive(std::hash::Hash))]
pub enum Address {
// input: {signature} {pubkey}
// output: OP_DUP OP_HASH160 {hash160(pubkey)} OP_EQUALVERIFY OP_CHECKSIG
Expand Down Expand Up @@ -150,7 +153,6 @@ impl From<[u8; PUBLIC_KEY_SIZE]> for PublicKey {
}
}

#[cfg(feature = "std")]
impl serde::Serialize for PublicKey {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand All @@ -161,7 +163,6 @@ impl serde::Serialize for PublicKey {
}
}

#[cfg(feature = "std")]
impl<'de> serde::Deserialize<'de> for PublicKey {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
Expand Down
6 changes: 3 additions & 3 deletions crates/bitcoin/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ use alloc::{vec, vec::Vec};
#[cfg(feature = "std")]
use codec::alloc::string::String;

#[cfg(feature = "std")]
use serde::{Deserialize, Serialize};

/// We also check the coinbase proof in order to defend against the 'leaf-node weakness'.
Expand Down Expand Up @@ -591,8 +590,9 @@ pub struct BlockChain {
}

/// Represents a bitcoin 32 bytes hash digest encoded in little-endian
#[derive(Encode, Decode, Default, PartialEq, Eq, Clone, Copy, Debug, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(
Serialize, Deserialize, Encode, Decode, Default, PartialEq, Eq, Clone, Copy, Debug, TypeInfo, MaxEncodedLen,
)]
pub struct H256Le {
content: [u8; 32],
}
Expand Down
2 changes: 1 addition & 1 deletion crates/loans/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ version = "1.9.3"
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
serde = { version = "1.0.136", default-features = false, features = ["derive"] , optional = true }
serde = { version = "1.0.136", default-features = false, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false }
scale-info = { version = "2.2.0", default-features = false, features = ["derive"] }
num-traits = { default-features = false, version = "0.2" }
Expand Down
45 changes: 39 additions & 6 deletions crates/loans/src/rate_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,19 @@ use sp_runtime::traits::{CheckedAdd, CheckedDiv, CheckedSub, Saturating};
use crate::*;

/// Parallel interest rate model
#[derive(Encode, Decode, Eq, PartialEq, Copy, Clone, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
serde::Deserialize,
serde::Serialize,
Encode,
Decode,
Eq,
PartialEq,
Copy,
Clone,
RuntimeDebug,
TypeInfo,
MaxEncodedLen,
)]
pub enum InterestRateModel {
Jump(JumpModel),
Curve(CurveModel),
Expand Down Expand Up @@ -76,8 +87,19 @@ impl InterestRateModel {
}

/// The jump interest rate model
#[derive(Encode, Decode, Eq, PartialEq, Copy, Clone, RuntimeDebug, Default, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
serde::Deserialize,
serde::Serialize,
Encode,
Decode,
Eq,
PartialEq,
Copy,
Clone,
RuntimeDebug,
TypeInfo,
MaxEncodedLen,
)]
pub struct JumpModel {
/// The base interest rate when utilization rate is 0
pub base_rate: Rate,
Expand Down Expand Up @@ -147,8 +169,19 @@ impl JumpModel {
}

/// The curve interest rate model
#[derive(Encode, Decode, Eq, PartialEq, Copy, Clone, RuntimeDebug, Default, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
serde::Deserialize,
serde::Serialize,
Encode,
Decode,
Eq,
PartialEq,
Copy,
Clone,
RuntimeDebug,
TypeInfo,
MaxEncodedLen,
)]
pub struct CurveModel {
pub base_rate: Rate,
}
Expand Down
30 changes: 26 additions & 4 deletions crates/loans/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,19 @@ pub struct EarnedSnapshot<Balance> {
}

/// The current state of a market. For more information, see [Market].
#[derive(Clone, Copy, PartialEq, Eq, codec::Decode, codec::Encode, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
serde::Deserialize,
serde::Serialize,
Encode,
Decode,
Eq,
PartialEq,
Copy,
Clone,
RuntimeDebug,
TypeInfo,
MaxEncodedLen,
)]
pub enum MarketState {
Active,
Pending,
Expand All @@ -89,8 +100,19 @@ pub enum MarketState {
/// Market.
///
/// A large pool of liquidity where accounts can lend and borrow.
#[derive(Clone, PartialEq, Eq, codec::Decode, codec::Encode, RuntimeDebug, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(serde::Serialize, serde::Deserialize))]
#[derive(
serde::Deserialize,
serde::Serialize,
Encode,
Decode,
Eq,
PartialEq,
Copy,
Clone,
RuntimeDebug,
TypeInfo,
MaxEncodedLen,
)]
pub struct Market<Balance> {
/// The secure collateral ratio
pub collateral_factor: Ratio,
Expand Down
3 changes: 1 addition & 2 deletions crates/vault-registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = ["Interlay Ltd"]
edition = "2021"

[dependencies]
serde = { version = "1.0.130", default-features = false, features = ["derive"] , optional = true }
serde = { version = "1.0.130", default-features = false, features = ["derive"] }
codec = { package = "parity-scale-codec", version = "3.1.5", default-features = false, features = ["derive", "max-encoded-len"] }
scale-info = { version = "2.2.0", default-features = false, features = ["derive"] }

Expand Down Expand Up @@ -80,7 +80,6 @@ std = [
"loans/std",
]
runtime-benchmarks = [
# "primitives",
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
Expand Down
4 changes: 2 additions & 2 deletions crates/vault-registry/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,8 @@ pub struct Vault<AccountId, BlockNumber, Balance, CurrencyId: Copy, UnsignedFixe
pub liquidated_collateral: Balance,
}

#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Debug, serde::Serialize, serde::Deserialize))]
#[derive(serde::Serialize, serde::Deserialize, Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct SystemVault<Balance, CurrencyId: Copy> {
// Number of tokens pending issue
pub to_be_issued_tokens: Balance,
Expand Down
96 changes: 62 additions & 34 deletions primitives/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use scale_decode::DecodeAsType;
#[cfg(feature = "std")]
use scale_encode::EncodeAsType;
use scale_info::TypeInfo;
#[cfg(feature = "std")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};

pub use bitcoin::types::H256Le;
Expand Down Expand Up @@ -77,15 +76,19 @@ mod arithmetic {
}
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, std::hash::Hash))]
#[derive(
Serialize, Deserialize, Encode, Decode, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, TypeInfo, MaxEncodedLen,
)]
#[cfg_attr(feature = "std", derive(std::hash::Hash))]
pub struct VaultCurrencyPair<CurrencyId: Copy> {
pub collateral: CurrencyId,
pub wrapped: CurrencyId,
}

#[derive(Encode, Decode, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize, std::hash::Hash))]
#[derive(
Serialize, Deserialize, Encode, Decode, Clone, PartialEq, Eq, Debug, PartialOrd, Ord, TypeInfo, MaxEncodedLen,
)]
#[cfg_attr(feature = "std", derive(std::hash::Hash))]
pub struct VaultId<AccountId, CurrencyId: Copy> {
pub account_id: AccountId,
pub currencies: VaultCurrencyPair<CurrencyId>,
Expand Down Expand Up @@ -124,9 +127,9 @@ impl<AccountId, CurrencyId: Copy> From<(AccountId, VaultCurrencyPair<CurrencyId>
pub mod issue {
use super::*;

#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
#[derive(Serialize, Deserialize, Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Debug))]
#[serde(rename_all = "camelCase")]
pub enum IssueRequestStatus {
/// opened, but not yet executed or cancelled
Pending,
Expand All @@ -144,8 +147,8 @@ pub mod issue {

// Due to a known bug in serde we need to specify how u128 is (de)serialized.
// See https://github.com/paritytech/substrate/issues/4641
#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
#[derive(Serialize, Deserialize, Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct IssueRequest<AccountId, BlockNumber, Balance, CurrencyId: Copy> {
/// the vault associated with this issue request
pub vault: VaultId<AccountId, CurrencyId>,
Expand Down Expand Up @@ -186,9 +189,8 @@ 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"))]
#[derive(Serialize, Deserialize, Eq, PartialEq, Encode, Decode, Default, TypeInfo)]
#[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> {
Expand All @@ -214,9 +216,9 @@ fn deserialize_from_string<'de, D: Deserializer<'de>, T: std::str::FromStr>(dese
pub mod redeem {
use super::*;

#[derive(Encode, Decode, Clone, Eq, PartialEq, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
#[derive(Serialize, Deserialize, Encode, Decode, Clone, Eq, PartialEq, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Debug))]
#[serde(rename_all = "camelCase")]
pub enum RedeemRequestStatus {
/// opened, but not yet executed or cancelled
Pending,
Expand All @@ -236,8 +238,8 @@ pub mod redeem {

// Due to a known bug in serde we need to specify how u128 is (de)serialized.
// See https://github.com/paritytech/substrate/issues/4641
#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
#[derive(Serialize, Deserialize, Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Debug))]
pub struct RedeemRequest<AccountId, BlockNumber, Balance, CurrencyId: Copy> {
/// the vault associated with this redeem request
pub vault: VaultId<AccountId, CurrencyId>,
Expand Down Expand Up @@ -283,9 +285,9 @@ pub mod redeem {
pub mod replace {
use super::*;

#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize, Eq))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
#[derive(Serialize, Deserialize, Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Debug, Eq))]
#[serde(rename_all = "camelCase")]
pub enum ReplaceRequestStatus {
/// accepted, but not yet executed or cancelled
Pending,
Expand All @@ -303,8 +305,8 @@ pub mod replace {

// Due to a known bug in serde we need to specify how u128 is (de)serialized.
// See https://github.com/paritytech/substrate/issues/4641
#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize, Eq))]
#[derive(Serialize, Deserialize, Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Eq, Debug))]
pub struct ReplaceRequest<AccountId, BlockNumber, Balance, CurrencyId: Copy> {
/// the vault which has requested to be replaced
pub old_vault: VaultId<AccountId, CurrencyId>,
Expand Down Expand Up @@ -344,9 +346,8 @@ pub mod replace {
pub mod oracle {
use super::*;

#[derive(Encode, Decode, Clone, Eq, PartialEq, Debug, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
#[derive(Serialize, Deserialize, Encode, Decode, Clone, Eq, PartialEq, Debug, TypeInfo, MaxEncodedLen)]
#[serde(rename_all = "camelCase")]
pub enum Key {
ExchangeRate(CurrencyId),
FeeEstimation,
Expand Down Expand Up @@ -503,8 +504,7 @@ macro_rules! create_currency_id {
}

create_currency_id! {
#[derive(Encode, Decode, Eq, Hash, PartialEq, Copy, Clone, Debug, PartialOrd, Ord, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Serialize, Deserialize,Encode, Decode, Eq, Hash, PartialEq, Copy, Clone, Debug, PartialOrd, Ord, TypeInfo, MaxEncodedLen)]
#[cfg_attr(feature = "std", derive(EncodeAsType,DecodeAsType))]
#[repr(u8)]
pub enum TokenSymbol {
Expand All @@ -518,19 +518,47 @@ create_currency_id! {
}
}

#[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"))]
#[derive(
Serialize,
Deserialize,
Encode,
Decode,
Eq,
Hash,
PartialEq,
Copy,
Clone,
Debug,
PartialOrd,
Ord,
TypeInfo,
MaxEncodedLen,
)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "std", derive(EncodeAsType, DecodeAsType))]
pub enum LpToken {
Token(TokenSymbol),
ForeignAsset(ForeignAssetId),
StableLpToken(StablePoolId),
}

#[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"))]
#[derive(
Serialize,
Deserialize,
Encode,
Decode,
Eq,
Hash,
PartialEq,
Copy,
Clone,
Debug,
PartialOrd,
Ord,
TypeInfo,
MaxEncodedLen,
)]
#[serde(rename_all = "camelCase")]
#[cfg_attr(feature = "std", derive(EncodeAsType, DecodeAsType))]
pub enum CurrencyId {
Token(TokenSymbol),
Expand Down

0 comments on commit f50fc32

Please sign in to comment.