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
use futures_util::Future;
use serde_derive::Deserialize;
use std::collections::HashMap;

use crate::http::{Client, HttpError, HttpVerb};

static APPROVAL_RULES_RESOURCE: &str = "approval-rules";
static APPROVAL_RESOURCE: &str = "approvals";
static CUSTODY_RESOURCE: &str = "whitelisted-addresses";

#[derive(Clone, Debug, Deserialize)]
pub enum ApprovalRuleType {
    #[serde(rename = "ADD_ALTER_COLL")]
    AddAlterColl,

    #[serde(rename = "ALTER_SAFE")]
    AlterSafe,

    #[serde(rename = "WITHDRAW")]
    Withdraw,
}

#[derive(Clone, Debug, Deserialize)]
pub struct LoanMetrics {
    pub account_value: f64,
    pub equity: f64,
    pub position_notional: f64,
    pub collateral: f64,
    pub free_collateral: f64,
    pub margin_level: f64,
    pub margin_call_level: f64,
    pub maintenance_margin_level: f64,
}

#[derive(Clone, Debug, Deserialize)]
pub struct CustodyAddressesResponse {
    pub data: Vec<CustodyAddress>,
}

#[derive(Clone, Debug, Deserialize)]
pub struct CustodyAddress {
    // Only present on POST response
    pub id: Option<String>,
    pub alias: String,
    pub address: String,
    pub currency_symbol: String,
    pub date_created: String,
    pub date_updated: String,
    // Only present on POST response
    pub tag: Option<String>,
}

#[derive(Clone, Debug, Deserialize)]
pub struct ApprovalRequestResponse {
    pub data: Vec<ApprovalRequest>,
}

#[derive(Clone, Debug, Deserialize)]
pub struct ApprovalRequest {
    pub approval_id: usize,
    pub requested_by_username: String,
    pub requested_by_uaid: String,
    pub date_added: String,
    pub status: String,
    pub approval_type: ApprovalRuleType,
    pub required_approvals: usize,
    pub received_approvals: usize,
    pub action_details: ActionDetails,
    pub approval_responses: ApprovalResponse,
}

#[derive(Clone, Debug, Deserialize)]
pub struct ActionDetails {
    pub atx_currency_code: String,
    pub atx_amount: f64,
    pub atx_dest_address: String,
    pub threshold: usize,
}

#[derive(Clone, Debug, Deserialize)]
pub struct ApprovalResponse {
    pub ua_display_id: String,
    pub username: String,
    pub approved: bool,
}

#[derive(Clone, Debug, Deserialize)]
pub struct ApprovalRulesResponse {
    pub data: Vec<ApprovalRule>,
}

#[derive(Clone, Debug, Deserialize)]
pub struct ApprovalRule {
    pub id: usize,
    pub rule_type: ApprovalRuleType,
    pub date_added: String,
    pub status: String,
    pub available_approver_count: usize,
    pub required_approvals: usize,
    pub threshold: usize,
}

