Skip to content

Commit

Permalink
New rpc methods (#161)
Browse files Browse the repository at this point in the history
closes #157 
completes integration of `communities_getReputations` into CLI  (merged PR: #155 )
  • Loading branch information
brenzi authored Feb 13, 2022
1 parent 9b97254 commit 2d17f5e
Show file tree
Hide file tree
Showing 6 changed files with 134 additions and 17 deletions.
32 changes: 16 additions & 16 deletions Cargo.lock

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

8 changes: 8 additions & 0 deletions client/bot-community.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,14 @@ def init(ctx):
client.await_block()
specfile = random_community_spec(b, ipfs_cid, NUMBER_OF_LOCATIONS)
print(f'generated community spec: {specfile} first bootstrapper {b[0]}')

while True:
phase = client.get_phase()
if phase == 'REGISTERING':
break
print(f"waiting for ceremony phase REGISTERING. now is {phase}")
client.await_block()

cid = client.new_community(specfile)
print(f'created community with cid: {cid}')
write_cid(cid)
Expand Down
18 changes: 18 additions & 0 deletions client/src/cli_args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const IPFS_CID_ARG: &'static str = "ceremony-index";
const BOOTSTRAPPER_ARG: &'static str = "bootstrapper";
const FUNDEES_ARG: &'static str = "fundees";
const ENDORSEES_ARG: &'static str = "endorsees";
const ALL_FLAG: &'static str = "all";

pub trait EncointerArgs<'b> {
fn account_arg(self) -> Self;
Expand All @@ -20,6 +21,7 @@ pub trait EncointerArgs<'b> {
fn bootstrapper_arg(self) -> Self;
fn fundees_arg(self) -> Self;
fn endorsees_arg(self) -> Self;
fn all_flag(self) -> Self;
}

pub trait EncointerArgsExtractor {
Expand All @@ -32,6 +34,7 @@ pub trait EncointerArgsExtractor {
fn bootstrapper_arg(&self) -> Option<&str>;
fn fundees_arg(&self) -> Option<Vec<&str>>;
fn endorsees_arg(&self) -> Option<Vec<&str>>;
fn all_flag(&self) -> bool;
}

impl<'a, 'b> EncointerArgs<'b> for App<'a, 'b> {
Expand Down Expand Up @@ -135,6 +138,17 @@ impl<'a, 'b> EncointerArgs<'b> for App<'a, 'b> {
.help("Account(s) to be endorsed, ss58check encoded"),
)
}

fn all_flag(self) -> Self {
self.arg(
Arg::with_name(ALL_FLAG)
.short("a")
.long("all")
.takes_value(false)
.required(false)
.help("list all community currency balances for account"),
)
}
}

