maicoin_max 2.1.1

MaiCoin Max API client for Rust
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
use chrono::serde as chrono_serde;
use rust_decimal::Decimal;
use serde::{Deserialize, Serialize};

use crate::common::*;
use crate::v2::rest::api_impl::*;

// ========
// Requests
// ========

/// GET /api/v2/withdrawal
///
/// Get details of a specific external withdraw.
#[derive(Serialize, Debug)]
pub struct GetWithdrawal {
    /// Unique withdraw id.
    pub uuid: String,
}
impl_api!(GetWithdrawal => RespWithdrawalDetail : auth GET, "/api/v2/withdrawal");

/// GET /api/v2/withdrawals
///
/// Get your external withdrawals history.
#[derive(Serialize, Debug)]
pub struct GetWithdrawals {
    /// Unique currency id, check /api/v2/currencies for available currencies.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub currency: Option<String>,
    /// Target period start (Epoch time in seconds).
    #[serde(rename = "from", skip_serializing_if = "Option::is_none")]
    pub from_timestamp: Option<DateTime>,
    /// Target period end (Epoch time in seconds).
    #[serde(rename = "to", skip_serializing_if = "Option::is_none")]
    pub to_timestamp: Option<DateTime>,
    /// Withdrawal state.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub state: Option<WithdrawalState>,
    /// Do pagination & return metadata in header (default `false`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pagination: Option<bool>,
    /// Pagination parameters, see [`crate::common::PageParams`].
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub page_params: Option<PageParams>,
    /// Records to skip, not applied for pagination (default `0`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub offset: Option<u64>,
}
impl_api!(GetWithdrawals => Vec<RespWithdrawalDetail> : auth GET, "/api/v2/withdrawals");

/// POST /api/v2/withdrawal
///
/// Submit a withdrawal. IP whitelist for api token is required.
#[derive(Serialize, Debug)]
pub struct CreateWithdrawal {
    /// Unique currency id, check /api/v2/currencies for available currencies.
    pub currency: String,
    /// Unique withdraw address id, check GET /api/v2/withdraw_addresses for available withdraw addresses.
    pub withdraw_address_uuid: String,
    /// Withdraw amount.
    pub amount: Decimal,
}
impl_api!(CreateWithdrawal => RespCreatedWithdraw : auth POST, "/api/v2/withdrawal");

/// GET /api/v2/withdraw_addresses
///
/// Get withdraw addresses by currency.
#[derive(Serialize, Debug)]
pub struct GetWithdrawAddresses {
    /// Unique currency id, check /api/v2/currencies for available currencies.
    pub currency: String,
    /// Do pagination & return metadata in header (default `false`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub pagination: Option<bool>,
    /// Pagination parameters, see [`crate::common::PageParams`].
    #[serde(flatten, skip_serializing_if = "Option::is_none")]
    pub page_params: Option<PageParams>,
    /// Records to skip, not applied for pagination (default `0`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub offset: Option<u64>,
}
impl_api!(GetWithdrawAddresses => Vec<WithdrawAddress> : auth GET, "/api/v2/withdraw_addresses");

// =========
// Responses
// =========

/// Withdrawal detail
#[derive(Deserialize, Eq, PartialEq, Default, Debug)]
#[serde(default)]
pub struct RespWithdrawalDetail {
    /// uuid (string, optional): unique withdraw id.
    pub uuid: String,
    /// currency (string, optional): currency id.
    pub currency: String,
    /// currency_version (string, optional): currency version id.
    pub currency_version: String,
    /// amount (string, optional): withdraw amount.
    pub amount: Decimal,
    /// fee (string, optional): withdraw fee.
    pub fee: Decimal,
    /// fee_currency (string, optional): withdraw fee currency.
    pub fee_currency: String,
    /// txid (string, optional): transaction id.
    pub txid: Option<String>,
    /// created_at (integer, optional): created timestamp (second).
    #[serde(with = "chrono_serde::ts_seconds_option")]
    pub created_at: Option<DateTime>,
    /// updated_at (integer, optional): lastest updated timestamp (second).
    #[serde(with = "chrono_serde::ts_seconds_option")]
    pub updated_at: Option<DateTime>,
    /// state (string, optional): current state.
    pub state: WithdrawalState,
}

/// Response of a withdrawal submission
#[derive(Deserialize, Eq, PartialEq, Debug)]
pub struct RespCreatedWithdraw {
    /// Withdrawal detail.
    #[serde(flatten)]
    pub detail: RespWithdrawalDetail,
    /// type (string, optional): internal/external transfer.
    #[serde(default, rename = "type")]
    pub transaction_direction: TransactionDirection,
    /// transaction_type (string, optional): transaction type.
    pub transaction_type: String,
    /// notes (string, optional): withdraw note.
    pub notes: Option<String>,
    /// sender (object, optional): sender mask email.
    ///
    /// Note: the actual type is string.
    pub sender: Option<String>,
    /// recipient (object, optional): recipient address.
    ///
    /// Note: the actual type is string.
    pub recipient: Option<String>,
}

