mexc_rust_sdk/
lib.rs

1pub mod utils;
2pub mod market;
3pub mod orders;
4pub mod testing;
5pub mod account;
6
7
8use std::time::{Duration, Instant};
9use reqwest::Client;
10use serde::Deserialize;
11
12
13
14pub const PROD_API_URL: &str = "https://api.mexc.com";
15
16pub struct Mexc {
17    pub api_key: Option<String>,
18    pub api_secret: Option<String>,
19    pub client: Client
20}
21
22// https://mexcdevelop.github.io/apidocs/spot_v3_en/#header
23
24
25#[derive(Deserialize, Debug)]
26pub struct ServerTime {
27    #[serde(rename= "serverTime")]
28    pub timestamp: u128
29}
30
31impl Mexc {
32
33    pub fn new(api_key: Option<String>, api_secret: Option<String>, proxy_url: Option<String>) -> anyhow::Result<Self> {
34
35        let client = match proxy_url {
36            Some(url) => {
37                let proxy = reqwest::Proxy::all(url)?;
38                reqwest::Client::builder().proxy(proxy).build()?
39            },
40            None => reqwest::Client::new()
41        };
42
43
44        Ok(Self {
45            api_key,
46            api_secret,
47            client
48        })
49    }
50
51    pub async fn get_server_time(&self) -> anyhow::Result<u128> {
52        let url = format!("{PROD_API_URL}/api/v3/time");
53        let resp = self.client.get(url).send().await?;
54
55        let st: ServerTime = resp.json().await?;
56        Ok(st.timestamp)
57    }
58
59    pub async fn ping(&self) -> anyhow::Result<Duration> {
60        let url = format!("{PROD_API_URL}/api/v3/ping");
61
62        let t0 = Instant::now();
63        let _ = self.client.get(url).send().await?;
64        let t1 = Instant::now();
65
66        Ok(t1 - t0)
67    }
68}