impl<'a> EncointerArgsExtractor for ArgMatches<'a> {
Expand Down Expand Up @@ -173,4 +187,8 @@ impl<'a> EncointerArgsExtractor for ArgMatches<'a> {
fn endorsees_arg(&self) -> Option<Vec<&str>> {
self.values_of(ENDORSEES_ARG).map(|v| v.collect())
}

fn all_flag(&self) -> bool {
self.is_present(ALL_FLAG)
}
}
82 changes: 82 additions & 0 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ fn main() {
.options(|app| {
app.setting(AppSettings::ColoredHelp)
.account_arg()
.all_flag()
})
.runner(|_args: &str, matches: &ArgMatches<'_>| {
let api = get_chain_api(matches);
Expand All @@ -226,6 +227,14 @@ fn main() {
println!("{}", balance);
}
None => {
if matches.all_flag() {
let community_balances = get_all_balances(&api, &accountid).unwrap();
let bn = get_block_number(&api);
for b in community_balances.iter() {
let dr = get_demurrage_per_block(&api, b.0);
println!("{}: {}", b.0, apply_demurrage(b.1, bn, dr))
}
}
let balance = if let Some(data) = api.get_account_data(&accountid).unwrap() {
data.free
} else {
Expand All @@ -237,6 +246,23 @@ fn main() {
Ok(())
}),
)
.add_cmd(
Command::new("reputation")
.description("List reputation history for an account")
.options(|app| {
app.setting(AppSettings::ColoredHelp)
.account_arg()})
.runner(move |_args: &str, matches: &ArgMatches<'_>| {
let api = get_chain_api(matches);
let account = matches.account_arg().unwrap();
let account_id = get_accountid_from_str(account);
let reputation = get_reputation_history(&api, &account_id);
// only print plain offerings to be able to parse them in python scripts
println!("{:?}", reputation);
Ok(())
}),
)

.add_cmd(
Command::new("transfer")
.description("transfer funds from one account to another. If --cid is supplied, send that community (amount is fixpoint). Otherwise send native ERT tokens (amount is integer)")
Expand Down Expand Up @@ -872,6 +898,22 @@ fn main() {
Ok(())
}),
)
.add_cmd(
Command::new("reputation")
.description("List reputation history for an account")
.options(|app| {
app.setting(AppSettings::ColoredHelp)
.account_arg()})
.runner(move |_args: &str, matches: &ArgMatches<'_>| {
let api = get_chain_api(matches);
let account = matches.account_arg().unwrap();
let account_id = get_accountid_from_str(account);
let reputation = get_reputation_history(&api, &account_id);
// only print plain offerings to be able to parse them in python scripts
println!("{:?}", reputation);
Ok(())
}),
)
.add_cmd(
Command::new("create-business")
.description("Register a community business on behalf of the account")
Expand Down Expand Up @@ -1346,6 +1388,46 @@ fn get_offerings_for_business(
Some(serde_json::from_str(&n).unwrap())
}

fn get_reputation_history(
api: &Api<sr25519::Pair, WsRpcClient>,
account_id: &AccountId,
) -> Option<Vec<(CommunityIdentifier, Reputation)>> {
let req = json!({
"method": "ceremonies_getReputations",
"params": vec![account_id],
"jsonrpc": "2.0",
"id": "1",
});

let n = api
.get_request(req.into())
.unwrap()
.expect("Could not query reputation history...");
Some(serde_json::from_str(&n).unwrap())
}

fn get_all_balances(
api: &Api<sr25519::Pair, WsRpcClient>,
account_id: &AccountId,
) -> Option<Vec<(CommunityIdentifier, BalanceEntry<BlockNumber>)>> {
let req = json!({
"method": "encointerBalances_getAllBalances",
"params": vec![account_id],
"jsonrpc": "2.0",
"id": "1",
});

let n = api.get_request(req.into()).unwrap().unwrap(); //expect("Could not query all balances...");

let balances: Vec<(CommunityIdentifier, Vec<u8>)> = serde_json::from_str(&n).unwrap();
Some(
balances
.iter()
.map(|b| (b.0, BalanceEntry::decode(&mut b.1.as_slice()).unwrap()))
.collect(),
)
}

fn prove_attendance(
prover: AccountId,
cid: CommunityIdentifier,
Expand Down
6 changes: 5 additions & 1 deletion node/src/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@

use std::sync::Arc;

use encointer_node_notee_runtime::{opaque::Block, AccountId, Balance, Index};
use encointer_node_notee_runtime::{opaque::Block, AccountId, Balance, BlockNumber, Index};
use pallet_encointer_balances_rpc::{Balances, BalancesApi};
use pallet_encointer_bazaar_rpc::{Bazaar, BazaarApi};
use pallet_encointer_ceremonies_rpc::{Ceremonies, CeremoniesApi};
pub use sc_rpc_api::DenyUnsafe;
Expand Down Expand Up @@ -43,6 +44,7 @@ where
C::Api: substrate_frame_rpc_system::AccountNonceApi<Block, AccountId, Index>,
C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi<Block, Balance>,
C::Api: BlockBuilder<Block>,
C::Api: pallet_encointer_balances_rpc_runtime_api::BalancesApi<Block, AccountId, BlockNumber>,
C::Api: pallet_encointer_ceremonies_rpc_runtime_api::CeremoniesApi<Block, AccountId>,
C::Api: pallet_encointer_communities_rpc_runtime_api::CommunitiesApi<Block>,
C::Api: pallet_encointer_bazaar_rpc_runtime_api::BazaarApi<Block, AccountId>,
Expand All @@ -61,6 +63,8 @@ where

io.extend_with(TransactionPaymentApi::to_delegate(TransactionPayment::new(client.clone())));

io.extend_with(BalancesApi::to_delegate(Balances::new(client.clone(), deny_unsafe)));

io.extend_with(BazaarApi::to_delegate(Bazaar::new(client.clone(), deny_unsafe)));

io.extend_with(CeremoniesApi::to_delegate(Ceremonies::new(client.clone(), deny_unsafe)));
Expand Down
5 changes: 5 additions & 0 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -602,6 +602,11 @@ impl_runtime_apis! {
}
}

impl pallet_encointer_balances_rpc_runtime_api::BalancesApi<Block, AccountId, BlockNumber> for Runtime {
fn get_all_balances(account: &AccountId) -> Vec<(CommunityIdentifier, BalanceEntry<BlockNumber>)> {
EncointerBalances::get_all_balances(account)
}
}

impl pallet_encointer_ceremonies_rpc_runtime_api::CeremoniesApi<Block, AccountId> for Runtime {
fn get_reputations() -> Vec<(CommunityCeremony, AccountId, Reputation)> {
Expand Down

0 comments on commit 2d17f5e

Please sign in to comment.