Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
502 changes: 278 additions & 224 deletions Cargo.lock

Large diffs are not rendered by default.

336 changes: 169 additions & 167 deletions Cargo.toml

Large diffs are not rendered by default.

400 changes: 400 additions & 0 deletions docs/rpc.md

Large diffs are not rendered by default.

17 changes: 9 additions & 8 deletions frame/assets/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,26 @@ frame-benchmarking = { default-features = false, optional = true, git = 'https:/
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }

sp-api = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
# sp-blockchain = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
Comment thread
benluelo marked this conversation as resolved.
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
sp-arithmetic = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
sp-io = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }

composable-traits = { path = "../composable-traits", default-features = false }
# local
composable-traits = { path = "../composable-traits", default-features = false }

orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "2b1c9fb367ccb8e13601b2da43d1c5d9737b93c6", default-features = false }
num-traits = { version = "0.2.14", default-features = false }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }

[dependencies.codec]
default-features = false
features = ["derive"]
package = "parity-scale-codec"
version = "2.0.0"
# misc
codec = { default-features = false, features = ["derive"], package = "parity-scale-codec", version = "2.0.0" }
num-traits = { version = "0.2.14", default-features = false }

[dev-dependencies]
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16", default-features = false }
orml-tokens = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "2b1c9fb367ccb8e13601b2da43d1c5d9737b93c6", default-features = false }
governance-registry = { package = "pallet-governance-registry", path = "../governance-registry", default-features = false }
proptest = "0.9.6"
Expand All @@ -49,6 +49,7 @@ std = [
"codec/std",
"frame-support/std",
"sp-runtime/std",
"sp-api/std",
"orml-traits/std",
"num-traits/std",
]
Expand Down
30 changes: 30 additions & 0 deletions frame/assets/rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
[package]
name = "assets-rpc"
version = "0.0.1"
authors = ["Composable Developers"]
homepage = "https://composable.finance"
edition = "2021"
rust-version = "1.56"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
# substrate primitives
sp-api = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
sp-blockchain = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
sp-runtime = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
sp-std = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }

# local
composable-support = { path = "../../composable-support" }
assets-runtime-api = { path = "../runtime-api" }

# SCALE
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
codec = { default-features = false, features = ["derive"], package = "parity-scale-codec", version = "2.0.0" }

# rpc
jsonrpc-core = "18.0.0"
jsonrpc-core-client = "18.0.0"
jsonrpc-derive = "18.0.0"
73 changes: 73 additions & 0 deletions frame/assets/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
use assets_runtime_api::AssetsRuntimeApi;
use codec::Codec;
use composable_support::rpc_helpers::{SafeRpcWrapper, SafeRpcWrapperType};
use jsonrpc_core::{Error as RpcError, ErrorCode, Result as RpcResult};
use jsonrpc_derive::rpc;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_runtime::{generic::BlockId, traits::Block as BlockT};
use sp_std::sync::Arc;

#[rpc]
pub trait AssetsApi<BlockHash, AssetId, AccountId, Balance>
where
AssetId: SafeRpcWrapperType,
Balance: SafeRpcWrapperType,
{
#[rpc(name = "assets_balanceOf")]
fn balance_of(
&self,
currency: SafeRpcWrapper<AssetId>,
account: AccountId,
at: Option<BlockHash>,
) -> RpcResult<SafeRpcWrapper<Balance>>;
}

pub struct Assets<C, Block> {
client: Arc<C>,
_marker: sp_std::marker::PhantomData<Block>,
}

impl<C, M> Assets<C, M> {
pub fn new(client: Arc<C>) -> Self {
Self { client, _marker: Default::default() }
}
}

impl<C, Block, AssetId, AccountId, Balance>
AssetsApi<<Block as BlockT>::Hash, 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,
C: Send + Sync + 'static,
C: ProvideRuntimeApi<Block>,
C: HeaderBackend<Block>,
C::Api: AssetsRuntimeApi<Block, AssetId, AccountId, Balance>,
{
fn balance_of(
&self,
asset_id: SafeRpcWrapper<AssetId>,
account_id: AccountId,
at: Option<<Block as BlockT>::Hash>,
) -> RpcResult<SafeRpcWrapper<Balance>> {
let api = self.client.runtime_api();

let at = BlockId::hash(at.unwrap_or_else(|| {
// If the block hash is not supplied assume the best block.
self.client.info().best_hash
}));

let runtime_api_result = api.balance_of(&at, asset_id, account_id);
// TODO(benluelo): Review what error message & code to use
runtime_api_result.map_err(|e| {
RpcError {
code: ErrorCode::ServerError(9876), // No real reason for this value
message: "Something wrong".into(),
data: Some(format!("{:?}", e).into()),
}
})
}
}
19 changes: 19 additions & 0 deletions frame/assets/runtime-api/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
[package]
name = "assets-runtime-api"
version = "0.0.1"
authors = ["Composable Developers"]
homepage = "https://composable.finance"
edition = "2021"
rust-version = "1.56"

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
sp-api = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
codec = { default-features = false, features = ["derive"], package = "parity-scale-codec", version = "2.0.0" }
composable-support = { path = "../../composable-support", default-features = false }

