-
Notifications
You must be signed in to change notification settings - Fork 76
Ben/assets rpc #493
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
Merged
Merged
Ben/assets rpc #493
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
dbcf560
Implement basic RPC structure
benluelo e5e618b
Formatting
benluelo 8d5cc63
Remove assets tests.rs file
benluelo 15d0414
Remove commented-out code
benluelo cf23320
Remove specific RPC crate entries in workspace Cargo.toml
benluelo 3ed19ea
Remove std feature from assets-rpc
benluelo 37ef4af
Update Cargo.lock
benluelo c640eed
Update generated api types
benluelo af74eec
Remove std feature from crowdloan-rewards-rpc
benluelo b6c7bcb
Clean up crowdloan-rewards-rpc
benluelo 1c565d4
Update Cargo.lock
benluelo 09190ef
Update RPC docs
benluelo ce4b795
Clean up RPC tests
benluelo 8068e40
Remove unused imports in crowdloan-rewards-rpc
benluelo f5592d5
Update Cargo.lock
benluelo File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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()), | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 */; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -13,4 +13,5 @@ | |
| #![cfg_attr(not(feature = "std"), no_std)] | ||
|
|
||
| pub mod collections; | ||
| pub mod rpc_helpers; | ||
| pub mod validation; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.