Icon HelpCircleForumIcon Link

⌘K

Icon HelpCircleForumIcon Link
Querying the Chain

Icon LinkQuerying the Chain

Once you have set up a provider, you're ready to interact with the Fuel blockchain.

We can connect to either a local or an external node:

Icon InfoCircle
  1. Running a local node
  2. Connecting to an external node

Let's look at a few examples below.

Icon LinkGet the Base Asset ID

The base asset is the underlying asset used to perform any transaction on a chain. This should be fetched from a provider to then be used in transactions.

// #import { Provider, FUEL_NETWORK_URL, ScriptTransactionRequest };
 
// Fetch the base asset ID using the provider
const provider = await Provider.create(FUEL_NETWORK_URL);
const baseAssetId = provider.getBaseAssetId();
// 0x...
 
// Create a transaction request
const transactionRequest = new ScriptTransactionRequest();
// Use the base asset for an operation
transactionRequest.addCoinOutput(recipientAddress, 100, baseAssetId);

Icon LinkGet all coins from an address

This method returns all coins (of an optional given asset ID) from a wallet, including spent ones.

// #import { Provider, FUEL_NETWORK_URL, generateTestWallet };
const provider = await Provider.create(FUEL_NETWORK_URL);
const assetIdA = '0x0101010101010101010101010101010101010101010101010101010101010101';
const baseAssetId = provider.getBaseAssetId();
 
const wallet = await generateTestWallet(provider, [
  [42, baseAssetId],
  [100, assetIdA],
]);
 
// get single coin
const coin = await wallet.getCoins(baseAssetId);
// [{ amount: bn(42), assetId: baseAssetId }]
 
// get all coins
const coins = await wallet.getCoins();
// [
//   { amount: bn(42), assetId: baseAssetId }
//   { amount: bn(100), assetId: assetIdA }
// ]

Icon LinkGet spendable resources from an address

The last argument says how much you want to spend. This method returns only spendable, i.e., unspent coins (of a given asset ID). If you ask for more spendable than the amount of unspent coins you have, it returns an error.

// #import { Provider, FUEL_NETWORK_URL, generateTestWallet, ScriptTransactionRequest };
 
const provider = await Provider.create(FUEL_NETWORK_URL);
const assetIdA = '0x0101010101010101010101010101010101010101010101010101010101010101';
const baseAssetId = provider.getBaseAssetId();
 
const wallet = await generateTestWallet(provider, [
  [42, baseAssetId],
  [100, assetIdA],
]);
 
const spendableResources = await wallet.getResourcesToSpend([
  { amount: 32, assetId: baseAssetId, max: 42 },
  { amount: 50, assetId: assetIdA },
]);
 
const tx = new ScriptTransactionRequest();
tx.addResources(spendableResources);

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.

// #import { Provider, FUEL_NETWORK_URL, generateTestWallet };
 
const provider = await Provider.create(FUEL_NETWORK_URL);
const assetIdA = '0x0101010101010101010101010101010101010101010101010101010101010101';
const baseAssetId = provider.getBaseAssetId();
 
const wallet = await generateTestWallet(provider, [
  [42, baseAssetId],
  [100, assetIdA],
]);
 
const walletBalances = await wallet.getBalances();
// [
//   { amount: bn(42), assetId: baseAssetId }
//   { amount: bn(100), assetId: assetIdA }
// ]

Icon LinkGet blocks

This method returns all the blocks from the blockchain that match the given query. The below code snippet shows how to get the last 10 blocks.

// #import { Provider, FUEL_NETWORK_URL };
const provider = await Provider.create(FUEL_NETWORK_URL);
// Force-producing some blocks to make sure that 10 blocks exist
await provider.produceBlocks(10);
const blocks = await provider.getBlocks({
  last: 10,
});

Icon LinkGet a message by its nonce

You can use the getMessageByNonce method to retrieve a message by its nonce.

// #import { FUEL_NETWORK_URL, Provider };
 
const provider = await Provider.create(FUEL_NETWORK_URL);
 
const nonce = '0x381de90750098776c71544527fd253412908dec3d07ce9a7367bd1ba975908a0';
const message = await provider.getMessageByNonce(nonce);
 
expect(message).toBeDefined();
expect(message?.nonce).toEqual(nonce);