Skip to content

Commit

Permalink
Add RPC calls for getting treasury account and balance
Browse files Browse the repository at this point in the history
Signed-off-by: lovesh <[email protected]>
  • Loading branch information
lovesh committed Sep 23, 2020
1 parent 0eb3994 commit d2bec19
Show file tree
Hide file tree
Showing 11 changed files with 178 additions and 15 deletions.
25 changes: 22 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
members = [
'node',
'pallets/poa',
'pallets/poa/rpc',
'pallets/token_migration',
'runtime',
]
Expand Down
9 changes: 7 additions & 2 deletions node/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ authors = ['Dock.io']
build = 'build.rs'
edition = '2018'
name = 'dock-node'
version = '0.3.0'
version = '0.4.0'

[package.metadata.docs.rs]
targets = ['x86_64-unknown-linux-gnu']
Expand All @@ -14,6 +14,7 @@ log = '0.4.8'
parking_lot = '0.10.0'
hex-literal = "0.3.0"
jsonrpc-core = '14.0.3'
jsonrpc-derive = '14.0.3'
serde = { version = '1.0.101', features = ['derive'] }
serde_json = '1.0.41'

Expand All @@ -23,7 +24,7 @@ optional = true

[dependencies.dock-runtime]
path = '../runtime'
version = '0.3.0'
version = '0.4.0'

[dependencies.sc-basic-authorship]
git = 'https://github.com/paritytech/substrate.git'
Expand Down Expand Up @@ -165,6 +166,10 @@ git = 'https://github.com/paritytech/substrate.git'
tag = 'v2.0.0-rc6'
version = '2.0.0-rc6'

[dependencies.poa_rpc]
path = '../pallets/poa/rpc'
version = '0.0.1'

[[bin]]
name = 'dock-node'
path = 'src/main.rs'
Expand Down
8 changes: 4 additions & 4 deletions node/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,10 +32,12 @@ where
C: Send + Sync + 'static,
C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,
C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,
C::Api: poa_rpc::PoARuntimeApi<Block, AccountId, Balance>,
C::Api: BlockBuilder<Block>,
P: TransactionPool + 'static,
{
use pallet_transaction_payment_rpc::{TransactionPayment, TransactionPaymentApi};
use poa_rpc::{PoA, PoAApi};
use substrate_frame_rpc_system::{FullSystem, SystemApi};

let mut io = jsonrpc_core::IoHandler::default();
Expand All @@ -55,10 +57,8 @@ where
client.clone(),
)));

// Extend this RPC with a custom API by using the following syntax.
// `YourRpcStruct` should have a reference to a client, which is needed
// to call into the runtime.
// `io.extend_with(YourRpcTrait::to_delegate(YourRpcStruct::new(ReferenceToClient, ...)));`
// RPC calls for PoA pallet
io.extend_with(PoAApi::to_delegate(PoA::new(client.clone())));

io
}
13 changes: 11 additions & 2 deletions pallets/poa/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
[package]
name = "poa"
version = "0.2.0"
version = "0.3.0"
authors = ["Dock.io"]
edition = "2018"
license = 'Apache-2.0'

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

Expand Down Expand Up @@ -66,12 +67,18 @@ default-features = false
tag = 'v2.0.0-rc6'
version = '2.0.0-rc6'

[dev-dependencies.sp-runtime]
[dependencies.sp-runtime]
default-features = false
git = 'https://github.com/paritytech/substrate.git'
tag = 'v2.0.0-rc6'
version = '2.0.0-rc6'

[dependencies.sp-api]
git = 'https://github.com/paritytech/substrate.git'
default-features = false
tag = 'v2.0.0-rc6'
version = '2.0.0-rc6'

