ccxt-exchanges 0.1.5

Exchange implementations for CCXT 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
#![allow(dead_code)]

use ccxt_core::{
    Result,
    error::{Error, ParseError},
    types::{
        DepositAddress, Transaction, TransactionFee, TransactionStatus, TransactionType, Transfer,
    },
};
use rust_decimal::Decimal;
use rust_decimal::prelude::FromStr;
use serde_json::Value;

/// Check if a currency is a fiat currency.
pub fn is_fiat_currency(currency: &str) -> bool {
    matches!(
        currency.to_uppercase().as_str(),
        "USD" | "EUR" | "GBP" | "JPY" | "CNY" | "KRW" | "AUD" | "CAD" | "CHF" | "HKD" | "SGD"
    )
}

/// Extract internal transfer ID from transaction ID.
pub fn extract_internal_transfer_id(txid: &str) -> String {
    const PREFIX: &str = "Internal transfer ";
    txid.strip_prefix(PREFIX)
        .map_or_else(|| txid.to_string(), ToString::to_string)
}

/// Parse transaction status based on transaction type.
pub fn parse_transaction_status_by_type(
    status_value: &Value,
    is_deposit: bool,
) -> ccxt_core::types::TransactionStatus {
    if let Some(status_int) = status_value.as_i64() {
        if is_deposit {
            match status_int {
                1 | 6 => TransactionStatus::Ok,
                _ => TransactionStatus::Pending,
            }
        } else {
            match status_int {
                1 => TransactionStatus::Canceled,
                3 | 5 => TransactionStatus::Failed,
                6 => TransactionStatus::Ok,
                _ => TransactionStatus::Pending,
            }
        }
    } else if let Some(status_str) = status_value.as_str() {
        match status_str {
            "Failed" | "Refund Failed" => TransactionStatus::Failed,
            "Successful" => TransactionStatus::Ok,
            "Refunding" | "Refunded" => TransactionStatus::Canceled,
            _ => TransactionStatus::Pending,
        }
    } else {
        TransactionStatus::Pending
    }
}

/// Parse transaction (deposit or withdrawal) from Binance API response.
pub fn parse_transaction(
    data: &Value,
    transaction_type: ccxt_core::types::TransactionType,
) -> Result<ccxt_core::types::Transaction> {
    let is_deposit = matches!(transaction_type, TransactionType::Deposit);

    let id = if is_deposit {
        data["id"]
            .as_str()
            .or_else(|| data["orderNo"].as_str())
            .unwrap_or("")
            .to_string()
    } else {
        data["id"]
            .as_str()
            .or_else(|| data["withdrawOrderId"].as_str())
            .unwrap_or("")
            .to_string()
    };

    let currency = data["coin"]
        .as_str()
        .or_else(|| data["fiatCurrency"].as_str())
        .unwrap_or("")
        .to_string();

    let amount = data["amount"]
        .as_str()
        .and_then(|s| Decimal::from_str(s).ok())
        .unwrap_or(Decimal::ZERO);

    let fee = if is_deposit {
        None
    } else {
        data["transactionFee"]
            .as_str()
            .or_else(|| data["totalFee"].as_str())
            .and_then(|s| Decimal::from_str(s).ok())
            .map(|cost| TransactionFee {
                currency: currency.clone(),
                cost,
            })
    };

    let timestamp = if is_deposit {
        data["insertTime"].as_i64()
    } else {
        data["createTime"].as_i64().or_else(|| {
            data["applyTime"].as_str().and_then(|s| {
                chrono::NaiveDateTime::parse_from_str(s, "%Y-%m-%d %H:%M:%S")
                    .ok()
                    .map(|dt| dt.and_utc().timestamp_millis())
            })
        })
    };

    let datetime = timestamp.and_then(|ts| ccxt_core::time::iso8601(ts).ok());

    let network = data["network"].as_str().map(ToString::to_string);

    let address = data["address"]
        .as_str()
        .or_else(|| data["depositAddress"].as_str())
        .map(ToString::to_string);

    let tag = data["addressTag"]
        .as_str()
        .or_else(|| data["tag"].as_str())
        .filter(|s| !s.is_empty())
        .map(ToString::to_string);

    let mut txid = data["txId"]
        .as_str()
        .or_else(|| data["hash"].as_str())
        .map(ToString::to_string);

    let transfer_type = data["transferType"].as_i64();
    let is_internal = transfer_type == Some(1);

    if is_internal {
        if let Some(ref tx) = txid {
            txid = Some(extract_internal_transfer_id(tx));
        }
    }

    let status = if let Some(status_value) = data.get("status") {
        parse_transaction_status_by_type(status_value, is_deposit)
    } else {
        TransactionStatus::Pending
    };

    let updated = data["updateTime"].as_i64();

    let comment = data["info"]
        .as_str()
        .or_else(|| data["comment"].as_str())
        .map(ToString::to_string);

    Ok(Transaction {
        info: Some(data.clone()),
        id,
        txid,
        timestamp,
        datetime,
        network,
        address: address.clone(),
        address_to: if is_deposit { address.clone() } else { None },
        address_from: if is_deposit { None } else { address },
        tag: tag.clone(),
        tag_to: if is_deposit { tag.clone() } else { None },
        tag_from: if is_deposit { None } else { tag },
        transaction_type,
        amount,
        currency,
        status,
        updated,
        internal: Some(is_internal),
        comment,
        fee,
    })
}

