sfox 0.1.2

Unofficial HTTP and Websocket Client for the SFox API
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
use std::collections::HashMap;

use futures_util::Future;
use serde::Deserialize;

use super::super::{Client, HttpError, HttpVerb};

static DONE_ORDERS_RESOURCE: &str = "orders/done";
static LIST_ASSET_PAIRS_RESOURCE: &str = "markets/currency_pairs";
static OPEN_ORDERS_RESOURCE: &str = "orders/open";
static ORDERS_RESOURCE: &str = "orders";

#[derive(Clone, Debug, Deserialize)]
pub enum OrderStatus {
    Started,
    #[serde(rename = "Cancel pending")]
    Pending,
    Canceled,
    Filled,
    Done,
}

#[derive(Clone, Debug, Deserialize)]
pub struct AssetPair {
    pub formatted_symbol: String,
    pub symbol: String,
}

#[derive(Clone, Debug, Deserialize)]
pub struct CancelledOrderResponse {
    pub orders: Vec<CancelledOrder>,
}

#[derive(Clone, Debug, Deserialize)]
pub struct CancelledOrder {
    pub id: Option<usize>,
    pub status: OrderStatus,
}

#[derive(Clone, Debug, Deserialize)]
pub struct CryptoDepositAddress {
    pub address: String,
    pub currency: String,
}

#[derive(Clone, Debug, Deserialize)]
pub struct ExecutedQuote {
    pub id: usize,
    pub side_id: usize,
    pub action: String,
    pub algorithm_id: usize,
    pub algorithm: String,
    #[serde(rename = "type")]
    pub execution_type: String,
    pub pair: String,
    pub quantity: f64,
    pub price: f64,
    pub amount: f64,
    pub net_market_amount: f64,
    pub filled: f64,
    pub vwap: f64,
    pub filled_amount: f64,
    pub fees: f64,
    pub net_proceeds: f64,
    pub status: String,
    pub status_code: usize,
    pub routing_option: String,
    pub routing_type: String,
    pub time_in_force: String,
    pub expires: Option<String>,
    pub dateupdated: String,
    pub client_order_id: Option<String>,
    pub user_tx_id: Option<String>,
    pub o_action: String,
    pub algo_id: usize,
    pub algorithm_options: Option<String>,
    pub destination: Option<String>,
}

#[derive(Clone, Debug, Deserialize)]
pub struct Order {
    pub id: usize,
    pub quantity: f64,
    pub price: f64,
    pub o_action: String,
    pub pair: String,
    #[serde(rename = "type")]
    pub order_type: String,
    pub vwap: f64,
    pub filled: f64,
    pub status: OrderStatus,
}

impl Client {
    pub fn cancel_all_orders(
        self,
    ) -> impl Future<Output = Result<CancelledOrderResponse, HttpError>> {
        let url = self.url_for_v1_resource(&format!("{}", OPEN_ORDERS_RESOURCE));
        self.request(HttpVerb::Delete, &url, None)
    }

    pub fn cancel_order(
        self,
        order_id: usize,
    ) -> impl Future<Output = Result<CancelledOrder, HttpError>> {
        let url = self.url_for_v1_resource(&format!("{}/{}", ORDERS_RESOURCE, order_id));
        self.request(HttpVerb::Delete, &url, None)
    }

    pub fn cancel_orders(
        self,
        order_ids: Vec<usize>,
    ) -> impl Future<Output = Result<CancelledOrderResponse, HttpError>> {
        // Create a comma separated list of order ids from the vector
        let order_ids_query_param = order_ids
            .iter()
            .map(|id| id.to_string())
            .collect::<Vec<String>>()
            .join(",");

        let url = self.url_for_v1_resource(&format!(
            "{}?ids={}",
            ORDERS_RESOURCE, order_ids_query_param
        ));

        self.request(HttpVerb::Delete, &url, None)
    }

