maxt 0.2.1

One Rust API for Upbit, Bithumb, Binance, and Hyperliquid market data, accounts, and orders.
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
535
536
537
//! Bithumb public REST requests and response handling.

use serde_json::Value;

use crate::adapters::candles as candle_pages;
use crate::error::{Error, Result};
use crate::feature::Feature;
use crate::request::CandleRequest;
use crate::transport::{HttpRequest, HttpTransport};
use crate::types::{
    Candle, Interval, Market, MarketInfo, MarketKind, OrderBook, Ticker, Timestamp, Trade,
};

use super::parse::{self, EXCHANGE};

/// Maximum recent trades in one Bithumb response.
const MAX_TRADE_COUNT: u32 = 500;
/// Maximum order-book levels per side in one Bithumb response.
const MAX_BOOK_DEPTH: u32 = 30;
/// Maximum candles in one Bithumb response.
const MAX_CANDLE_COUNT: u32 = 200;
/// Maximum notices in one Bithumb response.
const MAX_NOTICE_COUNT: u32 = 20;

/// Percent-encodes one query value.
fn encode(raw: &str) -> String {
    raw.bytes()
        .map(|byte| match byte {
            b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'.' | b'_' | b'~' => {
                char::from(byte).to_string()
            }
            _ => format!("%{byte:02X}"),
        })
        .collect()
}

pub(crate) fn query(params: &[(&str, String)]) -> String {
    params
        .iter()
        .map(|(key, value)| format!("{key}={}", encode(value)))
        .collect::<Vec<_>>()
        .join("&")
}

pub(crate) fn markets_request() -> HttpRequest {
    // `isDetails` adds the investment-warning flag used by `MarketStatus`.
    HttpRequest::get("/v1/market/all").query("isDetails=true")
}

/// Builds the separate alert-system request (경보제).
pub(crate) fn market_alerts_request() -> HttpRequest {
    HttpRequest::get("/v1/market/virtual_asset_warning")
}

/// Builds Bithumb's public exchange-notice request.
pub(crate) fn notices_request(count: Option<u32>) -> Result<HttpRequest> {
    let mut request = HttpRequest::get("/v1/notices");
    if let Some(count) = count {
        if !(1..=MAX_NOTICE_COUNT).contains(&count) {
            return Err(Error::invalid_request(
                "count",
                format!("bithumb serves 1 to {MAX_NOTICE_COUNT} notices per call, not {count}"),
            ));
        }
        request = request.query(format!("count={count}"));
    }
    Ok(request)
}

/// Builds Bithumb's public transfer-fee catalog request.
pub(crate) fn transfer_fees_request(currency: &str) -> Result<HttpRequest> {
    let currency = currency.trim();
    if currency.is_empty() {
        return Err(Error::invalid_request(
            "currency",
            "currency must not be empty",
        ));
    }
    let currency = currency.to_ascii_uppercase();
    Ok(HttpRequest::get(format!(
        "/v2/fee/inout/{}",
        encode(&currency)
    )))
}

pub(crate) fn trades_request(market: &Market, limit: Option<u32>) -> Result<HttpRequest> {
    let mut params = vec![("market", parse::native_symbol(market)?)];
    if let Some(limit) = limit {
        if !(1..=MAX_TRADE_COUNT).contains(&limit) {
            return Err(Error::invalid_request(
                "limit",
                format!("bithumb serves 1 to {MAX_TRADE_COUNT} trades per call, not {limit}"),
            ));
        }
        params.push(("count", limit.to_string()));
    }

    Ok(HttpRequest::get("/v1/trades/ticks").query(query(&params)))
}

