cli_util.rs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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::{io::Cursor, process::exit};
  19. use darkfi::{
  20. util::{async_util::sleep, parse::decode_base10},
  21. Result,
  22. };
  23. use darkfi_sdk::crypto::TokenId;
  24. use rodio::{source::Source, Decoder, OutputStream};
  25. use super::Drk;
  26. pub fn parse_value_pair(s: &str) -> Result<(u64, u64)> {
  27. let v: Vec<&str> = s.split(':').collect();
  28. if v.len() != 2 {
  29. eprintln!("Invalid value pair. Use a pair such as 13.37:11.0");
  30. exit(1);
  31. }
  32. // TODO: We shouldn't be hardcoding everything to 8 decimals.
  33. let val0 = decode_base10(v[0], 8, true);
  34. let val1 = decode_base10(v[1], 8, true);
  35. if val0.is_err() || val1.is_err() {
  36. eprintln!("Invalid value pair. Use a pair such as 13.37:11.0");
  37. exit(1);
  38. }
  39. Ok((val0.unwrap(), val1.unwrap()))
  40. }
  41. pub async fn parse_token_pair(drk: &Drk, s: &str) -> Result<(TokenId, TokenId)> {
  42. let v: Vec<&str> = s.split(':').collect();
  43. if v.len() != 2 {
  44. eprintln!("Invalid token pair. Use a pair such as:");
  45. eprintln!("WCKD:MLDY");
  46. eprintln!("or");
  47. eprintln!("A7f1RKsCUUHrSXA7a9ogmwg8p3bs6F47ggsW826HD4yd:FCuoMii64H5Ee4eVWBjP18WTFS8iLUJmGi16Qti1xFQ2");
  48. exit(1);
  49. }
  50. let tok0 = drk.get_token(v[0].to_string()).await;
  51. let tok1 = drk.get_token(v[1].to_string()).await;
  52. if tok0.is_err() || tok1.is_err() {
  53. eprintln!("Invalid token pair. Use a pair such as:");
  54. eprintln!("WCKD:MLDY");
  55. eprintln!("or");
  56. eprintln!("A7f1RKsCUUHrSXA7a9ogmwg8p3bs6F47ggsW826HD4yd:FCuoMii64H5Ee4eVWBjP18WTFS8iLUJmGi16Qti1xFQ2");
  57. exit(1);
  58. }
  59. Ok((tok0.unwrap(), tok1.unwrap()))
  60. }
  61. /// Fun police go away
  62. pub async fn kaching() {
  63. const WALLET_MP3: &[u8] = include_bytes!("../wallet.mp3");
  64. let cursor = Cursor::new(WALLET_MP3);
  65. let Ok((_stream, stream_handle)) = OutputStream::try_default() else { return };
  66. let Ok(source) = Decoder::new(cursor) else { return };
  67. if stream_handle.play_raw(source.convert_samples()).is_err() {
  68. return
  69. }
  70. sleep(2).await;
  71. }