    pub fn open_orders(self) -> impl Future<Output = Result<Vec<Order>, HttpError>> {
        let url = self.url_for_v1_resource(&format!("{}", ORDERS_RESOURCE));
        self.request(HttpVerb::Get, &url, None)
    }

    #[allow(clippy::too_many_arguments)]
    pub fn place_order(
        self,
        side: &str,
        currency_pair: &str,
        price: f64,
        quantity: f64,
        routing_type: &str,
        algorithm_id: usize,
        client_order_id: Option<&str>,
    ) -> impl Future<Output = Result<Order, HttpError>> {
        let mut params = HashMap::new();
        params.insert("currency_pair".to_string(), currency_pair.to_string());
        params.insert("price".to_string(), price.to_string());
        params.insert("quantity".to_string(), quantity.to_string());
        params.insert("routing_type".to_string(), routing_type.to_string());
        params.insert("algorithm_id".to_string(), algorithm_id.to_string());
        if let Some(client_order_id) = client_order_id {
            params.insert("client_order_id".to_string(), client_order_id.to_string());
        }

        let url = self.url_for_v1_resource(&format!("{}/{}", ORDERS_RESOURCE, side));

        self.request(HttpVerb::Post, &url, Some(&params))
    }

    pub fn order_status(self, order_id: &str) -> impl Future<Output = Result<Order, HttpError>> {
        let url = self.url_for_v1_resource(&format!("{}/{}", ORDERS_RESOURCE, order_id));

        self.request(HttpVerb::Get, &url, None)
    }

    pub fn done_orders(self) -> impl Future<Output = Result<Vec<ExecutedQuote>, HttpError>> {
        let url = self.url_for_v1_resource(DONE_ORDERS_RESOURCE);

        self.request(HttpVerb::Get, &url, None)
    }

