main.rs 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  1. use std::process::exit;
  2. use clap::{IntoApp, Parser, Subcommand};
  3. use prettytable::{format, row, Table};
  4. use url::Url;
  5. use darkfi::{rpc::client::RpcClient, Result};
  6. mod rpc;
  7. #[derive(Subcommand)]
  8. pub enum CliDaoSubCommands {
  9. /// Create DAO
  10. Create {
  11. /// Minium number of governance tokens a user must have to propose a vote.
  12. dao_proposer_limit: u64,
  13. /// Minimum number of governance tokens staked on a proposal for it to pass.
  14. dao_quorum: u64,
  15. /// Quotient value of minimum vote ratio of yes:no votes required for a proposal to pass.
  16. dao_approval_ratio_quot: u64,
  17. /// Base value of minimum vote ratio of yes:no votes required for a proposal to pass.
  18. dao_approval_ratio_base: u64,
  19. },
  20. /// Mint tokens
  21. Addr {},
  22. GetVotes {},
  23. GetProposals {},
  24. Mint {
  25. /// Number of treasury tokens to mint.
  26. token_supply: u64,
  27. /// Public key of the DAO treasury.
  28. dao_addr: String,
  29. },
  30. UserBalance {
  31. nym: String,
  32. },
  33. DaoBalance {},
  34. DaoBulla {},
  35. Keygen {},
  36. /// Airdrop tokens
  37. Airdrop {
  38. nym: String,
  39. value: u64,
  40. },
  41. /// Propose
  42. Propose {
  43. sender: String,
  44. recipient: String,
  45. amount: u64,
  46. },
  47. /// Vote
  48. Vote {
  49. nym: String,
  50. vote: String,
  51. },
  52. /// Execute
  53. Exec {
  54. bulla: String,
  55. },
  56. }
  57. /// DAO cli
  58. #[derive(Parser)]
  59. #[clap(name = "dao")]
  60. #[clap(arg_required_else_help(true))]
  61. pub struct CliDao {
  62. /// Increase verbosity
  63. #[clap(short, parse(from_occurrences))]
  64. pub verbose: u8,
  65. #[clap(subcommand)]
  66. pub command: Option<CliDaoSubCommands>,
  67. }
  68. pub struct Rpc {
  69. client: RpcClient,
  70. }
  71. async fn start(options: CliDao) -> Result<()> {
  72. let rpc_addr = "tcp://127.0.0.1:7777";
  73. let client = Rpc { client: RpcClient::new(Url::parse(rpc_addr)?).await? };
  74. match options.command {
  75. Some(CliDaoSubCommands::Create {
  76. dao_proposer_limit,
  77. dao_quorum,
  78. dao_approval_ratio_base,
  79. dao_approval_ratio_quot,
  80. }) => {
  81. let reply = client
  82. .create(
  83. dao_proposer_limit,
  84. dao_quorum,
  85. dao_approval_ratio_quot,
  86. dao_approval_ratio_base,
  87. )
  88. .await?;
  89. println!("Created DAO bulla: {}", &reply.to_string());
  90. return Ok(())
  91. }
  92. Some(CliDaoSubCommands::Addr {}) => {
  93. let reply = client.addr().await?;
  94. println!("DAO public address: {}", &reply.to_string());
  95. return Ok(())
  96. }
  97. Some(CliDaoSubCommands::GetVotes {}) => {
  98. let reply = client.get_votes().await?;
  99. println!("{}", &reply.to_string());
  100. return Ok(())
  101. }
  102. Some(CliDaoSubCommands::GetProposals {}) => {
  103. let reply = client.get_proposals().await?;
  104. println!("{}", &reply.to_string());
  105. return Ok(())
  106. }
  107. Some(CliDaoSubCommands::Mint { token_supply, dao_addr }) => {
  108. let reply = client.mint(token_supply, dao_addr).await?;
  109. println!("{}", &reply.as_str().unwrap().to_string());
  110. return Ok(())
  111. }
  112. Some(CliDaoSubCommands::Keygen {}) => {
  113. let reply = client.keygen().await?;
  114. println!("User public key: {}", &reply.to_string());
  115. return Ok(())
  116. }
  117. Some(CliDaoSubCommands::Airdrop { nym, value }) => {
  118. let reply = client.airdrop(nym, value).await?;
  119. println!("{}", &reply.as_str().unwrap().to_string());
  120. return Ok(())
  121. }
  122. Some(CliDaoSubCommands::DaoBalance {}) => {
  123. let rep = client.dao_balance().await?;
  124. if !rep.is_object() {
  125. eprintln!("Invalid balance data received from darkfid RPC endpoint.");
  126. exit(1);
  127. }
  128. let mut table = Table::new();
  129. table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
  130. table.set_titles(row!["Token", "Balance"]);
  131. for i in rep.as_object().unwrap().keys() {
  132. if let Some(balance) = rep[i].as_u64() {
  133. table.add_row(row![i, balance]);
  134. continue
  135. }
  136. eprintln!("Found invalid balance data for key \"{}\"", i);
  137. }
  138. if table.is_empty() {
  139. println!("No balances.");
  140. } else {
  141. println!("{}", table);
  142. }
  143. // println!("DAO balance: {}", &reply.to_string());
  144. return Ok(())
  145. }
  146. Some(CliDaoSubCommands::DaoBulla {}) => {
  147. let reply = client.dao_bulla().await?;
  148. println!("DAO bulla: {}", &reply.to_string());
  149. return Ok(())
  150. }
  151. Some(CliDaoSubCommands::UserBalance { nym }) => {
  152. let rep = client.user_balance(nym).await?;
  153. if !rep.is_object() {
  154. eprintln!("Invalid balance data received from darkfid RPC endpoint.");
  155. exit(1);
  156. }
  157. let mut table = Table::new();
  158. table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
  159. table.set_titles(row!["Token", "Balance"]);
  160. for i in rep.as_object().unwrap().keys() {
  161. if let Some(balance) = rep[i].as_u64() {
  162. table.add_row(row![i, balance]);
  163. continue
  164. }
  165. eprintln!("Found invalid balance data for key \"{}\"", i);
  166. }
  167. if table.is_empty() {
  168. println!("No balances.");
  169. } else {
  170. println!("{}", table);
  171. }
  172. // println!("User balance: {}", &reply.to_string());
  173. return Ok(())
  174. }
  175. Some(CliDaoSubCommands::Propose { sender, recipient, amount }) => {
  176. let reply = client.propose(sender, recipient, amount).await?;
  177. println!("Proposal bulla: {}", &reply.to_string());
  178. return Ok(())
  179. }
  180. Some(CliDaoSubCommands::Vote { nym, vote }) => {
  181. let reply = client.vote(nym, vote).await?;
  182. println!("{}", &reply.to_string());
  183. return Ok(())
  184. }
  185. Some(CliDaoSubCommands::Exec { bulla }) => {
  186. let reply = client.exec(bulla).await?;
  187. println!("{}", &reply.to_string());
  188. return Ok(())
  189. }
  190. None => {}
  191. }
  192. Ok(())
  193. }
  194. #[async_std::main]
  195. async fn main() -> Result<()> {
  196. let args = CliDao::parse();
  197. let _matches = CliDao::command().get_matches();
  198. //let config_path = if args.config.is_some() {
  199. // expand_path(&args.config.clone().unwrap())?
  200. //} else {
  201. // join_config_path(&PathBuf::from("drk.toml"))?
  202. //};
  203. // Spawn config file if it's not in place already.
  204. //spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
  205. //let (lvl, conf) = log_config(matches)?;
  206. //TermLogger::init(lvl, conf, TerminalMode::Mixed, ColorChoice::Auto)?;
  207. //let config = Config::<DrkConfig>::load(config_path)?;
  208. start(args).await
  209. }