pub(crate) fn order_book_request(market: &Market, depth: Option<u32>) -> Result<HttpRequest> {
    if let Some(depth) = depth {
        if !(1..=MAX_BOOK_DEPTH).contains(&depth) {
            return Err(Error::invalid_request(
                "depth",
                format!("bithumb serves 1 to {MAX_BOOK_DEPTH} levels per side, not {depth}"),
            ));
        }
    }

    // Bithumb has no depth parameter; the parsed book is truncated locally.
    let params = [("markets", parse::native_symbol(market)?)];

    Ok(HttpRequest::get("/v1/orderbook").query(query(&params)))
}

pub(crate) fn ticker_request(market: &Market) -> Result<HttpRequest> {
    let params = [("markets", parse::native_symbol(market)?)];

    Ok(HttpRequest::get("/v1/ticker").query(query(&params)))
}

/// Returns the endpoint for an interval supported by both Bithumb and `Interval`.
pub(crate) fn candle_path(interval: Interval) -> Option<&'static str> {
    Some(match interval {
        Interval::Min1 => "/v1/candles/minutes/1",
        Interval::Min3 => "/v1/candles/minutes/3",
        Interval::Min5 => "/v1/candles/minutes/5",
        Interval::Min10 => "/v1/candles/minutes/10",
        Interval::Min15 => "/v1/candles/minutes/15",
        Interval::Min30 => "/v1/candles/minutes/30",
        Interval::Hour1 => "/v1/candles/minutes/60",
        Interval::Hour4 => "/v1/candles/minutes/240",
        Interval::Day1 => "/v1/candles/days",
        Interval::Week1 => "/v1/candles/weeks",
        Interval::Month1 => "/v1/candles/months",
        _ => return None,
    })
}

/// Builds one candle page ending before the optional exclusive cursor.
pub(crate) fn candles_request(
    request: &CandleRequest,
    cursor: Option<Timestamp>,
    count: u32,
) -> Result<HttpRequest> {
    let Some(path) = candle_path(request.interval) else {
        return Err(Error::unsupported(
            Feature::Candles,
            EXCHANGE,
            format!(
                "bithumb publishes candles at 1m, 3m, 5m, 10m, 15m, 30m, 1h, 4h, 1d, 1w and 1M, \
                 and none at {:?}; every one of those it serves and `Interval` can name is \
                 mapped, so this is absent from bithumb rather than missing from `maxt`",
                request.interval
            ),
        ));
    };

    let mut params = vec![("market", parse::native_symbol(&request.market)?)];
    if let Some(cursor) = cursor {
        params.push(("to", kst_wall_clock(cursor)?));
    }
    params.push(("count", count.to_string()));

    Ok(HttpRequest::get(path).query(query(&params)))
}

/// Formats an exclusive UTC cursor as Bithumb's second-resolution KST wall clock.
///
/// Subsecond values round up so the exclusive bound is preserved.
fn kst_wall_clock(cursor: Timestamp) -> Result<String> {
    const KST_OFFSET_SECS: i64 = 9 * 3_600;

    let nanos = cursor.as_nanos();
    let secs = nanos.div_euclid(1_000_000_000) + i64::from(nanos.rem_euclid(1_000_000_000) != 0);

    secs.checked_add(KST_OFFSET_SECS)
        .and_then(|secs| chrono::DateTime::<chrono::Utc>::from_timestamp(secs, 0))
        .map(|kst| kst.format("%Y-%m-%dT%H:%M:%S").to_string())
        .ok_or_else(|| Error::invalid_request("to", format!("{cursor} is not a calendar date")))
}

fn read_response(status: u16, body: &str) -> Result<Value> {
    if !(200..300).contains(&status) {
        return Err(parse::exchange_error(status, body));
    }

    let value = parse::body(body)?;
    if value.get("error").is_some() {
        return Err(parse::exchange_error(status, body));
    }

    Ok(value)
}

/// Sends a request and maps Bithumb error envelopes to [`Error::Exchange`],
/// including envelopes returned with a successful HTTP status.
pub(crate) async fn send(http: &HttpTransport, request: &HttpRequest) -> Result<Value> {
    let response = http.send(request).await?;

    read_response(response.status, &response.body)
}