    pub fn list_asset_pairs(
        self,
    ) -> impl Future<Output = Result<HashMap<String, AssetPair>, HttpError>> {
        let url = self.url_for_v1_resource(LIST_ASSET_PAIRS_RESOURCE);

        self.request(HttpVerb::Get, &url, None)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    use crate::util::server::{new_test_server_and_client, ApiMock};

    const CANCEL_PENDING_ORDER_RESPONSE_BODY: &str = r#"
        {"id": 3, "status": "Cancel pending"}
    "#;

    const CANCEL_MULTIPLE_ORDERS_FAILED_RESPONSE_BODY: &str = r#"
        { "error": "the order ids provided were invalid or the orders were already done/canceled" }
    "#;

    const OPEN_ORDERS_RESPONSE_BODY: &str = r#"
        [
            {
                "id": 123,
                "quantity": 1,
                "price": 10,
                "o_action": "Buy",
                "pair": "btcusd",
                "type": "Limit",
                "vwap": 0,
                "filled": 0,
                "status": "Started"
            },
            {
                "id": 456,
                "quantity": 1,
                "price": 10,
                "o_action": "Sell",
                "pair": "btcusd",
                "type": "Limit",
                "vwap": 0,
                "filled": 0,
                "status": "Started"
            }
        ]
    "#;

    const ORDERS_RESPONSE_BODY: &str = r#"
        {
            "orders": [
                {"id": 2, "status": "Cancel pending"},
                {"id": 3, "status": "Canceled"},
                {"id": 4, "status": "Canceled"}
            ]
        }
    "#;

    const ORDER_RESPONSE_BODY: &str = r#"
        {
            "id": 123,
            "quantity": 1,
            "price": 10,
            "o_action": "Buy",
            "pair": "btcusd",
            "type": "Limit",
            "vwap": 0,
            "filled": 0,
            "status": "Started"
        }
    "#;

    const DONE_ORDERS_RESPONSE_BODY: &str = r#"
        [
            {
                "id": 701968334,
                "side_id": 500,
                "action": "Buy",
                "algorithm_id": 200,
                "algorithm": "Smart",
                "type": "Smart",
                "pair": "btcusd",
                "quantity": 0.001,
                "price": 16900.6,
                "amount": 0,
                "net_market_amount": 0,
                "filled": 0.001,
                "vwap": 16900.6,
                "filled_amount": 16.9006,
                "fees": 0.0338012,
                "net_proceeds": -16.8667988,
                "status": "Done",
                "status_code": 300,
                "routing_option": "BestPrice",
                "routing_type": "NetPrice",
                "time_in_force": "GTC",
                "expires": null,
                "dateupdated": "2022-11-18T01:26:40.000Z",
                "client_order_id": "94b0e7c4-0fa7-403d-a0d0-6c4ccec76630",
                "user_tx_id": "94b0e7c4-0fa7-403d-a0d0-6c4ccec76630",
                "o_action": "Buy",
                "algo_id": 200,
                "algorithm_options": null,
                "destination": ""
            },
            {
                "id": 701945645,
                "side_id": 500,
                "action": "Buy",
                "algorithm_id": 201,
                "algorithm": "Limit",
                "type": "Limit",
                "pair": "btcusd",
                "quantity": 0.01,
                "price": 16905,
                "amount": 0,
                "net_market_amount": 0,
                "filled": 0.01,
                "vwap": 16643,
                "filled_amount": 166.43,
                "fees": 0.16643,
                "net_proceeds": -166.26357,
                "status": "Done",
                "status_code": 300,
                "routing_option": "BestPrice",
                "routing_type": "NetPrice",
                "time_in_force": "GTC",
                "expires": null,
                "dateupdated": "2022-11-17T19:39:18.000Z",
                "client_order_id": "",
                "user_tx_id": "",
                "o_action": "Buy",
                "algo_id": 201,
                "algorithm_options": null,
                "destination": ""
            }
        ]
    "#;

    const LIST_ASSET_PAIRS_RESPONSE_BODY: &str = r#"
        {
            "bchbtc": {
                "formatted_symbol": "BCH/BTC",
                "symbol": "bchbtc"
            },
            "bchusd": {
                "formatted_symbol": "BCH/USD",
                "symbol": "bchusd"
            },
            "btcusd": {
                "formatted_symbol": "BTC/USD",
                "symbol": "btcusd"
            }
        }
    "#;

    #[tokio::test]
    async fn test_cancel_all_orders() {
        let mock = ApiMock {
            action: HttpVerb::Delete,
            body: ORDERS_RESPONSE_BODY.into(),
            path: format!("/v1/{}", OPEN_ORDERS_RESOURCE),
            response_code: 200,
        };

        let (client, _server, mock_results) = new_test_server_and_client(vec![mock]).await;

        let result = client.cancel_all_orders().await;

        assert!(result.is_ok());

        for mock in mock_results {
            mock.assert_async().await;
        }
    }

    #[tokio::test]
    async fn test_cancel_multiple_orders() {
        let ids = vec![2, 3];
        let ids_param = ids
            .iter()
            .map(|id| id.to_string())
            .collect::<Vec<String>>()
            .join(",");

        let mock = ApiMock {
            action: HttpVerb::Delete,
            body: ORDERS_RESPONSE_BODY.into(),
            path: format!("/v1/{}?ids={}", ORDERS_RESOURCE, ids_param),
            response_code: 200,
        };

        let (client, _server, mock_results) = new_test_server_and_client(vec![mock]).await;

        let result = client.cancel_orders(vec![2, 3]).await;

        assert!(result.is_ok());

        for mock in mock_results {
            mock.assert_async().await;
        }
    }

    #[tokio::test]
    async fn test_cancel_multiple_orders_failed() {
        let ids = vec![2, 3];
        let ids_param = ids
            .iter()
            .map(|id| id.to_string())
            .collect::<Vec<String>>()
            .join(",");

        let mock = ApiMock {
            action: HttpVerb::Delete,
            body: CANCEL_MULTIPLE_ORDERS_FAILED_RESPONSE_BODY.into(),
            path: format!("/v1/{}?ids={}", ORDERS_RESOURCE, ids_param),
            response_code: 400,
        };

        let (client, _server, mock_results) = new_test_server_and_client(vec![mock]).await;

        let result = client.cancel_orders(vec![2, 3]).await;

        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.to_string()
                == "Error while making request: `\"the order ids provided were invalid or the orders were already done/canceled\"`"
        );

        for mock in mock_results {
            mock.assert_async().await;
        }
    }

