nautechsystems/nautilus_trader · critical

invalid `CurrencyType` enum string value, was '{value}'

Error message

invalid `CurrencyType` enum string value, was '{value}'

What it means

`currency_type_from_cstr` parses a C string into the Rust `CurrencyType` enum (FIAT, CRYPTO, COMMODITY, etc.) and panics if the string is not a valid variant. As with the other FFI enum parsers, unknown strings cannot be represented, so the function panics and `abort_on_panic` converts the panic into a process abort at the C boundary. Only canonical strings produced by `currency_type_to_cstr` are accepted.

Source

Thrown at crates/model/src/ffi/enums.rs:505

pub extern "C" fn currency_type_to_cstr(value: CurrencyType) -> *const c_char {
    str_to_cstr(value.as_ref())
}

/// Returns an enum from a C string.
///
/// # Safety
///
/// Assumes `ptr` is a valid C string pointer.
///
/// # Panics
///
/// Panics if the C string does not correspond to a valid `CurrencyType` variant.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn currency_type_from_cstr(ptr: *const c_char) -> CurrencyType {
    abort_on_panic(|| {
        let value = unsafe { cstr_as_str(ptr) };
        CurrencyType::from_str(value)
            .unwrap_or_else(|_| panic!("invalid `CurrencyType` enum string value, was '{value}'"))
    })
}

/// Returns an enum from a C string.
///
/// # Safety
///
/// Assumes `ptr` is a valid C string pointer.
///
/// # Panics
///
/// Panics if the C string does not correspond to a valid `InstrumentCloseType` variant.
#[unsafe(no_mangle)]
pub unsafe extern "C" fn instrument_close_type_from_cstr(
    ptr: *const c_char,
) -> InstrumentCloseType {
    abort_on_panic(|| {
        let value = unsafe { cstr_as_str(ptr) };

View on GitHub (pinned to 18893faf8b)

Solutions

  1. Log the exact failing string and diff it against the canonical `CurrencyType` variant spellings.
  2. Use the `CurrencyType` enum plus `currency_type_to_cstr` to generate valid strings rather than hand-typing.
  3. Normalize case and trim whitespace before the call; matching is exact.
  4. Maintain an explicit mapping from venue/exchange currency classifications to `CurrencyType` strings.
  5. Validate the input against the accepted set before calling — failure aborts the process.

Example fix

// before
cur = "fiat"
currency_type_from_cstr(cur.encode())
// after
cur = "FIAT"  # canonical CurrencyType spelling
currency_type_from_cstr(cur.encode())
Defensive patterns

Strategy: validation

Validate before calling

VALID_CURRENCY_TYPES = {"FIAT", "CRYPTO", "COMMODITY"}  # verify against CurrencyType definition
assert cur_type_str in VALID_CURRENCY_TYPES, f"bad CurrencyType: {cur_type_str!r}"
currency_type_from_cstr(cur_type_str.encode())

Type guard

def is_valid_currency_type(s: str) -> bool:
    return s in VALID_CURRENCY_TYPES  # canonical strings from CurrencyType::as_ref

Prevention

When it happens

Trigger: Calling `currency_type_from_cstr(ptr)` with a non-canonical currency type (e.g. lowercase 'fiat', 'digital', or an empty string), or values copied from another SDK's currency classification.

Common situations: Registering currencies via the C API; adapters classifying venue currencies; config files with free-form currency-type text; version drift after enum changes.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


AI-assisted analysis of nautechsystems/nautilus_trader@18893faf8b (2026-09-08). Data as JSON: /api/errors/a2fb20aa50fd7176. Report an issue: GitHub.