Fuel and Spark 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

Step 2: Install the Spark 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 Spark and environment packages by running the following commands:

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:

# 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.

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:

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

Last updated