utils.rs 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. use rust_decimal::Decimal;
  2. use tracing::trace;
  3. use exchanges::proxy;
  4. use crate::exchange::ExchangeEnum;
  5. /// 修改交易对连接符号
  6. /// - `symbol(str)`: 交易对, "BTC_USDT", 默认以下划线传递
  7. /// - `pat(str)`: 替换字符, "-", 把 “_” 替换为 "-"
  8. pub fn format_symbol(symbol: String, pat: &str) -> String {
  9. return symbol.to_uppercase().replace("_", pat);
  10. }
  11. // 检测是否走代理
  12. pub fn proxy_handle(is_unusual: Option<&str>) {
  13. if proxy::ParsingDetail::http_enable_proxy(is_unusual) {
  14. trace!("检测有代理配置,配置走代理");
  15. }
  16. }
  17. /// 币种映射器
  18. #[allow(dead_code)]
  19. pub fn symbol_enter_mapper(exchange_enum: ExchangeEnum, symbol: &str) -> String {
  20. let symbol_upper = symbol.to_uppercase();
  21. match exchange_enum {
  22. // ExchangeEnum::KucoinSwap => {
  23. // if symbol_upper.contains("BTC") {
  24. // symbol_upper.replace("BTC", "XBT")
  25. // } else { symbol_upper.to_string() }
  26. // }
  27. _ => {
  28. symbol_upper.to_string()
  29. }
  30. }
  31. }
  32. // 截取指定精度的decimal(不会四舍五入)
  33. pub fn truncate_decimal(amount: Decimal, precision: u32) -> Decimal {
  34. let scale = Decimal::new(10i64.pow(precision), 0);
  35. (amount * scale).trunc() / scale
  36. }
  37. // 获取指定精度下的tick_size
  38. pub fn get_tick_size(precision: u32) -> Decimal {
  39. return Decimal::new(1, 0) / Decimal::new(10i64.pow(precision), 0);
  40. }
  41. /// 币种映射器
  42. #[allow(dead_code)]
  43. pub fn symbol_out_mapper(exchange_enum: ExchangeEnum, symbol: &str) -> String {
  44. let symbol_upper = symbol.to_uppercase();
  45. match exchange_enum {
  46. // ExchangeEnum::KucoinSwap => {
  47. // if symbol_upper.contains("XBT") {
  48. // symbol_upper.replace("XBT", "BTC")
  49. // } else { symbol_upper.to_string() }
  50. // }
  51. _ => {
  52. symbol_upper.to_string()
  53. }
  54. }
  55. }