Skip to content

Querying 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:

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

Let's look at a few examples below.

Get all coins from an address

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

ts
import { generateTestWallet } from '@fuel-ts/account/test-utils';
import { BaseAssetId, FUEL_NETWORK_URL, Provider } from 'fuels';
const provider = await Provider.create(FUEL_NETWORK_URL);
const assetIdA = '0x0101010101010101010101010101010101010101010101010101010101010101';

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 }
// ]
See code in context

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

ts
import { generateTestWallet } from '@fuel-ts/account/test-utils';
import { BaseAssetId, FUEL_NETWORK_URL, Provider, ScriptTransactionRequest } from 'fuels';

const provider = await Provider.create(FUEL_NETWORK_URL);
const assetIdA = '0x0101010101010101010101010101010101010101010101010101010101010101';

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);
See code in context

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

ts
import { generateTestWallet } from '@fuel-ts/account/test-utils';
import { BaseAssetId, FUEL_NETWORK_URL, Provider } from 'fuels';

const provider = await Provider.create(FUEL_NETWORK_URL);
const assetIdA = '0x0101010101010101010101010101010101010101010101010101010101010101';

const wallet = await generateTestWallet(provider, [
  [42, BaseAssetId],
  [100, assetIdA],
]);

const walletBalances = await wallet.getBalances();
// [
//   { amount: bn(42), assetId: BaseAssetId }
//   { amount: bn(100), assetId: assetIdA }
// ]
See code in context

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

ts
import { FUEL_NETWORK_URL, Provider } from 'fuels';
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,
});
See code in context