| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241 |
- use std::process::exit;
- use clap::{IntoApp, Parser, Subcommand};
- use prettytable::{format, row, Table};
- use url::Url;
- use darkfi::{rpc::client::RpcClient, Result};
- mod rpc;
- #[derive(Subcommand)]
- pub enum CliDaoSubCommands {
- /// Create DAO
- Create {
- /// Minium number of governance tokens a user must have to propose a vote.
- dao_proposer_limit: u64,
- /// Minimum number of governance tokens staked on a proposal for it to pass.
- dao_quorum: u64,
- /// Quotient value of minimum vote ratio of yes:no votes required for a proposal to pass.
- dao_approval_ratio_quot: u64,
- /// Base value of minimum vote ratio of yes:no votes required for a proposal to pass.
- dao_approval_ratio_base: u64,
- },
- /// Mint tokens
- Addr {},
- GetVotes {},
- GetProposals {},
- Mint {
- /// Number of treasury tokens to mint.
- token_supply: u64,
- /// Public key of the DAO treasury.
- dao_addr: String,
- },
- UserBalance {
- nym: String,
- },
- DaoBalance {},
- DaoBulla {},
- Keygen {},
- /// Airdrop tokens
- Airdrop {
- nym: String,
- value: u64,
- },
- /// Propose
- Propose {
- sender: String,
- recipient: String,
- amount: u64,
- },
- /// Vote
- Vote {
- nym: String,
- vote: String,
- },
- /// Execute
- Exec {
- bulla: String,
- },
- }
- /// DAO cli
- #[derive(Parser)]
- #[clap(name = "dao")]
- #[clap(arg_required_else_help(true))]
- pub struct CliDao {
- /// Increase verbosity
- #[clap(short, parse(from_occurrences))]
- pub verbose: u8,
- #[clap(subcommand)]
- pub command: Option<CliDaoSubCommands>,
- }
- pub struct Rpc {
- client: RpcClient,
- }
- async fn start(options: CliDao) -> Result<()> {
- let rpc_addr = "tcp://127.0.0.1:7777";
- let client = Rpc { client: RpcClient::new(Url::parse(rpc_addr)?).await? };
- match options.command {
- Some(CliDaoSubCommands::Create {
- dao_proposer_limit,
- dao_quorum,
- dao_approval_ratio_base,
- dao_approval_ratio_quot,
- }) => {
- let reply = client
- .create(
- dao_proposer_limit,
- dao_quorum,
- dao_approval_ratio_quot,
- dao_approval_ratio_base,
- )
- .await?;
- println!("Created DAO bulla: {}", &reply.to_string());
- return Ok(())
- }
- Some(CliDaoSubCommands::Addr {}) => {
- let reply = client.addr().await?;
- println!("DAO public address: {}", &reply.to_string());
- return Ok(())
- }
- Some(CliDaoSubCommands::GetVotes {}) => {
- let reply = client.get_votes().await?;
- println!("{}", &reply.to_string());
- return Ok(())
- }
- Some(CliDaoSubCommands::GetProposals {}) => {
- let reply = client.get_proposals().await?;
- println!("{}", &reply.to_string());
- return Ok(())
- }
- Some(CliDaoSubCommands::Mint { token_supply, dao_addr }) => {
- let reply = client.mint(token_supply, dao_addr).await?;
- println!("{}", &reply.as_str().unwrap().to_string());
- return Ok(())
- }
- Some(CliDaoSubCommands::Keygen {}) => {
- let reply = client.keygen().await?;
- println!("User public key: {}", &reply.to_string());
- return Ok(())
- }
- Some(CliDaoSubCommands::Airdrop { nym, value }) => {
- let reply = client.airdrop(nym, value).await?;
- println!("{}", &reply.as_str().unwrap().to_string());
- return Ok(())
- }
- Some(CliDaoSubCommands::DaoBalance {}) => {
- let rep = client.dao_balance().await?;
- if !rep.is_object() {
- eprintln!("Invalid balance data received from darkfid RPC endpoint.");
- exit(1);
- }
- let mut table = Table::new();
- table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
- table.set_titles(row!["Token", "Balance"]);
- for i in rep.as_object().unwrap().keys() {
- if let Some(balance) = rep[i].as_u64() {
- table.add_row(row![i, balance]);
- continue
- }
- eprintln!("Found invalid balance data for key \"{}\"", i);
- }
- if table.is_empty() {
- println!("No balances.");
- } else {
- println!("{}", table);
- }
- // println!("DAO balance: {}", &reply.to_string());
- return Ok(())
- }
- Some(CliDaoSubCommands::DaoBulla {}) => {
- let reply = client.dao_bulla().await?;
- println!("DAO bulla: {}", &reply.to_string());
- return Ok(())
- }
- Some(CliDaoSubCommands::UserBalance { nym }) => {
- let rep = client.user_balance(nym).await?;
- if !rep.is_object() {
- eprintln!("Invalid balance data received from darkfid RPC endpoint.");
- exit(1);
- }
- let mut table = Table::new();
- table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
- table.set_titles(row!["Token", "Balance"]);
- for i in rep.as_object().unwrap().keys() {
- if let Some(balance) = rep[i].as_u64() {
- table.add_row(row![i, balance]);
- continue
- }
- eprintln!("Found invalid balance data for key \"{}\"", i);
- }
- if table.is_empty() {
- println!("No balances.");
- } else {
- println!("{}", table);
- }
- // println!("User balance: {}", &reply.to_string());
- return Ok(())
- }
- Some(CliDaoSubCommands::Propose { sender, recipient, amount }) => {
- let reply = client.propose(sender, recipient, amount).await?;
- println!("Proposal bulla: {}", &reply.to_string());
- return Ok(())
- }
- Some(CliDaoSubCommands::Vote { nym, vote }) => {
- let reply = client.vote(nym, vote).await?;
- println!("{}", &reply.to_string());
- return Ok(())
- }
- Some(CliDaoSubCommands::Exec { bulla }) => {
- let reply = client.exec(bulla).await?;
- println!("{}", &reply.to_string());
- return Ok(())
- }
- None => {}
- }
- Ok(())
- }
- #[async_std::main]
- async fn main() -> Result<()> {
- let args = CliDao::parse();
- let _matches = CliDao::command().get_matches();
- //let config_path = if args.config.is_some() {
- // expand_path(&args.config.clone().unwrap())?
- //} else {
- // join_config_path(&PathBuf::from("drk.toml"))?
- //};
- // Spawn config file if it's not in place already.
- //spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
- //let (lvl, conf) = log_config(matches)?;
- //TermLogger::init(lvl, conf, TerminalMode::Mixed, ColorChoice::Auto)?;
- //let config = Config::<DrkConfig>::load(config_path)?;
- start(args).await
- }
|