Skip to content

Commit

Permalink
bump substrate and api-client (#225)
Browse files Browse the repository at this point in the history
* bump substrate fe5bf49290d166b9552f65e751d46ec592173ebd

* bump rust-toolchain: nightly-2022-05-31

* unpatch encointer pallets

* WIP merge updates

* runtime compiles

* node compiles

* [client] use master branch of api-client

* add scale-codec to api-client-extension

* [client] define community currency `AssetTip`

* [client] compiles

* updated api-client for hotfix; bootstrapping script works

* remove patches

* remove ac_primitives dependency

* fmt

* fix asset type

* Rename `AssetTip` to `CommunityCurrencyTip`

* change api-client branch to master
  • Loading branch information
clangenb authored Jun 2, 2022
1 parent 87adbc9 commit 0f2c62e
Show file tree
Hide file tree
Showing 18 changed files with 1,718 additions and 1,043 deletions.
2,227 changes: 1,326 additions & 901 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ pallet-encointer-communities = { git = "https://github.com/encointer/pallets", b
pallet-encointer-scheduler = { git = "https://github.com/encointer/pallets", branch = "master" }

# scs deps
substrate-api-client = { git = "https://github.com/scs/substrate-api-client", branch = "cl/encointer-community-currency-fees-hotfix"}
substrate-client-keystore = { git = "https://github.com/scs/substrate-api-client", branch = "cl/encointer-community-currency-fees-hotfix"}
substrate-api-client = { git = "https://github.com/scs/substrate-api-client", branch = "master" }
substrate-client-keystore = { git = "https://github.com/scs/substrate-api-client", branch = "master" }

# substrate deps
frame-system = { version = "4.0.0-dev", git = "https://github.com/paritytech/substrate.git", branch = "master" }
Expand Down
5 changes: 3 additions & 2 deletions client/encointer-api-client-extension/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.8.2"
edition = "2021"

[dependencies]
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["derive"] }
log = "0.4.14"
serde_json = { version = "1.0.79"}
serde = { features = ["derive"], version = "1.0.132" }
Expand All @@ -13,8 +14,8 @@ encointer-primitives = { git = "https://github.com/encointer/pallets", branch =
encointer-ceremonies-assignment = { git = "https://github.com/encointer/pallets", branch = "master" }

# scs deps
substrate-api-client = { git = "https://github.com/scs/substrate-api-client", branch = "cl/encointer-community-currency-fees-hotfix"}
substrate-client-keystore = { git = "https://github.com/scs/substrate-api-client", branch = "cl/encointer-community-currency-fees-hotfix"}
substrate-api-client = { git = "https://github.com/scs/substrate-api-client", branch = "master"}
substrate-client-keystore = { git = "https://github.com/scs/substrate-api-client", branch = "master"}

# substrate deps
sp-core = { version = "6.0.0", git = "https://github.com/paritytech/substrate.git", branch = "master" }
51 changes: 51 additions & 0 deletions client/encointer-api-client-extension/src/extrinsic_params.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
use codec::{Decode, Encode};
use encointer_primitives::communities::CommunityIdentifier;
use substrate_api_client::{
BaseExtrinsicParams, BaseExtrinsicParamsBuilder, SubstrateDefaultSignedExtra,
UncheckedExtrinsicV4,
};

/// A struct representing the signed extra and additional parameters required
/// to construct a transaction and pay in asset fees
pub type CommunityCurrencyTipExtrinsicParams = BaseExtrinsicParams<CommunityCurrencyTip>;
/// A builder which leads to [`CommunityCurrencyTipExtrinsicParams`] being constructed.
/// This is what you provide to methods like `sign_and_submit()`.
pub type CommunityCurrencyTipExtrinsicParamsBuilder =
BaseExtrinsicParamsBuilder<CommunityCurrencyTip>;

pub type EncointerXt<Call> =
UncheckedExtrinsicV4<Call, SubstrateDefaultSignedExtra<CommunityCurrencyTip>>;

/// A tip payment made in the form of a specific asset.
#[derive(Copy, Clone, Debug, Default, Decode, Encode, Eq, PartialEq)]
pub struct CommunityCurrencyTip {
#[codec(compact)]
tip: u128,
asset: Option<CommunityIdentifier>,
}

impl CommunityCurrencyTip {
/// Create a new tip of the amount provided.
pub fn new(amount: u128) -> Self {
CommunityCurrencyTip { tip: amount, asset: None }
}

/// Designate the tip as being of a particular asset class.
/// If this is not set, then the native currency is used.
pub fn of_community(mut self, asset: CommunityIdentifier) -> Self {
self.asset = Some(asset);
self
}
}

impl From<u128> for CommunityCurrencyTip {
fn from(n: u128) -> Self {
CommunityCurrencyTip::new(n)
}
}

impl From<CommunityCurrencyTip> for u128 {
fn from(tip: CommunityCurrencyTip) -> Self {
tip.tip
}
}
8 changes: 7 additions & 1 deletion client/encointer-api-client-extension/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ use substrate_api_client::rpc::WsRpcClient;

pub use substrate_api_client::{ApiClientError, ApiResult as Result};

pub type Api = substrate_api_client::Api<sr25519::Pair, WsRpcClient>;
pub type Api = substrate_api_client::Api<
sr25519::Pair,
WsRpcClient,
extrinsic_params::CommunityCurrencyTipExtrinsicParams,
>;

pub use ceremonies::*;
pub use communities::*;
pub use extrinsic_params::*;
pub use scheduler::*;

mod ceremonies;
mod communities;
mod extrinsic_params;
mod scheduler;
85 changes: 33 additions & 52 deletions client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use clap_nested::{Command, Commander};
use cli_args::{EncointerArgs, EncointerArgsExtractor};
use codec::{Compact, Decode, Encode};
use encointer_api_client_extension::{
CeremoniesApi, CommunitiesApi, SchedulerApi, ENCOINTER_CEREMONIES,
Api, CeremoniesApi, CommunitiesApi, EncointerXt, SchedulerApi, ENCOINTER_CEREMONIES,
};
use encointer_node_notee_runtime::{
AccountId, BalanceEntry, BalanceType, BlockNumber, Event, Hash, Header, Moment, Signature,
Expand Down Expand Up @@ -67,8 +67,8 @@ use std::{
};
use substrate_api_client::{
compose_call, compose_extrinsic, compose_extrinsic_offline, rpc::WsRpcClient,
utils::FromHexString, Api, ApiClientError, ApiResult, GenericAddress, Metadata,
UncheckedExtrinsicV4, XtStatus,
utils::FromHexString, ApiClientError, ApiResult, ExtrinsicParams, GenericAddress, Metadata,
XtStatus,
};
use substrate_client_keystore::{KeystoreExt, LocalKeystore};

Expand Down Expand Up @@ -188,15 +188,10 @@ fn main() {
GenericAddress::Id(to.clone()),
Compact(amount)
);
let xt: UncheckedExtrinsicV4<_> = compose_extrinsic_offline!(
let xt: EncointerXt<_> = compose_extrinsic_offline!(
api.clone().signer.unwrap(),
call.clone(),
nonce,
Era::Immortal,
api.genesis_hash,
api.genesis_hash,
api.runtime_version.spec_version,
api.runtime_version.transaction_version
api.extrinsic_params(nonce)
);
ensure_payment(&api, &xt.hex_encode());
// send and watch extrinsic until finalized
Expand Down Expand Up @@ -293,7 +288,7 @@ fn main() {
let cid = verify_cid(&_api, cid_str);
let amount = BalanceType::from_str(matches.value_of("amount").unwrap())
.expect("amount can be converted to fixpoint");
let xt: UncheckedExtrinsicV4<_> = compose_extrinsic!(
let xt: EncointerXt<_> = compose_extrinsic!(
_api.clone(),
"EncointerBalances",
"transfer",
Expand Down Expand Up @@ -711,7 +706,7 @@ fn main() {
std::process::exit(exit_code::WRONG_PHASE);
}
let _api = api.clone().set_signer(sr25519_core::Pair::from(signer.clone()));
let xt: UncheckedExtrinsicV4<_> = compose_extrinsic!(
let xt: EncointerXt<_> = compose_extrinsic!(
_api.clone(),
"EncointerCeremonies",
"register_participant",
Expand Down Expand Up @@ -816,7 +811,7 @@ fn main() {
info!("send attest_claims by {}", who.public());

let api = get_chain_api(matches).set_signer(who.clone().into());
let xt: UncheckedExtrinsicV4<_> = compose_extrinsic!(
let xt: EncointerXt<_> = compose_extrinsic!(
api.clone(),
"EncointerCeremonies",
"attest_claims",
Expand Down Expand Up @@ -880,7 +875,7 @@ fn main() {
let signer = matches.signer_arg().map(get_pair_from_str).unwrap();
let api = api.set_signer(signer.clone().into());

let xt: UncheckedExtrinsicV4<_> = compose_extrinsic!(
let xt: EncointerXt<_> = compose_extrinsic!(
api.clone(),
ENCOINTER_CEREMONIES,
"claim_rewards",
Expand Down Expand Up @@ -1051,7 +1046,7 @@ fn main() {
batch_call.clone()
);
info!("raw sudo batch call to sign with js/apps {}: 0x{}", cid, hex::encode(unsigned_sudo_call.encode()));
let xt: UncheckedExtrinsicV4<_> = compose_extrinsic!(
let xt: EncointerXt<_> = compose_extrinsic!(
api,
"Sudo",
"sudo",
Expand Down Expand Up @@ -1108,18 +1103,18 @@ fn main() {
.run();
}

fn get_chain_api(matches: &ArgMatches<'_>) -> Api<sr25519::Pair, WsRpcClient> {
fn get_chain_api(matches: &ArgMatches<'_>) -> Api {
let url = format!(
"{}:{}",
matches.value_of("node-url").unwrap(),
matches.value_of("node-port").unwrap()
);
debug!("connecting to {}", url);
let client = WsRpcClient::new(&url);
Api::<sr25519::Pair, _>::new(client).unwrap()
Api::new(client).unwrap()
}

fn reasonable_native_balance(api: &Api<sr25519::Pair, WsRpcClient>) -> u128 {
fn reasonable_native_balance(api: &Api) -> u128 {
let xt = api.balance_transfer(GenericAddress::Id(AccountKeyring::Alice.into()), 9999);
let fee = api
.get_fee_details(&xt.hex_encode(), None)
Expand Down Expand Up @@ -1251,14 +1246,14 @@ fn listen(matches: &ArgMatches<'_>) {
/// Extracts api and cid from `matches` and execute the given `closure` with them.
fn extract_and_execute<T>(
matches: &ArgMatches<'_>,
closure: impl FnOnce(Api<sr25519::Pair, WsRpcClient>, CommunityIdentifier) -> T,
closure: impl FnOnce(Api, CommunityIdentifier) -> T,
) -> T {
let api = get_chain_api(matches);
let cid = verify_cid(&api, matches.cid_arg().expect("please supply argument --cid"));
closure(api, cid)
}

fn verify_cid(api: &Api<sr25519::Pair, WsRpcClient>, cid: &str) -> CommunityIdentifier {
fn verify_cid(api: &Api, cid: &str) -> CommunityIdentifier {
let cids = get_community_identifiers(&api).expect("no community registered");
let cid = CommunityIdentifier::from_str(cid).unwrap();
if !cids.contains(&cid) {
Expand All @@ -1267,17 +1262,14 @@ fn verify_cid(api: &Api<sr25519::Pair, WsRpcClient>, cid: &str) -> CommunityIden
cid
}

fn get_block_number(api: &Api<sr25519::Pair, WsRpcClient>) -> BlockNumber {
fn get_block_number(api: &Api) -> BlockNumber {
let hdr: Header = api.get_header(None).unwrap().unwrap();
debug!("decoded: {:?}", hdr);
//let hdr: Header= Decode::decode(&mut .as_bytes()).unwrap();
hdr.number
}

fn get_demurrage_per_block(
api: &Api<sr25519::Pair, WsRpcClient>,
cid: CommunityIdentifier,
) -> Demurrage {
fn get_demurrage_per_block(api: &Api, cid: CommunityIdentifier) -> Demurrage {
let d: Option<Demurrage> = api
.get_storage_map("EncointerBalances", "DemurragePerBlock", cid, None)
.unwrap();
Expand All @@ -1295,24 +1287,21 @@ fn get_demurrage_per_block(
}
}

fn get_ceremony_index(api: &Api<sr25519::Pair, WsRpcClient>) -> CeremonyIndexType {
fn get_ceremony_index(api: &Api) -> CeremonyIndexType {
api.get_storage_value("EncointerScheduler", "CurrentCeremonyIndex", None)
.unwrap()
.unwrap()
}

fn get_attestee_count(
api: &Api<sr25519::Pair, WsRpcClient>,
key: CommunityCeremony,
) -> ParticipantIndexType {
fn get_attestee_count(api: &Api, key: CommunityCeremony) -> ParticipantIndexType {
api.get_storage_map("EncointerCeremonies", "AttestationCount", key, None)
.unwrap()
.or(Some(0))
.unwrap()
}

fn get_attestees(
api: &Api<sr25519::Pair, WsRpcClient>,
api: &Api,
key: CommunityCeremony,
windex: ParticipantIndexType,
) -> Option<Vec<AccountId>> {
Expand All @@ -1321,7 +1310,7 @@ fn get_attestees(
}

fn get_participant_attestation_index(
api: &Api<sr25519::Pair, WsRpcClient>,
api: &Api,
key: CommunityCeremony,
accountid: &AccountId,
) -> Option<ParticipantIndexType> {
Expand All @@ -1330,7 +1319,7 @@ fn get_participant_attestation_index(
}

fn new_claim_for(
api: &Api<sr25519::Pair, WsRpcClient>,
api: &Api,
claimant: &sr25519::Pair,
cid: CommunityIdentifier,
n_participants: u32,
Expand Down Expand Up @@ -1368,15 +1357,13 @@ fn new_claim_for(
claim.encode()
}

fn get_community_identifiers(
api: &Api<sr25519::Pair, WsRpcClient>,
) -> Option<Vec<CommunityIdentifier>> {
fn get_community_identifiers(api: &Api) -> Option<Vec<CommunityIdentifier>> {
api.get_storage_value("EncointerCommunities", "CommunityIdentifiers", None)
.unwrap()
}

/// This rpc needs to have offchain indexing enabled in the node.
fn get_cid_names(api: &Api<sr25519::Pair, WsRpcClient>) -> Option<Vec<CidName>> {
fn get_cid_names(api: &Api) -> Option<Vec<CidName>> {
let req = json!({
"method": "encointer_getAllCommunities",
"params": [],
Expand All @@ -1390,10 +1377,7 @@ fn get_cid_names(api: &Api<sr25519::Pair, WsRpcClient>) -> Option<Vec<CidName>>
Some(serde_json::from_str(&n).unwrap())
}

fn get_businesses(
api: &Api<sr25519::Pair, WsRpcClient>,
cid: CommunityIdentifier,
) -> Option<Vec<BusinessData>> {
fn get_businesses(api: &Api, cid: CommunityIdentifier) -> Option<Vec<BusinessData>> {
let req = json!({
"method": "encointer_bazaarGetBusinesses",
"params": vec![cid],
Expand All @@ -1405,10 +1389,7 @@ fn get_businesses(
Some(serde_json::from_str(&n).unwrap())
}

fn get_offerings(
api: &Api<sr25519::Pair, WsRpcClient>,
cid: CommunityIdentifier,
) -> Option<Vec<OfferingData>> {
fn get_offerings(api: &Api, cid: CommunityIdentifier) -> Option<Vec<OfferingData>> {
let req = json!({
"method": "encointer_bazaarGetOfferings",
"params": vec![cid],
Expand All @@ -1424,7 +1405,7 @@ fn get_offerings(
}

fn get_offerings_for_business(
api: &Api<sr25519::Pair, WsRpcClient>,
api: &Api,
cid: CommunityIdentifier,
account_id: AccountId,
) -> Option<Vec<OfferingData>> {
Expand All @@ -1445,7 +1426,7 @@ fn get_offerings_for_business(
}

fn get_reputation_history(
api: &Api<sr25519::Pair, WsRpcClient>,
api: &Api,
account_id: &AccountId,
) -> Option<Vec<(CeremonyIndexType, CommunityReputation)>> {
let req = json!({
Expand All @@ -1463,7 +1444,7 @@ fn get_reputation_history(
}

fn get_all_balances(
api: &Api<sr25519::Pair, WsRpcClient>,
api: &Api,
account_id: &AccountId,
) -> Option<Vec<(CommunityIdentifier, BalanceEntry<BlockNumber>)>> {
let req = json!({
Expand Down Expand Up @@ -1501,7 +1482,7 @@ fn prove_attendance(
}

fn get_reputation(
api: &Api<sr25519::Pair, WsRpcClient>,
api: &Api,
prover: &AccountId,
cid: CommunityIdentifier,
cindex: CeremonyIndexType,
Expand Down Expand Up @@ -1542,7 +1523,7 @@ fn send_bazaar_xt(matches: &ArgMatches<'_>, business_call: &BazaarCalls) -> Resu
let cid = verify_cid(&api, matches.cid_arg().expect("please supply argument --cid"));
let ipfs_cid = matches.ipfs_cid_arg().expect("ipfs cid needed");

let xt: UncheckedExtrinsicV4<_> = compose_extrinsic!(
let xt: EncointerXt<_> = compose_extrinsic!(
api.clone(),
"EncointerBazaar",
&business_call.to_string(),
Expand All @@ -1557,7 +1538,7 @@ fn send_bazaar_xt(matches: &ArgMatches<'_>, business_call: &BazaarCalls) -> Resu
}

fn endorse_newcomers(
api: &mut Api<sr25519::Pair, WsRpcClient>,
api: &mut Api,
cid: CommunityIdentifier,
matches: &ArgMatches<'_>,
) -> Result<(), ApiClientError> {
Expand Down Expand Up @@ -1594,7 +1575,7 @@ struct BootstrapperWithTickets {
}

fn get_bootstrappers_with_remaining_newbie_tickets(
api: &Api<sr25519::Pair, WsRpcClient>,
api: &Api,
cid: CommunityIdentifier,
) -> Result<Vec<BootstrapperWithTickets>, ApiClientError> {
let total_newbie_tickets: u8 = api
Expand Down
Loading

0 comments on commit 0f2c62e

Please sign in to comment.