Skip to main content

ccxt_exchanges/okx/rest/futures/
funding.rs

1//! Funding rate operations for OKX futures/swap.
2
3use super::super::super::{Okx, parser};
4use ccxt_core::{
5    Error, ParseError, Result,
6    types::{FundingRate, FundingRateHistory},
7};
8use std::collections::HashMap;
9use tracing::warn;
10
11impl Okx {
12    /// Fetch current funding rate for a symbol.
13    ///
14    /// Uses OKX GET `/api/v5/public/funding-rate` (public endpoint).
15    pub async fn fetch_funding_rate_impl(&self, symbol: &str) -> Result<FundingRate> {
16        let inst_id = Self::symbol_to_inst_id(symbol);
17
18        let mut params = HashMap::new();
19        params.insert("instId".to_string(), inst_id);
20
21        let data = self
22            .public_request("GET", "/api/v5/public/funding-rate", Some(&params))
23            .await?;
24
25        let rates_array = data["data"].as_array().ok_or_else(|| {
26            Error::from(ParseError::invalid_format("data", "Expected data array"))
27        })?;
28
29        if rates_array.is_empty() {
30            return Err(Error::from(ParseError::missing_field_owned(format!(
31                "No funding rate found for symbol: {}",
32                symbol
33            ))));
34        }
35
36        parser::parse_funding_rate(&rates_array[0], symbol)
37    }
38
39    /// Fetch funding rates for multiple symbols.
40    ///
41    /// Calls `fetch_funding_rate_impl` for each symbol.
42    pub async fn fetch_funding_rates_impl(&self, symbols: &[&str]) -> Result<Vec<FundingRate>> {
43        let mut rates = Vec::new();
44        for symbol in symbols {
45            match self.fetch_funding_rate_impl(symbol).await {
46                Ok(rate) => rates.push(rate),
47                Err(e) => {
48                    warn!(error = %e, symbol = %symbol, "Failed to fetch funding rate");
49                }
50            }
51        }
52        Ok(rates)
53    }
54
55    /// Fetch funding rate history for a symbol.
56    ///
57    /// Uses OKX GET `/api/v5/public/funding-rate-history` (public endpoint).
58    pub async fn fetch_funding_rate_history_impl(
59        &self,
60        symbol: &str,
61        since: Option<i64>,
62        limit: Option<u32>,
63    ) -> Result<Vec<FundingRateHistory>> {
64        let inst_id = Self::symbol_to_inst_id(symbol);
65
66        let mut params = HashMap::new();
67        params.insert("instId".to_string(), inst_id);
68
69        if let Some(s) = since {
70            params.insert("before".to_string(), s.to_string());
71        }
72
73        if let Some(l) = limit {
74            params.insert("limit".to_string(), l.to_string());
75        }
76
77        let data = self
78            .public_request("GET", "/api/v5/public/funding-rate-history", Some(&params))
79            .await?;
80
81        let history_array = data["data"].as_array().ok_or_else(|| {
82            Error::from(ParseError::invalid_format("data", "Expected data array"))
83        })?;
84
85        let mut history = Vec::new();
86        for item in history_array {
87            match parser::parse_funding_rate_history(item, symbol) {
88                Ok(record) => history.push(record),
89                Err(e) => {
90                    warn!(error = %e, "Failed to parse OKX funding rate history");
91                }
92            }
93        }
94
95        Ok(history)
96    }
97}