Skip to content

Commit

Permalink
Cli add locations (#227)
Browse files Browse the repository at this point in the history
* add-locations command and example filr for Leu

* fixes

* add decoded berlin locations

* add location

* fix along changes on master

* add signer argument

* revert unnecessary fix

* more verbose output for list-locations to get all info for js/apps who doesn't understand our fixed types
  • Loading branch information
brenzi authored Jun 26, 2022
1 parent e4d4f22 commit 359dbe0
Show file tree
Hide file tree
Showing 3 changed files with 193 additions and 1 deletion.
63 changes: 62 additions & 1 deletion client/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -477,6 +477,64 @@ fn main() {
Ok(())
}),
)
.add_cmd(
Command::new("add-locations")
.description("Register new locations for a community")
.options(|app| {
app.setting(AppSettings::ColoredHelp)
.signer_arg("account with necessary privileges (sudo or councillor)")
.arg(
Arg::with_name("specfile")
.takes_value(true)
.required(true)
.help("geojson file that specifies locations to add as points"),
)
})
.runner(|_args: &str, matches: &ArgMatches<'_>| {
// -----setup
let spec_file = matches.value_of("specfile").unwrap();
let spec = read_community_spec_from_file(spec_file);

let signer = matches.signer_arg()
.map_or_else(|| AccountKeyring::Alice.pair(), |signer| get_pair_from_str(signer).into());

let api = get_chain_api(matches).set_signer(signer);

let tx_payment_cid_arg = matches.tx_payment_cid_arg();

let cid = verify_cid(&api, matches.cid_arg().unwrap(), None);

let add_location_calls = spec.locations().into_iter().map(|l|
{
info!("adding location {:?}", l);
add_location_call(&api.metadata, cid, l)
}
).collect();
let add_location_batch_call = batch_call(&api.metadata, add_location_calls);

// return calls as `OpaqueCall`s to get the same return type in both branches
let add_location_batch_call = if contains_sudo_pallet(&api.metadata) {
let sudo_add_location_batch = sudo_call(&api.metadata, add_location_batch_call);
info!("Printing raw sudo calls for js/apps for cid: {}", cid);
print_raw_call("sudo(utility_batch(add_location))", &sudo_add_location_batch);
OpaqueCall::from_tuple(&sudo_add_location_batch)
} else {
let threshold = (get_councillors(&api).unwrap().len() / 2 + 1) as u32;
info!("Printing raw collective propose calls with threshold {} for js/apps for cid: {}", threshold, cid);
let propose_add_location_batch = collective_propose_call(&api.metadata, threshold, add_location_batch_call);
print_raw_call("collective_propose(utility_batch(add_location))", &propose_add_location_batch);
OpaqueCall::from_tuple(&propose_add_location_batch)
};
// ---- send xt's to chain
if api.get_current_phase().unwrap() != CeremonyPhaseType::Registering {
error!("Wrong ceremony phase for registering new locations for {}", cid);
error!("Aborting without registering additional locations");
std::process::exit(exit_code::WRONG_PHASE);
}
send_and_wait_for_in_block(&api, xt(&api, add_location_batch_call), tx_payment_cid_arg);
Ok(())
}),
)
.add_cmd(
Command::new("list-communities")
.description("list all registered communities")
Expand Down Expand Up @@ -510,7 +568,10 @@ fn main() {
println!("listing locations for cid {}", cid);
let loc = api.get_locations(cid).unwrap();
for l in loc.iter() {
println!("lat: {} lon: {}", l.lat, l.lon);
println!("lat: {} lon: {} (raw lat: {} lon: {})", l.lat, l.lon,
i128::decode(&mut l.lat.encode().as_slice()).unwrap(),
i128::decode(&mut l.lon.encode().as_slice()).unwrap()
);
}
Ok(())
}),
Expand Down
16 changes: 16 additions & 0 deletions client/test-data/decoded.berlin.locations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
13.36288869380951,
52.578908944510694
]
}
}
]
}
115 changes: 115 additions & 0 deletions client/test-data/leu.zuerich.locations.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
8.519076704978941,
47.39222307831016
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
8.519897460937498,
47.38699878926379
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
8.526830971240997,
47.38784547884539
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
8.510806113481522,
47.39043449402448
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
8.51675558835268,
47.386044260973456
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
8.519576266407967,
47.38859227954682
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
8.512996137142181,
47.389171553533316
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
8.523604273796082,
47.389670014610374
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
8.523376621305943,
47.38633890674662
]
}
},
{
"type": "Feature",
"properties": {},
"geometry": {
"type": "Point",
"coordinates": [
8.51304441690445,
47.39408059955919
]
}
}
]
}

0 comments on commit 359dbe0

Please sign in to comment.