main.rs 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2022 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use std::process::exit;
  19. use clap::{IntoApp, Parser, Subcommand};
  20. use prettytable::{format, row, Table};
  21. use url::Url;
  22. use darkfi::{rpc::client::RpcClient, Result};
  23. mod rpc;
  24. #[derive(Subcommand)]
  25. pub enum CliDaoSubCommands {
  26. /// Create DAO
  27. Create {
  28. /// Minium number of governance tokens a user must have to propose a vote.
  29. dao_proposer_limit: u64,
  30. /// Minimum number of governance tokens staked on a proposal for it to pass.
  31. dao_quorum: u64,
  32. /// Quotient value of minimum vote ratio of yes:no votes required for a proposal to pass.
  33. dao_approval_ratio_quot: u64,
  34. /// Base value of minimum vote ratio of yes:no votes required for a proposal to pass.
  35. dao_approval_ratio_base: u64,
  36. },
  37. /// Get DAO public address.
  38. Addr {},
  39. /// Get votes on current proposal in the form of [[true/false, user's GOV_tokens],[...]].
  40. GetVotes {},
  41. /// Get proposals in the form of [[destination, amount, token_id], [...]].
  42. GetProposals {},
  43. /// Mint tokens.
  44. Mint {
  45. /// Number of treasury tokens to mint.
  46. token_supply: u64,
  47. /// Public key of the DAO treasury.
  48. dao_addr: String,
  49. },
  50. /// Get user balance.
  51. UserBalance {
  52. /// User public address.
  53. addr: String,
  54. },
  55. /// Get DAO treasury balance.
  56. DaoBalance {},
  57. /// Get DAO bulla.
  58. DaoBulla {},
  59. /// Generate a new PublicKey.
  60. Keygen {},
  61. /// Airdrop tokens given recipient address and value.
  62. Airdrop {
  63. /// Airdrop recipient address.
  64. addr: String,
  65. /// Value to be airdropped.
  66. value: u64,
  67. },
  68. /// Create a Proposal.
  69. Propose {
  70. /// Sender PublicKey.
  71. sender: String,
  72. /// Recipient PublicKey.
  73. recipient: String,
  74. /// Amount of tokens to be sent.
  75. amount: u64,
  76. },
  77. /// Vote
  78. Vote {
  79. /// Voter's public address.
  80. addr: String,
  81. /// Vote value [yes/no].
  82. vote: String,
  83. },
  84. /// Execute proposal bulla.
  85. Exec {
  86. /// Bulla.
  87. bulla: String,
  88. },
  89. }
  90. /// DAO cli
  91. #[derive(Parser)]
  92. #[clap(name = "dao")]
  93. #[clap(arg_required_else_help(true))]
  94. pub struct CliDao {
  95. /// Increase verbosity
  96. #[clap(short, parse(from_occurrences))]
  97. pub verbose: u8,
  98. #[clap(subcommand)]
  99. pub command: Option<CliDaoSubCommands>,
  100. }
  101. pub struct Rpc {
  102. client: RpcClient,
  103. }
  104. async fn start(options: CliDao) -> Result<()> {
  105. let rpc_addr = "tcp://127.0.0.1:7777";
  106. let client = Rpc { client: RpcClient::new(Url::parse(rpc_addr)?).await? };
  107. match options.command {
  108. Some(CliDaoSubCommands::Create {
  109. dao_proposer_limit,
  110. dao_quorum,
  111. dao_approval_ratio_base,
  112. dao_approval_ratio_quot,
  113. }) => {
  114. let reply = client
  115. .create(
  116. dao_proposer_limit,
  117. dao_quorum,
  118. dao_approval_ratio_quot,
  119. dao_approval_ratio_base,
  120. )
  121. .await?;
  122. println!("Created DAO bulla: {}", &reply.to_string());
  123. return Ok(())
  124. }
  125. Some(CliDaoSubCommands::Addr {}) => {
  126. let reply = client.addr().await?;
  127. println!("DAO public address: {}", &reply.to_string());
  128. return Ok(())
  129. }
  130. Some(CliDaoSubCommands::GetVotes {}) => {
  131. let reply = client.get_votes().await?;
  132. println!("Votes on current proposals: {}", &reply.to_string());
  133. return Ok(())
  134. }
  135. Some(CliDaoSubCommands::GetProposals {}) => {
  136. let reply = client.get_proposals().await?;
  137. println!("Current proposals: {}", &reply.to_string());
  138. return Ok(())
  139. }
  140. Some(CliDaoSubCommands::Mint { token_supply, dao_addr }) => {
  141. let reply = client.mint(token_supply, dao_addr).await?;
  142. println!("{}", &reply.as_str().unwrap().to_string());
  143. return Ok(())
  144. }
  145. Some(CliDaoSubCommands::Keygen {}) => {
  146. let reply = client.keygen().await?;
  147. println!("User public key: {}", &reply.to_string());
  148. return Ok(())
  149. }
  150. Some(CliDaoSubCommands::Airdrop { addr, value }) => {
  151. println!("Requesting airdrop of {} GOV", value);
  152. let reply = client.airdrop(addr, value).await?;
  153. println!("{}", &reply.as_str().unwrap().to_string());
  154. return Ok(())
  155. }
  156. Some(CliDaoSubCommands::DaoBalance {}) => {
  157. let rep = client.dao_balance().await?;
  158. if !rep.is_object() {
  159. eprintln!("Invalid balance data received from darkfid RPC endpoint.");
  160. exit(1);
  161. }
  162. let mut table = Table::new();
  163. table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
  164. table.set_titles(row!["Token", "Balance"]);
  165. for i in rep.as_object().unwrap().keys() {
  166. if let Some(balance) = rep[i].as_u64() {
  167. table.add_row(row![i, balance]);
  168. continue
  169. }
  170. eprintln!("Found invalid balance data for key \"{}\"", i);
  171. }
  172. if table.is_empty() {
  173. println!("No balances.");
  174. } else {
  175. println!("{}", table);
  176. }
  177. // println!("DAO balance: {}", &reply.to_string());
  178. return Ok(())
  179. }
  180. Some(CliDaoSubCommands::DaoBulla {}) => {
  181. let reply = client.dao_bulla().await?;
  182. println!("DAO bulla: {}", &reply.to_string());
  183. return Ok(())
  184. }
  185. Some(CliDaoSubCommands::UserBalance { addr }) => {
  186. let rep = client.user_balance(addr).await?;
  187. if !rep.is_object() {
  188. eprintln!("Invalid balance data received from darkfid RPC endpoint.");
  189. exit(1);
  190. }
  191. let mut table = Table::new();
  192. table.set_format(*format::consts::FORMAT_NO_BORDER_LINE_SEPARATOR);
  193. table.set_titles(row!["Token", "Balance"]);
  194. for i in rep.as_object().unwrap().keys() {
  195. if let Some(balance) = rep[i].as_u64() {
  196. table.add_row(row![i, balance]);
  197. continue
  198. }
  199. eprintln!("Found invalid balance data for key \"{}\"", i);
  200. }
  201. if table.is_empty() {
  202. println!("No balances.");
  203. } else {
  204. println!("{}", table);
  205. }
  206. // println!("User balance: {}", &reply.to_string());
  207. return Ok(())
  208. }
  209. Some(CliDaoSubCommands::Propose { sender, recipient, amount }) => {
  210. let reply = client.propose(sender.clone(), recipient.clone(), amount).await?;
  211. println!(
  212. "Proposal bulla: {}\nSender: {}\nRecipient: {}\nAmount: {} DRK",
  213. &reply.to_string(),
  214. sender,
  215. recipient,
  216. amount
  217. );
  218. return Ok(())
  219. }
  220. Some(CliDaoSubCommands::Vote { addr, vote }) => {
  221. let reply = client.vote(addr, vote).await?;
  222. println!("{}", &reply.to_string());
  223. return Ok(())
  224. }
  225. Some(CliDaoSubCommands::Exec { bulla }) => {
  226. let reply = client.exec(bulla).await?;
  227. println!("{}", &reply.to_string());
  228. return Ok(())
  229. }
  230. None => {}
  231. }
  232. Ok(())
  233. }
  234. #[async_std::main]
  235. async fn main() -> Result<()> {
  236. let args = CliDao::parse();
  237. let _matches = CliDao::command().get_matches();
  238. //let config_path = if args.config.is_some() {
  239. // expand_path(&args.config.clone().unwrap())?
  240. //} else {
  241. // join_config_path(&PathBuf::from("drk.toml"))?
  242. //};
  243. // Spawn config file if it's not in place already.
  244. //spawn_config(&config_path, CONFIG_FILE_CONTENTS)?;
  245. //let (lvl, conf) = log_config(matches)?;
  246. //TermLogger::init(lvl, conf, TerminalMode::Mixed, ColorChoice::Auto)?;
  247. //let config = Config::<DrkConfig>::load(config_path)?;
  248. start(args).await
  249. }