feat(data): native Binance REST kline fetcher in 9 languages (#315)

Adds `BinanceRest::fetch_klines` to `wickra-data`: a blocking historical kline
downloader (`GET /api/v3/klines`) and the historical counterpart to the F4 live
`BinanceFeed`. It is the last native data-layer primitive needed to drop
third-party HTTP/JSON download helpers (`jackson`, `jsonlite`, `urllib`, …) from
the examples.

## What

- **Core** (`wickra-data`): `fetch_klines(symbol, interval, limit, start?, end?)`
  built on `ureq` with native-tls — sharing the exact same TLS backend
  (native-tls 0.2 / SChannel) as the existing tokio-tungstenite live feed, so the
  two pull one TLS stack, not two. Parses Binance's 12-element array rows via the
  existing serde infrastructure into validated `Candle`s. Blocking by design (a
  one-shot request needs no async runtime; the FFI boundary is synchronous
  anyway). Nine mock-HTTP-server tests cover parse / empty / limit / transport /
  JSON / invariant-violation paths.
- **C ABI**: `wickra_binance_fetch_klines(...)` (blocking drain into a caller
  buffer, `-1` on error) + regenerated cbindgen header and its vendored Go copy.
- **Bindings**: native Node `fetchBinanceKlines` / Python `fetch_binance_klines`;
  generated Go `FetchBinanceKlines` / C# `BinanceFeed.FetchKlines` / Java
  `BinanceFeed.fetchKlines` / R `fetch_binance_klines`. C / C++ call the C ABI
  directly. **WASM is excluded** (browsers use the host `fetch`).

The four C-ABI bindings are regenerated from the ScriptHelpers generators (not
hand-edited); the regen diff is exactly the new wrapper in each.

## Verification

All ten toolchains green locally: Rust (`cargo test`/`clippy`/`fmt`), Node, Python,
Go, C#, Java, R (`R CMD INSTALL` + smoke), WASM (`cargo check`, confirmed `ureq`
is not pulled). Each binding has an error-path smoke test; the parse/HTTP success
path is covered by the Rust mock-server tests.

No release in this PR — ships with the data-layer + numpy bundle later.
This commit is contained in:
kingchenc
2026-06-17 01:48:24 +02:00
committed by GitHub
parent b92ad32037
commit 2ae76bb90e
29 changed files with 863 additions and 209 deletions
+116
View File
@@ -68572,6 +68572,71 @@ pub unsafe extern "C" fn wickra_binance_free(handle: *mut BinanceStream) {
}
}
/// Fetch historical klines from Binance's REST endpoint into `out` (up to `cap`
/// candles). `symbol` is the trading pair (case-insensitive, e.g. `"BTCUSDT"`),
/// `interval` the code `0..=15` (the `Interval` declaration order), and `limit`
/// the number of candles to request (`1..=1000`). `start_ms`/`end_ms` are
/// inclusive Unix-millisecond bounds; pass a negative value for "unset".
/// `base_url` overrides the host (`NULL` = production `https://api.binance.com`;
/// pass an `http://…` URL to target a test server). This blocks until the HTTP
/// response arrives. Returns the number of candles written (`<= cap`), or `-1`
/// on a null/invalid symbol, an unknown interval, an out-of-range limit, a
/// transport/JSON error, or a `NULL` `out`.
///
/// # Safety
/// `symbol` must be a valid NUL-terminated UTF-8 C string; `base_url` must be
/// `NULL` or a valid NUL-terminated UTF-8 C string; when non-`NULL`, `out` must
/// cover `cap` `WickraCandle` elements.
#[cfg(feature = "live-binance")]
#[no_mangle]
pub unsafe extern "C" fn wickra_binance_fetch_klines(
symbol: *const c_char,
interval: u8,
limit: u32,
start_ms: i64,
end_ms: i64,
base_url: *const c_char,
out: *mut WickraCandle,
cap: usize,
) -> isize {
if symbol.is_null() || out.is_null() {
return -1;
}
let Ok(symbol_str) = core::ffi::CStr::from_ptr(symbol).to_str() else {
return -1;
};
let Some(interval) = binance_interval(interval) else {
return -1;
};
let Ok(limit) = u16::try_from(limit) else {
return -1;
};
let start = (start_ms >= 0).then_some(start_ms);
let end = (end_ms >= 0).then_some(end_ms);
let fetched = if base_url.is_null() {
wickra_data::live::binance_rest::fetch_klines(symbol_str, interval, limit, start, end)
} else {
let Ok(url) = core::ffi::CStr::from_ptr(base_url).to_str() else {
return -1;
};
let config = wickra_data::live::binance_rest::BinanceRestConfig {
base_url: url.to_owned(),
};
wickra_data::live::binance_rest::fetch_klines_with_config(
symbol_str, interval, limit, start, end, &config,
)
};
let Ok(candles) = fetched else {
return -1;
};
let count = candles.len().min(cap);
let slots = slice::from_raw_parts_mut(out, count);
for (slot, candle) in slots.iter_mut().zip(&candles[..count]) {
*slot = candle_to_c(*candle);
}
isize::try_from(count).unwrap_or(isize::MAX)
}
#[cfg(test)]
mod tests {
use super::*;
@@ -68621,4 +68686,55 @@ mod tests {
wickra_sma_free(ptr::null_mut());
}
}
#[cfg(feature = "live-binance")]
#[test]
fn fetch_klines_rejects_invalid_arguments() {
// Each of these short-circuits to -1 before any network access, so the
// test is deterministic and offline.
unsafe {
let symbol = c"BTCUSDT".as_ptr();
let mut out = [WickraCandle {
open: 0.0,
high: 0.0,
low: 0.0,
close: 0.0,
volume: 0.0,
timestamp: 0,
}; 4];
// Null symbol.
assert_eq!(
wickra_binance_fetch_klines(
ptr::null(),
6,
1,
-1,
-1,
ptr::null(),
out.as_mut_ptr(),
4
),
-1
);
// Null output buffer.
assert_eq!(
wickra_binance_fetch_klines(symbol, 6, 1, -1, -1, ptr::null(), ptr::null_mut(), 4),
-1
);
// Unknown interval code.
assert_eq!(
wickra_binance_fetch_klines(
symbol,
99,
1,
-1,
-1,
ptr::null(),
out.as_mut_ptr(),
4
),
-1
);
}
}
}