# Fuel and V12 Market Setup Tutorial

### Step 1: Install the Fuel Toolchain

First, install the Fuel toolchain by following the instructions provided in the Fuel documentation:

[Install the Fuel Toolchain](https://docs.fuel.network/guides/installation/)<br>

### Step 2: Install the V12 Rust Packages

Next, create a new directory for your project and initialize a new Rust package using `cargo init`. You’ll also need to install the necessary V12 and environment packages by running the following commands:

```bash
cargo init
cargo add tokio
cargo add env
cargo add dotenv
cargo add fuels
cargo add spark-market-sdk
cargo add spark-registry-sdk
```

### Step 3: Set Up Your \`.env\` File

Create a `.env` file in the root of your project and add the following environment variables:

```toml
# Mnemonic phrase 
MNEMONIC="your mnemonic here"

# Spark Contracts
MARKET_REGISTRY="0x194987ad2314d2de50646078ac1841f00b2dffda863a7d3dd421d220eb83d019"
BTC_USDC_CONTRACT_ID="0x7b88385ae73dd3ccc62012e7a52cddd05c7e82ad54a5df721dfa0c1f8b5998f0"
ETH_USDC_CONTRACT_ID="0xc18094a283193c9b4726d2f644ed07ec9806bbe60a0688d45bffb26c379c1428"

# Asset IDs
BTC_ID="0x38e4ca985b22625fff93205e997bfc5cc8453a953da638ad297ca60a9f2600bc"
ETH_ID="0xf8f8b6283d7fa5b672b530cbb84fcccb4ff8dc40f8176ef4544ddb1f1952ad07"
USDC_ID="0x336b7c06352a4b736ff6f688ba6885788b3df16e136e95310ade51aa32dc6f05"
```

**Note**: Be sure to check for the latest contract deployment addresses [here](https://github.com/compolabs/orderbook-contract/releases).

### Step 4: Call the Spark Market Contract

Now, let’s call the Spark Market contract to retrieve the matcher and protocol fees. Here's an example code to achieve that:

```rust
use dotenv::dotenv;
use std::env;

use fuels::{accounts::provider::Provider, accounts::wallet::WalletUnlocked, types::ContractId};
use std::str::FromStr;
use std::error::Error;

use spark_market_sdk::SparkMarketContract;

pub fn format_value_with_decimals(value: u64, decimals: u32) -> u64 {
    value * 10u64.pow(decimals)
}

pub fn format_to_readable_value(value: u64, decimals: u32) -> f64 {
    value as f64 / 10u64.pow(decimals) as f64
}

#[tokio::main]
async fn main() -> Result<(), Box<dyn Error>> {
    dotenv().ok();

    // Environment variables
    let mnemonic = env::var("MNEMONIC")?;
    let contract_id = env::var("BTC_USDC_CONTRACT_ID")?;

    // Connect to provider
    let provider = Provider::connect("testnet.fuel.network").await?;

    let main_wallet =
        WalletUnlocked::new_from_mnemonic_phrase(&mnemonic, Some(provider.clone())).unwrap();
    let contract_id = ContractId::from_str(&contract_id).unwrap();
    let market = SparkMarketContract::new(contract_id.clone(), main_wallet.clone()).await;

    // Getting Fees from Spark Market
    let matcher_fee = market.matcher_fee().await?.value;
    println!("matcher_fee: {:?}", matcher_fee);

    let protocol_fee = market.protocol_fee().await?.value;
    println!("protocol fee: {:?}", protocol_fee);

    Ok(())
}
```

### Additional Resources

* **Example Use Cases**: Find more examples, including opening orders, batch operations, and canceling orders, by exploring the [Spark Rust SDK Examples](https://github.com/compolabs/spark-rust-sdk-examples).
* **V12 Rust SDK Packages**:
  * [spark-market-sdk](https://crates.io/crates/spark-market-sdk)
  * [spark-registry-sdk](https://crates.io/crates/spark-registry-sdk)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.v12.trade/build/getting-started-as-a-developer/rust-integration-guide/fuel-and-v12-market-setup-tutorial.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
