⌘K

Icon SunFilledIcon MoonStars

Icon LinkQuerying the blockchain

Once you set up a provider, you can interact with the Fuel blockchain. Here are a few examples of what you can do with a provider; for a more in-depth overview of the API, check the official provider API documentation Icon Link.

Icon LinkSet up

You might need to set up a test blockchain first. You can skip this step if you're connecting to an external blockchain.

use fuels::prelude::*;
 
// Set up our test blockchain.
 
// Create a random wallet (more on wallets later).
let wallet = WalletUnlocked::new_random(None);
 
// How many coins in our wallet.
let number_of_coins = 1;
 
// The amount/value in each coin in our wallet.
let amount_per_coin = 3;
 
let coins = setup_single_asset_coins(
    wallet.address(),
    BASE_ASSET_ID,
    number_of_coins,
    amount_per_coin,
);
 
let (provider, _) = setup_test_provider(coins.clone(), vec![], None, None).await;

Icon LinkGet all coins from an address

This method returns all unspent coins (of a given asset ID) from a wallet.

let coins = provider.get_coins(wallet.address(), BASE_ASSET_ID).await?;
assert_eq!(coins.len(), 1);

Icon LinkGet spendable resources owned by an address

The following example shows how to fetch resources owned by an address. First, you create a ResourceFilter which specifies the target address, asset id, and amount. You can also define utxo ids and message ids that should be excluded when retrieving the resources:

pub struct ResourceFilter {
    pub from: Bech32Address,
    pub asset_id: AssetId,
    pub amount: u64,
    pub excluded_utxos: Vec<UtxoId>,
    pub excluded_message_nonces: Vec<Nonce>,
}

The example uses default values for the asset id and the exclusion lists. This resolves to the base asset id and empty vectors for the id lists respectively:

let filter = ResourceFilter {
    from: wallet.address().clone(),
    amount: 1,
    ..Default::default()
};
let spendable_resources = provider.get_spendable_resources(filter).await?;
assert_eq!(spendable_resources.len(), 1);

Icon LinkGet balances from an address

Get all the spendable balances of all assets for an address. This is different from getting the coins because we only return the numbers (the sum of UTXOs coins amount for each asset id) and not the UTXOs coins themselves.

let _balances = provider.get_balances(wallet.address()).await?;