From 2d17f5e056b35d3d8650e5d1dd62d3344d1a8af7 Mon Sep 17 00:00:00 2001 From: brenzi Date: Sun, 13 Feb 2022 21:50:31 +0100 Subject: [PATCH] New rpc methods (#161) closes #157 completes integration of `communities_getReputations` into CLI (merged PR: #155 ) --- Cargo.lock | 32 ++++++++-------- client/bot-community.py | 8 ++++ client/src/cli_args.rs | 18 +++++++++ client/src/main.rs | 82 +++++++++++++++++++++++++++++++++++++++++ node/src/rpc.rs | 6 ++- runtime/src/lib.rs | 5 +++ 6 files changed, 134 insertions(+), 17 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index c9654c87..db56b566 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1386,7 +1386,7 @@ dependencies = [ [[package]] name = "encointer-ceremonies-assignment" version = "0.1.0" -source = "git+https://github.com/encointer/pallets?branch=master#9b17ac528baeda7ce3f0370f4b1d51d2c38747d8" +source = "git+https://github.com/encointer/pallets?branch=master#b50b6c1b24b41d6e6a8e0862854c9d4339dbc2f2" dependencies = [ "encointer-primitives", "sp-runtime", @@ -1519,7 +1519,7 @@ dependencies = [ [[package]] name = "encointer-primitives" version = "0.8.0" -source = "git+https://github.com/encointer/pallets?branch=master#9b17ac528baeda7ce3f0370f4b1d51d2c38747d8" +source = "git+https://github.com/encointer/pallets?branch=master#b50b6c1b24b41d6e6a8e0862854c9d4339dbc2f2" dependencies = [ "bs58", "concat-arrays", @@ -1583,7 +1583,7 @@ checksum = "68b91989ae21441195d7d9b9993a2f9295c7e1a8c96255d8b729accddc124797" [[package]] name = "ep-core" version = "0.1.0" -source = "git+https://github.com/encointer/pallets?branch=master#9b17ac528baeda7ce3f0370f4b1d51d2c38747d8" +source = "git+https://github.com/encointer/pallets?branch=master#b50b6c1b24b41d6e6a8e0862854c9d4339dbc2f2" dependencies = [ "impl-serde", "parity-scale-codec", @@ -4150,7 +4150,7 @@ dependencies = [ [[package]] name = "pallet-encointer-balances" version = "0.8.0" -source = "git+https://github.com/encointer/pallets?branch=master#9b17ac528baeda7ce3f0370f4b1d51d2c38747d8" +source = "git+https://github.com/encointer/pallets?branch=master#b50b6c1b24b41d6e6a8e0862854c9d4339dbc2f2" dependencies = [ "encointer-primitives", "frame-support", @@ -4166,7 +4166,7 @@ dependencies = [ [[package]] name = "pallet-encointer-balances-rpc" version = "0.1.0" -source = "git+https://github.com/encointer/pallets?branch=master#9b17ac528baeda7ce3f0370f4b1d51d2c38747d8" +source = "git+https://github.com/encointer/pallets?branch=master#b50b6c1b24b41d6e6a8e0862854c9d4339dbc2f2" dependencies = [ "encointer-primitives", "jsonrpc-core", @@ -4185,7 +4185,7 @@ dependencies = [ [[package]] name = "pallet-encointer-balances-rpc-runtime-api" version = "0.1.0" -source = "git+https://github.com/encointer/pallets?branch=master#9b17ac528baeda7ce3f0370f4b1d51d2c38747d8" +source = "git+https://github.com/encointer/pallets?branch=master#b50b6c1b24b41d6e6a8e0862854c9d4339dbc2f2" dependencies = [ "encointer-primitives", "frame-support", @@ -4196,7 +4196,7 @@ dependencies = [ [[package]] name = "pallet-encointer-bazaar" version = "0.8.0" -source = "git+https://github.com/encointer/pallets?branch=master#9b17ac528baeda7ce3f0370f4b1d51d2c38747d8" +source = "git+https://github.com/encointer/pallets?branch=master#b50b6c1b24b41d6e6a8e0862854c9d4339dbc2f2" dependencies = [ "encointer-primitives", "frame-support", @@ -4211,7 +4211,7 @@ dependencies = [ [[package]] name = "pallet-encointer-bazaar-rpc" version = "0.1.0" -source = "git+https://github.com/encointer/pallets?branch=master#9b17ac528baeda7ce3f0370f4b1d51d2c38747d8" +source = "git+https://github.com/encointer/pallets?branch=master#b50b6c1b24b41d6e6a8e0862854c9d4339dbc2f2" dependencies = [ "encointer-primitives", "jsonrpc-core", @@ -4230,7 +4230,7 @@ dependencies = [ [[package]] name = "pallet-encointer-bazaar-rpc-runtime-api" version = "0.1.0" -source = "git+https://github.com/encointer/pallets?branch=master#9b17ac528baeda7ce3f0370f4b1d51d2c38747d8" +source = "git+https://github.com/encointer/pallets?branch=master#b50b6c1b24b41d6e6a8e0862854c9d4339dbc2f2" dependencies = [ "encointer-primitives", "frame-support", @@ -4241,7 +4241,7 @@ dependencies = [ [[package]] name = "pallet-encointer-ceremonies" version = "0.8.0" -source = "git+https://github.com/encointer/pallets?branch=master#9b17ac528baeda7ce3f0370f4b1d51d2c38747d8" +source = "git+https://github.com/encointer/pallets?branch=master#b50b6c1b24b41d6e6a8e0862854c9d4339dbc2f2" dependencies = [ "encointer-ceremonies-assignment", "encointer-primitives", @@ -4261,7 +4261,7 @@ dependencies = [ [[package]] name = "pallet-encointer-ceremonies-rpc" version = "0.1.0" -source = "git+https://github.com/encointer/pallets?branch=master#9b17ac528baeda7ce3f0370f4b1d51d2c38747d8" +source = "git+https://github.com/encointer/pallets?branch=master#b50b6c1b24b41d6e6a8e0862854c9d4339dbc2f2" dependencies = [ "encointer-primitives", "jsonrpc-core", @@ -4280,7 +4280,7 @@ dependencies = [ [[package]] name = "pallet-encointer-ceremonies-rpc-runtime-api" version = "0.1.0" -source = "git+https://github.com/encointer/pallets?branch=master#9b17ac528baeda7ce3f0370f4b1d51d2c38747d8" +source = "git+https://github.com/encointer/pallets?branch=master#b50b6c1b24b41d6e6a8e0862854c9d4339dbc2f2" dependencies = [ "encointer-primitives", "frame-support", @@ -4291,7 +4291,7 @@ dependencies = [ [[package]] name = "pallet-encointer-communities" version = "0.8.0" -source = "git+https://github.com/encointer/pallets?branch=master#9b17ac528baeda7ce3f0370f4b1d51d2c38747d8" +source = "git+https://github.com/encointer/pallets?branch=master#b50b6c1b24b41d6e6a8e0862854c9d4339dbc2f2" dependencies = [ "encointer-primitives", "frame-benchmarking", @@ -4309,7 +4309,7 @@ dependencies = [ [[package]] name = "pallet-encointer-communities-rpc" version = "0.1.0" -source = "git+https://github.com/encointer/pallets?branch=master#9b17ac528baeda7ce3f0370f4b1d51d2c38747d8" +source = "git+https://github.com/encointer/pallets?branch=master#b50b6c1b24b41d6e6a8e0862854c9d4339dbc2f2" dependencies = [ "encointer-primitives", "jsonrpc-core", @@ -4328,7 +4328,7 @@ dependencies = [ [[package]] name = "pallet-encointer-communities-rpc-runtime-api" version = "0.1.0" -source = "git+https://github.com/encointer/pallets?branch=master#9b17ac528baeda7ce3f0370f4b1d51d2c38747d8" +source = "git+https://github.com/encointer/pallets?branch=master#b50b6c1b24b41d6e6a8e0862854c9d4339dbc2f2" dependencies = [ "encointer-primitives", "sp-api", @@ -4338,7 +4338,7 @@ dependencies = [ [[package]] name = "pallet-encointer-scheduler" version = "0.8.0" -source = "git+https://github.com/encointer/pallets?branch=master#9b17ac528baeda7ce3f0370f4b1d51d2c38747d8" +source = "git+https://github.com/encointer/pallets?branch=master#b50b6c1b24b41d6e6a8e0862854c9d4339dbc2f2" dependencies = [ "encointer-primitives", "frame-support", diff --git a/client/bot-community.py b/client/bot-community.py index 21e7be1b..c8d243e0 100755 --- a/client/bot-community.py +++ b/client/bot-community.py @@ -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) diff --git a/client/src/cli_args.rs b/client/src/cli_args.rs index 4794a2b1..d271f156 100644 --- a/client/src/cli_args.rs +++ b/client/src/cli_args.rs @@ -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; @@ -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 { @@ -32,6 +34,7 @@ pub trait EncointerArgsExtractor { fn bootstrapper_arg(&self) -> Option<&str>; fn fundees_arg(&self) -> Option>; fn endorsees_arg(&self) -> Option>; + fn all_flag(&self) -> bool; } impl<'a, 'b> EncointerArgs<'b> for App<'a, 'b> { @@ -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> { @@ -173,4 +187,8 @@ impl<'a> EncointerArgsExtractor for ArgMatches<'a> { fn endorsees_arg(&self) -> Option> { self.values_of(ENDORSEES_ARG).map(|v| v.collect()) } + + fn all_flag(&self) -> bool { + self.is_present(ALL_FLAG) + } } diff --git a/client/src/main.rs b/client/src/main.rs index 4aea8291..926284a9 100644 --- a/client/src/main.rs +++ b/client/src/main.rs @@ -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); @@ -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 { @@ -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)") @@ -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") @@ -1346,6 +1388,46 @@ fn get_offerings_for_business( Some(serde_json::from_str(&n).unwrap()) } +fn get_reputation_history( + api: &Api, + account_id: &AccountId, +) -> Option> { + 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, + account_id: &AccountId, +) -> Option)>> { + 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)> = 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, diff --git a/node/src/rpc.rs b/node/src/rpc.rs index ac11cd94..a378878a 100644 --- a/node/src/rpc.rs +++ b/node/src/rpc.rs @@ -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; @@ -43,6 +44,7 @@ where C::Api: substrate_frame_rpc_system::AccountNonceApi, C::Api: pallet_transaction_payment_rpc::TransactionPaymentRuntimeApi, C::Api: BlockBuilder, + C::Api: pallet_encointer_balances_rpc_runtime_api::BalancesApi, C::Api: pallet_encointer_ceremonies_rpc_runtime_api::CeremoniesApi, C::Api: pallet_encointer_communities_rpc_runtime_api::CommunitiesApi, C::Api: pallet_encointer_bazaar_rpc_runtime_api::BazaarApi, @@ -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))); diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index 07d0d98e..860c5204 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -602,6 +602,11 @@ impl_runtime_apis! { } } + impl pallet_encointer_balances_rpc_runtime_api::BalancesApi for Runtime { + fn get_all_balances(account: &AccountId) -> Vec<(CommunityIdentifier, BalanceEntry)> { + EncointerBalances::get_all_balances(account) + } + } impl pallet_encointer_ceremonies_rpc_runtime_api::CeremoniesApi for Runtime { fn get_reputations() -> Vec<(CommunityCeremony, AccountId, Reputation)> {