// ============================
// Inner structures and options
// ============================

/// Possible withdraw states.
#[derive(Serialize, Deserialize, Copy, Clone, Eq, PartialEq, Debug)]
#[serde(rename_all = "snake_case")]
pub enum WithdrawalState {
    Submitting,
    Submitted,
    Rejected,
    Accepted,
    Suspect,
    Approved,
    DelistedProcessing,
    Reviewing,
    Processing,
    Retryable,
    Sent,
    Canceled,
    Failed,
    Pending,
    Confirmed,
    Overdue,
    KgiManuallyProcessing,
    KgiManuallyConfirmed,
    KgiPossibleFailed,
    SygnaVerifying,
    Unknown,
}

impl WithdrawalState {
    pub fn is_unknown(&self) -> bool {
        self == &Self::Unknown
    }
}

impl Default for WithdrawalState {
    fn default() -> Self {
        Self::Unknown
    }
}

/// Response of a withdrawal submission.
#[derive(Deserialize, Eq, PartialEq, Debug)]
#[serde(rename_all = "lowercase")]
pub enum TransactionDirection {
    Internal,
    External,
    Unknown,
}

impl TransactionDirection {
    pub fn is_unknown(&self) -> bool {
        self == &Self::Unknown
    }
}

impl Default for TransactionDirection {
    fn default() -> Self {
        Self::Unknown
    }
}

/// Withdraw address state: unverified/verified/disabled.
#[derive(Deserialize, Eq, PartialEq, Debug)]
#[serde(rename_all = "lowercase")]
pub enum WithdrawAddressState {
    Unverified,
    Verified,
    Disabled,
    Unknown,
}

impl WithdrawAddressState {
    pub fn is_unknown(&self) -> bool {
        self == &Self::Unknown
    }
}

impl Default for WithdrawAddressState {
    fn default() -> Self {
        Self::Unknown
    }
}

/// Withdraw address.
#[derive(Deserialize, Eq, PartialEq, Default, Debug)]
pub struct WithdrawAddress {
    /// uuid (string, optional): unique withdraw address id.
    pub uuid: String,
    /// currency (string, optional): currency id.
    pub currency: String,
    /// currency_version (string, optional): currency version id.
    pub currency_version: String,
    /// currency_protocol_name (string, optional).
    pub currency_protocol_name: Option<String>,
    /// address (string, optional): address, - for bank account.
    pub address: String,
    /// extra_label (string, optional): descriptive label, null for EOS; bank name for bank account.
    pub extra_label: String,
    /// created_at (integer, optional): created timestamp (second).
    #[serde(with = "chrono_serde::ts_seconds_option")]
    pub created_at: Option<DateTime>,
    /// deleted_at (integer, optional): deleted timestamp (second).
    #[serde(with = "chrono_serde::ts_seconds_option")]
    pub deleted_at: Option<DateTime>,
    /// state (string, optional): bank account state (unverified/verified/disabled), nil for others.
    pub state: Option<WithdrawAddressState>,
    /// sygna_vasp_code (string, optional): sygna vasp code.
    pub sygna_vasp_code: Option<String>,
    /// sygna_user_type (string, optional): sygna user type.
    pub sygna_user_type: Option<String>,
    /// sygna_user_code (string, optional): sygna user code.
    pub sygna_user_code: Option<String>,
    /// is_internal (boolean, optional): internal address or not.
    pub is_internal: Option<bool>,
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::util::test_util::*;
    use chrono::{TimeZone, Utc};
    use rust_decimal_macros::dec;
    use surf::Client as HTTPClient;
    use surf_vcr::VcrMode;

    async fn create_client(cassette: &'static str) -> HTTPClient {
        let mut path_builder = test_resource_path();
        path_builder.push("rest");
        path_builder.push("private");
        path_builder.push("withdrawal");
        path_builder.push(cassette);
        create_test_recording_client(VcrMode::Replay, path_builder.as_path().to_str().unwrap())
            .await
    }

