| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- use std::collections::BTreeMap;
- use std::str::FromStr;
- use async_trait::async_trait;
- use chrono::{FixedOffset, NaiveDateTime, TimeZone};
- use rust_decimal::Decimal;
- use rust_decimal::prelude::{FromPrimitive, ToPrimitive};
- use rust_decimal_macros::dec;
- use serde::{Deserialize, Serialize};
- use tracing::{error, warn};
- use exchanges::gate_swap_rest::GateSwapRest;
- use standard::utils;
- use crate::ExportConnector;
- pub struct GateSwapExport {
- request: GateSwapRest,
- }
- impl GateSwapExport {
- pub async fn new(is_colo: bool, params: BTreeMap<String, String>) -> GateSwapExport {
- GateSwapExport {
- request: GateSwapRest::new(is_colo, params.clone())
- }
- }
- }
- /// TradesSwap
- /// - `id`: i64, 成交记录 ID
- /// - `create_time`: i64, 成交时间
- /// - `contract`: String, 合约标识
- /// - `order_id`: String, 成交记录关联订单 ID
- /// - `size`: i64, 成交数量
- /// - `price`: String, 成交价格
- /// - `text`: String, 成交角色, taker - 吃单, maker - 做单
- /// - `fee`: String, 订单的自定义信息
- /// - `point_fee`: String, 成交手续费
- /// - `role`: String, 成交点卡手续费
- #[derive(Debug, Deserialize, Serialize)]
- struct TradesSwap {
- id: i64,
- create_time: f64,
- contract: String,
- order_id: String,
- size: i64,
- price: String,
- text: String,
- fee: String,
- point_fee: String,
- role: String,
- }
- #[async_trait]
- impl ExportConnector for GateSwapExport {
- async fn export_trades(&mut self, prefix_name: &str, symbol: String, start_time: i64, end_time: i64, limit: i64) -> String {
- let symbol_array: Vec<&str> = symbol.split("_").collect();
- let symbol_format = utils::format_symbol(symbol.clone(), "_");
- let limit_params = if limit > 1000 {
- warn!("查询条数最大为1000条,已修改为1000条!");
- 1000
- } else { limit };
- let res_data = self.request.my_trades(symbol_array[1].to_lowercase(), symbol_format, limit_params).await;
- if start_time > 0 || end_time > 0 { error!("该交易所不支持根据时间查询!") };
- if res_data.code == "200" {
- let trades_info: Vec<TradesSwap> = serde_json::from_str(&res_data.data).unwrap();
- let header_array = vec!["交易编号", "订单编号", "交易币对", "买卖方向", "成交价格", "成交数量", "成交价值", "交易费用", "成交角色", "成交时间"];
- let mut data_array: Vec<Vec<String>> = Vec::new();
- for (index, value) in trades_info.iter().enumerate() {
- if index >= data_array.len() {
- data_array.push(Vec::new());
- }
- let size = Decimal::from_i64(value.size).unwrap();
- let side = if size < Decimal::ZERO { "sell" } else { "buy" };
- let created_time = Decimal::from_f64(value.create_time).unwrap() * dec!(1000);
- let created_at = FixedOffset::east_opt(8 * 3600).unwrap().from_utc_datetime(&NaiveDateTime::from_timestamp_millis(created_time.to_i64().unwrap()).unwrap()).format("%Y-%m-%d %T").to_string();
- let trade_value = Decimal::from_str(&value.price).unwrap() * size.abs();
- data_array[index] = vec![
- value.id.to_string(),
- value.order_id.clone(),
- value.contract.clone(),
- side.to_string(),
- value.price.clone(),
- size.abs().to_string(),
- trade_value.to_string(),
- value.fee.clone(),
- value.role.clone(),
- created_at,
- ];
- }
- global::export_utils::export_excel(header_array, data_array, prefix_name)
- } else {
- res_data.to_string()
- }
- }
- }
|