[dev-dependencies.balances]
git = 'https://github.com/paritytech/substrate.git'
default-features = false
Expand All @@ -86,6 +93,8 @@ std = [
'frame-support/std',
'frame-system/std',
'sp-std/std',
'sp-runtime/std',
'sp-api/std',
'serde',
'pallet-session/std',
'pallet-authorship/std',
Expand Down
21 changes: 21 additions & 0 deletions pallets/poa/rpc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
[package]
name = "poa_rpc"
version = "0.0.1"
authors = ["Dock.io"]
edition = "2018"
license = 'Apache-2.0'

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

[dependencies]
codec = { package = "parity-scale-codec", version = "1.3.1" }
jsonrpc-core = "14.0.3"
jsonrpc-core-client = "14.0.3"
jsonrpc-derive = "14.0.3"
serde = { version = "1.0.101", features = ["derive"], optional = true }
sp-rpc = { git = 'https://github.com/paritytech/substrate.git', tag = 'v2.0.0-rc6', version = '2.0.0-rc6' }
sp-runtime = { git = 'https://github.com/paritytech/substrate.git', tag = 'v2.0.0-rc6', version = '2.0.0-rc6' }
sp-api = { git = 'https://github.com/paritytech/substrate.git', tag = 'v2.0.0-rc6', version = '2.0.0-rc6' }
sp-blockchain = { git = 'https://github.com/paritytech/substrate.git', tag = 'v2.0.0-rc6', version = '2.0.0-rc6' }
poa = { version = '0.3.0', path = '..' }
76 changes: 76 additions & 0 deletions pallets/poa/rpc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
pub use self::gen_client::Client as PoAClient;
use codec::Codec;
use jsonrpc_core::{Error as RpcError, ErrorCode, Result};
use jsonrpc_derive::rpc;
pub use poa::runtime_api::PoAApi as PoARuntimeApi;
use sp_api::ProvideRuntimeApi;
use sp_blockchain::HeaderBackend;
use sp_runtime::{
generic::BlockId,
traits::{Block as BlockT, MaybeDisplay, MaybeFromStr},
};
use std::sync::Arc;

#[rpc]
pub trait PoAApi<BlockHash, AccountId, Balance> {
/// Return account address of treasury. The account address can then be used to query the
/// chain for balance
#[rpc(name = "poa_treasuryAccount")]
fn treasury_account(&self, at: Option<BlockHash>) -> Result<AccountId>;

/// Return free balance of treasury account. In the context of PoA, only free balance makes
/// sense for treasury. But just in case, to check all kinds of balance (locked, reserved, etc),
/// get the account address with above call and query the chain.
#[rpc(name = "poa_treasuryBalance")]
fn treasury_balance(&self, at: Option<BlockHash>) -> Result<Balance>;
}

/// A struct that implements the [`PoAApi`].
pub struct PoA<C, P> {
client: Arc<C>,
_marker: std::marker::PhantomData<P>,
}

impl<C, P> PoA<C, P> {
/// Create new `PoA` with the given reference to the client.
pub fn new(client: Arc<C>) -> Self {
PoA {
client,
_marker: Default::default(),
}
}
}

impl<C, Block, AccountId, Balance> PoAApi<<Block as BlockT>::Hash, AccountId, Balance>
for PoA<C, Block>
where
Block: BlockT,
C: Send + Sync + 'static + ProvideRuntimeApi<Block> + HeaderBackend<Block>,
C::Api: PoARuntimeApi<Block, AccountId, Balance>,
AccountId: Codec + MaybeDisplay + MaybeFromStr,
Balance: Codec + MaybeDisplay + MaybeFromStr,
{
fn treasury_account(&self, at: Option<<Block as BlockT>::Hash>) -> Result<AccountId> {
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));
api.get_treasury_account(&at).map_err(|e| RpcError {
code: ErrorCode::ServerError(1),
message: "Unable to query treasury account address.".into(),
data: Some(format!("{:?}", e).into()),
})
}

fn treasury_balance(&self, at: Option<<Block as BlockT>::Hash>) -> Result<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));
api.get_treasury_balance(&at).map_err(|e| RpcError {
code: ErrorCode::ServerError(2),
message: "Unable to query treasury account balance.".into(),
data: Some(format!("{:?}", e).into()),
})
}
}
4 changes: 3 additions & 1 deletion pallets/poa/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ extern crate alloc;

use alloc::collections::{BTreeMap, BTreeSet};

pub mod runtime_api;

// TODO: Remove all print statements and panics before releasing for mainnet

type EpochNo = u32;
Expand Down Expand Up @@ -610,7 +612,7 @@ impl<T: Trait> Module<T> {
TREASURY_ID.into_account()
}

/// Treasury's balance
/// Treasury's free balance. Only free balance makes sense for treasury in context of PoA
pub fn treasury_balance() -> BalanceOf<T> {
T::Currency::free_balance(&Self::treasury_account())
}
Expand Down
20 changes: 20 additions & 0 deletions pallets/poa/src/runtime_api.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
#![cfg_attr(not(feature = "std"), no_std)]

use codec::Codec;
use sp_runtime::traits::{MaybeDisplay, MaybeFromStr};

sp_api::decl_runtime_apis! {
pub trait PoAApi<AccountId, Balance> where
AccountId: Codec + MaybeDisplay + MaybeFromStr,
Balance: Codec + MaybeDisplay + MaybeFromStr, {

/// Return account address of treasury. The account address can then be used to query the
/// chain for balance
fn get_treasury_account() -> AccountId;

/// Return free balance of treasury account. In the context of PoA, only free balance makes
/// sense for treasury. But just in case, to check all kinds of balance (locked, reserved, etc),
/// get the account address with above call and query the chain.
fn get_treasury_balance() -> Balance;
}
}
4 changes: 2 additions & 2 deletions runtime/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
authors = ['Dock.io']
edition = '2018'
name = 'dock-runtime'
version = '0.3.0'
version = '0.4.0'
license = 'Apache-2.0'

[package.metadata.docs.rs]
Expand Down Expand Up @@ -219,7 +219,7 @@ optional = true
[dependencies.poa]
default-features = false
path = '../pallets/poa'
version = '0.2.0'
version = '0.3.0'

[dependencies.token_migration]
default-features = false
Expand Down
12 changes: 11 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
spec_name: create_runtime_str!("dock-testnet"),
impl_name: create_runtime_str!("dock-runtime"),
authoring_version: 1,
spec_version: 7,
spec_version: 8,
impl_version: 1,
transaction_version: 1,
apis: RUNTIME_API_VERSIONS,
Expand Down Expand Up @@ -530,6 +530,16 @@ impl_runtime_apis! {
}
}

impl poa::runtime_api::PoAApi<Block, AccountId, Balance> for Runtime {
fn get_treasury_account() -> AccountId {
PoAModule::treasury_account()
}

fn get_treasury_balance() -> Balance {
PoAModule::treasury_balance()
}
}

#[cfg(feature = "runtime-benchmarks")]
impl frame_benchmarking::Benchmark<Block> for Runtime {
fn dispatch_benchmark(
Expand Down

0 comments on commit d2bec19

Please sign in to comment.