pub(crate) async fn markets(http: &HttpTransport, kind: MarketKind) -> Result<Vec<MarketInfo>> {
    // Bithumb lists spot markets only.
    if kind != MarketKind::Spot {
        return Ok(Vec::new());
    }

    parse::markets(&send(http, &markets_request()).await?)
}

pub(crate) async fn market_warnings(http: &HttpTransport) -> Result<Vec<(Market, String)>> {
    parse::market_warnings(&send(http, &markets_request()).await?)
}

pub(crate) async fn market_alerts(
    http: &HttpTransport,
) -> Result<Vec<(Market, super::BithumbMarketAlert)>> {
    parse::market_alerts(&send(http, &market_alerts_request()).await?)
}

pub(crate) async fn notices(
    http: &HttpTransport,
    count: Option<u32>,
) -> Result<Vec<super::BithumbNotice>> {
    parse::notices(&send(http, &notices_request(count)?).await?)
}

pub(crate) async fn transfer_fees(
    http: &HttpTransport,
    currency: &str,
) -> Result<Vec<super::BithumbAssetFee>> {
    parse::transfer_fees(&send(http, &transfer_fees_request(currency)?).await?)
}

pub(crate) async fn trades(
    http: &HttpTransport,
    market: &Market,
    limit: Option<u32>,
) -> Result<Vec<Trade>> {
    parse::trades(&send(http, &trades_request(market, limit)?).await?)
}

pub(crate) async fn order_book(
    http: &HttpTransport,
    market: &Market,
    depth: Option<u32>,
) -> Result<OrderBook> {
    let body = send(http, &order_book_request(market, depth)?).await?;
    let entry = only(&body, market)?;
    let timestamp = parse::millis(entry, "timestamp")?;

    parse::order_book(entry, market.clone(), timestamp, depth)
}

pub(crate) async fn ticker(http: &HttpTransport, market: &Market) -> Result<Ticker> {
    let body = send(http, &ticker_request(market)?).await?;

    parse::ticker(only(&body, market)?, market.clone())
}

/// Extracts the only entry expected from a single-market request.
fn only<'a>(body: &'a Value, market: &Market) -> Result<&'a Value> {
    match body.as_array().map(Vec::as_slice) {
        Some([entry]) => Ok(entry),
        Some(entries) => Err(Error::decode(format!(
            "bithumb answered with {} entries for {market}, expected 1",
            entries.len()
        ))),
        None => Err(Error::decode("expected a JSON array")),
    }
}

