cli_util.rs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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::{path::Path, process::exit};
  19. use async_std::{fs::File, io::WriteExt};
  20. use darkfi::{util::parse::decode_base10, Result};
  21. use darkfi_sdk::crypto::TokenId;
  22. pub fn parse_value_pair(s: &str) -> Result<(u64, u64)> {
  23. let v: Vec<&str> = s.split(':').collect();
  24. if v.len() != 2 {
  25. eprintln!("Invalid value pair. Use a pair such as 13.37:11.0");
  26. exit(1);
  27. }
  28. // TODO: We shouldn't be hardcoding everything to 8 decimals.
  29. let val0 = decode_base10(v[0], 8, true);
  30. let val1 = decode_base10(v[1], 8, true);
  31. if val0.is_err() || val1.is_err() {
  32. eprintln!("Invalid value pair. Use a pair such as 13.37:11.0");
  33. exit(1);
  34. }
  35. Ok((val0.unwrap(), val1.unwrap()))
  36. }
  37. pub fn parse_token_pair(s: &str) -> Result<(TokenId, TokenId)> {
  38. let v: Vec<&str> = s.split(':').collect();
  39. if v.len() != 2 {
  40. eprintln!("Invalid token pair. Use a pair such as:");
  41. eprintln!("A7f1RKsCUUHrSXA7a9ogmwg8p3bs6F47ggsW826HD4yd:FCuoMii64H5Ee4eVWBjP18WTFS8iLUJmGi16Qti1xFQ2");
  42. exit(1);
  43. }
  44. let tok0 = TokenId::try_from(v[0]);
  45. let tok1 = TokenId::try_from(v[1]);
  46. if tok0.is_err() || tok1.is_err() {
  47. eprintln!("Invalid token pair. Use a pair such as:");
  48. eprintln!("A7f1RKsCUUHrSXA7a9ogmwg8p3bs6F47ggsW826HD4yd:FCuoMii64H5Ee4eVWBjP18WTFS8iLUJmGi16Qti1xFQ2");
  49. exit(1);
  50. }
  51. Ok((tok0.unwrap(), tok1.unwrap()))
  52. }
  53. /// Fun police go away
  54. pub async fn kaching() -> Result<()> {
  55. #[cfg(feature = "play")]
  56. {
  57. const WALLET_MP3: &[u8] = include_bytes!("../wallet.mp3");
  58. const MP3_DROP: &str = "/tmp/wallet.mp3";
  59. if !Path::new(MP3_DROP).exists() {
  60. let mut f = File::create(MP3_DROP).await?;
  61. f.write_all(WALLET_MP3).await?;
  62. }
  63. if let Err(_) = play::play(MP3_DROP) {
  64. return Ok(())
  65. }
  66. }
  67. Ok(())
  68. }