    #[async_std::test]
    async fn get_single_withdrawal() {
        let params = GetWithdrawal {
            uuid: "211120074215374658171".into(),
        };
        let resp = create_client("get_single_withdrawal.yaml")
            .await
            .send(params.to_request(&TEST_CREDENTIALS))
            .await
            .expect("Error while sending request");
        let result: RespWithdrawalDetail = GetWithdrawal::read_response(resp.into()).await.unwrap();
        assert_eq!(
            result,
            RespWithdrawalDetail {
                uuid: "(test erased uuid)".into(),
                currency: "sol".into(),
                currency_version: "sol".into(),
                amount: dec!(1.0),
                fee: dec!(4.21265078),
                fee_currency: "max".into(),
                txid: Some("(test erased txid)".into()),
                created_at: Some(Utc.timestamp(1637394145, 0)),
                updated_at: Some(Utc.timestamp(1637394215, 0)),
                state: WithdrawalState::Confirmed,
            }
        );
    }

    #[async_std::test]
    async fn get_all_withdrawal() {
        let params = GetWithdrawals {
            currency: Some("sol".into()),
            from_timestamp: None,
            to_timestamp: None,
            state: None,
            pagination: None,
            page_params: None,
            offset: None,
        };
        let resp = create_client("get_all_withdrawal.yaml")
            .await
            .send(params.to_request(&TEST_CREDENTIALS))
            .await
            .expect("Error while sending request");
        let result: Vec<RespWithdrawalDetail> =
            GetWithdrawals::read_response(resp.into()).await.unwrap();
        assert_eq!(
            result,
            vec![
                RespWithdrawalDetail {
                    uuid: "(test erased uuid)".into(),
                    currency: "sol".into(),
                    currency_version: "sol".into(),
                    amount: dec!(1.0),
                    fee: dec!(4.21265078),
                    fee_currency: "max".into(),
                    txid: Some("(test erased txid)".into()),
                    created_at: Some(Utc.timestamp(1637394145, 0)),
                    updated_at: Some(Utc.timestamp(1637394215, 0)),
                    state: WithdrawalState::Confirmed,
                },
                RespWithdrawalDetail {
                    uuid: "(test erased uuid)".into(),
                    currency: "sol".into(),
                    currency_version: "sol".into(),
                    amount: dec!(4.32),
                    fee: dec!(4.60232158),
                    fee_currency: "max".into(),
                    txid: Some("(test erased txid)".into()),
                    created_at: Some(Utc.timestamp(1635983513, 0)),
                    updated_at: Some(Utc.timestamp(1635983641, 0)),
                    state: WithdrawalState::Confirmed,
                }
            ]
        );
    }

    #[async_std::test]
    async fn create_withdrawal() {
        let params = CreateWithdrawal {
            currency: "sol".into(),
            withdraw_address_uuid: "f79ad0c7-c321-4234-b0b3-4b3f8445dee9".into(),
            amount: dec!(1),
        };
        let resp = create_client("create_withdrawal.yaml")
            .await
            .send(params.to_request(&TEST_CREDENTIALS))
            .await
            .expect("Error while sending request");
        let result: RespCreatedWithdraw =
            CreateWithdrawal::read_response(resp.into()).await.unwrap();
        assert_eq!(
            result,
            RespCreatedWithdraw {
                detail: RespWithdrawalDetail {
                    uuid: "(test erased uuid)".into(),
                    currency: "sol".into(),
                    currency_version: "sol".into(),
                    amount: dec!(1.0),
                    fee: dec!(4.21265078),
                    fee_currency: "max".into(),
                    txid: None,
                    created_at: Some(Utc.timestamp(1637394145, 0)),
                    updated_at: Some(Utc.timestamp(1637394145, 0)),
                    state: WithdrawalState::Submitted,
                },
                transaction_direction: TransactionDirection::External,
                transaction_type: "external_send".into(),
                notes: None,
                sender: Some("(test erased sender)".into()),
                recipient: Some("(test erased recipient)".into()),
            }
        );
    }

    #[async_std::test]
    async fn get_withdraw_addresses() {
        let params = GetWithdrawAddresses {
            currency: "sol".into(),
            pagination: None,
            page_params: None,
            offset: None,
        };
        let resp = create_client("get_withdraw_addresses.yaml")
            .await
            .send(params.to_request(&TEST_CREDENTIALS))
            .await
            .expect("Error while sending request");
        let result: Vec<WithdrawAddress> = GetWithdrawAddresses::read_response(resp.into())
            .await
            .unwrap();
        assert_eq!(
            result,
            vec![WithdrawAddress {
                uuid: "(test erased uuid)".into(),
                currency: "sol".into(),
                currency_version: "sol".into(),
                currency_protocol_name: None,
                address: "(test erased address)".to_string(),
                extra_label: "(test erased extra_label)".to_string(),
                created_at: Some(Utc.timestamp(1635983472, 0)),
                deleted_at: None,
                state: None,
                sygna_vasp_code: None,
                sygna_user_type: None,
                sygna_user_code: None,
                is_internal: Some(false),
            }]
        );
    }
}