/// Parse transfer record data from Binance universal transfer API.
pub fn parse_transfer(data: &Value) -> Result<Transfer> {
    let id = data["tranId"]
        .as_i64()
        .or_else(|| data["txId"].as_i64())
        .or_else(|| data["transactionId"].as_i64())
        .map(|id| id.to_string());

    let timestamp = data["timestamp"]
        .as_i64()
        .or_else(|| data["transactionTime"].as_i64())
        .unwrap_or_else(|| chrono::Utc::now().timestamp_millis());

    let datetime = chrono::DateTime::from_timestamp_millis(timestamp)
        .map(|dt| dt.to_rfc3339())
        .unwrap_or_default();

    let currency = data["asset"]
        .as_str()
        .or_else(|| data["currency"].as_str())
        .ok_or_else(|| Error::from(ParseError::missing_field("asset")))?
        .to_string();

    let amount = if let Some(amount_str) = data["amount"].as_str() {
        amount_str.parse::<f64>().unwrap_or(0.0)
    } else {
        data["amount"].as_f64().unwrap_or(0.0)
    };

    let mut from_account = data["fromAccountType"].as_str().map(ToString::to_string);

    let mut to_account = data["toAccountType"].as_str().map(ToString::to_string);

    if from_account.is_none() || to_account.is_none() {
        if let Some(type_str) = data["type"].as_str() {
            let parts: Vec<&str> = type_str.split('_').collect();
            if parts.len() == 2 {
                from_account = Some(parts[0].to_lowercase());
                to_account = Some(parts[1].to_lowercase());
            }
        }
    }

    let status = data["status"].as_str().unwrap_or("SUCCESS").to_lowercase();

    Ok(Transfer {
        id,
        timestamp,
        datetime,
        currency,
        amount,
        from_account,
        to_account,
        status,
        info: Some(data.clone()),
    })
}

/// Parse deposit address from Binance API response.
pub fn parse_deposit_address(data: &Value) -> Result<ccxt_core::types::DepositAddress> {
    let currency = data["coin"]
        .as_str()
        .ok_or_else(|| Error::from(ParseError::missing_field("coin")))?
        .to_string();

    let address = data["address"]
        .as_str()
        .ok_or_else(|| Error::from(ParseError::missing_field("address")))?
        .to_string();

    let network = data["network"]
        .as_str()
        .map(ToString::to_string)
        .or_else(|| {
            data["url"].as_str().and_then(|url| {
                if url.contains("btc.com") {
                    Some("BTC".to_string())
                } else if url.contains("etherscan.io") {
                    Some("ETH".to_string())
                } else if url.contains("tronscan.org") {
                    Some("TRX".to_string())
                } else {
                    None
                }
            })
        });

    let tag = data["tag"]
        .as_str()
        .or_else(|| data["addressTag"].as_str())
        .filter(|s| !s.is_empty())
        .map(ToString::to_string);

    Ok(DepositAddress {
        info: Some(data.clone()),
        currency,
        network,
        address,
        tag,
    })
}

