git.delta.rocks / unique-network / refs/commits / b70e369d3c1a

difftreelog

feat custom rpc

Yaroslav Bolyukin2021-10-12parent: #5c24805.patch.diff
in: master

7 files changed

modifiedCargo.tomldiffbeforeafterboth
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -3,6 +3,7 @@
 members = [
     'node/*',
     'pallets/*',
+    'client/*',
     'primitives/*',
     'runtime',
     'crates/*',
addedclient/rpc/Cargo.tomldiffbeforeafterboth
--- /dev/null
+++ b/client/rpc/Cargo.toml
@@ -0,0 +1,19 @@
+[package]
+name = "uc-rpc"
+version = "0.1.0"
+edition = "2018"
+
+[dependencies]
+pallet-common = { default-features = false, path = '../../pallets/common' }
+nft-data-structs = { default-features = false, path = '../../primitives/nft' }
+up-rpc = { path = "../../primitives/rpc" }
+codec = { package = "parity-scale-codec", version = "2.0.0" }
+jsonrpc-core = "15.1.0"
+jsonrpc-core-client = "15.1.0"
+jsonrpc-derive = "15.1.0"
+
+sp-api = { default-features = false, version = '4.0.0-dev', git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.10" }
+sp-blockchain = { default-features = false, version = '4.0.0-dev', git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.10" }
+sp-core = { default-features = false, version = '4.0.0-dev', git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.10" }
+sp-rpc = { default-features = false, version = '4.0.0-dev', git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.10" }
+sp-runtime = { default-features = false, version = '4.0.0-dev', git = "https://github.com/paritytech/substrate.git", branch = "polkadot-v0.9.10" }
addedclient/rpc/src/lib.rsdiffbeforeafterboth
after · client/rpc/src/lib.rs
1use std::sync::Arc;23use jsonrpc_core::{Error as RpcError, ErrorCode, Result};4use jsonrpc_derive::rpc;5use nft_data_structs::{CollectionId, TokenId};6use sp_api::{BlockId, BlockT, ProvideRuntimeApi};7use sp_blockchain::HeaderBackend;8use up_rpc::NftApi as NftRuntimeApi;910#[rpc]11pub trait NftApi<BlockHash, CrossAccountId, AccountId> {12	#[rpc(name = "nft_accountTokens")]13	fn account_tokens(14		&self,15		collection: CollectionId,16		account: CrossAccountId,17		at: Option<BlockHash>,18	) -> Result<Vec<TokenId>>;19	#[rpc(name = "nft_tokenExists")]20	fn token_exists(21		&self,22		collection: CollectionId,23		token: TokenId,24		at: Option<BlockHash>,25	) -> Result<bool>;2627	#[rpc(name = "nft_tokenOwner")]28	fn token_owner(29		&self,30		collection: CollectionId,31		token: TokenId,32		at: Option<BlockHash>,33	) -> Result<CrossAccountId>;34	#[rpc(name = "nft_constMetadata")]35	fn const_metadata(36		&self,37		collection: CollectionId,38		token: TokenId,39		at: Option<BlockHash>,40	) -> Result<Vec<u8>>;41	#[rpc(name = "nft_variableMetadata")]42	fn variable_metadata(43		&self,44		collection: CollectionId,45		token: TokenId,46		at: Option<BlockHash>,47	) -> Result<Vec<u8>>;4849	#[rpc(name = "nft_collectionTokens")]50	fn collection_tokens(&self, collection: CollectionId, at: Option<BlockHash>) -> Result<u32>;51	#[rpc(name = "nft_accountBalance")]52	fn account_balance(53		&self,54		collection: CollectionId,55		account: CrossAccountId,56		at: Option<BlockHash>,57	) -> Result<u32>;58	#[rpc(name = "nft_balance")]59	fn balance(60		&self,61		collection: CollectionId,62		account: CrossAccountId,63		token: TokenId,64		at: Option<BlockHash>,65	) -> Result<u128>;66	#[rpc(name = "nft_allowance")]67	fn allowance(68		&self,69		collection: CollectionId,70		sender: CrossAccountId,71		spender: CrossAccountId,72		token: TokenId,73		at: Option<BlockHash>,74	) -> Result<u128>;75}7677pub struct Nft<C, P> {78	client: Arc<C>,79	_marker: std::marker::PhantomData<P>,80}8182impl<C, P> Nft<C, P> {83	pub fn new(client: Arc<C>) -> Self {84		Self {85			client,86			_marker: Default::default(),87		}88	}89}9091pub enum Error {92	RuntimeError,93}9495impl From<Error> for i64 {96	fn from(e: Error) -> i64 {97		match e {98			Error::RuntimeError => 1,99		}100	}101}102103macro_rules! pass_method {104	($method_name:ident($($name:ident: $ty:ty),* $(,)?) -> $result:ty) => {105		fn $method_name(106			&self,107			$(108				$name: $ty,109			)*110			at: Option<<Block as BlockT>::Hash>,111		) -> Result<$result> {112			let api = self.client.runtime_api();113			let at = BlockId::hash(at.unwrap_or_else(|| self.client.info().best_hash));114115			api.$method_name(&at, $($name),*).map_err(|e| RpcError {116				code: ErrorCode::ServerError(Error::RuntimeError.into()),117				message: "Unable to query".into(),118				data: Some(format!("{:?}", e).into()),119			})120		}121	};122}123124impl<C, Block, CrossAccountId, AccountId> NftApi<<Block as BlockT>::Hash, CrossAccountId, AccountId>125	for Nft<C, Block>126where127	Block: BlockT,128	C: 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,129	C::Api: NftRuntimeApi<Block, CrossAccountId, AccountId>,130	CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,131{132	pass_method!(account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>);133	pass_method!(token_exists(collection: CollectionId, token: TokenId) -> bool);134	pass_method!(token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId);135	pass_method!(const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);136	pass_method!(variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>);137	pass_method!(collection_tokens(collection: CollectionId) -> u32);138	pass_method!(account_balance(collection: CollectionId, account: CrossAccountId) -> u32);139	pass_method!(balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128);140	pass_method!(allowance(collection: CollectionId, sender: CrossAccountId, spender: CrossAccountId, token: TokenId) -> u128);141}
modifiednode/rpc/Cargo.tomldiffbeforeafterboth
--- a/node/rpc/Cargo.toml
+++ b/node/rpc/Cargo.toml
@@ -13,7 +13,7 @@
 futures = { version = "0.3.17", features = ["compat"] }
 jsonrpc-core = "15.1.0"
 jsonrpc-pubsub = "15.1.0"
-# pallet-contracts-rpc = { version = "4.0.0-dev", git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.9' }
+# pallet-contracts-rpc = { version = "4.0.0-dev", git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.10' }
 pallet-transaction-payment-rpc = { version = "4.0.0-dev", git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.10' }
 pallet-transaction-payment-rpc-runtime-api = { version = "4.0.0-dev", git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.10' }
 sc-client-api = { version = "4.0.0-dev", git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.10' }
@@ -49,6 +49,8 @@
 fc-mapping-sync = { default-features = false, git = "https://github.com/uniquenetwork/frontier.git", branch = "unique-polkadot-v0.9.10" }
 
 pallet-nft = { path = "../../pallets/nft" }
+uc-rpc = { path = "../../client/rpc" }
+up-rpc = { path = "../../primitives/rpc" }
 nft-runtime = { path = "../../runtime" }
 
 [features]
modifiednode/rpc/src/lib.rsdiffbeforeafterboth
--- a/node/rpc/src/lib.rs
+++ b/node/rpc/src/lib.rs
@@ -1,4 +1,4 @@
-use nft_runtime::{Hash, AccountId, Index, opaque::Block, BlockNumber, Balance};
+use nft_runtime::{AccountId, Balance, BlockNumber, CrossAccountId, Hash, Index, opaque::Block};
 use fc_rpc::{OverrideHandle, RuntimeApiStorageOverride, SchemaV1Override, StorageOverride};
 use fc_rpc_core::types::{FilterPool, PendingTransactions};
 use jsonrpc_pubsub::manager::SubscriptionManager;
@@ -7,7 +7,6 @@
 	backend::{AuxStore, StorageProvider},
 	client::BlockchainEvents,
 };
-use pallet_nft::NftApi;
 use sc_finality_grandpa::{
 	FinalityProofProvider, GrandpaJustificationStream, SharedAuthoritySet, SharedVoterState,
 };
@@ -98,9 +97,10 @@
 where
 	Block: sp_api::BlockT,
 	C: ProvideRuntimeApi<Block>,
-	C::Api: pallet_nft::NftApi<Block>,
+	C::Api: up_rpc::NftApi<Block, CrossAccountId, AccountId>,
 {
 	fn code(&self, block: &sp_api::BlockId<Block>, account: sp_core::H160) -> Option<Vec<u8>> {
+		use up_rpc::NftApi;
 		self.client
 			.runtime_api()
 			.eth_contract_code(block, account)
@@ -124,7 +124,7 @@
 	// C::Api: pallet_contracts_rpc::ContractsRuntimeApi<Block, AccountId, Balance, BlockNumber, Hash>,
 	C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,
 	C::Api: fp_rpc::EthereumRuntimeRPCApi<Block>,
-	C::Api: pallet_nft::NftApi<Block>,
+	C::Api: up_rpc::NftApi<Block, CrossAccountId, AccountId>,
 	P: TransactionPool<Block = Block> + 'static,
 	SC: SelectChain<Block> + 'static,
 	B: sc_client_api::Backend<Block> + Send + Sync + 'static,
@@ -135,6 +135,7 @@
 		EthPubSubApiServer, EthSigner, HexEncodedIdProvider, NetApi, NetApiServer, Web3Api,
 		Web3ApiServer,
 	};
+	use uc_rpc::{NftApi, Nft};
 	// use pallet_contracts_rpc::{Contracts, ContractsApi};
 	use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};
 	use substrate_frame_rpc_system::{FullSystem, SystemApi};
@@ -196,6 +197,7 @@
 		is_authority,
 		max_past_logs,
 	)));
+	io.extend_with(NftApi::to_delegate(Nft::new(client.clone())));
 
 	if let Some(filter_pool) = filter_pool {
 		io.extend_with(EthFilterApiServer::to_delegate(EthFilterApi::new(
addedprimitives/rpc/Cargo.tomldiffbeforeafterboth
--- /dev/null
+++ b/primitives/rpc/Cargo.toml
@@ -0,0 +1,25 @@
+[package]
+name = "up-rpc"
+version = "0.1.0"
+edition = "2018"
+
+[dependencies]
+pallet-common = { default-features = false, path = '../../pallets/common' }
+nft-data-structs = { default-features = false, path = '../nft' }
+codec = { package = "parity-scale-codec", version = "2.0.0", default-features = false, features = ["derive"] }
+sp-core = { default-features = false, version = "4.0.0-dev", git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.10' }
+sp-std = { default-features = false, version = "4.0.0-dev", git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.10' }
+sp-api = { default-features = false, version = "4.0.0-dev", git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.10' }
+sp-runtime = { default-features = false, version = '4.0.0-dev', git = 'https://github.com/paritytech/substrate.git', branch = 'polkadot-v0.9.10' }
+
+[features]
+default = ["std"]
+std = [
+	"codec/std",
+	"sp-core/std",
+	"sp-std/std",
+	"sp-api/std",
+	"sp-runtime/std",
+	"pallet-common/std",
+	"nft-data-structs/std",
+]
addedprimitives/rpc/src/lib.rsdiffbeforeafterboth
--- /dev/null
+++ b/primitives/rpc/src/lib.rs
@@ -0,0 +1,31 @@
+#![cfg_attr(not(feature = "std"), no_std)]
+
+use nft_data_structs::{CollectionId, TokenId};
+use sp_std::vec::Vec;
+use sp_core::H160;
+
+sp_api::decl_runtime_apis! {
+	pub trait NftApi<CrossAccountId, AccountId> where
+		CrossAccountId: pallet_common::account::CrossAccountId<AccountId>,
+	{
+		fn account_tokens(collection: CollectionId, account: CrossAccountId) -> Vec<TokenId>;
+		fn token_exists(collection: CollectionId, token: TokenId) -> bool;
+
+		fn token_owner(collection: CollectionId, token: TokenId) -> CrossAccountId;
+		fn const_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>;
+		fn variable_metadata(collection: CollectionId, token: TokenId) -> Vec<u8>;
+
+		fn collection_tokens(collection: CollectionId) -> u32;
+		fn account_balance(collection: CollectionId, account: CrossAccountId) -> u32;
+		fn balance(collection: CollectionId, account: CrossAccountId, token: TokenId) -> u128;
+		fn allowance(
+			collection: CollectionId,
+			sender: CrossAccountId,
+			spender: CrossAccountId,
+			token: TokenId,
+		) -> u128;
+
+		/// Used for ethereum integration
+		fn eth_contract_code(account: H160) -> Option<Vec<u8>>;
+	}
+}