drk.rs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. use std::path::{Path, PathBuf};
  2. use serde_json::json;
  3. use drk::cli::{Config, DrkCli, DrkConfig};
  4. use drk::rpc::jsonrpc;
  5. use drk::rpc::jsonrpc::JsonResult;
  6. use drk::util::join_config_path;
  7. use drk::{Error, Result};
  8. use log::info;
  9. struct Drk {
  10. url: String,
  11. }
  12. impl Drk {
  13. pub fn new(url: String) -> Self {
  14. Self { url }
  15. }
  16. async fn request(&self, r: jsonrpc::JsonRequest) -> Result<()> {
  17. // TODO: Return actual JSON result
  18. let data = surf::Body::from_json(&r).unwrap();
  19. info!("--> {:?}", r);
  20. let mut req = surf::post(&self.url).body(data).await?;
  21. let resp = req.take_body();
  22. let json = resp.into_string().await.unwrap();
  23. let v: JsonResult = serde_json::from_str(&json).unwrap();
  24. match v {
  25. JsonResult::Resp(r) => {
  26. info!("<-- {:?}", r);
  27. return Ok(());
  28. }
  29. JsonResult::Err(e) => {
  30. info!("<-- {:?}", e);
  31. return Err(Error::JsonRpcError(e.error.message.to_string()));
  32. }
  33. };
  34. }
  35. pub async fn say_hello(&self) -> Result<()> {
  36. let r = jsonrpc::request(json!("say_hello"), json!([]));
  37. Ok(self.request(r).await?)
  38. }
  39. pub async fn create_cashier_wallet(&self) -> Result<()> {
  40. let r = jsonrpc::request(json!("create_cashier_wallet"), json!([]));
  41. Ok(self.request(r).await?)
  42. }
  43. pub async fn create_wallet(&self) -> Result<()> {
  44. let r = jsonrpc::request(json!("create_wallet"), json!([]));
  45. Ok(self.request(r).await?)
  46. }
  47. pub async fn key_gen(&self) -> Result<()> {
  48. let r = jsonrpc::request(json!("key_gen"), json!([]));
  49. Ok(self.request(r).await?)
  50. }
  51. pub async fn get_key(&self) -> Result<()> {
  52. let r = jsonrpc::request(json!("get_key"), json!([]));
  53. Ok(self.request(r).await?)
  54. }
  55. pub async fn get_info(&self) -> Result<()> {
  56. let r = jsonrpc::request(json!("get_info"), json!([]));
  57. Ok(self.request(r).await?)
  58. }
  59. pub async fn stop(&self) -> Result<()> {
  60. let r = jsonrpc::request(json!("stop"), json!([]));
  61. Ok(self.request(r).await?)
  62. }
  63. pub async fn deposit(&self) -> Result<()> {
  64. let r = jsonrpc::request(json!("deposit"), json!([]));
  65. Ok(self.request(r).await?)
  66. }
  67. pub async fn transfer(&self, address: String, amount: u64) -> Result<()> {
  68. let r = jsonrpc::request(json!("transfer"), json!([address, amount]));
  69. Ok(self.request(r).await?)
  70. }
  71. pub async fn withdraw(&self, address: String, amount: u64) -> Result<()> {
  72. let r = jsonrpc::request(json!("withdraw"), json!([address, amount]));
  73. Ok(self.request(r).await?)
  74. }
  75. }
  76. async fn start(config: &DrkConfig, options: DrkCli) -> Result<()> {
  77. let url = config.rpc_url.clone();
  78. let client = Drk::new(url);
  79. if options.cashier {
  80. client.create_cashier_wallet().await?;
  81. }
  82. if options.wallet {
  83. client.create_wallet().await?;
  84. }
  85. if options.key {
  86. client.key_gen().await?;
  87. }
  88. if options.get_key {
  89. client.get_key().await?;
  90. }
  91. if options.info {
  92. client.get_info().await?;
  93. }
  94. if options.hello {
  95. client.say_hello().await?;
  96. }
  97. if let Some(transfer) = options.transfer {
  98. client.transfer(transfer.pub_key, transfer.amount).await?;
  99. }
  100. if let Some(_deposit) = options.deposit {
  101. client.deposit().await?;
  102. }
  103. if let Some(withdraw) = options.withdraw {
  104. client.withdraw(withdraw.pub_key, withdraw.amount).await?;
  105. }
  106. if options.stop {
  107. client.stop().await?;
  108. }
  109. Ok(())
  110. }
  111. fn main() -> Result<()> {
  112. let options = DrkCli::load()?;
  113. let path = join_config_path(&PathBuf::from("drk.toml")).unwrap();
  114. let config: DrkConfig = if Path::new(&path).exists() {
  115. Config::<DrkConfig>::load(path)?
  116. } else {
  117. Config::<DrkConfig>::load_default(path)?
  118. };
  119. {
  120. use simplelog::*;
  121. let logger_config = ConfigBuilder::new().set_time_format_str("%T%.6f").build();
  122. let debug_level = if options.verbose {
  123. LevelFilter::Debug
  124. } else {
  125. LevelFilter::Off
  126. };
  127. let log_path = config.log_path.clone();
  128. CombinedLogger::init(vec![
  129. TermLogger::new(debug_level, logger_config, TerminalMode::Mixed).unwrap(),
  130. WriteLogger::new(
  131. LevelFilter::Debug,
  132. Config::default(),
  133. std::fs::File::create(log_path).unwrap(),
  134. ),
  135. ])
  136. .unwrap();
  137. }
  138. futures::executor::block_on(start(&config, options))
  139. }