/// Parse deposit and withdrawal fee information from Binance API.
pub fn parse_deposit_withdraw_fee(data: &Value) -> Result<ccxt_core::types::DepositWithdrawFee> {
    let currency = data["coin"]
        .as_str()
        .ok_or_else(|| Error::from(ParseError::missing_field("coin")))?
        .to_string();

    let mut networks = Vec::new();
    let mut withdraw_fee = 0.0;
    let mut withdraw_min = 0.0;
    let mut withdraw_max = 0.0;
    let mut deposit_enable = false;
    let mut withdraw_enable = false;

    if let Some(network_list) = data["networkList"].as_array() {
        for network_data in network_list {
            let network = parse_network_info(network_data)?;

            if network_data["isDefault"].as_bool().unwrap_or(false) {
                withdraw_fee = network.withdraw_fee;
                withdraw_min = network.withdraw_min;
                withdraw_max = network.withdraw_max;
                deposit_enable = network.deposit_enable;
                withdraw_enable = network.withdraw_enable;
            }

            networks.push(network);
        }
    }

    if !networks.is_empty() && withdraw_fee == 0.0 {
        let first = &networks[0];
        withdraw_fee = first.withdraw_fee;
        withdraw_min = first.withdraw_min;
        withdraw_max = first.withdraw_max;
        deposit_enable = first.deposit_enable;
        withdraw_enable = first.withdraw_enable;
    }

    Ok(ccxt_core::types::DepositWithdrawFee {
        currency,
        withdraw_fee,
        withdraw_min,
        withdraw_max,
        deposit_enable,
        withdraw_enable,
        networks,
        info: Some(data.clone()),
    })
}

/// Parse network information from Binance API.
pub fn parse_network_info(data: &Value) -> Result<ccxt_core::types::NetworkInfo> {
    let network = data["network"]
        .as_str()
        .ok_or_else(|| Error::from(ParseError::missing_field("network")))?
        .to_string();

    let name = data["name"].as_str().unwrap_or(&network).to_string();

    let withdraw_fee = data["withdrawFee"]
        .as_str()
        .and_then(|s| s.parse::<f64>().ok())
        .or_else(|| data["withdrawFee"].as_f64())
        .unwrap_or(0.0);

    let withdraw_min = data["withdrawMin"]
        .as_str()
        .and_then(|s| s.parse::<f64>().ok())
        .or_else(|| data["withdrawMin"].as_f64())
        .unwrap_or(0.0);

    let withdraw_max = data["withdrawMax"]
        .as_str()
        .and_then(|s| s.parse::<f64>().ok())
        .or_else(|| data["withdrawMax"].as_f64())
        .unwrap_or(0.0);

    let deposit_enable = data["depositEnable"].as_bool().unwrap_or(false);
    let withdraw_enable = data["withdrawEnable"].as_bool().unwrap_or(false);
    let deposit_confirmations = data["minConfirm"].as_u64().map(|v| v as u32);
    let withdraw_confirmations = data["unlockConfirm"].as_u64().map(|v| v as u32);

    Ok(ccxt_core::types::NetworkInfo {
        network,
        name,
        withdraw_fee,
        withdraw_min,
        withdraw_max,
        deposit_enable,
        withdraw_enable,
        deposit_confirmations,
        withdraw_confirmations,
    })
}

/// Parse multiple deposit and withdrawal fee information entries from Binance API.
pub fn parse_deposit_withdraw_fees(
    data: &Value,
) -> Result<Vec<ccxt_core::types::DepositWithdrawFee>> {
    if let Some(array) = data.as_array() {
        array
            .iter()
            .map(|item| parse_deposit_withdraw_fee(item))
            .collect()
    } else {
        Ok(vec![parse_deposit_withdraw_fee(data)?])
    }
}

/// Parse futures transfer type code from Binance API.
pub fn parse_futures_transfer_type(transfer_type: i32) -> Result<(&'static str, &'static str)> {
    match transfer_type {
        1 => Ok(("spot", "future")),
        2 => Ok(("future", "spot")),
        3 => Ok(("spot", "delivery")),
        4 => Ok(("delivery", "spot")),
        _ => Err(Error::invalid_request(format!(
            "Invalid futures transfer type: {}. Must be between 1 and 4",
            transfer_type
        ))),
    }
}