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

CU-2p64ar7 - Update custom rpc wrapper representation #900

Merged
merged 7 commits into from
Apr 19, 2022
13 changes: 7 additions & 6 deletions frame/assets/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use assets_runtime_api::AssetsRuntimeApi;
use codec::Codec;
use composable_support::rpc_helpers::{SafeRpcWrapper, SafeRpcWrapperType};
use composable_support::rpc_helpers::SafeRpcWrapper;
use core::{fmt::Display, str::FromStr};
use jsonrpc_core::{Error as RpcError, ErrorCode, Result as RpcResult};
use jsonrpc_derive::rpc;
use sp_api::ProvideRuntimeApi;
Expand All @@ -11,8 +12,8 @@ use sp_std::sync::Arc;
#[rpc]
pub trait AssetsApi<BlockHash, AssetId, AccountId, Balance>
where
AssetId: SafeRpcWrapperType,
Balance: SafeRpcWrapperType,
AssetId: FromStr + Display,
Balance: FromStr + Display,
{
#[rpc(name = "assets_balanceOf")]
fn balance_of(
Expand All @@ -39,9 +40,9 @@ impl<C, Block, AssetId, AccountId, Balance>
for Assets<C, (Block, AssetId, AccountId, Balance)>
where
Block: BlockT,
AssetId: Codec + Send + Sync + 'static + SafeRpcWrapperType,
AccountId: Codec + Send + Sync + 'static,
Balance: Send + Sync + 'static + SafeRpcWrapperType,
AssetId: Codec + Send + Sync + 'static + Codec + FromStr + Display,
AccountId: Codec + Send + Sync + 'static + Codec + FromStr + Display,
Balance: Send + Sync + 'static + Codec + FromStr + Display,
C: Send + Sync + 'static,
C: ProvideRuntimeApi<Block>,
C: HeaderBackend<Block>,
Expand Down
6 changes: 3 additions & 3 deletions frame/assets/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@
#![allow(clippy::unnecessary_mut_passed)]

use codec::Codec;
use composable_support::rpc_helpers::{SafeRpcWrapper, SafeRpcWrapperType};
use composable_support::rpc_helpers::SafeRpcWrapper;

// Here we declare the runtime API. It is implemented it the `impl` block in
// runtime amalgamator file (the `runtime/src/lib.rs`)
sp_api::decl_runtime_apis! {
// REVIEW(benluelo): Should the AssetId type parameter be removed and then just use CurencyId directly?
pub trait AssetsRuntimeApi<AssetId, AccountId, Balance>
where
AssetId: SafeRpcWrapperType,
AccountId: Codec,
Balance: SafeRpcWrapperType,
Balance: Codec,
AssetId: Codec,
{
fn balance_of(asset_id: SafeRpcWrapper<AssetId>, account_id: AccountId) -> SafeRpcWrapper<Balance> /* Balance */;
}
Expand Down
89 changes: 26 additions & 63 deletions frame/composable-support/src/rpc_helpers.rs
Original file line number Diff line number Diff line change
@@ -1,79 +1,42 @@
use codec::{Codec, Decode, Encode};
use codec::{Decode, Encode, MaxEncodedLen};
#[cfg(feature = "std")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};

/// https://github.com/interlay/interbtc/blob/a7c0e69ac041176a2531bafb1c4e35cbc2f7e192/crates/oracle/rpc/runtime-api/src/lib.rs#L10
hussein-aitlahcen marked this conversation as resolved.
Show resolved Hide resolved
#[derive(
scale_info::TypeInfo,
Clone,
Copy,
Debug,
Decode,
Default,
Encode,
Eq,
MaxEncodedLen,
Ord,
PartialEq,
PartialOrd,
)]
#[cfg_attr(feature = "std", derive(Serialize, Deserialize))]
#[derive(Debug, PartialEq, Eq, Encode, Decode)]
pub struct SafeRpcWrapper<T: SafeRpcWrapperType>(
#[cfg_attr(feature = "std", serde(serialize_with = "serialize_to_hex"))]
hussein-aitlahcen marked this conversation as resolved.
Show resolved Hide resolved
#[cfg_attr(feature = "std", serde(deserialize_with = "deserialize_from_hex"))]
#[cfg_attr(feature = "std", serde(rename_all = "camelCase"))]
pub struct SafeRpcWrapper<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 T,
);

pub trait SafeRpcWrapperType
where
Self: sp_std::fmt::LowerHex + FromHexStr + Codec,
{
}

impl<T> SafeRpcWrapperType for T where T: sp_std::fmt::LowerHex + FromHexStr + Codec {}

pub trait FromHexStr: sp_std::marker::Sized {
type Err: sp_std::fmt::Display;

fn from_hex_str(src: &str) -> sp_std::result::Result<Self, Self::Err>;
}

#[derive(Debug)]
pub enum FromHexStrErr {
No0xPrefix,
ParseIntError(sp_std::num::ParseIntError),
}

impl sp_std::fmt::Display for FromHexStrErr {
fn fmt(&self, f: &mut sp_std::fmt::Formatter<'_>) -> sp_std::fmt::Result {
match self {
FromHexStrErr::No0xPrefix => f.write_str("No `0x` prefix"),
FromHexStrErr::ParseIntError(parse_int_error) =>
f.write_fmt(format_args!("{}", parse_int_error)),
}
}
}

impl FromHexStr for u128 {
type Err = FromHexStrErr;

fn from_hex_str(src: &str) -> sp_std::result::Result<Self, Self::Err> {
match src.strip_prefix("0x") {
Some(stripped) =>
u128::from_str_radix(stripped, 16).map_err(FromHexStrErr::ParseIntError),
None => Err(FromHexStrErr::No0xPrefix),
}
}
}

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

#[cfg(feature = "std")]
fn deserialize_from_hex<'de, D: Deserializer<'de>, T: SafeRpcWrapperType>(
fn deserialize_from_string<'de, D: Deserializer<'de>, T: std::str::FromStr>(
deserializer: D,
) -> Result<T, D::Error> {
use serde::de::Error;
let hex_string = String::deserialize(deserializer)?;

T::from_hex_str(&hex_string).map_err(|err| {
D::Error::custom(format!(
"Unable to parse as 0x-prefixed hex string: {} (error: {})",
hex_string, err
))
})
let s = String::deserialize(deserializer)?;
s.parse::<T>().map_err(|_| serde::de::Error::custom("Parse from string failed"))
}

// TODO: tests?
8 changes: 5 additions & 3 deletions frame/crowdloan-rewards/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use composable_support::rpc_helpers::{SafeRpcWrapper, SafeRpcWrapperType};
use codec::Codec;
use composable_support::rpc_helpers::SafeRpcWrapper;
use core::{fmt::Display, str::FromStr};
use crowdloan_rewards_runtime_api::CrowdloanRewardsRuntimeApi;
use frame_support::{pallet_prelude::MaybeSerializeDeserialize, Parameter};
use jsonrpc_core::{Error as RpcError, ErrorCode, Result as RpcResult};
Expand All @@ -11,7 +13,7 @@ use sp_std::{marker::PhantomData, sync::Arc};
#[rpc]
pub trait CrowdloanRewardsApi<BlockHash, AccountId, Balance>
where
Balance: SafeRpcWrapperType,
Balance: FromStr + Display,
{
#[rpc(name = "crowdloanRewards_amountAvailableToClaimFor")]
fn amount_available_to_claim_for(
Expand Down Expand Up @@ -39,7 +41,7 @@ impl<C, Block, AccountId, Balance> CrowdloanRewardsApi<<Block as BlockT>::Hash,
where
Block: BlockT,
AccountId: Send + Sync + Parameter + MaybeSerializeDeserialize + Ord + 'static,
Balance: Send + Sync + 'static + SafeRpcWrapperType,
Balance: Send + Sync + 'static + Codec + FromStr + Display,
C: Send + Sync + ProvideRuntimeApi<Block> + HeaderBackend<Block> + 'static,
C::Api: CrowdloanRewardsRuntimeApi<Block, AccountId, Balance>,
{
Expand Down
4 changes: 2 additions & 2 deletions frame/crowdloan-rewards/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
#![allow(clippy::unnecessary_mut_passed)]

use codec::Codec;
use composable_support::rpc_helpers::{SafeRpcWrapper, SafeRpcWrapperType};
use composable_support::rpc_helpers::SafeRpcWrapper;

// Here we declare the runtime API. It is implemented it the `impl` block in
// runtime amalgamator file (the `runtime/src/lib.rs`)
sp_api::decl_runtime_apis! {
pub trait CrowdloanRewardsRuntimeApi<AccountId, Balance>
where
AccountId: Codec,
Balance: SafeRpcWrapperType,
Balance: Codec
{
fn amount_available_to_claim_for(account: AccountId) -> SafeRpcWrapper<Balance>;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export default {
params: [
{
name: "asset",
type: "CurrencyId"
type: "CustomRpcCurrencyId"
},
{
name: "account",
Expand All @@ -17,11 +17,8 @@ export default {
isOptional: true,
},
],
type: "AssetsBalance"
type: "CustomRpcBalance"
},
},
types: {
CurrencyId: "u128",
AssetsBalance: "u128",
},
types: {}
};
Original file line number Diff line number Diff line change
@@ -1,12 +1,4 @@
// Auto-generated via `yarn polkadot-types-from-defs`, do not edit
/* eslint-disable */

import type { u128 } from '@polkadot/types-codec';

/** @name AssetsBalance */
export interface AssetsBalance extends u128 {}

/** @name CurrencyId */
export interface CurrencyId extends u128 {}

export type PHANTOM_ASSETS = 'assets';
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,10 @@ declare module '@polkadot/api-base/types/consts' {
};
dutchAuction: {
palletId: FrameSupportPalletId & AugmentedConst<ApiType>;
/**
* ED taken to create position. Part of if returned when position is liqudated.
**/
positionExistentialDeposit: u128 & AugmentedConst<ApiType>;
/**
* Generic const
**/
Expand Down Expand Up @@ -255,6 +259,13 @@ declare module '@polkadot/api-base/types/consts' {
**/
[key: string]: Codec;
};
liquidations: {
palletId: FrameSupportPalletId & AugmentedConst<ApiType>;
/**
* Generic const
**/
[key: string]: Codec;
};
liquidityBootstrapping: {
/**
* Maximum initial weight.
Expand Down Expand Up @@ -326,6 +337,33 @@ declare module '@polkadot/api-base/types/consts' {
**/
[key: string]: Codec;
};
pablo: {
/**
* Maximum initial weight.
**/
lbpMaxInitialWeight: Permill & AugmentedConst<ApiType>;
/**
* Maximum duration for a sale.
**/
lbpMaxSaleDuration: u32 & AugmentedConst<ApiType>;
/**
* Minimum final weight.
**/
lbpMinFinalWeight: Permill & AugmentedConst<ApiType>;
/**
* Minimum duration for a sale.
**/
lbpMinSaleDuration: u32 & AugmentedConst<ApiType>;
palletId: FrameSupportPalletId & AugmentedConst<ApiType>;
/**
* The interval between TWAP computations.
**/
twapInterval: u64 & AugmentedConst<ApiType>;
/**
* Generic const
**/
[key: string]: Codec;
};
scheduler: {
/**
* The maximum weight that may be scheduled per block for any dispatchables of less
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -413,7 +413,14 @@ declare module '@polkadot/api-base/types/errors' {
OrderParametersIsInvalid: AugmentedError<ApiType>;
RequestedOrderDoesNotExists: AugmentedError<ApiType>;
TakeLimitDoesNotSatisfyOrder: AugmentedError<ApiType>;
TakeOrderDidNotHappen: AugmentedError<ApiType>;
TakeParametersIsInvalid: AugmentedError<ApiType>;
/**
* errors trying to decode and parse XCM input
**/
XcmCannotDecodeRemoteParametersToLocalRepresentations: AugmentedError<ApiType>;
XcmCannotFindLocalIdentifiersAsDecodedFromRemote: AugmentedError<ApiType>;
XcmNotFoundConfigurationById: AugmentedError<ApiType>;
/**
* Generic error
**/
Expand Down Expand Up @@ -789,6 +796,24 @@ declare module '@polkadot/api-base/types/errors' {
**/
[key: string]: AugmentedError<ApiType>;
};
pablo: {
AmpFactorMustBeGreaterThanZero: AugmentedError<ApiType>;
AssetAmountMustBePositiveNumber: AugmentedError<ApiType>;
CannotRespectMinimumRequested: AugmentedError<ApiType>;
InvalidAmount: AugmentedError<ApiType>;
InvalidFees: AugmentedError<ApiType>;
InvalidPair: AugmentedError<ApiType>;
InvalidSaleState: AugmentedError<ApiType>;
MissingAmount: AugmentedError<ApiType>;
MustBeOwner: AugmentedError<ApiType>;
PairMismatch: AugmentedError<ApiType>;
PoolConfigurationNotSupported: AugmentedError<ApiType>;
PoolNotFound: AugmentedError<ApiType>;
/**
* Generic error
**/
[key: string]: AugmentedError<ApiType>;
};
parachainSystem: {
/**
* The inherent which supplies the host configuration did not run this block
Expand Down
Loading