FuelLabs/sway · error · anyhow::Error

Fetching gas costs from mainnet is currently not implemented

Error message

Fetching gas costs from mainnet is currently not implemented.

What it means

forc-test's GasCostsSource::provide_gas_costs() only implements the BuiltIn variant, which deserializes the bundled gas_costs_values.json. The Mainnet variant is an explicit stub (tracked in FuelLabs/sway#7472) that always returns this anyhow error. It exists so the CLI flag --gas-costs mainnet parses (FromStr maps "mainnet" to Self::Mainnet) but fails loudly instead of silently using wrong costs.

Source

Thrown at forc-test/src/lib.rs:205

    #[default]
    BuiltIn,
    Mainnet,
    Testnet,
    File(String),
}

impl GasCostsSource {
    pub fn provide_gas_costs(&self) -> Result<GasCostsValues, anyhow::Error> {
        match self {
            // Values in the `gas_costs_values.json` are taken from the `chain-configuration` repository:
            //      chain-configuration/upgradelog/ignition/consensus_parameters/<version>.json
            // Update these values when there are changes to the on-chain gas costs.
            Self::BuiltIn => Ok(serde_json::from_str(include_str!(
                "../gas_costs_values.json"
            ))?),
            // TODO: (GAS-COSTS) Fetch actual gas costs from mainnet/testnet and JSON file.
            //       See: https://github.com/FuelLabs/sway/issues/7472
            Self::Mainnet => Err(anyhow::anyhow!(
                "Fetching gas costs from mainnet is currently not implemented."
            )),
            Self::Testnet => Err(anyhow::anyhow!(
                "Fetching gas costs from testnet is currently not implemented."
            )),
            Self::File(_file_path) => Err(anyhow::anyhow!(
                "Loading gas costs from a JSON file is currently not implemented."
            )),
        }
    }
}

impl FromStr for GasCostsSource {
    type Err = anyhow::Error;

    fn from_str(value: &str) -> Result<Self, Self::Err> {
        match value {
            "built-in" => Ok(Self::BuiltIn),

View on GitHub (pinned to 47e5e902fa)

Solutions

  1. Use the default built-in costs: drop the flag or pass `forc test --gas-costs built-in` (values track chain-configuration repo's ignition consensus parameters)
  2. Upgrade forc to a release where mainnet fetching landed (check sway issue #7472 status)
  3. As a library user, match on GasCostsSource::BuiltIn before calling provide_gas_costs and reject Mainnet early with your own message

Example fix

# before
forc test --gas-costs mainnet

# after
forc test --gas-costs built-in
Defensive patterns

Strategy: validation

Validate before calling

// Rust caller of forc_test::GasCostsSource
use forc_test::GasCostsSource;
use std::str::FromStr;

fn gas_costs(src: &str) -> anyhow::Result<()> {
    let source = GasCostsSource::from_str(src)?;
    if source != GasCostsSource::BuiltIn {
        anyhow::bail!("gas cost source '{src}' is not implemented in this forc version; use 'built-in'");
    }
    let values = source.provide_gas_costs()?;
    Ok(())
}

Type guard

fn is_supported_gas_costs_source(s: &str) -> bool {
    s == "built-in"
}

Try / catch

match GasCostsSource::from_str(src)?.provide_gas_costs() {
    Ok(v) => { /* ... */ }
    Err(e) if e.to_string().contains("not implemented") => {
        eprintln!("gas-cost source unsupported, falling back to built-in");
        GasCostsSource::BuiltIn.provide_gas_costs()?;
    }
    Err(e) => return Err(e),
}

Prevention

When it happens

Trigger: Running `forc test --gas-costs mainnet`, or programmatically GasCostsSource::from_str("mainnet")?.provide_gas_costs(). The FromStr parse succeeds; the error is deferred until provide_gas_costs() is called during test setup for coverage/gas profiling.

Common situations: A developer wants gas cost coverage results that match mainnet consensus parameters instead of the bundled snapshot, or copies a command from docs/CI that uses a value supported by a different forc version. Older/newer forc releases differ in which gas-cost sources are wired up.

Related errors


AI-assisted analysis of FuelLabs/sway@47e5e902fa (2026-08-16). Data as JSON: /api/errors/711d813ead5cd666. Report an issue: GitHub.