nautechsystems/nautilus_trader · error

Invalid factory address for DEX

Error message

Invalid factory address for DEX {name} on chain {chain} for factory address {factory}: {e}

What it means

AmmsDex/DEX constructor validates the factory contract address with validate_address() and panics if the hex string is not a valid address. The panic includes the DEX name, chain, offending address, and the underlying validation error so the misconfiguration is immediately obvious.

Solutions

  1. Fix the factory address in your configuration to a full 0x-prefixed 40-hex-character address
  2. Run validate_address(factory) yourself before constructing the DEX to fail early with a clean message
  3. Verify the address is the correct contract for the specified chain (e.g. via an explorer)

Example fix

// before
let dex = AmmDex::new(chain, "UniswapV3", "0x1f9843..."); // truncated -> panic
// after
let factory = "0x1F98431c8aD98523631AE4a59f267346ea31F984";
validate_address(factory).expect("valid factory address");
let dex = AmmDex::new(chain, "UniswapV3", factory);
Defensive patterns

Strategy: validation

Validate before calling

// Validate before constructing the DEX
fn valid_factory(factory: &str) -> bool {
    factory.len() == 42 && factory.starts_with("0x") && factory[2..].chars().all(|c| c.is_ascii_hexdigit())
}

Type guard

fn is_hex_address(s: &str) -> bool {
    s.len() == 42 && s.starts_with("0x") && s[2..].chars().all(|c| c.is_ascii_hexdigit())
}

Try / catch

assert!(is_hex_address(factory), "bad factory address: {factory}");
let dex = AmmDex::new(chain, name, factory);

Prevention

When it happens

Trigger: Constructing a DEX (AmmDex::new) with a factory address that is not a valid 20-byte hex address (wrong length, missing 0x, non-hex characters, or a checksum/shape error from validate_address).

Common situations: Hardcoded config files with a truncated or malformed address; copying a factory address from docs of a different network; placeholder addresses left in a config; config values typed as ENS names instead of hex addresses.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at crates/model/src/defi/dex.rs:204

        name: DexType,
        factory: &str,
        factory_creation_block: u64,
        amm_type: AmmType,
        pool_created_event: &str,
        swap_event: &str,
        mint_event: &str,
        burn_event: &str,
        collect_event: &str,
    ) -> Self {
        let encoded_pool_created_event =
            hex::encode_prefixed(keccak256(pool_created_event.as_bytes()));
        let encoded_swap_event = hex::encode_prefixed(keccak256(swap_event.as_bytes()));
        let encoded_mint_event = hex::encode_prefixed(keccak256(mint_event.as_bytes()));
        let encoded_burn_event = hex::encode_prefixed(keccak256(burn_event.as_bytes()));
        let encoded_collect_event = hex::encode_prefixed(keccak256(collect_event.as_bytes()));
        let factory_address = match validate_address(factory) {
            Ok(address) => address,
            Err(e) => panic!(
                "Invalid factory address for DEX {name} on chain {chain} for factory address {factory}: {e}"
            ),
        };
        Self {
            chain,
            name,
            factory: factory_address,
            factory_creation_block,
            pool_created_event: encoded_pool_created_event.into(),
            initialize_event: None,
            swap_created_event: encoded_swap_event.into(),
            mint_created_event: encoded_mint_event.into(),
            burn_created_event: encoded_burn_event.into(),
            collect_created_event: encoded_collect_event.into(),
            flash_created_event: None,
            fee_protocol_update_event: None,
            fee_protocol_collect_event: None,
            amm_type,

View on GitHub (pinned to 18893faf8b)