    #[tokio::test]
    async fn test_cancel_order() {
        let id = 2;

        let mock = ApiMock {
            action: HttpVerb::Delete,
            body: CANCEL_PENDING_ORDER_RESPONSE_BODY.into(),
            path: format!("/v1/{}/{}", ORDERS_RESOURCE, id),
            response_code: 200,
        };

        let (client, _server, mock_results) = new_test_server_and_client(vec![mock]).await;

        let result = client.cancel_order(id).await;
        assert!(result.is_ok());

        for mock in mock_results {
            mock.assert_async().await;
        }
    }

    #[tokio::test]
    async fn test_place_order() {
        let side = "sell";

        let mock = ApiMock {
            action: HttpVerb::Post,
            body: ORDER_RESPONSE_BODY.into(),
            path: format!("/v1/{}/{}", ORDERS_RESOURCE, side),
            response_code: 200,
        };

        let (client, _server, mock_results) = new_test_server_and_client(vec![mock]).await;

        let result = client
            .place_order(side, "ethusd", 0.123, 0.456, "NetPrice", 100, "123A".into())
            .await;

        assert!(result.is_ok());

        for mock in mock_results {
            mock.assert_async().await;
        }
    }

    #[tokio::test]
    async fn test_order_status() {
        let order_id = "abc";
        let mock = ApiMock {
            action: HttpVerb::Get,
            body: ORDER_RESPONSE_BODY.into(),
            path: format!("/v1/{}/{}", ORDERS_RESOURCE, order_id),
            response_code: 200,
        };

        let (client, _server, mock_results) = new_test_server_and_client(vec![mock]).await;

        let result = client.order_status(order_id).await;

        assert!(result.is_ok());

        for mock in mock_results {
            mock.assert_async().await;
        }
    }

    #[tokio::test]
    async fn test_open_orders() {
        let mock = ApiMock {
            action: HttpVerb::Get,
            body: OPEN_ORDERS_RESPONSE_BODY.into(),
            path: format!("/v1/{}", ORDERS_RESOURCE),
            response_code: 200,
        };

        let (client, _server, mock_results) = new_test_server_and_client(vec![mock]).await;

        let result = client.open_orders().await;

        assert!(result.is_ok());

        for mock in mock_results {
            mock.assert_async().await;
        }
    }

    #[tokio::test]
    async fn test_done_orders() {
        let mock = ApiMock {
            action: HttpVerb::Get,
            body: DONE_ORDERS_RESPONSE_BODY.into(),
            path: format!("/v1/{}", DONE_ORDERS_RESOURCE),
            response_code: 200,
        };

        let (client, _server, mock_results) = new_test_server_and_client(vec![mock]).await;

        let result = client.done_orders().await;

        assert!(result.is_ok());

        for mock in mock_results {
            mock.assert_async().await;
        }
    }

    #[tokio::test]
    async fn test_list_asset_pairs() {
        let mock = ApiMock {
            action: HttpVerb::Get,
            body: LIST_ASSET_PAIRS_RESPONSE_BODY.into(),
            path: format!("/v1/{}", LIST_ASSET_PAIRS_RESOURCE),
            response_code: 200,
        };

        let (client, _server, mock_results) = new_test_server_and_client(vec![mock]).await;

        let result = client.list_asset_pairs().await;

        assert!(result.is_ok());

        for mock in mock_results {
            mock.assert_async().await;
        }
    }
}