#![allow(non_snake_case)]
use indexmap::IndexMap as HashMap;
use std::sync::Arc;
use hmac::{Hmac, Mac};
use sha2::{Sha256, Sha512, Digest as Sha2Digest};
use sha1::Sha1;
use md5::Md5;
use base64::{engine::general_purpose::STANDARD as B64, Engine as _};
use crate::{Value, ExchangeError, Result};
type HmacSha256 = Hmac<Sha256>;
type HmacSha512 = Hmac<Sha512>;
type HmacSha1 = Hmac<Sha1>;
type HmacMd5 = Hmac<Md5>;
pub type DescribeCallback = Arc<dyn Fn() -> Value + Send + Sync>;
static HTTP_NANOS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
static JSON_NANOS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
static HTTP_CALLS: std::sync::atomic::AtomicU64 = std::sync::atomic::AtomicU64::new(0);
tokio::task_local! {
static WS_DRIVEN_URLS: std::sync::Mutex<std::collections::HashSet<String>>;
}
pub fn take_http_timings() -> (u64, u64, u64) {
use std::sync::atomic::Ordering::Relaxed;
(
HTTP_NANOS.swap(0, Relaxed),
JSON_NANOS.swap(0, Relaxed),
HTTP_CALLS.swap(0, Relaxed),
)
}
pub struct Internals {
pub http_client: Option<reqwest::Client>,
pub describe_cb: Option<DescribeCallback>,
pub last_rest_ts: i64,
pub headers: HashMap<String, String>,
pub throttle: std::sync::Arc<tokio::sync::Mutex<(f64, i64)>>,
pub implicit_api: HashMap<String, (String, Vec<String>, String, Value)>,
pub dispatch_stack: Vec<String>,
}
pub trait DerivedExchange {
fn parse_ticker(&self, _ticker: Value, _market: Value) -> Value { Value::Null }
fn parse_trade(&self, _trade: Value, _market: Value) -> Value { Value::Null }
fn parse_order(&self, _order: Value, _market: Value) -> Value { Value::Null }
fn parse_market(&self, _market: Value) -> Value { Value::Null }
fn parse_ohlcv(&self, _ohlcv: Value, _market: Value) -> Value { Value::Null }
fn parse_order_book(&self, _ob: Value, _symbol: Value, _ts: Value, _bk: Value, _ak: Value, _pk: Value, _ak2: Value, _ck: Value) -> Value { Value::Null }
fn parse_balance(&self, _response: Value) -> Value { Value::Null }
fn parse_position(&self, _position: Value, _market: Value) -> Value { Value::Null }
fn parse_funding_rate(&self, _rate: Value, _market: Value) -> Value { Value::Null }
fn parse_deposit(&self, _tx: Value, _currency: Value) -> Value { Value::Null }
fn parse_deposit_address(&self, _addr: Value, _currency: Value) -> Value { Value::Null }
fn parse_last_price(&self, _entry: Value, _market: Value) -> Value { Value::Null }
fn parse_withdrawal(&self, _tx: Value, _currency: Value) -> Value { Value::Null }
fn parse_ledger_entry(&self, _entry: Value, _currency: Value) -> Value { Value::Null }
fn parse_transfer(&self, _transfer: Value, _currency: Value) -> Value { Value::Null }
fn parse_currency(&self, _currency: Value) -> Value { Value::Null }
fn parse_bid_ask(&self, _bidask: Value, _price_key: Value, _amount_key: Value, _market: Value) -> Value { Value::Null }
fn parse_open_interest(&self, _interest: Value, _market: Value) -> Value { Value::Null }
fn parse_liquidation(&self, _liquidation: Value, _market: Value) -> Value { Value::Null }
fn parse_funding_rate_history(&self, _entry: Value, _market: Value) -> Value { Value::Null }
fn parse_margin_modification(&self, _data: Value, _market: Value) -> Value { Value::Null }
fn parse_account(&self, _account: Value) -> Value { Value::Null }
fn parse_my_trade(&self, _trade: Value, _market: Value) -> Value { Value::Null }
fn parse_transaction(&self, _transaction: Value, _currency: Value) -> Value { Value::Null }
fn parse_borrow_interest(&self, _info: Value, _market: Value) -> Value { Value::Null }
fn parse_adl_rank(&self, _info: Value, _market: Value) -> Value { Value::Null }
fn parse_income(&self, _info: Value, _market: Value) -> Value { Value::Null }
fn parse_greeks(&self, _greeks: Value, _market: Value) -> Value { Value::Null }
fn parse_margin_mode(&self, _margin_mode: Value, _market: Value) -> Value { Value::Null }
fn parse_conversion(&self, _conversion: Value, _from_currency: Value, _to_currency: Value) -> Value { Value::Null }
fn parse_borrow_rate(&self, _info: Value, _currency: Value) -> Value { Value::Null }
fn parse_leverage(&self, _leverage: Value, _market: Value) -> Value { Value::Null }
fn parse_market_leverage_tiers(&self, _info: Value, _market: Value) -> Value { Value::Null }
fn parse_deposit_withdraw_fee(&self, _fee: Value, _currency: Value) -> Value { Value::Null }
fn create_expired_option_market(&self, _symbol: Value) -> Value { Value::Null }
fn parse_prediction_trade(&self, _trade: Value, _market: Value) -> Value { Value::Null }
fn parse_prediction_order(&self, _order: Value, _market: Value) -> Value { Value::Null }
fn parse_prediction_position(&self, _position: Value, _market: Value) -> Value { Value::Null }
fn nonce(&self) -> Value { Value::Null }
fn sign(&self, _path: Value, _api: Value, _method: Value, _params: Value, _headers: Value, _body: Value) -> Value { Value::Null }
fn handle_errors(&self, _code: Value, _reason: Value, _url: Value, _method: Value, _headers: Value, _body: Value, _response: Value, _request_headers: Value, _request_body: Value) -> Value { Value::Null }
}
impl Clone for Internals {
fn clone(&self) -> Self { Internals::default() }
}
impl Default for Internals {
fn default() -> Self {
Self {
http_client: None,
describe_cb: None,
last_rest_ts: 0,
headers: HashMap::new(),
throttle: std::sync::Arc::new(tokio::sync::Mutex::new((0.0, 0))),
implicit_api: HashMap::new(),
dispatch_stack: Vec::new(),
}
}
}
#[derive(Clone)]
pub struct Exchange {
pub id: Value,
pub name: Value,
pub countries: Value,
pub version: Value,
pub alias: Value,
pub certified: Value,
pub pro: Value,
pub hostname: Value,
pub apiKey: Value,
pub secret: Value,
pub password: Value,
pub uid: Value,
pub walletAddress: Value,
pub privateKey: Value,
pub twofa: Value,
pub token: Value,
pub login: Value,
pub accountId: Value,
pub requiredCredentials: Value,
pub timeout: Value,
pub rateLimit: Value,
pub enableRateLimit: Value,
pub rateLimiterAlgorithm: Value,
pub rollingWindowSize: Value,
pub tokenBucket: Value,
pub userConfig: Value,
pub verbose: Value,
pub isSandboxModeEnabled: Value,
pub proxy: Value,
pub proxyUrl: Value,
pub proxy_url: Value,
pub proxyUrlCallback: Value,
pub proxy_url_callback: Value,
pub httpProxy: Value,
pub http_proxy: Value,
pub httpProxyCallback: Value,
pub http_proxy_callback: Value,
pub httpsProxy: Value,
pub https_proxy: Value,
pub httpsProxyCallback: Value,
pub https_proxy_callback: Value,
pub socksProxy: Value,
pub socks_proxy: Value,
pub socksProxyCallback: Value,
pub socks_proxy_callback: Value,
pub wsProxy: Value,
pub ws_proxy: Value,
pub wssProxy: Value,
pub wss_proxy: Value,
pub wsSocksProxy: Value,
pub ws_socks_proxy: Value,
pub markets: Value,
pub markets_by_id: Value,
pub currencies: Value,
pub currencies_by_id: Value,
pub commonCurrencies: Value,
pub baseCurrencies: Value,
pub quoteCurrencies: Value,
pub symbols: Value,
pub ids: Value,
pub codes: Value,
pub timeframes: Value,
pub precision: Value,
pub limits: Value,
pub fees: Value,
pub features: Value,
pub has: Value,
pub exceptions: Value,
pub urls: Value,
pub api: Value,
pub options: Value,
pub headers: Value,
pub accounts: Value,
pub accountsById: Value,
pub orderbooks: Value,
pub orders: Value,
pub trades: Value,
pub myTrades: Value,
pub positions: Value,
pub newUpdates: Value,
pub tickers: Value,
pub bidsasks: Value,
pub ohlcvs: Value,
pub clients: Value,
pub balance: Value,
pub liquidations: Value,
pub myLiquidations: Value,
pub fundingRates: Value,
pub triggerOrders: Value,
pub transactions: Value,
pub reloadingMarkets: Value,
pub marketsLoading: Value,
pub last_request_url: Value,
pub last_request_headers: Value,
pub last_request_body: Value,
pub last_http_response: Value,
pub last_response_headers: Value,
pub mock_response: Value,
pub last_json_response: Value,
pub lastRestRequestTimestamp: Value,
pub fetchHistoryCache: Value,
pub fetchHistoryCacheSize: Value,
pub paddingMode: Value,
pub precisionMode: Value,
pub substituteCommonCurrencyCodes: Value,
pub reduceFees: Value,
pub minFundingAddressLength: Value,
pub MAX_VALUE: Value,
pub returnResponseHeaders: Value,
pub httpExceptions: Value,
pub status: Value,
pub userAgent: Value,
pub user_agent: Value,
pub userAgents: Value,
pub describe_data: Value,
pub derived_describe: Option<DescribeCallback>,
pub internals: Internals,
}
impl Default for Exchange {
fn default() -> Self { Self::new(None) }
}
fn vmap(pairs: &[(&str, Value)]) -> Value {
let mut m = HashMap::new();
for (k, v) in pairs { m.insert((*k).to_string(), v.clone()); }
Value::Map(m)
}
fn min_max() -> Value { vmap(&[("min", Value::Null), ("max", Value::Null)]) }
fn base_limits() -> Value {
vmap(&[
("leverage", min_max()),
("amount", min_max()),
("price", min_max()),
("cost", min_max()),
])
}
fn base_fees() -> Value {
vmap(&[
("trading", vmap(&[
("tierBased", Value::Null),
("percentage", Value::Null),
("taker", Value::Null),
("maker", Value::Null),
])),
("funding", vmap(&[
("tierBased", Value::Null),
("percentage", Value::Null),
("withdraw", Value::Map(HashMap::new())),
("deposit", Value::Map(HashMap::new())),
])),
])
}
fn base_urls() -> Value {
vmap(&[
("logo", Value::Null), ("api", Value::Null), ("test", Value::Null),
("www", Value::Null), ("doc", Value::Null), ("api_management", Value::Null),
("fees", Value::Null), ("referral", Value::Null),
])
}
fn base_status() -> Value {
vmap(&[
("status", Value::Str("ok".to_string())),
("updated", Value::Null),
("eta", Value::Null),
("url", Value::Null),
("info", Value::Null),
])
}
fn deep_merge(a: &Value, b: &Value) -> Value {
match (a, b) {
(Value::Dict(am), Value::Dict(bm)) => {
let mut out = (**am).clone();
for (k, bv) in bm.iter() {
let merged = match out.get(k) {
Some(av) => deep_merge(av, bv),
None => bv.clone(),
};
out.insert(k.clone(), merged);
}
Value::Map(out)
}
_ => b.clone(),
}
}
fn base_http_exceptions() -> Value {
let mut m = HashMap::new();
for (code, class) in [
("422", "ExchangeError"), ("418", "DDoSProtection"),
("429", "RateLimitExceeded"), ("404", "ExchangeNotAvailable"),
("409", "ExchangeNotAvailable"), ("410", "ExchangeNotAvailable"),
("451", "ExchangeNotAvailable"), ("500", "ExchangeNotAvailable"),
("501", "ExchangeNotAvailable"), ("502", "ExchangeNotAvailable"),
("520", "ExchangeNotAvailable"), ("521", "ExchangeNotAvailable"),
("522", "ExchangeNotAvailable"), ("525", "ExchangeNotAvailable"),
("526", "ExchangeNotAvailable"), ("400", "ExchangeNotAvailable"),
("403", "ExchangeNotAvailable"), ("405", "ExchangeNotAvailable"),
("503", "ExchangeNotAvailable"), ("530", "ExchangeNotAvailable"),
("408", "RequestTimeout"), ("504", "RequestTimeout"),
("401", "AuthenticationError"), ("407", "AuthenticationError"),
("511", "AuthenticationError"),
] {
m.insert(code.to_string(), Value::Str(class.to_string()));
}
Value::Map(m)
}
impl Exchange {
pub fn new(config: Option<Value>) -> Self {
let mut ex = Exchange {
id: Value::Str("Exchange".to_string()),
name: Value::Null,
countries: Value::Null,
version: Value::Null,
alias: Value::Bool(false),
certified: Value::Bool(false),
pro: Value::Bool(false),
hostname: Value::Null,
apiKey: Value::Null,
secret: Value::Null,
password: Value::Null,
uid: Value::Null,
walletAddress: Value::Null,
privateKey: Value::Null,
twofa: Value::Null,
token: Value::Null,
login: Value::Null,
accountId: Value::Null,
requiredCredentials: {
let mut m = HashMap::new();
m.insert("apiKey".to_string(), Value::Bool(true));
m.insert("secret".to_string(), Value::Bool(true));
m.insert("uid".to_string(), Value::Bool(false));
m.insert("accountId".to_string(), Value::Bool(false));
m.insert("login".to_string(), Value::Bool(false));
m.insert("password".to_string(), Value::Bool(false));
m.insert("twofa".to_string(), Value::Bool(false));
m.insert("privateKey".to_string(), Value::Bool(false));
m.insert("walletAddress".to_string(), Value::Bool(false));
m.insert("token".to_string(), Value::Bool(false));
Value::Map(m)
},
timeout: Value::Int(10_000),
rateLimit: Value::Int(2_000),
enableRateLimit: Value::Bool(true),
rateLimiterAlgorithm: Value::Str("leakyBucket".to_string()),
rollingWindowSize: Value::Int(60_000),
tokenBucket: Value::Map(HashMap::new()),
userConfig: Value::Null,
verbose: Value::Bool(false),
isSandboxModeEnabled: Value::Bool(false),
proxy: Value::Null,
proxyUrl: Value::Null,
proxy_url: Value::Null,
proxyUrlCallback: Value::Null,
proxy_url_callback: Value::Null,
httpProxy: Value::Null,
http_proxy: Value::Null,
httpProxyCallback: Value::Null,
http_proxy_callback: Value::Null,
httpsProxy: Value::Null,
https_proxy: Value::Null,
httpsProxyCallback: Value::Null,
https_proxy_callback: Value::Null,
socksProxy: Value::Null,
socks_proxy: Value::Null,
socksProxyCallback: Value::Null,
socks_proxy_callback: Value::Null,
wsProxy: Value::Null,
ws_proxy: Value::Null,
wssProxy: Value::Null,
wss_proxy: Value::Null,
wsSocksProxy: Value::Null,
ws_socks_proxy: Value::Null,
markets: Value::Null,
markets_by_id: Value::Null,
currencies: Value::Map(HashMap::new()),
currencies_by_id: Value::Null,
commonCurrencies: {
let mut m = HashMap::new();
m.insert("XBT".to_string(), Value::Str("BTC".to_string()));
m.insert("BCHSV".to_string(), Value::Str("BSV".to_string()));
Value::Map(m)
},
baseCurrencies: Value::Null,
quoteCurrencies: Value::Null,
symbols: Value::Null,
ids: Value::Null,
codes: Value::Null,
timeframes: Value::Null,
precision: Value::Null,
limits: base_limits(),
fees: base_fees(),
features: Value::Null,
has: Value::Map(HashMap::new()),
exceptions: Value::Null,
urls: base_urls(),
api: Value::Null,
options: {
let mk = |p: &str, s: &str, d: &str| {
let mut e = HashMap::new();
e.insert("primary".to_string(), Value::Str(p.to_string()));
e.insert("secondary".to_string(), Value::Str(s.to_string()));
e.insert("default".to_string(), Value::Str(d.to_string()));
Value::Map(e)
};
let mut repl = HashMap::new();
repl.insert("ETH".to_string(), mk("ETH", "ERC20", "secondary"));
repl.insert("CRO".to_string(), mk("CRONOS", "CRC20", "secondary"));
repl.insert("TRX".to_string(), mk("TRX", "TRC20", "secondary"));
repl.insert("BTC".to_string(), mk("BTC", "BRC20", "primary"));
let mut o = HashMap::new();
o.insert("defaultNetworkCodeReplacements".to_string(), Value::Map(repl));
Value::Map(o)
},
headers: Value::Map(HashMap::new()),
accounts: Value::Null,
accountsById: Value::Null,
orderbooks: Value::Map(HashMap::new()),
orders: Value::Null,
trades: Value::Map(HashMap::new()),
myTrades: Value::Null,
positions: Value::Null,
newUpdates: Value::Bool(true),
tickers: Value::Map(HashMap::new()),
bidsasks: Value::Map(HashMap::new()),
ohlcvs: Value::Map(HashMap::new()),
clients: Value::Map(HashMap::new()),
balance: Value::Map(HashMap::new()),
liquidations: Value::Null,
myLiquidations: Value::Null,
fundingRates: Value::Null,
triggerOrders: Value::Null,
transactions: Value::Map(HashMap::new()),
reloadingMarkets: Value::Null,
marketsLoading: Value::Null,
last_request_url: Value::Null,
last_request_headers: Value::Null,
last_request_body: Value::Null,
last_http_response: Value::Null,
last_response_headers: Value::Null,
mock_response: Value::Null,
last_json_response: Value::Null,
lastRestRequestTimestamp: Value::Int(0),
fetchHistoryCache: Value::List(vec![]),
fetchHistoryCacheSize: Value::Int(0),
paddingMode: Value::Null,
precisionMode: Value::Null,
substituteCommonCurrencyCodes: Value::Bool(true),
reduceFees: Value::Bool(true),
minFundingAddressLength: Value::Int(1),
MAX_VALUE: Value::Float(f64::MAX),
returnResponseHeaders: Value::Bool(false),
httpExceptions: base_http_exceptions(),
status: base_status(),
userAgent: Value::Null,
user_agent: Value::Null,
userAgents: {
let mut m = HashMap::new();
m.insert("chrome".to_string(), Value::Str("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36".to_string()));
m.insert("chrome39".to_string(), Value::Str("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36".to_string()));
m.insert("chrome100".to_string(), Value::Str("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.75 Safari/537.36".to_string()));
Value::Map(m)
},
describe_data: Value::Null,
derived_describe: None,
internals: Internals::default(),
};
if let Some(cfg) = config {
ex.userConfig = cfg.clone();
ex.apply_config(&cfg);
}
ex
}
pub fn initialize_properties(&mut self, described: Value) {
let props = if matches!(self.userConfig, Value::Null) {
described
} else {
crate::runtime::deep_merge_dict(&described, &self.userConfig.clone())
};
let get = |k: &str| crate::get_value(&props, &Value::Str(k.to_string()));
let assign = |field: &mut Value, v: Value| {
if !matches!(v, Value::Null) {
*field = v;
}
};
assign(&mut self.api, get("api"));
assign(&mut self.urls, get("urls"));
assign(&mut self.has, get("has"));
assign(&mut self.hostname, get("hostname"));
assign(&mut self.version, get("version"));
assign(&mut self.id, get("id"));
assign(&mut self.name, get("name"));
assign(&mut self.exceptions, get("exceptions"));
assign(&mut self.requiredCredentials, get("requiredCredentials"));
assign(&mut self.precisionMode, get("precisionMode"));
assign(&mut self.timeframes, get("timeframes"));
assign(&mut self.fees, get("fees"));
assign(&mut self.features, get("features"));
let rate_limit = get("rateLimit");
if matches!(rate_limit, Value::Int(_) | Value::Float(_)) {
self.rateLimit = rate_limit;
}
if let Value::Dict(extra) = get("options") {
let mut merged = match &self.options {
Value::Dict(m) => (**m).clone(),
_ => HashMap::new(),
};
let extra = Arc::try_unwrap(extra).unwrap_or_else(|a| (*a).clone());
for (k, v) in extra { merged.insert(k, v); }
self.options = Value::Map(merged);
}
if let Value::Dict(extra) = get("commonCurrencies") {
let mut merged = match &self.commonCurrencies {
Value::Dict(m) => (**m).clone(),
_ => HashMap::new(),
};
let extra = Arc::try_unwrap(extra).unwrap_or_else(|a| (*a).clone());
for (k, v) in extra { merged.insert(k, v); }
self.commonCurrencies = Value::Map(merged);
}
let described_markets = get("markets");
if matches!(&described_markets, Value::Dict(m) if !m.is_empty())
&& matches!(self.markets, Value::Null)
{
self.set_markets_inline(described_markets);
}
self.build_implicit_api();
}
fn apply_config(&mut self, cfg: &Value) {
use crate::value::safe_string;
if let Some(v) = safe_string(cfg, "id", None) { self.id = Value::Str(v); }
if let Some(v) = safe_string(cfg, "name", None) { self.name = Value::Str(v); }
if let Some(v) = safe_string(cfg, "hostname", None) { self.hostname = Value::Str(v); }
let rate_limit = crate::get_value(cfg, &Value::Str("rateLimit".to_string()));
if matches!(rate_limit, Value::Int(_) | Value::Float(_)) { self.rateLimit = rate_limit; }
let timeout = crate::get_value(cfg, &Value::Str("timeout".to_string()));
if matches!(timeout, Value::Int(_) | Value::Float(_)) { self.timeout = timeout; }
let fetch_history_cache_size = crate::get_value(cfg, &Value::Str("fetchHistoryCacheSize".to_string()));
if matches!(fetch_history_cache_size, Value::Int(_) | Value::Float(_)) { self.fetchHistoryCacheSize = fetch_history_cache_size; }
let urls = crate::get_value(cfg, &Value::Str("urls".to_string()));
if let Value::Dict(_) = urls { self.urls = deep_merge(&self.urls, &urls); }
if let Some(v) = safe_string(cfg, "apiKey", None) { self.apiKey = Value::Str(v); }
if let Some(v) = safe_string(cfg, "secret", None) { self.secret = Value::Str(v); }
if let Some(v) = safe_string(cfg, "password", None) { self.password = Value::Str(v); }
if let Some(v) = safe_string(cfg, "uid", None) { self.uid = Value::Str(v); }
if let Some(v) = safe_string(cfg, "walletAddress", None) { self.walletAddress = Value::Str(v); }
if let Some(v) = safe_string(cfg, "privateKey", None) { self.privateKey = Value::Str(v); }
if let Some(v) = safe_string(cfg, "token", None) { self.token = Value::Str(v); }
if let Some(v) = safe_string(cfg, "login", None) { self.login = Value::Str(v); }
if let Some(v) = safe_string(cfg, "accountId", None) { self.accountId = Value::Str(v); }
if let Some(v) = safe_string(cfg, "httpProxy", None) { self.httpProxy = Value::Str(v); }
if let Some(v) = safe_string(cfg, "httpsProxy", None) { self.httpsProxy = Value::Str(v); }
if let Some(v) = safe_string(cfg, "socksProxy", None) { self.socksProxy = Value::Str(v); }
if let Some(v) = safe_string(cfg, "wsProxy", None) { self.wsProxy = Value::Str(v); }
if let Some(v) = safe_string(cfg, "wssProxy", None) { self.wssProxy = Value::Str(v); }
if let Some(b) = crate::value::safe_bool(cfg, "verbose", None) { self.verbose = Value::Bool(b); }
if let Some(b) = crate::value::safe_bool(cfg, "enableRateLimit", None) { self.enableRateLimit = Value::Bool(b); }
let markets = crate::get_value(cfg, &Value::Str("markets".to_string()));
let currencies = crate::get_value(cfg, &Value::Str("currencies".to_string()));
if !matches!(markets, Value::Null) { self.set_markets_inline(markets); }
if !matches!(currencies, Value::Null) { self.currencies = currencies; }
let opts = crate::get_value(cfg, &Value::Str("options".to_string()));
if let Value::Dict(extra) = opts {
let mut merged = match &self.options {
Value::Dict(m) => (**m).clone(),
_ => HashMap::new(),
};
let extra = Arc::try_unwrap(extra).unwrap_or_else(|a| (*a).clone());
for (k, v) in extra { merged.insert(k, v); }
self.options = Value::Map(merged);
}
let accounts = crate::get_value(cfg, &Value::Str("accounts".to_string()));
if !matches!(accounts, Value::Null) { self.accounts = accounts; }
}
fn set_markets_inline(&mut self, markets: Value) {
let arr: Vec<Value> = match &markets {
Value::Dict(m) => m.values().cloned().collect(),
Value::Arr(a) => (**a).clone(),
_ => return,
};
let mut by_symbol = HashMap::new();
let mut by_id: HashMap<String, Vec<Value>> = HashMap::new();
let mut symbols = Vec::new();
for m in &arr {
if let Some(s) = crate::value::safe_string(m, "symbol", None) {
by_symbol.insert(s.clone(), m.clone());
symbols.push(Value::Str(s));
}
if let Some(i) = crate::value::safe_string(m, "id", None) {
by_id.entry(i).or_default().push(m.clone());
}
}
self.markets = if by_symbol.is_empty() { markets } else { Value::Map(by_symbol) };
self.markets_by_id = Value::Map(
by_id.into_iter().map(|(k, v)| (k, Value::Array(v))).collect()
);
self.symbols = Value::Array(symbols);
}
fn http_client(&mut self) -> &reqwest::Client {
if self.internals.http_client.is_none() {
let mut b = reqwest::Client::builder()
.timeout(std::time::Duration::from_millis(self.timeout_ms()))
.gzip(true);
if let Some(ua) = self.user_agent_str() { b = b.user_agent(ua); }
let mut proxies: Vec<reqwest::Proxy> = Vec::new();
for field in [&self.socksProxy, &self.socks_proxy] {
if let Value::Str(s) = field {
if !s.is_empty() {
if let Ok(p) = reqwest::Proxy::all(s.as_str()) { proxies.push(p); }
}
}
}
for field in [&self.httpsProxy, &self.https_proxy] {
if let Value::Str(s) = field {
if !s.is_empty() {
if let Ok(p) = reqwest::Proxy::https(s.as_str()) { proxies.push(p); }
}
}
}
for field in [&self.httpProxy, &self.http_proxy] {
if let Value::Str(s) = field {
if !s.is_empty() {
if let Ok(p) = reqwest::Proxy::http(s.as_str()) { proxies.push(p); }
}
}
}
if proxies.is_empty() {
if let Value::Str(s) = &self.proxy {
if !s.is_empty() {
if let Ok(p) = reqwest::Proxy::all(s.as_str()) { proxies.push(p); }
}
}
}
for p in proxies { b = b.proxy(p); }
self.internals.http_client = Some(b.build().expect("reqwest client"));
}
self.internals.http_client.as_ref().unwrap()
}
fn timeout_ms(&self) -> u64 {
match &self.timeout {
Value::Int(n) => *n as u64,
Value::Float(f) => *f as u64,
_ => 10_000,
}
}
fn user_agent_str(&self) -> Option<&str> {
match &self.userAgent { Value::Str(s) => Some(s.as_str()), _ => match &self.user_agent {
Value::Str(s) => Some(s.as_str()), _ => None
}}
}
fn is_verbose(&self) -> bool {
matches!(self.verbose, Value::Bool(true))
}
pub fn urlencode_kv(&self, params: &Value) -> String {
let m = match params { Value::Dict(m) => m, _ => return String::new() };
m.iter().map(|(k, v)| format!(
"{}={}",
url_pct(k),
url_pct(&crate::runtime::stringify_param(v)),
)).collect::<Vec<_>>().join("&")
}
pub fn implode_params(&self, path: Value, params: Value) -> Value {
let path_str = match &path { Value::Str(s) => s.clone(), _ => return path };
let m = match ¶ms { Value::Dict(m) => m, _ => return Value::Str(path_str) };
let mut out = path_str;
for (k, v) in m.iter() {
let needle = format!("{{{k}}}");
if out.contains(&needle) {
out = out.replace(&needle, &crate::runtime::stringify_param(v));
}
}
Value::Str(out)
}
pub fn json_str(&self, v: &Value) -> String {
serde_json::to_string(&v.to_json()).unwrap_or_default()
}
pub fn hmac(&self, data: Value, secret: Value, hash: Value, optional_args: &[Value]) -> Value {
let dbytes = value_to_bytes(&data);
let sbytes = value_to_bytes(&secret);
let h = match &hash { Value::Str(s) => s.clone(), _ => "sha256".to_string() };
let digest = match optional_args.get(0) {
Some(Value::Str(d)) => d.clone(),
_ => "hex".to_string(),
};
match self.hmac_typed(&dbytes, &sbytes, &h, &digest) {
Ok(v) => Value::Str(v),
Err(_) => Value::Null,
}
}
pub fn hmac_typed(&self, data: &[u8], secret: &[u8], hash: &str, digest: &str) -> Result<String> {
let dbytes = data;
let sbytes = secret;
let raw: Vec<u8> = match hash.to_ascii_lowercase().as_str() {
"sha256" => { let mut m = HmacSha256::new_from_slice(sbytes).unwrap(); m.update(dbytes); m.finalize().into_bytes().to_vec() }
"sha512" => { let mut m = HmacSha512::new_from_slice(sbytes).unwrap(); m.update(dbytes); m.finalize().into_bytes().to_vec() }
"sha1" => { let mut m = HmacSha1::new_from_slice(sbytes).unwrap(); m.update(dbytes); m.finalize().into_bytes().to_vec() }
"md5" => { let mut m = HmacMd5::new_from_slice(sbytes).unwrap(); m.update(dbytes); m.finalize().into_bytes().to_vec() }
other => return Err(ExchangeError::new("BadRequest", format!("unknown hmac: {other}"))),
};
Ok(match digest {
"base64" => B64.encode(&raw),
_ => hex::encode(&raw),
})
}
pub fn hash(&self, data: Value, algo: Value, optional_args: &[Value]) -> Value {
let dbytes = value_to_bytes(&data);
let a = match &algo { Value::Str(s) => s.clone(), _ => "sha256".to_string() };
let digest = match optional_args.get(0) {
Some(Value::Str(d)) => d.clone(),
_ => "hex".to_string(),
};
let raw = hash_raw(&dbytes, &a);
match digest.as_str() {
"binary" => Value::Array(raw.iter().map(|b| Value::Int(*b as i64)).collect()),
"base64" => Value::Str(B64.encode(&raw)),
_ => Value::Str(hex::encode(&raw)),
}
}
pub fn hash_typed(&self, data: &[u8], algo: &str, digest: &str) -> String {
let raw = hash_raw(data, algo);
match digest {
"base64" => B64.encode(&raw),
_ => hex::encode(&raw),
}
}
pub fn binary_to_base64(&self, data: Value, _optional_args: &[Value]) -> Value {
let bytes = value_to_bytes(&data);
Value::Str(B64.encode(&bytes))
}
pub fn base64_to_binary(&self, s: Value, _optional_args: &[Value]) -> Value {
let str_val = match &s { Value::Str(s) => s.clone(), _ => return Value::Null };
match B64.decode(&str_val) {
Ok(b) => Value::Array(b.into_iter().map(|n| Value::Int(n as i64)).collect()),
Err(_) => Value::Null,
}
}
pub fn binary_to_base16(&self, data: Value, _optional_args: &[Value]) -> Value {
let bytes = value_to_bytes(&data);
Value::Str(hex::encode(&bytes))
}
pub fn base16_to_binary(&self, s: Value, _optional_args: &[Value]) -> Value {
let str_val = match &s { Value::Str(s) => s.clone(), _ => return Value::Null };
match hex::decode(&str_val) {
Ok(b) => Value::Array(b.into_iter().map(|n| Value::Int(n as i64)).collect()),
Err(_) => Value::Null,
}
}
pub fn milliseconds(&self) -> Value {
let n = std::time::SystemTime::now()
.duration_since(std::time::UNIX_EPOCH)
.map(|d| d.as_millis() as i64).unwrap_or(0);
Value::Int(n)
}
pub fn seconds(&self) -> Value {
match self.milliseconds() { Value::Int(n) => Value::Int(n / 1000), v => v }
}
pub fn microseconds(&self) -> Value {
match self.milliseconds() { Value::Int(n) => Value::Int(n * 1000), v => v }
}
pub fn iso8601(&self, ts: Value) -> Value {
let n = match ts {
Value::Int(n) => n,
Value::Float(f) => f as i64,
Value::Str(ref s) => match s.parse::<i64>() { Ok(v) => v, Err(_) => return Value::Null },
_ => return Value::Null,
};
if n < 0 {
return Value::Null;
}
chrono::DateTime::<chrono::Utc>::from_timestamp_millis(n)
.map(|t| Value::Str(t.to_rfc3339_opts(chrono::SecondsFormat::Millis, true)))
.unwrap_or(Value::Null)
}
pub fn parse8601(&self, s: Value) -> Value {
let str_val = match s { Value::Str(s) => s, _ => return Value::Null };
if str_val.is_empty() {
return Value::Null;
}
if let Ok(t) = chrono::DateTime::parse_from_rfc3339(&str_val) {
return Value::Int(t.timestamp_millis());
}
for fmt in [
"%Y-%m-%dT%H:%M:%S%.f%z", "%Y-%m-%dT%H:%M:%S%z",
"%Y-%m-%d %H:%M:%S%.f%z", "%Y-%m-%d %H:%M:%S%z",
] {
if let Ok(t) = chrono::DateTime::parse_from_str(&str_val, fmt) {
return Value::Int(t.timestamp_millis());
}
}
for fmt in [
"%Y-%m-%d %H:%M:%S%.f", "%Y-%m-%d %H:%M:%S",
"%Y-%m-%dT%H:%M:%S%.f", "%Y-%m-%dT%H:%M:%S",
"%Y-%m-%d %H:%M", "%Y-%m-%dT%H:%M",
] {
if let Ok(ndt) = chrono::NaiveDateTime::parse_from_str(&str_val, fmt) {
return Value::Int(ndt.and_utc().timestamp_millis());
}
}
if let Ok(d) = chrono::NaiveDate::parse_from_str(&str_val, "%Y-%m-%d") {
if let Some(ndt) = d.and_hms_opt(0, 0, 0) {
return Value::Int(ndt.and_utc().timestamp_millis());
}
}
Value::Null
}
}
impl Exchange {
pub fn dispatch_guard_enter(&mut self, method: &str) -> bool {
if self.internals.dispatch_stack.iter().any(|m| m == method) { return false; }
self.internals.dispatch_stack.push(method.to_string());
true
}
pub fn dispatch_guard_exit(&mut self) {
self.internals.dispatch_stack.pop();
}
}
fn ws_string_list(v: &Value) -> Vec<String> {
match v {
Value::Str(s) => vec![s.clone()],
Value::Arr(a) => a
.iter()
.filter_map(|x| match x {
Value::Str(s) => Some(s.clone()),
_ => None,
})
.collect(),
_ => Vec::new(),
}
}
pub trait ExchangeRuntime: crate::exchange_generated::ExchangeBase {
fn fetch(&mut self, url: Value, optional_args: &[Value]) -> impl ::std::future::Future<Output = Value> + Send { async move {
let url_str = match &url { Value::Str(s) => s.clone(), _ => crate::runtime::stringify_param(&url) };
let method = optional_args.get(0).cloned().unwrap_or(Value::Str("GET".to_string()));
let headers = optional_args.get(1).cloned().unwrap_or(Value::Null);
let body = optional_args.get(2).cloned().unwrap_or(Value::Null);
let method_str = match &method { Value::Str(s) => s.clone(), _ => "GET".to_string() };
let body_str = match &body { Value::Str(s) => Some(s.clone()), _ => None };
let headers_map: HashMap<String, String> = match &headers {
Value::Dict(m) => m.iter().filter_map(|(k, v)| match v {
Value::Str(s) => Some((k.clone(), s.clone())),
_ => None,
}).collect(),
_ => HashMap::new(),
};
match self.fetch_typed(&url_str, &method_str, headers_map, body_str).await {
Ok(v) => v,
Err(e) if e.kind == "InvalidProxySettings" => panic!("{}", e),
Err(_) => Value::Null,
}
} }
fn ws_run(
&mut self,
url: String,
hashes: Vec<String>,
message: Value,
subscribe_hashes: Vec<String>,
subscription: Value,
) -> impl ::std::future::Future<Output = Value> + Send { async move {
let __ws_proxy = [&self.wsProxy, &self.ws_proxy, &self.wssProxy]
.iter()
.find_map(|v| match v { Value::Str(s) if !s.is_empty() => Some(s.clone()), _ => None });
let client = match crate::pro::ws_client::ensure_client(&url, __ws_proxy).await {
Ok(c) => c,
Err(e) => panic!("{}", e),
};
if std::env::var("CCXT_WS_DEBUG").is_ok() {
eprintln!("[wswait] {:?}", hashes);
}
client.note_futures(&hashes);
let mut send_subscribe = subscribe_hashes.is_empty();
for sh in &subscribe_hashes {
if client.subscribe_once(sh, subscription.clone()) {
send_subscribe = true;
}
}
if send_subscribe && !matches!(message, Value::Null) {
let payload = match &message {
Value::Str(s) => s.clone(),
v => v.to_json().to_string(),
};
client.send_text(payload);
}
crate::runtime::add_element_to_object(
&mut self.clients,
&Value::Str(url.clone()),
crate::pro::ws_client::client_value(&url),
);
let nested_same = WS_DRIVEN_URLS
.try_with(|s| s.lock().unwrap().contains(&url))
.unwrap_or(false);
if nested_same {
return match client.take_settled(&hashes) {
Some(Ok(v)) => v,
_ => Value::Null,
};
}
if WS_DRIVEN_URLS.try_with(|_| ()).is_ok() {
WS_DRIVEN_URLS.with(|s| { s.lock().unwrap().insert(url.clone()); });
let r = self.ws_drive_loop(client, hashes, url.clone()).await;
WS_DRIVEN_URLS.with(|s| { s.lock().unwrap().remove(&url); });
r
} else {
let mut set = std::collections::HashSet::new();
set.insert(url.clone());
WS_DRIVEN_URLS
.scope(std::sync::Mutex::new(set), self.ws_drive_loop(client, hashes, url.clone()))
.await
}
} }
fn ws_drive_loop(
&mut self,
client: std::sync::Arc<crate::pro::ws_client::ClientState>,
hashes: Vec<String>,
url: String,
) -> impl ::std::future::Future<Output = Value> + Send { async move {
loop {
loop {
let batch = crate::exchange_stubs::drain_spawn_queue();
if batch.is_empty() { break; }
for (method, args) in batch {
if std::env::var("CCXT_WS_DEBUG").is_ok() {
eprintln!("[wsdrain] {}", method);
}
if method == "load_order_book" {
self.ws_load_order_book(args).await;
} else {
let _ = self.dispatch_to_derived(&method, args).await;
}
}
}
if let Some(settled) = client.take_settled(&hashes) {
match settled {
Ok(v) => return v,
Err(e) => panic!(
"{}",
match &e {
Value::Str(s) => s.clone(),
v => crate::runtime::stringify_param(v),
}
),
}
}
let next_due = crate::exchange_stubs::next_spawn_due();
let received = match next_due {
Some(d) => match tokio::time::timeout(d, client.next_message()).await {
Ok(m) => m,
Err(_) => continue,
},
None => client.next_message().await,
};
let msg = match received {
Some(m) => m,
None => panic!("[NetworkError] {} websocket connection closed", url),
};
if std::env::var("CCXT_WS_DEBUG").is_ok() {
let n: usize = std::env::var("CCXT_WS_DEBUG").ok()
.and_then(|v| v.parse().ok()).filter(|v| *v > 1).unwrap_or(220);
let s = msg.to_json().to_string();
eprintln!("[wsmsg] {}", s.chars().take(n).collect::<String>());
}
let client_value = crate::pro::ws_client::client_value(&url);
let _ = self
.dispatch_to_derived("handle_message", vec![client_value, msg])
.await;
}
} }
fn ws_load_order_book(
&mut self,
args: Vec<Value>,
) -> impl ::std::future::Future<Output = ()> + Send { async move {
let client = args.get(0).cloned().unwrap_or(Value::Null);
let message_hash = args.get(1).cloned().unwrap_or(Value::Null);
let symbol = args.get(2).cloned().unwrap_or(Value::Null);
let limit = args.get(3).cloned().unwrap_or(Value::Null);
let params = args.get(4).cloned().unwrap_or(Value::Map(indexmap::IndexMap::new()));
let err = |id: &Value| Value::Str(format!(
"[ExchangeError] {} loadOrderBook() failed to synchronize",
match id { Value::Str(s) => s.as_str(), _ => "" }));
if !crate::runtime::in_op(&self.orderbooks, &symbol) {
crate::pro::ws_client::value_reject(&client, &[err(&self.id), message_hash]);
return;
}
let max_retries: i64 = 3;
let mut tries = 0;
while tries < max_retries {
let mut stored = crate::get_value(&self.orderbooks, &symbol);
let cache = crate::get_value(&stored, &Value::Str("cache".to_string()));
let snapshot = self
.dispatch_to_derived("fetch_rest_order_book_safe",
vec![symbol.clone(), limit.clone(), params.clone()])
.await
.unwrap_or(Value::Null);
let index = self
.dispatch_to_derived("get_cache_index", vec![snapshot.clone(), cache.clone()])
.await;
let idx = match index { Some(Value::Int(n)) => n, _ => -1 };
if idx >= 0 {
stored.reset(snapshot);
let clen = match crate::runtime::get_array_length(&cache) { Value::Int(n) => n, _ => 0 };
let deltas: Vec<Value> = (idx..clen)
.map(|i| crate::get_value(&cache, &Value::Int(i)))
.collect();
self.dispatch_to_derived("handle_deltas", vec![stored.clone(), Value::List(deltas)]).await;
crate::set_value(&mut stored, &Value::Str("cache".to_string()), Value::List(Vec::new()));
crate::pro::ws_client::value_resolve(&client, &[stored.clone(), message_hash.clone()]);
return;
}
tries += 1;
}
crate::pro::ws_client::value_reject(&client, &[err(&self.id), message_hash]);
} }
fn watch(
&mut self,
url: Value,
message_hash: Value,
optional_args: &[Value],
) -> impl ::std::future::Future<Output = Value> + Send { async move {
let url_s = match &url { Value::Str(s) => s.clone(), v => crate::runtime::stringify_param(v) };
let hash_s = match &message_hash { Value::Str(s) => s.clone(), v => crate::runtime::stringify_param(v) };
let message = optional_args.get(0).cloned().unwrap_or(Value::Null);
let subscribe_hash = optional_args.get(1).cloned().unwrap_or(message_hash.clone());
let sub_hash_s = match &subscribe_hash { Value::Str(s) => s.clone(), v => crate::runtime::stringify_param(v) };
let subscription = optional_args.get(2).cloned().unwrap_or(Value::Null);
self.ws_run(url_s, vec![hash_s], message, vec![sub_hash_s], subscription).await
} }
fn watch_multiple(
&mut self,
url: Value,
message_hashes: Value,
optional_args: &[Value],
) -> impl ::std::future::Future<Output = Value> + Send { async move {
let url_s = match &url { Value::Str(s) => s.clone(), v => crate::runtime::stringify_param(v) };
let hashes = ws_string_list(&message_hashes);
let message = optional_args.get(0).cloned().unwrap_or(Value::Null);
let subscribe_hashes = ws_string_list(&optional_args.get(1).cloned().unwrap_or(Value::Null));
let subscription = optional_args.get(2).cloned().unwrap_or(Value::Null);
self.ws_run(url_s, hashes, message, subscribe_hashes, subscription).await
} }
fn fetch_typed(
&mut self,
url: &str,
method: &str,
headers: HashMap<String, String>,
body: Option<String>,
) -> impl ::std::future::Future<Output = Result<Value>> + Send { async move {
let _fetch_call_count = HTTP_CALLS.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
self.last_request_url = Value::Str(url.to_string());
self.last_request_headers = Value::Map(headers.iter()
.map(|(k, v)| (k.clone(), Value::Str(v.clone())))
.collect());
self.last_request_body = match &body {
Some(b) => Value::Str(b.clone()),
None => Value::Null,
};
if !matches!(self.mock_response, Value::Null) {
return Ok(self.mock_response.clone());
}
let proxy_defined = |v: &Value| matches!(v, Value::Str(s) if !s.is_empty());
let used_proxies = [
proxy_defined(&self.httpProxy) || proxy_defined(&self.http_proxy),
proxy_defined(&self.httpsProxy) || proxy_defined(&self.https_proxy),
proxy_defined(&self.socksProxy) || proxy_defined(&self.socks_proxy),
].iter().filter(|x| **x).count();
if used_proxies > 1 {
return Err(ExchangeError::new(
"InvalidProxySettings",
format!("{} you have multiple conflicting proxy settings, please use only one from: httpProxy, httpsProxy, socksProxy",
match &self.id { Value::Str(s) => s.as_str(), _ => "" }),
));
}
let client = self.http_client().clone();
let method_parsed = reqwest::Method::from_bytes(method.as_bytes())
.map_err(|_| ExchangeError::new("BadRequest", format!("invalid HTTP method: {method}")))?;
let mut req = client.request(method_parsed, url);
for (k, v) in &self.internals.headers { req = req.header(k, v); }
for (k, v) in &headers { req = req.header(k, v); }
if self.is_verbose() {
eprintln!("[ccxt] {method} {url}");
for (k, v) in &self.internals.headers { eprintln!("[ccxt] {k}: {v}"); }
for (k, v) in &headers { eprintln!("[ccxt] {k}: {v}"); }
if let Some(b) = &body {
eprintln!("[ccxt] request body: {b}");
}
}
if let Some(b) = body { req = req.body(b); }
let __http_t0 = std::time::Instant::now();
let resp = req.send().await?;
let status = resp.status().as_u16();
let text = resp.text().await?;
HTTP_NANOS.fetch_add(__http_t0.elapsed().as_nanos() as u64,
std::sync::atomic::Ordering::Relaxed);
if self.is_verbose() {
eprintln!("[ccxt] ← {status} {} bytes", text.len());
const CAP: usize = 16384;
if text.len() > CAP {
eprintln!("[ccxt] response body: {}… ({} bytes total)",
&text[..text.char_indices().take_while(|(i, _)| *i < CAP).last().map(|(i, c)| i + c.len_utf8()).unwrap_or(0)],
text.len());
} else {
eprintln!("[ccxt] response body: {text}");
}
}
let __json_t0 = std::time::Instant::now();
let json = match serde_json::from_str::<serde_json::Value>(&text) {
Ok(j) => Value::from_json(&j),
Err(_) => Value::Null,
};
JSON_NANOS.fetch_add(__json_t0.elapsed().as_nanos() as u64,
std::sync::atomic::Ordering::Relaxed);
if status >= 400 {
crate::exchange::DerivedExchange::handle_errors(
self,
Value::Int(status as i64),
Value::Null,
Value::Str(url.to_string()),
Value::Str(method.to_string()),
Value::Null,
Value::Str(text.clone()),
json.clone(),
Value::Null,
Value::Null,
);
return Err(ExchangeError::new(
if status == 429 { "RateLimitExceeded" }
else if status >= 500 { "ExchangeNotAvailable" }
else { "ExchangeError" },
format!("HTTP {status}: {text}"),
));
}
if matches!(json, Value::Null) {
return Ok(Value::Str(text));
}
Ok(json)
} }
fn request_typed(&mut self, path: &str, scope_segments: &[String], verb: &str, params: Value, cost: Value) -> impl ::std::future::Future<Output = Result<Value>> + Send { async move {
if !matches!(self.mock_response, Value::Null) {
return self.fetch_typed("", verb, HashMap::new(), None).await;
}
self.throttle(&[cost]).await;
let api_arg = if scope_segments.len() == 1 {
Value::Str(scope_segments[0].clone())
} else {
Value::Array(scope_segments.iter().map(|s| Value::Str(s.clone())).collect())
};
let scope_lookup: String = scope_segments.first().cloned().unwrap_or_default();
let signed = crate::exchange::DerivedExchange::sign(
self,
Value::Str(path.to_string()),
api_arg,
Value::Str(verb.to_string()),
params.clone(),
Value::Null,
Value::Null,
);
if let Value::Dict(m) = &signed {
let url = match m.get("url") { Some(Value::Str(s)) => s.clone(), _ => String::new() };
let method = match m.get("method") { Some(Value::Str(s)) => s.clone(), _ => verb.to_string() };
let body = match m.get("body") { Some(Value::Str(s)) => Some(s.clone()), _ => None };
let headers = match m.get("headers") {
Some(Value::Dict(h)) => h.iter()
.filter_map(|(k, v)| match v { Value::Str(s) => Some((k.clone(), s.clone())), _ => None })
.collect::<HashMap<_, _>>(),
_ => HashMap::new(),
};
if !url.is_empty() {
return self.fetch_typed(&url, &method, headers, body).await;
}
}
let base = self.url_for_scope(&scope_lookup).ok_or_else(|| ExchangeError::new(
"BadRequest",
format!("no URL configured for api scope `{scope_lookup}`"),
))?;
let imploded = self.implode_params(Value::Str(path.to_string()), params.clone());
let path_str = match &imploded { Value::Str(s) => s.clone(), _ => path.to_string() };
let consumed_keys = self.extract_path_params(path);
let remaining_params = match params {
Value::Dict(m) => {
let mut keep = HashMap::new();
let m = Arc::try_unwrap(m).unwrap_or_else(|a| (*a).clone());
for (k, v) in m {
if !consumed_keys.contains(&k) { keep.insert(k, v); }
}
Value::Map(keep)
}
other => other,
};
let mut url = format!("{}/{}", base.trim_end_matches('/'), path_str.trim_start_matches('/'));
let mut body: Option<String> = None;
let qs = match &remaining_params {
Value::Dict(m) if !m.is_empty() => self.urlencode_kv(&remaining_params),
_ => String::new(),
};
if verb == "GET" || verb == "DELETE" {
if !qs.is_empty() {
url = format!("{}?{}", url, qs);
}
} else if !qs.is_empty() {
body = Some(qs);
}
let mut headers = HashMap::new();
if scope_lookup != "public" {
if let Value::Str(k) = &self.apiKey {
headers.insert("X-MBX-APIKEY".to_string(), k.clone());
}
}
self.fetch_typed(&url, verb, headers, body).await
} }
fn implicit_api_call(&mut self, name: &str, params: Value) -> impl ::std::future::Future<Output = Result<Value>> + Send { async move {
if self.internals.implicit_api.is_empty() {
self.build_implicit_api();
}
let entry = self.internals.implicit_api.get(name).cloned();
let (path, scope_segments, verb, config) = match entry {
Some(t) => t,
None => return Err(ExchangeError::new(
"NotSupported",
format!("implicit API method {name} not found in api block"),
)),
};
let api = if scope_segments.len() == 1 {
Value::Str(scope_segments[0].clone())
} else {
Value::Array(scope_segments.iter().map(|s| Value::Str(s.clone())).collect())
};
let cost = if matches!(self.enableRateLimit, Value::Bool(true)) {
self.call_dynamic(
"calculate_rate_limiter_cost",
vec![api, Value::Str(verb.clone()), Value::Str(path.clone()), params.clone(), config],
).await
} else {
Value::Null
};
self.request_typed(&path, &scope_segments, &verb, params, cost).await
} }
#[cfg(test)]
fn implicit_api_rate_limit_cost(&mut self, name: &str, params: Value) -> impl ::std::future::Future<Output = Result<Value>> + Send { async move {
if self.internals.implicit_api.is_empty() {
self.build_implicit_api();
}
let (path, scope_segments, verb, config) = self.internals.implicit_api
.get(name)
.cloned()
.ok_or_else(|| ExchangeError::new("NotSupported", format!("implicit API method {name} not found in api block")))?;
let api = if scope_segments.len() == 1 {
Value::Str(scope_segments[0].clone())
} else {
Value::Array(scope_segments.iter().map(|s| Value::Str(s.clone())).collect())
};
Ok(self.call_dynamic(
"calculate_rate_limiter_cost",
vec![api, Value::Str(verb), Value::Str(path), params, config],
).await)
} }
fn call_method(&mut self, name: Value, args: &[Value]) -> impl ::std::future::Future<Output = Value> + Send { async move {
let n = match name { Value::Str(s) => s, _ => return Value::Null };
let params = args.get(0).cloned().unwrap_or(Value::Map(HashMap::new()));
match self.implicit_api_call(&n, params).await {
Ok(v) => v,
Err(e) => {
if matches!(self.verbose, Value::Bool(true)) {
eprintln!("[ccxt] {n} failed: {e}");
}
panic!("{e}");
}
}
} }
fn load_markets(&mut self, optional_args: &[Value]) -> impl ::std::future::Future<Output = Value> + Send { async move {
let reload = optional_args.get(0).cloned().unwrap_or(Value::Bool(false));
let reload_b = matches!(reload, Value::Bool(true));
if !reload_b && !self.markets.is_null() {
return self.markets.clone();
}
let has_fetch_currencies = matches!(
crate::get_value(&self.has, &Value::Str("fetchCurrencies".to_string())),
Value::Bool(true),
);
let mut currencies = Value::Null;
if has_fetch_currencies {
if let Some(v) = self.dispatch_to_derived("fetch_currencies", Vec::new()).await {
currencies = v;
}
}
let fetched = match self.dispatch_to_derived("fetch_markets", Vec::new()).await {
Some(v) => v,
None => return Value::Null,
};
if matches!(fetched, Value::Null) {
return self.markets.clone();
}
let preloaded_non_empty = matches!(&self.currencies, Value::Dict(m) if !m.is_empty());
let currencies_arg = if matches!(¤cies, Value::Dict(m) if !m.is_empty()) {
currencies
} else if preloaded_non_empty {
self.currencies.clone()
} else {
crate::exchange_stubs::synthesize_currencies_from_markets(&fetched, &self.precisionMode)
};
let is_prediction = matches!(
crate::get_value(&self.has, &Value::Str("prediction".to_string())),
Value::Bool(true),
);
if is_prediction {
let _ = self.dispatch_to_derived("set_markets", vec![fetched, currencies_arg]).await;
} else {
<Self as crate::exchange_generated::ExchangeBase>::set_markets(self, fetched, &[currencies_arg]);
}
let markets_snapshot = self.markets.clone();
if let Value::Dict(markets) = &markets_snapshot {
if let Value::Dict(by_id_arc) = &self.markets_by_id {
let mut by_id = (**by_id_arc).clone();
for market in markets.values() {
let id = crate::get_value(market, &Value::Str("id".to_string()));
let id2 = crate::get_value(market, &Value::Str("id2".to_string()));
if let (Value::Str(id2s), Value::Str(ids)) = (&id2, &id) {
if id2s != ids && !id2s.is_empty() && !by_id.contains_key(id2s) {
by_id.insert(id2s.clone(), Value::List(vec![market.clone()]));
}
}
}
self.markets_by_id = Value::Map(by_id);
}
}
self.markets.clone()
} }
fn super_describe(&self) -> Value {
<Self as crate::exchange_generated::ExchangeBase>::describe(self)
}
fn super_set_sandbox_mode(&mut self, enabled: Value) {
<Self as crate::exchange_generated::ExchangeBase>::set_sandbox_mode(self, enabled);
}
fn super_safe_market(&self, id: Value, market: Value, delim: Value, market_type: Value) -> Value {
<Self as crate::exchange_generated::ExchangeBase>::safe_market(self, &[id, market, delim, market_type])
}
fn super_handle_market_type_and_params(&self, method_name: Value, market: Value, params: Value, default_value: Value) -> Value {
<Self as crate::exchange_generated::ExchangeBase>::handle_market_type_and_params(self, method_name, &[market, params, default_value])
}
fn super_market(&self, symbol: Value) -> Value {
<Self as crate::exchange_generated::ExchangeBase>::market(self, symbol)
}
fn super_amount_to_precision(&self, symbol: Value, amount: Value) -> Value {
<Self as crate::exchange_generated::ExchangeBase>::amount_to_precision(self, symbol, amount)
}
fn super_handle_margin_mode_and_params(&self, method_name: Value, params: Value, default_value: Value) -> Value {
<Self as crate::exchange_generated::ExchangeBase>::handle_margin_mode_and_params(self, method_name, &[params, default_value])
}
fn super_safe_currency_code(&self, currency_id: Value, currency: Value) -> Value {
<Self as crate::exchange_generated::ExchangeBase>::safe_currency_code(self, currency_id, &[currency])
}
fn super_set_markets(&mut self, markets: Value, currencies: Value) -> Value {
<Self as crate::exchange_generated::ExchangeBase>::set_markets(self, markets, &[currencies])
}
fn super_network_id_to_code(&self, optional_args: &[Value]) -> Value {
<Self as crate::exchange_generated::ExchangeBase>::network_id_to_code(self, optional_args)
}
fn super_network_code_to_id(&self, network_code: Value, optional_args: &[Value]) -> Value {
<Self as crate::exchange_generated::ExchangeBase>::network_code_to_id(self, network_code, optional_args)
}
fn super_fetch_deposit_address(&mut self, code: Value, params: Value) -> impl ::std::future::Future<Output = Value> + Send { async move {
self.fetch_deposit_address(code, &[params]).await
} }
fn super_load_markets(&mut self, reload: Value, params: Value) -> impl ::std::future::Future<Output = Value> + Send { async move {
self.load_markets(&[reload, params]).await
} }
}
impl<T: crate::exchange_generated::ExchangeBase> ExchangeRuntime for T {}
pub struct BaseCore {
pub exchange: Exchange,
}
impl BaseCore {
pub fn new(exchange: Exchange) -> Self { Self { exchange } }
pub fn into_inner(self) -> Exchange { self.exchange }
}
impl std::ops::Deref for BaseCore {
type Target = Exchange;
fn deref(&self) -> &Exchange { &self.exchange }
}
impl std::ops::DerefMut for BaseCore {
fn deref_mut(&mut self) -> &mut Exchange { &mut self.exchange }
}
impl DerivedExchange for BaseCore {}
impl crate::exchange_generated::ExchangeBase for BaseCore {
fn call_dynamic<'a>(&'a mut self, method: &'a str, args: Vec<Value>)
-> std::pin::Pin<Box<dyn std::future::Future<Output = Value> + Send + 'a>>
{
Box::pin(async move { self.call_dynamic_base(method, args).await })
}
}
impl Exchange {
pub fn build_implicit_api(&mut self) {
let mut map: HashMap<String, (String, Vec<String>, String, Value)> = HashMap::new();
Self::walk_api_node(&self.api, &[], &mut map);
self.internals.implicit_api = map;
}
fn walk_api_node(
node: &Value,
crumbs: &[String],
out: &mut HashMap<String, (String, Vec<String>, String, Value)>,
) {
let known_verbs = ["get", "post", "put", "delete", "patch"];
let last_is_verb = crumbs.last()
.map(|c| known_verbs.iter().any(|v| *v == c.to_lowercase().as_str()))
.unwrap_or(false);
match node {
Value::Dict(m) if last_is_verb => {
let verb = crumbs.last().unwrap().to_lowercase();
let scope_snake = Self::api_scope_from_crumbs(&crumbs[..crumbs.len() - 1]);
let scope_segments: Vec<String> = crumbs[..crumbs.len() - 1].to_vec();
for (path_key, endpoint_config) in m.iter() {
let config = match endpoint_config {
Value::Dict(_) => endpoint_config.clone(),
Value::Int(_) | Value::Float(_) => Value::Map({
let mut config = crate::value::HashMap::new();
config.insert("cost".to_string(), endpoint_config.clone());
config
}),
_ => Value::Map(crate::value::HashMap::new()),
};
Self::register_implicit(&verb, &scope_snake, &scope_segments, path_key, config, out);
}
}
Value::Dict(m) => {
for (key, child) in m.iter() {
let mut next = crumbs.to_vec();
next.push(key.clone());
Self::walk_api_node(child, &next, out);
}
}
Value::Arr(a) if last_is_verb => {
let verb = crumbs.last().unwrap().to_lowercase();
let scope_snake = Self::api_scope_from_crumbs(&crumbs[..crumbs.len() - 1]);
let scope_segments: Vec<String> = crumbs[..crumbs.len() - 1].to_vec();
for path in a.iter() {
let (path_str, config) = match path {
Value::Str(s) => (s.clone(), Value::Map(crate::value::HashMap::new())),
Value::Dict(pm) => match pm.get("method").or_else(|| pm.get("path")) {
Some(Value::Str(s)) => (s.clone(), path.clone()),
_ => continue,
},
_ => continue,
};
Self::register_implicit(&verb, &scope_snake, &scope_segments, &path_str, config, out);
}
}
_ => {}
}
}
fn api_scope_from_crumbs(crumbs: &[String]) -> String {
crumbs.iter()
.map(|s| s
.chars()
.enumerate()
.flat_map(|(i, c)| {
if c.is_ascii_uppercase() && i > 0 {
vec!['_', c.to_ascii_lowercase()]
} else {
vec![c.to_ascii_lowercase()]
}
})
.collect::<String>())
.collect::<Vec<_>>()
.join("_")
}
fn register_implicit(
verb: &str,
scope_snake: &str,
scope_segments: &[String],
path: &str,
config: Value,
out: &mut HashMap<String, (String, Vec<String>, String, Value)>,
) {
let camel: String = path
.split(|c: char| !c.is_ascii_alphanumeric())
.filter(|s| !s.is_empty())
.map(|seg| {
let mut chars = seg.chars();
match chars.next() {
None => String::new(),
Some(c) => c.to_ascii_uppercase().to_string() + chars.as_str(),
}
})
.collect();
let snake = Self::to_snake_case(&camel);
let snake_name = if snake.is_empty() {
format!("{scope_snake}_{verb}")
} else {
format!("{scope_snake}_{verb}_{snake}")
};
let scope_camel: String = scope_segments.iter()
.enumerate()
.map(|(i, s)| {
if i == 0 {
s.to_string()
} else {
let mut chars = s.chars();
match chars.next() {
None => String::new(),
Some(c) => c.to_ascii_uppercase().to_string() + chars.as_str(),
}
}
})
.collect();
let verb_camel = {
let mut chars = verb.chars();
match chars.next() {
None => String::new(),
Some(c) => c.to_ascii_uppercase().to_string() + chars.as_str().to_lowercase().as_str(),
}
};
let camel_name = format!("{scope_camel}{verb_camel}{camel}");
let camel_snake_name = Self::to_snake_case(&camel_name);
let entry = (path.to_string(), scope_segments.to_vec(), verb.to_uppercase(), config);
out.insert(snake_name, entry.clone());
if !out.contains_key(&camel_snake_name) {
out.insert(camel_snake_name, entry.clone());
}
if !out.contains_key(&camel_name) {
out.insert(camel_name, entry);
}
}
fn to_snake_case(s: &str) -> String {
let bytes: Vec<char> = s.chars().collect();
let mut out = String::with_capacity(s.len() + 4);
for i in 0..bytes.len() {
let c = bytes[i];
let prev = if i > 0 { Some(bytes[i - 1]) } else { None };
let next = if i + 1 < bytes.len() { Some(bytes[i + 1]) } else { None };
if c.is_ascii_uppercase() {
let prev_lower_or_digit = prev
.map(|p| p.is_ascii_lowercase() || p.is_ascii_digit())
.unwrap_or(false);
let prev_upper = prev.map(|p| p.is_ascii_uppercase()).unwrap_or(false);
let next_lower = next.map(|n| n.is_ascii_lowercase()).unwrap_or(false);
if prev_lower_or_digit {
out.push('_');
} else if prev_upper && next_lower {
out.push('_');
}
}
out.push(c.to_ascii_lowercase());
}
while out.contains("__") {
out = out.replace("__", "_");
}
out.trim_matches('_').to_string()
}
fn url_for_scope(&self, scope: &str) -> Option<String> {
let api = crate::get_value(&self.urls, &Value::Str("api".to_string()));
let scoped = crate::get_value(&api, &Value::Str(scope.to_string()));
let raw = match scoped {
Value::Str(s) => s,
Value::Dict(m) => match m.get("rest").or_else(|| m.get("current")) {
Some(Value::Str(s)) => s.clone(),
_ => return self.fallback_rest_url(&api),
},
_ => return self.fallback_rest_url(&api),
};
Some(self.implode_hostname_typed(&raw))
}
fn fallback_rest_url(&self, api: &Value) -> Option<String> {
let rest = crate::get_value(api, &Value::Str("rest".to_string()));
if let Value::Str(s) = rest {
return Some(self.implode_hostname_typed(&s));
}
if let Value::Str(s) = api {
return Some(self.implode_hostname_typed(s));
}
None
}
fn implode_hostname_typed(&self, url: &str) -> String {
let host = match &self.hostname {
Value::Str(s) => s.clone(),
_ => return url.to_string(),
};
url.replace("{hostname}", &host)
}
fn extract_path_params(&self, path: &str) -> Vec<String> {
let mut out: Vec<String> = vec![];
let mut chars = path.chars().peekable();
while let Some(c) = chars.next() {
if c == '{' {
let mut name = String::new();
while let Some(&nc) = chars.peek() {
if nc == '}' { chars.next(); break; }
name.push(nc); chars.next();
}
if !name.is_empty() { out.push(name); }
}
}
out
}
}
pub(crate) fn hash_raw(data: &[u8], algo: &str) -> Vec<u8> {
match algo.to_ascii_lowercase().as_str() {
"sha256" => { let mut h = Sha256::new(); h.update(data); h.finalize().to_vec() }
"sha512" => { let mut h = Sha512::new(); h.update(data); h.finalize().to_vec() }
"sha1" => { let mut h = Sha1::new(); h.update(data); h.finalize().to_vec() }
"md5" => { let mut h = Md5::new(); h.update(data); h.finalize().to_vec() }
"keccak" | "keccak256" => {
use sha3::{Keccak256, Digest as _};
let mut h = Keccak256::new();
h.update(data);
h.finalize().to_vec()
}
_ => Vec::new(),
}
}
type Eip712Types = std::collections::BTreeMap<String, Vec<(String, String)>>;
fn eip712_base_type(ty: &str) -> &str {
match ty.find('[') { Some(i) => &ty[..i], None => ty }
}
fn eip712_encode_type(primary: &str, types: &Eip712Types) -> String {
fn collect(name: &str, types: &Eip712Types, deps: &mut std::collections::BTreeSet<String>) {
if let Some(fields) = types.get(name) {
for (_, ty) in fields {
let base = eip712_base_type(ty).to_string();
if types.contains_key(&base) && deps.insert(base.clone()) {
collect(&base, types, deps);
}
}
}
}
let fmt = |name: &str| -> String {
let fields = types.get(name).cloned().unwrap_or_default();
let inner: Vec<String> = fields.iter().map(|(n, t)| format!("{t} {n}")).collect();
format!("{}({})", name, inner.join(","))
};
let mut deps = std::collections::BTreeSet::new();
collect(primary, types, &mut deps);
deps.remove(primary);
let mut out = fmt(primary);
for dep in &deps { out.push_str(&fmt(dep)); } out
}
pub(crate) fn eip712_encode(domain: &Value, types: &Value, message: &Value) -> Vec<u8> {
let dm = match domain { Value::Dict(m) => m, _ => return Vec::new() };
let mut domain_fields: Vec<(String, String)> = Vec::new();
for (n, t) in [("name", "string"), ("version", "string"), ("chainId", "uint256"),
("verifyingContract", "address"), ("salt", "bytes32")] {
if dm.contains_key(n) { domain_fields.push((n.to_string(), t.to_string())); }
}
let mut domain_types: Eip712Types = Eip712Types::new();
domain_types.insert("EIP712Domain".to_string(), domain_fields);
let domain_sep = eip712_hash_struct("EIP712Domain", domain, &domain_types);
let mut type_map: Eip712Types = Eip712Types::new();
if let Value::Dict(m) = types {
for (name, def) in m.iter() {
if name == "EIP712Domain" { continue; }
if let Value::Arr(arr) = def {
let fields: Vec<(String, String)> = arr.iter().filter_map(|it| match it {
Value::Dict(fm) => Some((
fm.get("name")?.as_str()?.to_string(),
fm.get("type")?.as_str()?.to_string(),
)),
_ => None,
}).collect();
type_map.insert(name.clone(), fields);
}
}
}
if type_map.is_empty() { return Vec::new(); }
let referenced: std::collections::HashSet<String> = type_map.values()
.flat_map(|fs| fs.iter().map(|(_, t)| eip712_base_type(t).to_string()))
.collect();
let primary = type_map.keys().find(|k| !referenced.contains(*k)).cloned()
.unwrap_or_else(|| type_map.keys().next().cloned().unwrap_or_default());
let struct_hash = eip712_hash_struct(&primary, message, &type_map);
let mut out = vec![0x19u8, 0x01u8];
out.extend_from_slice(&domain_sep);
out.extend_from_slice(&struct_hash);
out
}
fn eip712_hash_struct(type_name: &str, data: &Value, types: &Eip712Types) -> Vec<u8> {
let type_str = eip712_encode_type(type_name, types);
let mut enc = hash_raw(type_str.as_bytes(), "keccak"); let dm = match data { Value::Dict(m) => Some(m), _ => None };
let fields = types.get(type_name).cloned().unwrap_or_default();
for (name, ty) in &fields {
let v = dm.and_then(|m| m.get(name)).cloned().unwrap_or(Value::Null);
let base = eip712_base_type(ty);
if types.contains_key(base) {
enc.extend_from_slice(&eip712_hash_struct(base, &v, types));
} else {
enc.extend_from_slice(&eip712_encode_value(ty, &v));
}
}
hash_raw(&enc, "keccak")
}
pub(crate) fn eip712_int_word(ty: &str, n: &num_bigint::BigInt) -> [u8; 32] {
use num_bigint::{BigInt, Sign};
let signed = ty.starts_with("int");
let digits = ty.trim_start_matches(if signed { "int" } else { "uint" });
let mut width: usize = if digits.is_empty() { 256 } else { digits.parse().unwrap_or(256) };
if !((8..=256).contains(&width) && width % 8 == 0) { width = 256; }
let mut out = [0u8; 32];
if !signed {
if n.sign() == Sign::Minus {
panic!("eip712/abi: negative value {n} supplied for unsigned type {ty}");
}
let max = (BigInt::from(1) << width) - 1;
if n > &max {
panic!("eip712/abi: value {n} overflows {ty} (max {max})");
}
let (_, be) = n.to_bytes_be();
let take = be.len().min(32);
out[32 - take..].copy_from_slice(&be[be.len() - take..]);
} else {
let min = -(BigInt::from(1) << (width - 1));
let max = (BigInt::from(1) << (width - 1)) - 1;
if n < &min || n > &max {
panic!("eip712/abi: value {n} out of range for {ty} ([{min}, {max}])");
}
if n.sign() == Sign::Minus { out = [0xff; 32]; }
let be = n.to_signed_bytes_be();
let take = be.len().min(32);
out[32 - take..].copy_from_slice(&be[be.len() - take..]);
}
out
}
fn eip712_encode_value(ty: &str, v: &Value) -> [u8; 32] {
let mut out = [0u8; 32];
if ty == "string" || ty == "bytes" {
out.copy_from_slice(&hash_raw(&value_to_bytes(v), "keccak"));
} else if ty.starts_with("bytes") {
let b = eip712_bytes(v);
let n = b.len().min(32);
out[..n].copy_from_slice(&b[..n]);
} else if ty == "address" {
let b = eip712_bytes(v);
let take = b.len().min(20);
out[32 - take..].copy_from_slice(&b[b.len() - take..]);
} else if ty == "bool" {
if matches!(v, Value::Bool(true)) { out[31] = 1; }
} else if ty.starts_with("uint") || ty.starts_with("int") {
use num_bigint::BigInt;
let n: BigInt = match v {
Value::Int(i) => BigInt::from(*i),
Value::Float(f) => BigInt::from(*f as i64),
Value::Str(s) => {
let t = s.trim();
if let Some(h) = t.strip_prefix("0x") {
BigInt::parse_bytes(h.as_bytes(), 16).unwrap_or_default()
} else {
BigInt::parse_bytes(t.as_bytes(), 10).unwrap_or_default()
}
}
_ => BigInt::from(0),
};
out = eip712_int_word(ty, &n);
}
out
}
fn eip712_bytes(v: &Value) -> Vec<u8> {
match v {
Value::Str(s) => {
let h = s.strip_prefix("0x").unwrap_or(s);
hex::decode(h).unwrap_or_else(|_| s.as_bytes().to_vec())
}
_ => value_to_bytes(v),
}
}
pub(crate) fn value_to_bytes(v: &Value) -> Vec<u8> {
match v {
Value::Str(s) => s.as_bytes().to_vec(),
Value::Arr(a) => a.iter().filter_map(|x| match x {
Value::Int(n) => Some(*n as u8),
_ => None,
}).collect(),
_ => Vec::new(),
}
}
pub(crate) fn url_pct(s: &str) -> String {
s.bytes().map(|b| match b {
b'A'..=b'Z' | b'a'..=b'z' | b'0'..=b'9' | b'-' | b'_' | b'.' | b'~' => (b as char).to_string(),
_ => format!("%{b:02X}"),
}).collect()
}
#[cfg(test)]
mod eip712_int_tests {
use super::eip712_int_word;
use num_bigint::BigInt;
#[test]
fn uint8_max_ok_and_int8_bounds_ok() {
assert_eq!(eip712_int_word("uint8", &BigInt::from(255))[31], 255);
let _ = eip712_int_word("int8", &BigInt::from(127));
let _ = eip712_int_word("int8", &BigInt::from(-128));
}
#[test]
fn i64_u64_boundaries_ok() {
let _ = eip712_int_word("int64", &BigInt::from(i64::MAX));
let _ = eip712_int_word("int64", &BigInt::from(i64::MIN));
let _ = eip712_int_word("uint64", &BigInt::from(u64::MAX));
}
#[test]
fn u64_plus_one_preserved_in_uint256() {
let n = BigInt::from(u64::MAX) + 1;
assert_eq!(eip712_int_word("uint256", &n)[23], 1);
}
#[test]
fn negative_int_is_twos_complement() {
assert_eq!(eip712_int_word("int256", &BigInt::from(-1)), [0xff; 32]);
}
#[test]
#[should_panic(expected = "overflows")]
fn uint8_overflow_rejected() { let _ = eip712_int_word("uint8", &BigInt::from(256)); }
#[test]
#[should_panic(expected = "negative")]
fn uint_negative_rejected() { let _ = eip712_int_word("uint256", &BigInt::from(-1)); }
#[test]
#[should_panic(expected = "out of range")]
fn int8_overflow_rejected() { let _ = eip712_int_word("int8", &BigInt::from(128)); }
#[test]
#[should_panic(expected = "overflows")]
fn uint64_plus_one_rejected() {
let _ = eip712_int_word("uint64", &(BigInt::from(u64::MAX) + 1));
}
}
#[cfg(test)]
mod throttle_tests {
use super::Exchange;
use crate::Value;
#[test]
fn implicit_api_retains_endpoint_cost_config() {
let mut by_limit = crate::value::HashMap::new();
by_limit.insert("cost".to_string(), Value::Int(2));
by_limit.insert("byLimit".to_string(), Value::List(vec![
Value::List(vec![Value::Int(100), Value::Int(5)]),
]));
let mut endpoints = crate::value::HashMap::new();
endpoints.insert("ticker".to_string(), Value::Float(0.1));
endpoints.insert("depth".to_string(), Value::Map(by_limit.clone()));
let mut verbs = crate::value::HashMap::new();
verbs.insert("get".to_string(), Value::Map(endpoints));
let mut api = crate::value::HashMap::new();
api.insert("public".to_string(), Value::Map(verbs));
let mut exchange = Exchange::new(None);
exchange.api = Value::Map(api);
exchange.build_implicit_api();
let ticker_config = &exchange.internals.implicit_api["public_get_ticker"].3;
assert_eq!(
crate::get_value(ticker_config, &Value::Str("cost".to_string())),
Value::Float(0.1),
"numeric endpoint costs must be normalized to a config object"
);
let depth_config = &exchange.internals.implicit_api["public_get_depth"].3;
assert_eq!(depth_config, &Value::Map(by_limit), "object endpoint config must be retained");
}
#[tokio::test]
async fn spaces_requests_by_rate_limit() {
let mut ex = Exchange::new(None);
ex.enableRateLimit = Value::Bool(true);
ex.rateLimit = Value::Int(15); let start = std::time::Instant::now();
for _ in 0..5 { ex.throttle(&[]).await; }
let elapsed = start.elapsed().as_millis();
assert!(elapsed >= 45, "throttle did not space requests: {elapsed}ms");
}
#[tokio::test]
async fn disabled_is_noop() {
let mut ex = Exchange::new(None);
ex.enableRateLimit = Value::Bool(false);
ex.rateLimit = Value::Int(1000);
let start = std::time::Instant::now();
for _ in 0..5 { ex.throttle(&[]).await; }
assert!(start.elapsed().as_millis() < 50, "disabled throttle slept");
}
}
#[cfg(all(test, feature = "transpiled-base"))]
mod rate_limit_config_tests {
use crate::exchange::ExchangeRuntime;
use crate::exchange_generated::ExchangeBase;
use crate::Value;
#[test]
fn binance_init_applies_describe_rate_limit() {
let b = crate::exchanges::binance::BinanceCore::new(None);
assert_eq!(
b.exchange.rateLimit,
Value::Int(50),
"init() dropped describe().rateLimit; got {:?}",
b.exchange.rateLimit
);
}
#[test]
fn config_rate_limit_overrides_describe() {
let mut cfg = crate::value::HashMap::new();
cfg.insert("rateLimit".to_string(), Value::Int(123));
let b = crate::exchanges::binance::BinanceCore::new(Some(Value::Map(cfg)));
assert_eq!(b.exchange.rateLimit, Value::Int(123), "config rateLimit was clobbered by describe()");
}
#[tokio::test]
async fn implicit_api_preserves_and_calculates_endpoint_cost() {
let mut binance = crate::exchanges::binance::BinanceCore::new(None);
binance.build_implicit_api();
let config = binance
.exchange
.internals
.implicit_api
.get("fapi_public_get_depth")
.expect("Binance futures depth endpoint must be registered")
.3
.clone();
assert_eq!(
crate::get_value(&config, &Value::Str("cost".to_string())),
Value::Int(2),
"the implicit API table dropped the endpoint's base cost"
);
let params = Value::Map({
let mut map = crate::value::HashMap::new();
map.insert("limit".to_string(), Value::Int(100));
map
});
let cost = binance
.implicit_api_rate_limit_cost("fapi_public_get_depth", params)
.await
.expect("registered endpoint must have a calculable cost");
assert_eq!(cost, Value::Int(5), "Binance byLimit cost was not applied");
}
}
#[cfg(all(test, feature = "transpiled-base"))]
mod sandbox_mode_tests {
use crate::Value;
#[test]
fn binance_sandbox_swaps_api_url() {
let mut b = crate::exchanges::binance::BinanceCore::new(None);
let test_url = crate::get_value(&b.exchange.urls, &Value::Str("test".to_string()));
assert!(!matches!(test_url, Value::Null), "binance describe() has no test url");
b.set_sandbox_mode(Value::Bool(true));
assert_eq!(b.exchange.isSandboxModeEnabled, Value::Bool(true));
let api_url = crate::get_value(&b.exchange.urls, &Value::Str("api".to_string()));
assert_eq!(api_url, test_url, "sandbox mode did not switch urls['api'] to urls['test']");
}
}
#[cfg(all(test, feature = "transpiled-base"))]
mod cow_alias_tests {
use super::Exchange;
use crate::Value;
use crate::runtime::get_array_length;
#[test]
fn convert_ohlcv_to_trading_view_preserves_pushes() {
use crate::exchange_generated::ExchangeBase;
let ex = super::BaseCore::new(Exchange::new(None));
let row = Value::List(vec![
Value::Int(1_700_000_000_000), Value::Int(1), Value::Int(2),
Value::Int(0), Value::Int(1), Value::Int(10),
]);
let ohlcvs = Value::List(vec![row.clone(), row]);
let r = ex.convert_ohlcv_to_trading_view(ohlcvs, &[]);
for key in ["t", "o", "h", "l", "c", "v"] {
let col = crate::get_value(&r, &Value::Str(key.to_string()));
assert_eq!(get_array_length(&col), Value::Int(2),
"result['{key}'] lost its pushes — COW write-back failed");
}
}
}