[features]
default = ["std"]
std = ["sp-api/std", "codec/std", "composable-support/std"]
20 changes: 20 additions & 0 deletions frame/assets/runtime-api/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![cfg_attr(not(feature = "std"), no_std)]
#![allow(clippy::too_many_arguments)]
#![allow(clippy::unnecessary_mut_passed)]

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

// 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,
{
fn balance_of(asset_id: SafeRpcWrapper<AssetId>, account_id: AccountId) -> SafeRpcWrapper<Balance> /* Balance */;
}
}
22 changes: 10 additions & 12 deletions frame/composable-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,32 @@ rust-version = "1.56"
[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dev-dependencies]
proptest = { version = "1.0" }
serde_json = "1.0.45"

[dependencies]
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }

sp-arithmetic = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }

codec = { version = "2.0.0", package = "parity-scale-codec", default-features = false, features = ["derive"] }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
sorted-vec = "0.7.0"

sorted-vec = { version = "0.7.0", optional = true }
serde = { version = "1.0.130", features = [ "derive" ], optional = true }
is_sorted = { version = "0.1.1", default-features = false }

[dependencies.codec]
default-features = false
features = ["derive"]
package = "parity-scale-codec"
version = "2.0.0"
[dev-dependencies]
proptest = { version = "1.0" }
serde_json = "1.0.45"

[features]
default = ["std"]
std = [
"codec/std",
"frame-support/std",
"frame-system/std",
"sp-runtime/std",
"sp-std/std",
"scale-info/std",
"serde",
"sorted-vec",
]
1 change: 1 addition & 0 deletions frame/composable-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,5 @@
#![cfg_attr(not(feature = "std"), no_std)]

pub mod collections;
pub mod rpc_helpers;
pub mod validation;
79 changes: 79 additions & 0 deletions frame/composable-support/src/rpc_helpers.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use codec::{Codec, Decode, Encode};
#[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
#[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"))]
#[cfg_attr(feature = "std", serde(deserialize_with = "deserialize_from_hex"))]
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>(
t: &T,
serializer: S,
) -> Result<S::Ok, S::Error> {
serializer.serialize_str(&format!("{:#x}", t))
}

#[cfg(feature = "std")]
fn deserialize_from_hex<'de, D: Deserializer<'de>, T: SafeRpcWrapperType>(
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
))
})
}

// TODO: tests?
16 changes: 7 additions & 9 deletions frame/composable-traits/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,23 +5,21 @@ authors = ["Composable Developers"]
homepage = "https://composable.finance"
edition = "2021"


[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
xcm = { default-features = false, package = "xcm", git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.16" }
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
xcm = { default-features = false, git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.16" }
sp-arithmetic = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
orml-traits = { git = "https://github.com/open-web3-stack/open-runtime-module-library", rev = "2b1c9fb367ccb8e13601b2da43d1c5d9737b93c6", default-features = false }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }

composable-support = {default-features = false, path = "../composable-support"}
composable-support = { default-features = false, path = "../composable-support" }
scale-info = { version = "1.0", default-features = false, features = ["derive"] }
serde = { version = '1', optional = true }
plotters = {version = "0.3.1", optional = true}
plotters = { version = "0.3.1", optional = true }
bitflags = "1.3.2"

[dev-dependencies]
Expand All @@ -48,4 +46,4 @@ std = [
visualization = ["plotters"]

[package.metadata.cargo-udeps.ignore]
normal = ["plotters"]
normal = ["plotters"]
17 changes: 9 additions & 8 deletions frame/crowdloan-rewards/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,26 +26,27 @@ hex-literal = "0.3"
balances = { package = "pallet-balances", git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16", default-features = false, features = [ "std" ]}

[dependencies]
### Benchmarking
# Benchmarking
hex-literal = { version = "0.3.3", optional = true }
libsecp256k1 = { version = "0.7.0", default-features = false, optional = true, features = [
"hmac",
"static-context",
] }
libsecp256k1 = { version = "0.7.0", default-features = false, optional = true, features = ["hmac","static-context"] }
frame-benchmarking = { default-features = false, optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
sp-application-crypto = { default-features = false, optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }

# FRAME
frame-support = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }

# substrate primitives
sp-runtime = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
sp-arithmetic = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
sp-io = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
sp-core = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
sp-std = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.16" }
scale-info = { version = "1.0", default-features = false, features = [
"derive",
] }

# SCALE
scale-info = { version = "1.0", default-features = false, features = ["derive"] }

# misc
serde = { version = "1.0.130", optional = true }
rustc-hex = { version = "2.1.0", default-features = false }
hex = { version = "0.4", default-features = false, features = ["alloc"] }
Expand Down
Loading