impl Client {
    pub fn custody_addresses(
        self,
    ) -> impl Future<Output = Result<CustodyAddressesResponse, HttpError>> {
        let query_str = self.url_for_v1_resource(CUSTODY_RESOURCE);

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

    pub fn add_custody_address(
        self,
        alias: String,
        currency_symbol: String,
        address: String,
    ) -> impl Future<Output = Result<CustodyAddress, HttpError>> {
        let query_str = self.url_for_v1_resource(CUSTODY_RESOURCE);

        let mut params = HashMap::new();
        params.insert("alias".into(), alias);
        params.insert("currency_symbol".into(), currency_symbol);
        params.insert("address".into(), address);

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

    pub fn approval_rules(self) -> impl Future<Output = Result<ApprovalRulesResponse, HttpError>> {
        let query_str = self.url_for_v1_resource(APPROVAL_RULES_RESOURCE);

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

    pub fn add_approval_rule(
        self,
        rule_type: String,
        required_approvals: usize,
        threshold: usize,
    ) -> impl Future<Output = Result<ApprovalRule, HttpError>> {
        let query_str = self.url_for_v1_resource(APPROVAL_RULES_RESOURCE);

        let mut params = HashMap::new();
        params.insert("rule_type".to_string(), rule_type);
        params.insert(
            "required_approvals".to_string(),
            required_approvals.to_string(),
        );
        params.insert("threshold".to_string(), threshold.to_string());

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

    pub fn edit_approval_rule(
        self,
        id: usize,
        required_approvals: usize,
        threshold: f64,
    ) -> impl Future<Output = Result<ApprovalRule, HttpError>> {
        let query_str = self.url_for_v1_resource(&format!("{}/{}", APPROVAL_RULES_RESOURCE, id));

        let mut params = HashMap::new();
        params.insert("required_approvals".into(), required_approvals.to_string());
        params.insert("threshold".into(), threshold.to_string());

        self.request(HttpVerb::Patch, &query_str, Some(&params))
    }

    pub fn approval_requests(
        self,
        pending: bool,
    ) -> impl Future<Output = Result<ApprovalRequestResponse, HttpError>> {
        let mut query_str = self.url_for_v1_resource(APPROVAL_RESOURCE);
        if pending {
            query_str = format!("{}?pending=true", query_str);
        }

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

    pub fn respond_to_approval_request(
        self,
        id: usize,
        approve: bool,
    ) -> impl Future<Output = Result<(), HttpError>> {
        let query_str = self.url_for_v1_resource(&format!("{}/{}", APPROVAL_RESOURCE, id));

        // TODO: handle polymorphic params
        let mut params = HashMap::new();
        params.insert("approve".to_string(), approve.to_string());

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

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

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

    const CUSTODY_ADDRESSES_RESPONSE_BODY: &str = r#"
        {
            "data": [
                {
                    "alias": "Satoshis Fund",
                    "address": "1NLqQmwkGxxQmzS9uwtCGXxbxrcNW4FpYp",
                    "currency_symbol": "btc",
                    "date_created": "2021-09-15T15:12:13.000Z",
                    "date_updated": "2021-09-15T15:12:13.000Z",
                    "status": "Pending"
                }
            ]
        }
    "#;

    const CREATE_CUSTODY_ADDRESS_RESPONSE_BODY: &str = r#"
        {
            "id": "4d90ee41-3b36-11ec-bdb0-0ab29ff926a1",
            "alias": "Satoshis Fund",
            "address": "1NLqQmwkGxxQmzS9uwtCGXxbxrcNW4FpYp",
            "currency_symbol": "btc",
            "date_created": "2021-11-01T17:08:15.000Z",
            "date_updated": "2021-11-01T17:08:15.000Z",
            "status": "Pending",
            "tag": null
        }
    "#;

    const APPROVAL_RULES_RESPONSE_BODY: &str = r#"
        {
            "data": [
                {
                    "id": 3,
                    "available_approver_count": 2,
                    "date_added": "2021-03-17T16:19:47.000Z",
                    "required_approvals": 2,
                    "rule_type": "WITHDRAW",
                    "status": "Pending Approval",
                    "threshold": 20
                }
            ]
        }
    "#;

    const APPROVAL_RULE_RESPONSE_BODY: &str = r#"
        {
            "id": 1,
            "rule_type": "WITHDRAW",
            "date_added": "2021-03-17T16:19:47.000Z",
            "status": "Pending",
            "available_approver_count": 2,
            "required_approvals": 2,
            "threshold": 0
        }
    "#;

    const APPROVALS_RESPONSE_BODY: &str = r#"
        {
            "data": [
                {
                    "approval_id": 1,
                    "requested_by_username": "example@email.com",
                    "requested_by_uaid": "6ea3fb9e-7797-11eb-aa51-0242ac120002",
                    "date_added": "2021-03-03T20:28:41.000Z",
                    "status": "Pending",
                    "approval_type": "WITHDRAW",
                    "required_approvals": 2,
                    "received_approvals": 0,
                    "action_details": {
                        "atx_currency_code": "btc",
                        "atx_amount": 5,
                        "atx_dest_address": "0x1232131223",
                        "threshold": 1
                    },
                    "approval_responses": {
                        "ua_display_id": "3fb9e6ea-7797-11eb-aa51-200020242ac1",
                        "username": "collaborator@email.com",
                        "approved": true
                    }
                }
            ]
        }
    "#;

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

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

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

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

    #[tokio::test]
    async fn test_create_custody_addresses() {
        let mock = ApiMock {
            action: HttpVerb::Post,
            body: CREATE_CUSTODY_ADDRESS_RESPONSE_BODY.into(),
            path: format!("/v1/{}", CUSTODY_RESOURCE),
            response_code: 200,
        };

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

        let result = client
            .add_custody_address("test alias".into(), "btc".into(), "0x123".into())
            .await;

        assert!(result.is_ok());

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

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

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

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

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

    #[tokio::test]
    async fn test_new_approval_rule() {
        let mock = ApiMock {
            action: HttpVerb::Post,
            body: APPROVAL_RULE_RESPONSE_BODY.into(),
            path: format!("/v1/{}", APPROVAL_RULES_RESOURCE),
            response_code: 200,
        };

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

        let result = client
            .add_approval_rule("test rule type".into(), 1, 1)
            .await;

        assert!(result.is_ok());

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

    #[tokio::test]
    async fn test_edit_approval_rule() {
        let rule_id = 1;

        let mock = ApiMock {
            action: HttpVerb::Patch,
            body: APPROVAL_RULE_RESPONSE_BODY.into(),
            path: format!("/v1/{}/{}", APPROVAL_RULES_RESOURCE, rule_id),
            response_code: 200,
        };

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

        let result = client.edit_approval_rule(rule_id, 2, 2.0).await;

        assert!(result.is_ok());

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

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

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

        let result = client.approval_requests(false).await;

        assert!(result.is_ok());

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

    #[tokio::test]
    async fn test_approval_request_response() {
        let request_id = 1;

        let mock = ApiMock {
            action: HttpVerb::Post,
            body: r#"null"#.into(),
            path: format!("/v1/{}/{}", APPROVAL_RESOURCE, request_id),
            response_code: 200,
        };

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

        let result = client.respond_to_approval_request(request_id, true).await;

        assert!(result.is_ok());

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