| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713 |
- use std::collections::{BTreeMap};
- use std::io::{Error, ErrorKind};
- use std::str::FromStr;
- use tokio::sync::mpsc::Sender;
- use async_trait::async_trait;
- use futures::stream::FuturesUnordered;
- use futures::TryStreamExt;
- use rust_decimal::Decimal;
- use rust_decimal_macros::dec;
- use serde_json::{json, Value};
- use tokio::spawn;
- use tokio::time::Instant;
- use tracing::{error, info, trace, warn};
- use exchanges::phemex_swap_rest::PhemexSwapRest;
- use exchanges::response_base::ResponseData;
- use crate::{Platform, ExchangeEnum, Account, Position, Ticker, Market, Order, OrderCommand, PositionModeEnum, utils};
- use global::trace_stack::TraceStack;
- #[allow(dead_code)]
- #[derive(Clone)]
- pub struct PhemexSwap {
- exchange: ExchangeEnum,
- symbol: String,
- is_colo: bool,
- params: BTreeMap<String, String>,
- request: PhemexSwapRest,
- market: Market,
- order_sender: Sender<Order>,
- error_sender: Sender<Error>,
- }
- impl PhemexSwap {
- pub async fn new(symbol: String, is_colo: bool, params: BTreeMap<String, String>, order_sender: Sender<Order>, error_sender: Sender<Error>) -> PhemexSwap {
- let market = Market::new();
- let mut phemex_swap = PhemexSwap {
- exchange: ExchangeEnum::PhemexSwap,
- symbol: symbol.to_uppercase(),
- is_colo,
- params: params.clone(),
- request: PhemexSwapRest::new(is_colo, params.clone()),
- market,
- order_sender,
- error_sender,
- };
- // 修改持仓模式
- let mode_result = phemex_swap.set_dual_mode("", false).await;
- match mode_result {
- Ok(_) => {
- trace!("Phemex:设置持仓模式成功!")
- }
- Err(error) => {
- error!("Phemex:设置持仓模式失败!mode_result={}", error)
- }
- }
- // 获取市场信息
- phemex_swap.market = PhemexSwap::get_market(&mut phemex_swap).await.unwrap_or(phemex_swap.market);
- // phemex_swap.market = Market::new();
- // 设置持仓杠杆
- let lever_rate_result = phemex_swap.set_dual_leverage("10").await;
- match lever_rate_result {
- Ok(ok) => {
- info!("Phemex:设置持仓杠杆成功!{:?}", ok);
- }
- Err(error) => {
- error!("Phemex:设置持仓杠杆失败!{:?}", error)
- }
- }
- return phemex_swap;
- }
- }
- #[async_trait]
- impl Platform for PhemexSwap {
- // 克隆方法
- fn clone_box(&self) -> Box<dyn Platform + Send + Sync> { Box::new(self.clone()) }
- // 获取交易所模式
- fn get_self_exchange(&self) -> ExchangeEnum {
- ExchangeEnum::PhemexSwap
- }
- // 获取交易对
- fn get_self_symbol(&self) -> String { self.symbol.clone() }
- // 获取是否使用高速通道
- fn get_self_is_colo(&self) -> bool {
- self.is_colo
- }
- // 获取params信息
- fn get_self_params(&self) -> BTreeMap<String, String> {
- self.params.clone()
- }
- // 获取market信息
- fn get_self_market(&self) -> Market { self.market.clone() }
- // 获取请求时间
- fn get_request_delays(&self) -> Vec<i64> { self.request.get_delays() }
- // 获取请求平均时间
- fn get_request_avg_delay(&self) -> Decimal { self.request.get_avg_delay() }
- // 获取请求最大时间
- fn get_request_max_delay(&self) -> i64 { self.request.get_max_delay() }
- // 获取服务器时间
- async fn get_server_time(&mut self) -> Result<String, Error> {
- let res_data = self.request.get_server().await;
- if res_data.code == 200 {
- let res_data_json: Value = res_data.data;
- let result = res_data_json["serverTime"].to_string();
- Ok(result)
- } else {
- Err(Error::new(ErrorKind::Other, res_data.to_string()))
- }
- }
- // 获取账号信息
- async fn get_account(&mut self) -> Result<Account, Error> {
- let params = json!({"currency": "USDT"});
- let response = self.request.get_account_and_positions(params).await;
- if response.code != 200 {
- return Err(Error::new(ErrorKind::NotFound, format!("phemex_swap 获取仓位异常{:?}", response).to_string()));
- }
- let account_info = response.data["account"].clone();
- let coin = account_info["currency"].as_str().unwrap().to_string();
- let available_balance = Decimal::from_str(account_info["accountBalanceRv"].as_str().unwrap()).unwrap();
- let frozen_balance = Decimal::from_str(account_info["totalUsedBalanceRv"].as_str().unwrap()).unwrap();
- let balance = available_balance + frozen_balance;
- let result = Account {
- coin,
- balance,
- available_balance,
- frozen_balance,
- stocks: Decimal::ZERO,
- available_stocks: Decimal::ZERO,
- frozen_stocks: Decimal::ZERO,
- };
- Ok(result)
- }
- async fn get_spot_account(&mut self) -> Result<Vec<Account>, Error> {
- Err(Error::new(ErrorKind::NotFound, "phemex_swap:该方法暂未实现".to_string()))
- }
- // 获取持仓信息
- async fn get_position(&mut self) -> Result<Vec<Position>, Error> {
- let symbol_format = utils::format_symbol(self.symbol.clone(), "").replace("1000", "u1000");
- let ct_val = self.market.ct_val;
- let params = json!({
- "symbol":symbol_format,
- "currency": "USDT"
- });
- let response = self.request.get_account_and_positions(params).await;
- if response.code != 200 {
- return Err(Error::new(ErrorKind::NotFound, format!("phemex_swap 获取仓位异常{:?}", response).to_string()));
- }
- let res_data_json = response.data["positions"].clone();
- let positions_info = res_data_json.as_array().unwrap();
- let result = positions_info.iter().map(|item| { format_position_item(item, ct_val) }).collect();
- Ok(result)
- }
- // 获取所有持仓
- async fn get_positions(&mut self) -> Result<Vec<Position>, Error> {
- let params = json!({
- "currency": "USDT"
- });
- let response = self.request.get_account_and_positions(params).await;
- if response.code != 200 {
- return Err(Error::new(ErrorKind::NotFound, format!("phemex_swap 获取仓位异常{:?}", response).to_string()));
- }
- let res_data_json = response.data["positions"].clone();
- let positions_info = res_data_json.as_array().unwrap();
- let result = positions_info.iter().map(|item| { format_position_item(item, Decimal::ONE) }).collect();
- Ok(result)
- }
- // 获取市场行情
- async fn get_ticker(&mut self) -> Result<Ticker, Error> {
- self.get_ticker_symbol(self.symbol.clone()).await
- }
- async fn get_ticker_symbol(&mut self, symbol: String) -> Result<Ticker, Error> {
- let symbol_format = utils::format_symbol(symbol.clone(), "").replace("1000", "u1000");
- let params = json!({
- "symbol":symbol_format
- });
- let response = self.request.get_ticker(params).await;
- if response.code != 200 {
- return Err(Error::new(ErrorKind::NotFound, format!("phemex_swap 获取市场行情异常{:?}", response).to_string()));
- }
- let ticker_info = response.data;
- let result = Ticker {
- time: ticker_info["timestamp"].as_i64().unwrap() / 1_000_000,
- high: Decimal::from_str(ticker_info["highRp"].as_str().unwrap()).unwrap(),
- low: Decimal::from_str(ticker_info["lowRp"].as_str().unwrap()).unwrap(),
- sell: Decimal::from_str(ticker_info["askRp"].as_str().unwrap()).unwrap(),
- buy: Decimal::from_str(ticker_info["bidRp"].as_str().unwrap()).unwrap(),
- last: Decimal::from_str(ticker_info["lastRp"].as_str().unwrap()).unwrap(),
- volume: Decimal::from_str(ticker_info["volumeRq"].as_str().unwrap()).unwrap(),
- };
- Ok(result)
- }
- async fn get_market(&mut self) -> Result<Market, Error> {
- self.get_market_symbol(self.symbol.clone()).await
- }
- async fn get_market_symbol(&mut self, symbol: String) -> Result<Market, Error> {
- let symbol_format = utils::format_symbol(symbol.clone(), "").replace("1000", "u1000");
- let params = json!({});
- let response = self.request.get_market(params).await;
- // let response = get_market_info(params);
- if response.code != 200 {
- return Err(Error::new(ErrorKind::NotFound, format!("phemex_swap 获取市场行情异常{:?}", response).to_string()));
- }
- let res_data_json = response.data["perpProductsV2"].as_array().unwrap();
- let market_info = res_data_json.iter().find(|item| {
- item["symbol"].as_str().unwrap() == symbol_format
- });
- match market_info {
- None => {
- error!("phemex_swap:获取Market信息错误!\nget_market:res_data={:?}", response);
- Err(Error::new(ErrorKind::Other, response.to_string()))
- }
- Some(value) => {
- let symbol = value["symbol"].as_str().unwrap().to_string();
- let base_asset = value["baseCurrency"].as_str().unwrap().to_string().split_whitespace().collect();
- let quote_asset = value["quoteCurrency"].as_str().unwrap().to_string();
- let price_precision = Decimal::from_str(&value["pricePrecision"].to_string()).unwrap();
- let tick_size = Decimal::from_str(value["tickSize"].as_str().unwrap()).unwrap();
- let amount_precision = Decimal::from_str(&value["qtyPrecision"].to_string()).unwrap();
- let amount_size = Decimal::from_str(&value["qtyStepSize"].as_str().unwrap()).unwrap();
- let min_notional = Decimal::from_str(value["minPriceRp"].as_str().unwrap()).unwrap();
- let max_qty = Decimal::from_str(value["maxOrderQtyRq"].as_str().unwrap()).unwrap();
- let min_qty = Decimal::from_str(value["minOrderValueRv"].as_str().unwrap()).unwrap() / min_notional;
- let max_notional = Decimal::from_str(value["maxPriceRp"].as_str().unwrap()).unwrap() * max_qty;
- let ct_val = Decimal::ONE;
- let result = Market {
- symbol,
- base_asset,
- quote_asset,
- tick_size,
- amount_size,
- price_precision,
- amount_precision,
- min_qty,
- max_qty,
- min_notional,
- max_notional,
- ct_val,
- };
- Ok(result)
- }
- }
- }
- // 获取订单详情
- async fn get_order_detail(&mut self, order_id: &str, custom_id: &str) -> Result<Order, Error> {
- let symbol_format = utils::format_symbol(self.symbol.clone(), "").replace("1000", "u1000");
- let ct_val = self.market.ct_val;
- let mut params = json!({
- "symbol":symbol_format,
- });
- if order_id.eq("") { params["clOrdID"] = json!(custom_id.to_string()) } else { params["orderID"] = json!(order_id.to_string()) };
- if order_id == "" && custom_id == "" { return Err(Error::new(ErrorKind::Other, format!("订单id和客户端id都为空,查询失败!order_id :{}, custom_id: {}", order_id, custom_id))); }
- let response = self.request.get_orders_by_id(params).await;
- if response.code != 200 {
- return Err(Error::new(ErrorKind::NotFound, format!("phemex_swap 获取订单详情异常{:?}", response).to_string()));
- }
- let res_data_json = response.data["rows"].as_array().unwrap();
- let order_info = res_data_json.iter().find(|item| item["orderId"].as_str().unwrap() == order_id || item["clOrdId"] == custom_id);
- match order_info {
- None => {
- error!("phemex_swap:获取order信息错误!\nget_order_detail:res_data={:?},order_id={},custom_id:{}", response, order_id, custom_id);
- Err(Error::new(ErrorKind::Other, response.to_string()))
- }
- Some(order) => {
- let result = format_order_item(order.clone(), ct_val);
- return Ok(result);
- }
- }
- }
- // 获取未完成订单列表
- async fn get_orders_list(&mut self, _status: &str) -> Result<Vec<Order>, Error> {
- let symbol_format = utils::format_symbol(self.symbol.clone(), "").replace("1000", "u1000");
- let ct_val = self.market.ct_val;
- let params = json!({
- "symbol": symbol_format
- });
- let response = self.request.get_orders(params).await;
- if response.code != 200 {
- return Err(Error::new(ErrorKind::NotFound, format!("phemex_swap 获取订单列表异常{:?}", response).to_string()));
- }
- let result = response.data["rows"].as_array().unwrap().iter().map(|item| format_order_item(item.clone(), ct_val)).collect();
- return Ok(result);
- }
- // 下单接口
- async fn take_order(&mut self, custom_id: &str, origin_side: &str, price: Decimal, amount: Decimal) -> Result<Order, Error> {
- self.take_order_symbol(self.symbol.clone(), self.market.ct_val, custom_id, origin_side, price, amount).await
- }
- async fn take_order_symbol(&mut self, symbol: String, ct_val: Decimal, custom_id: &str, origin_side: &str, price: Decimal, amount: Decimal) -> Result<Order, Error> {
- let symbol_format = utils::format_symbol(symbol.clone(), "").replace("1000", "u1000");
- let mut params = json!({
- "clOrdID": custom_id,
- "symbol":symbol_format,
- "priceRp": price,
- "orderQtyRq": amount * ct_val
- });
- if price == dec!(0) { params["ordType"] = json!("Market") };
- match origin_side {
- "kd" => {
- params["reduce_only"] = json!(false);
- params["posSide"] = json!("Long");
- params["side"] = json!("Buy");
- }
- "pd" => {
- params["reduce_only"] = json!(true);
- params["posSide"] = json!("Long");
- params["side"] = json!("Sell");
- }
- "kk" => {
- params["reduce_only"] = json!(false);
- params["posSide"] = json!("Short");
- params["side"] = json!("Sell");
- }
- "pk" => {
- params["reduce_only"] = json!(true);
- params["posSide"] = json!("Short");
- params["side"] = json!("Buy");
- }
- _ => {
- error!("下单参数错误");
- params["posSide"] = json!("error");
- params["side"] = json!("error");
- }
- };
- let res_data = self.request.orders(params).await;
- println!("{:?}", res_data);
- if res_data.code == 200 {
- let res_data_json: Value = res_data.data;
- let result = format_order_item(res_data_json, ct_val);
- Ok(result)
- } else {
- Err(Error::new(ErrorKind::Other, res_data.to_string()))
- }
- }
- // 撤销订单
- async fn cancel_order(&mut self, order_id: &str, custom_id: &str) -> Result<Order, Error> {
- let symbol = utils::format_symbol(self.symbol.clone(), "").replace("1000", "u1000");
- let ct_val = self.market.ct_val;
- let mut params = json!({
- "orderID": order_id,
- "clOrdID": custom_id,
- "symbol": symbol,
- });
- params["posSide"] = json!("Short");
- let response_long = self.request.cancel_order(params.clone()).await;
- params["posSide"] = json!("Long");
- let response_short = self.request.cancel_order(params.clone()).await;
- if response_long.code != 200 && response_short.code != 200 {
- return Err(Error::new(ErrorKind::NotFound, format!("phemex_swap 撤销订单失败 order_id: {}, custom_id: {}, response_long: {:?}, response_short: {:?}", order_id, custom_id, response_long, response_short)));
- };
- let res_data_json: Value = if response_long.code == 200 { response_long.data } else { response_short.data };
- let mut result = format_order_item(res_data_json, ct_val);
- result.status = "REMOVE".to_string();
- Ok(result)
- }
- // 批量撤销订单
- async fn cancel_orders(&mut self) -> Result<Vec<Order>, Error> {
- let symbol = utils::format_symbol(self.symbol.clone(), "").replace("1000", "u1000");
- let params = json!({
- "symbol":symbol
- });
- let response = self.request.cancel_order_all(params).await;
- if response.code != 200 {
- return Err(Error::new(ErrorKind::NotFound, format!("phemex_swap 批量撤销订单失败 res_data: {:?}", response)));
- };
- return Ok(vec![]);
- }
- async fn cancel_orders_all(&mut self) -> Result<Vec<Order>, Error> {
- let params = json!({});
- let response = self.request.cancel_order_all(params).await;
- if response.code != 200 {
- return Err(Error::new(ErrorKind::NotFound, format!("phemex_swap 批量撤销订单失败 res_data: {:?}", response)));
- };
- // info!("{}", response.data.to_string());
- return Ok(vec![]);
- }
- async fn take_stop_loss_order(&mut self, _stop_price: Decimal, _price: Decimal, _side: &str) -> Result<Value, Error> {
- Err(Error::new(ErrorKind::NotFound, "coin_ex:该交易所暂未实现自动订单下单".to_string()))
- }
- async fn cancel_stop_loss_order(&mut self, _order_id: &str) -> Result<Value, Error> {
- Err(Error::new(ErrorKind::NotFound, "coin_ex:该交易所暂未实现取消自动订单".to_string()))
- }
- // 设置持仓模式
- async fn set_dual_mode(&mut self, _coin: &str, _is_dual_mode: bool) -> Result<String, Error> {
- let symbol = utils::format_symbol(self.symbol.clone(), "").replace("1000", "u1000");
- let params = json!({
- "symbol": symbol,
- "targetPosMode": "Hedged"
- });
- let response = self.request.set_target_pos_mode(params).await;
- if response.code != 200 {
- return Err(Error::new(ErrorKind::NotFound, format!("phemex_swap 设置持仓模式{:?}", response).to_string()));
- }
- let result = &response.data;
- Ok(result.as_str().unwrap().to_string())
- }
- // 更新杠杆
- async fn set_dual_leverage(&mut self, leverage: &str) -> Result<String, Error> {
- let symbol = utils::format_symbol(self.symbol.clone(), "").replace("1000", "u1000");
- let params = json!({
- "symbol": symbol,
- "longLeverageRr": leverage,
- "shortLeverageRr": leverage,
- });
- let response = self.request.set_leverage(params).await;
- if response.code != 200 {
- return Err(Error::new(ErrorKind::NotFound, format!("phemex_swap 设置杠杆异常{:?}", response).to_string()));
- }
- let result = &response.data;
- Ok(result.as_str().unwrap().to_string())
- }
- async fn set_auto_deposit_status(&mut self, _status: bool) -> Result<String, Error> { Err(Error::new(ErrorKind::NotFound, "Phemex:该交易所方法未实现".to_string())) }
- async fn wallet_transfers(&mut self, _coin: &str, _from: &str, _to: &str, _amount: Decimal) -> Result<String, Error> {
- Err(Error::new(ErrorKind::NotFound, "Phemex wallet_transfers:该交易所方法未实现".to_string()))
- }
- // 指令下单
- async fn command_order(&mut self, order_command: &mut OrderCommand, trace_stack: &TraceStack) {
- let mut handles = vec![];
- // 下单指令
- for item in order_command.limits_open.keys() {
- let mut ts = trace_stack.clone();
- let amount = Decimal::from_str(&*order_command.limits_open[item].get(0).unwrap().clone()).unwrap();
- let side = order_command.limits_open[item].get(1).unwrap().clone();
- let price = Decimal::from_str(&*order_command.limits_open[item].get(2).unwrap().clone()).unwrap();
- let cid = order_command.limits_open[item].get(3).unwrap().clone();
- // order_name: [数量,方向,价格,c_id]
- let mut self_clone = self.clone();
- let handle = spawn(async move {
- // info!("数量 {},方向 {},价格 {},c_id {}", amount, side, price, cid);
- ts.on_before_send();
- let result = self_clone.take_order(&cid, &side, price, amount).await;
- ts.on_after_send();
- match result {
- Ok(mut result) => {
- result.trace_stack = ts;
- // info!("数量 {},方向 {},价格 {},c_id {}", amount, side, price, cid);
- self_clone.order_sender.send(result).await.unwrap();
- }
- Err(error) => {
- let mut err_order = Order::new();
- err_order.custom_id = cid.clone();
- err_order.status = "REMOVE".to_string();
- // info!("err 数量 {},方向 {},价格 {},c_id {}", amount, side, price, cid);
- self_clone.order_sender.send(err_order).await.unwrap();
- self_clone.error_sender.send(error).await.unwrap();
- // 触发限频
- // if error_info.to_string().contains("213:Please don't try too frequently") {
- // Err(Error::new(ErrorKind::Other, "触发限频, 请调整下单频率"))
- // }
- }
- }
- });
- handles.push(handle)
- }
- let futures = FuturesUnordered::from_iter(handles);
- // 等待所有任务完成
- let _: Result<Vec<_>, _> = futures.try_collect().await;
- // 撤销订单
- let mut cancel_handlers = vec![];
- for item in order_command.cancel.keys() {
- let order_id = order_command.cancel[item].get(1).unwrap().clone();
- let custom_id = order_command.cancel[item].get(0).unwrap().clone();
- let mut self_clone = self.clone();
- let handle = spawn(async move {
- let result = self_clone.cancel_order(&order_id, &custom_id).await;
- match result {
- Ok(_) => {
- // self_clone.order_sender.send(order).await.unwrap();
- }
- Err(error) => {
- // info!("撤单失败:{:?}", error.to_string());
- // 取消失败去查订单。
- let query_rst = self_clone.get_order_detail(&order_id, &custom_id).await;
- match query_rst {
- Ok(order) => {
- // info!("查单 订单详情:{:?}", order);
- self_clone.order_sender.send(order).await.unwrap();
- }
- Err(err) => {
- // 未成交已取消的订单会报不存在
- if err.to_string().contains("3103:order not exists") {
- let mut order = Order::new();
- order.id = order_id.to_string();
- order.custom_id = custom_id.to_string();
- order.status = "REMOVE".to_string();
- self_clone.order_sender.send(order).await.unwrap();
- } else {
- warn!("撤单失败,而且查单也失败了,Phemex_io_swap,oid={}, cid={} err={:?}", order_id.clone(), custom_id.clone(), err);
- }
- // panic!("撤单失败,而且查单也失败了,Phemex_io_swap,oid={}, cid={}。", order_id.clone(), custom_id.clone());
- }
- }
- self_clone.error_sender.send(error).await.unwrap();
- }
- }
- });
- cancel_handlers.push(handle)
- }
- let futures = FuturesUnordered::from_iter(cancel_handlers);
- // 等待所有任务完成
- let _: Result<Vec<_>, _> = futures.try_collect().await;
- // 检查订单指令
- let mut check_handlers = vec![];
- for item in order_command.check.keys() {
- let order_id = order_command.check[item].get(1).unwrap().clone();
- let custom_id = order_command.check[item].get(0).unwrap().clone();
- let mut self_clone = self.clone();
- let handle = spawn(async move {
- let result = self_clone.get_order_detail(&order_id, &custom_id).await;
- match result {
- Ok(result) => {
- self_clone.order_sender.send(result).await.unwrap();
- }
- Err(error) => {
- self_clone.error_sender.send(error).await.unwrap();
- }
- }
- });
- check_handlers.push(handle)
- }
- let futures = FuturesUnordered::from_iter(check_handlers);
- // 等待所有任务完成
- let _: Result<Vec<_>, _> = futures.try_collect().await;
- }
- }
- pub fn format_position_item(position: &Value, ct_val: Decimal) -> Position {
- let position_mode = match position["posSide"].as_str().unwrap_or("") {
- "Long" => PositionModeEnum::Long,
- "Short" => PositionModeEnum::Short,
- _ => {
- error!("phemex_swap:格式化持仓模式错误!\nformat_position_item:position={:?}", position);
- panic!("phemex_swap:格式化持仓模式错误!\nformat_position_item:position={:?}", position)
- }
- };
- let size = Decimal::from_str(&position["size"].as_str().unwrap()).unwrap();
- let amount = size * ct_val;
- Position {
- symbol: position["symbol"].as_str().unwrap().to_string(),
- margin_level: Decimal::from_str(position["leverageRr"].as_str().unwrap()).unwrap(),
- amount,
- frozen_amount: Decimal::ZERO,
- price: Decimal::from_str(position["avgEntryPrice"].as_str().unwrap()).unwrap(),
- profit: Decimal::from_str(position["curTermRealisedPnlRv"].as_str().unwrap()).unwrap(),
- position_mode,
- margin: Decimal::from_str(position["positionMarginRv"].as_str().unwrap()).unwrap(),
- }
- }
- pub fn format_order_item(order: Value, ct_val: Decimal) -> Order {
- // info!("需要格式化的订单数据: {}", order);
- let id = match order["orderId"].as_str() {
- Some(val) => {
- val.to_string()
- },
- None => {
- order["orderID"].as_str().unwrap().to_string()
- }
- };
- let custom_id = match order["clOrdId"].as_str() {
- Some(val) => {
- val.to_string()
- },
- None => {
- order["clOrdID"].as_str().unwrap().to_string()
- }
- };
- let price = Decimal::from_str(order["priceRp"].as_str().unwrap()).unwrap();
- let amount = Decimal::from_str(order["orderQtyRq"].as_str().unwrap()).unwrap() * ct_val;
- let deal_amount = Decimal::from_str(order["cumQtyRq"].as_str().unwrap()).unwrap() * ct_val;
- let avg_price = Decimal::from_str(order["cumValueRv"].as_str().unwrap()).unwrap();
- let status = order["ordStatus"].as_str().unwrap();
- let custom_status;
- if vec!["Rejected"].contains(&status) {
- custom_status = "NULL".to_string()
- } else if vec!["Filled", "Canceled"].contains(&status) {
- custom_status = "REMOVE".to_string()
- } else if vec!["New", "Init", "Created"].contains(&status) {
- custom_status = "NEW".to_string()
- } else {
- error!("gate_swap:格式化订单状态错误!\nformat_order_item:order={:?}", order);
- panic!("gate_swap:格式化订单状态错误!\nformat_order_item:order={:?}", order)
- };
- let result = Order {
- id,
- custom_id,
- price,
- amount,
- deal_amount,
- avg_price,
- status: custom_status,
- order_type: "limit".to_string(),
- trace_stack: TraceStack::new(0, Instant::now()).on_special("669 trace_stack".to_string()),
- };
- return result;
- }
- fn get_market_info(_params: Value) -> ResponseData {
- let aa = json!({"perpProductsV2": [{
- "symbol": "u1000PEPEUSDT",
- "code": 66041,
- "type": "PerpetualV2",
- "displaySymbol": "1000PEPE / USDT",
- "indexSymbol": ".u1000PEPEUSDT",
- "markSymbol": ".Mu1000PEPEUSDT",
- "fundingRateSymbol": ".u1000PEPEUSDTFR",
- "fundingRate8hSymbol": ".u1000PEPEUSDTFR8H",
- "contractUnderlyingAssets": "1000 PEPE",
- "settleCurrency": "USDT",
- "quoteCurrency": "USDT",
- "tickSize": "0.0000001",
- "priceScale": 0,
- "ratioScale": 0,
- "pricePrecision": 7,
- "baseCurrency": "1000 PEPE",
- "description": "1000 PEPE/USDT perpetual contracts are priced on the .u1000PEPEUSDT Index. Each contract is worth 1 1000PEPE. Funding fees are paid and received every 8 hours at UTC time: 00:00, 08:00 and 16:00.",
- "status": "Listed",
- "tipOrderQty": 0,
- "listTime": "1683367200000",
- "majorSymbol": false,
- "defaultLeverage": "-10",
- "fundingInterval": 28800,
- "maxLeverage": 50,
- "leverageMargin": 1015,
- "maxOrderQtyRq": "400000000",
- "maxPriceRp": "2000",
- "minOrderValueRv": "1",
- "minPriceRp": "0.001",
- "qtyPrecision": 0,
- "qtyStepSize": "1.0",
- "tipOrderQtyRq": "80000000",
- "maxOpenPosLeverage": 50.0
- }, {
- "symbol": "EDUUSDT",
- "code": 66241,
- "type": "PerpetualV2",
- "displaySymbol": "EDU / USDT",
- "indexSymbol": ".EDUUSDT",
- "markSymbol": ".MEDUUSDT",
- "fundingRateSymbol": ".EDUUSDTFR",
- "fundingRate8hSymbol": ".EDUUSDTFR8H",
- "contractUnderlyingAssets": "EDU",
- "settleCurrency": "USDT",
- "quoteCurrency": "USDT",
- "tickSize": "0.0001",
- "priceScale": 0,
- "ratioScale": 0,
- "pricePrecision": 4,
- "baseCurrency": "EDU",
- "description": "EDU/USDT perpetual contracts are priced on the .EDUUSDT Index. Each contract is worth 1 EDU. Funding fees are paid and received every 8 hours at UTC time: 00:00, 08:00 and 16:00.",
- "status": "Listed",
- "tipOrderQty": 0,
- "listTime": "1683367200000",
- "majorSymbol": false,
- "defaultLeverage": "-10",
- "fundingInterval": 28800,
- "maxLeverage": 50,
- "leverageMargin": 1015,
- "maxOrderQtyRq": "800000",
- "maxPriceRp": "2000000",
- "minOrderValueRv": "1",
- "minPriceRp": "1.0",
- "qtyPrecision": 0,
- "qtyStepSize": "1.0",
- "tipOrderQtyRq": "160000",
- "maxOpenPosLeverage": 50.0
- }]});
- ResponseData::new("".to_string(), 200, "".to_string(), aa)
- }
|