Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

v8.0.1 #297

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "nomic"
version = "8.0.0"
version = "8.0.1"
authors = ["The Turbofish Team <[email protected]>"]
edition = "2021"
default-run = "nomic"
Expand Down
112 changes: 110 additions & 2 deletions src/bin/nomic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ use orga::plugins::MIN_FEE;
use orga::prelude::*;
use orga::{client::AppClient, tendermint::client::HttpClient};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::convert::TryInto;
use std::fs::Permissions;
use std::os::unix::fs::PermissionsExt;
Expand Down Expand Up @@ -88,6 +89,7 @@ pub enum Command {
Redelegate(RedelegateCmd),
Unjail(UnjailCmd),
Edit(EditCmd),
Vote(VoteCmd),
Claim(ClaimCmd),
Airdrop(AirdropCmd),
ClaimAirdrop(ClaimAirdropCmd),
Expand Down Expand Up @@ -151,6 +153,7 @@ impl Command {
Redelegate(cmd) => cmd.run().await,
Unjail(cmd) => cmd.run().await,
Edit(cmd) => cmd.run().await,
Vote(cmd) => cmd.run().await,
Claim(cmd) => cmd.run().await,
ClaimAirdrop(cmd) => cmd.run().await,
Airdrop(cmd) => cmd.run().await,
Expand Down Expand Up @@ -852,6 +855,7 @@ pub struct DeclareInfo {
pub website: String,
pub identity: String,
pub details: String,
pub proposals: Option<HashMap<String, bool>>,
}

impl DeclareCmd {
Expand All @@ -866,6 +870,7 @@ impl DeclareCmd {
website: self.website.clone(),
identity: self.identity.clone(),
details: self.details.clone(),
proposals: None,
};
let info_json = serde_json::to_string(&info)
.map_err(|_| orga::Error::App("invalid json".to_string()))?;
Expand Down Expand Up @@ -923,11 +928,27 @@ pub struct EditCmd {

impl EditCmd {
async fn run(&self) -> Result<()> {
let wallet = wallet();
let client = self.config.client();

let addr = wallet.address()?.unwrap();
let vals = client.query(|app| app.staking.all_validators()).await?;
let val = vals.iter().find(|val| Address::from(val.address) == addr);

let existing_info: Option<DeclareInfo> = val
.map(|v| {
serde_json::from_slice(v.info.as_slice())
.map_err(|err| orga::Error::App(format!("invalid json: {}", err)))
})
.transpose()?;

// TODO: auto-populate from existing values
let info = DeclareInfo {
moniker: self.moniker.clone(),
website: self.website.clone(),
identity: self.identity.clone(),
details: self.details.clone(),
proposals: existing_info.and_then(|i| i.proposals),
};
let info_json = serde_json::to_string(&info)
.map_err(|_| orga::Error::App("invalid json".to_string()))?;
Expand All @@ -936,7 +957,7 @@ impl EditCmd {
Ok(self
.config
.client()
.with_wallet(wallet())
.with_wallet(wallet)
.call(
|app| build_call!(app.accounts.take_as_funding(MIN_FEE.into())),
|app| {
Expand All @@ -951,6 +972,93 @@ impl EditCmd {
}
}

#[derive(clap::ArgEnum, Debug, Clone, PartialEq, Eq)]
pub enum Vote {
Yes,
No,
None,
}

impl FromStr for Vote {
type Err = nomic::error::Error;

fn from_str(s: &str) -> Result<Self> {
match s.to_lowercase().as_str() {
"yes" => Ok(Vote::Yes),
"no" => Ok(Vote::No),
"none" => Ok(Vote::None),
_ => Err(orga::Error::App("invalid vote".to_string()).into()),
}
}
}

#[derive(Parser, Debug)]
pub struct VoteCmd {
proposal: String,
vote: Vote,

#[clap(flatten)]
config: nomic::network::Config,
}

impl VoteCmd {
async fn run(&self) -> Result<()> {
if self.proposal.len() != 64 {
return Err(orga::Error::App("invalid proposal".to_string()).into());
}

let wallet = wallet();
let client = self.config.client();

let addr = wallet.address()?.unwrap();
let vals = client.query(|app| app.staking.all_validators()).await?;
let val = vals
.iter()
.find(|val| Address::from(val.address) == addr)
.ok_or_else(|| orga::Error::App("validator not found".to_string()))?;

let mut info: DeclareInfo = serde_json::from_slice(val.info.as_slice())
.map_err(|err| orga::Error::App(format!("invalid json: {}", err)))?;

info.proposals = Some(info.proposals.unwrap_or_default());
let props = info.proposals.as_mut().unwrap();
if self.vote == Vote::Yes {
props.insert(self.proposal.clone(), true);
} else if self.vote == Vote::No {
props.insert(self.proposal.clone(), false);
} else {
props.remove(&self.proposal);
}

let info_json = serde_json::to_string(&info)
.map_err(|err| orga::Error::App(format!("failed to serialize to json: {}", err)))?;
let info_bytes = info_json.as_bytes().to_vec();

log::info!(
"Submitting updated vote map... {}",
serde_json::to_string_pretty(&info.proposals).unwrap()
);

client
.with_wallet(wallet)
.call(
|app| build_call!(app.accounts.take_as_funding(MIN_FEE.into())),
|app| {
build_call!(app.staking.edit_validator_self(
val.commission.rate,
val.min_self_delegation,
info_bytes.clone().try_into().unwrap()
))
},
)
.await?;

log::info!("Vote submitted.");

Ok(())
}
}

#[derive(Parser, Debug)]
pub struct UnbondCmd {
validator_addr: Address,
Expand Down Expand Up @@ -1592,7 +1700,7 @@ impl UpgradeStatusCmd {
async fn run(&self) -> Result<()> {
use orga::coins::staking::ValidatorQueryInfo;
use orga::coins::VersionedAddress;
use std::collections::{HashMap, HashSet};
use std::collections::HashSet;
let client = self.config.client();
let tm_client =
tendermint_rpc::HttpClient::new(self.config.node.as_ref().unwrap().as_str()).unwrap();
Expand Down
Loading