-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add RPC calls for getting treasury account and balance
Signed-off-by: lovesh <[email protected]>
- Loading branch information
Showing
11 changed files
with
178 additions
and
15 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains 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 |
---|---|---|
|
@@ -2,6 +2,7 @@ | |
members = [ | ||
'node', | ||
'pallets/poa', | ||
'pallets/poa/rpc', | ||
'pallets/token_migration', | ||
'runtime', | ||
] | ||
|
This file contains 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 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 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 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,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 = '..' } |
This file contains 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,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()), | ||
}) | ||
} | ||
} |
This file contains 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 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)] | ||
|
||
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; | ||
} | ||
} |
This file contains 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 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