/// Reads candles oldest-first, paging backward from Bithumb's exclusive cursor.
///
/// Each response is capped at [`MAX_CANDLE_COUNT`]. Paging follows Bithumb's
/// UTC+09:00 candle grid.
pub(crate) async fn candles(http: &HttpTransport, request: &CandleRequest) -> Result<Vec<Candle>> {
    let now = Timestamp::now();
    let interval = request.interval;

    candle_pages::read_on_grid(
        request,
        EXCHANGE,
        MAX_CANDLE_COUNT,
        move |at, count| parse::advance_open(interval, at, count),
        |end, count| async move {
            let body = send(http, &candles_request(request, end, count)?).await?;

            parse::candles(&body, request.interval, now)
        },
    )
    .await
}

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

    fn btc_krw() -> Market {
        Market::spot(Exchange::Bithumb, "BTC", "KRW")
    }

    fn candle_request(interval: Interval) -> CandleRequest {
        CandleRequest::new(btc_krw(), interval)
    }

    #[test]
    fn public_requests_target_the_documented_paths() {
        assert_eq!(markets_request().target(), "/v1/market/all?isDetails=true");
        assert_eq!(
            notices_request(None)
                .expect("the documented default")
                .target(),
            "/v1/notices"
        );
        assert_eq!(
            notices_request(Some(20))
                .expect("the documented maximum")
                .target(),
            "/v1/notices?count=20"
        );
        assert_eq!(
            transfer_fees_request("btc")
                .expect("one asset fee catalog")
                .target(),
            "/v2/fee/inout/BTC"
        );
        assert_eq!(
            transfer_fees_request("ALL")
                .expect("complete fee catalog")
                .target(),
            "/v2/fee/inout/ALL"
        );
        assert_eq!(
            trades_request(&btc_krw(), Some(10))
                .expect("a valid limit")
                .target(),
            "/v1/trades/ticks?market=KRW-BTC&count=10"
        );
        assert_eq!(
            order_book_request(&btc_krw(), Some(5))
                .expect("a valid depth")
                .target(),
            "/v1/orderbook?markets=KRW-BTC"
        );
        assert_eq!(
            ticker_request(&btc_krw()).expect("a market").target(),
            "/v1/ticker?markets=KRW-BTC"
        );
    }

    #[test]
    fn each_interval_reaches_the_endpoint_that_serves_it() {
        // Shape references: https://apidocs.bithumb.com/reference/분minute-캔들-조회.md
        // and https://apidocs.bithumb.com/reference/월month-캔들-조회.md
        let cases = [
            (Interval::Min1, "/v1/candles/minutes/1"),
            (Interval::Min3, "/v1/candles/minutes/3"),
            (Interval::Min5, "/v1/candles/minutes/5"),
            (Interval::Min10, "/v1/candles/minutes/10"),
            (Interval::Min15, "/v1/candles/minutes/15"),
            (Interval::Min30, "/v1/candles/minutes/30"),
            (Interval::Hour1, "/v1/candles/minutes/60"),
            (Interval::Hour4, "/v1/candles/minutes/240"),
            (Interval::Day1, "/v1/candles/days"),
            (Interval::Week1, "/v1/candles/weeks"),
            (Interval::Month1, "/v1/candles/months"),
        ];

        for (interval, path) in cases {
            assert_eq!(
                candles_request(&candle_request(interval), None, 200)
                    .expect("a served interval")
                    .target(),
                format!("{path}?market=KRW-BTC&count=200"),
                "{interval:?}"
            );
        }
    }

    #[test]
    fn an_interval_bithumb_does_not_aggregate_is_refused_as_the_exchanges_gap() {
        // Unsupported intervals must not be mapped to a neighboring endpoint.
        for interval in [
            Interval::Sec1,
            Interval::Hour2,
            Interval::Hour6,
            Interval::Hour8,
            Interval::Hour12,
            Interval::Day3,
        ] {
            let Err(Error::Unsupported {
                feature: Feature::Candles,
                exchange: "bithumb",
                detail,
            }) = candles_request(&candle_request(interval), None, 1)
            else {
                panic!("{interval:?} should not silently become another interval");
            };
            assert!(
                detail.contains("bithumb publishes candles at"),
                "{interval:?} was refused without saying what bithumb serves: {detail}"
            );
            assert!(
                !detail.contains("no endpoint mapped"),
                "{interval:?} was refused as a gap in `maxt`: {detail}"
            );
        }
    }

    #[test]
    fn a_cursor_is_sent_as_korean_wall_clock_and_percent_encoded() {
        // 2017-07-03T00:00:00Z is 09:00 KST.
        let cursor = Timestamp::from_secs(1_499_040_000);

        assert_eq!(
            kst_wall_clock(cursor).expect("a representable date"),
            "2017-07-03T09:00:00"
        );
        assert_eq!(
            candles_request(&candle_request(Interval::Min5), Some(cursor), 2)
                .expect("a served interval")
                .target(),
            "/v1/candles/minutes/5?market=KRW-BTC&to=2017-07-03T09%3A00%3A00&count=2"
        );
    }

    #[test]
    fn a_sub_second_cursor_is_rounded_up_before_kst_formatting() {
        let half_past = Timestamp::from_millis(1_785_394_320_500);

        assert_eq!(
            kst_wall_clock(half_past).expect("a representable date"),
            "2026-07-30T15:52:01"
        );
        assert_eq!(
            candles_request(&candle_request(Interval::Min1), Some(half_past), 1)
                .expect("a served interval")
                .target(),
            "/v1/candles/minutes/1?market=KRW-BTC&to=2026-07-30T15%3A52%3A01&count=1"
        );

        let whole_second = Timestamp::from_secs(1_785_394_320);
        assert_eq!(
            kst_wall_clock(whole_second).expect("a representable date"),
            "2026-07-30T15:52:00"
        );
    }

    #[test]
    fn every_limit_outside_bithumbs_range_is_refused_rather_than_clamped() {
        for limit in [0, 501, u32::MAX] {
            assert!(
                matches!(
                    trades_request(&btc_krw(), Some(limit)),
                    Err(Error::InvalidRequest { field, .. }) if field == "limit"
                ),
                "{limit}"
            );
        }
        assert!(trades_request(&btc_krw(), Some(MAX_TRADE_COUNT)).is_ok());
        assert!(matches!(
            order_book_request(&btc_krw(), Some(0)),
            Err(Error::InvalidRequest { field, .. }) if field == "depth"
        ));
        assert!(matches!(
            order_book_request(&btc_krw(), Some(31)),
            Err(Error::InvalidRequest { field, .. }) if field == "depth"
        ));
        assert!(order_book_request(&btc_krw(), Some(30)).is_ok());
        for count in [0, 21, u32::MAX] {
            assert!(matches!(
                notices_request(Some(count)),
                Err(Error::InvalidRequest { field, .. }) if field == "count"
            ));
        }
        assert!(notices_request(Some(MAX_NOTICE_COUNT)).is_ok());
        assert!(matches!(
            transfer_fees_request("  "),
            Err(Error::InvalidRequest { field, .. }) if field == "currency"
        ));
    }

    #[test]
    fn the_caps_are_the_ones_bithumb_documents() {
        assert_eq!(MAX_TRADE_COUNT, 500);
        assert_eq!(MAX_BOOK_DEPTH, 30);
        assert_eq!(MAX_CANDLE_COUNT, 200);
        assert_eq!(MAX_NOTICE_COUNT, 20);
    }

    #[test]
    fn a_market_from_another_exchange_never_reaches_the_wire() {
        let elsewhere = Market::spot(Exchange::Upbit, "BTC", "KRW");

        assert!(trades_request(&elsewhere, None).is_err());
        assert!(ticker_request(&elsewhere).is_err());
        assert!(order_book_request(&elsewhere, None).is_err());
    }

    #[test]
    fn an_error_envelope_on_a_success_status_is_an_exchange_error() {
        let error = read_response(200, r#"{"error":{"name":404,"message":"Code not found"}}"#)
            .expect_err("Bithumb returned an error envelope");

        let Error::Exchange {
            code,
            message,
            status,
            ..
        } = error
        else {
            panic!("expected an exchange error");
        };
        assert_eq!(code, "404");
        assert_eq!(message, "Code not found");
        assert_eq!(status, Some(200));
    }

    #[test]
    fn a_multi_market_answer_to_a_single_market_question_is_a_decode_error() {
        let one = parse::body(r#"[{"market":"KRW-BTC"}]"#).expect("JSON");
        let two = parse::body(r#"[{"market":"KRW-BTC"},{"market":"KRW-ETH"}]"#).expect("JSON");
        let neither = parse::body(r#"{"market":"KRW-BTC"}"#).expect("JSON");

        assert!(only(&one, &btc_krw()).is_ok());
        assert!(matches!(only(&two, &btc_krw()), Err(Error::Decode { .. })));
        assert!(matches!(
            only(&neither, &btc_krw()),
            Err(Error::Decode { .. })
        ));
    }
}