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 Icon Link
  2. Connecting to an external node

Let's look at a few examples below.

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 wallet = await generateTestWallet(provider, [
	[42, BaseAssetId],
	[100, assetIdA],
]);
 
// get single coin
const coin = await wallet.getCoins(BaseAssetId);
 
// get all coins
const coins = await wallet.getCoins();
 
expect(coin.length).toEqual(1);
expect(coin).toEqual([
	expect.objectContaining({
	assetId: BaseAssetId,
	amount: bn(42),
	}),
]);
expect(coins).toEqual([
	expect.objectContaining({
	assetId: BaseAssetId,
	amount: bn(42),
	}),
	expect.objectContaining({
	assetId: assetIdA,
	amount: bn(100),
	}),
]);

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.

const spendableResources = await wallet.getResourcesToSpend([
	{ amount: 32, assetId: BaseAssetId, max: 42 },
	{ amount: 50, assetId: assetIdA },
]);
expect(spendableResources[0].amount).toEqual(bn(42));
expect(spendableResources[1].amount).toEqual(bn(100));

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.

const walletBalances = await wallet.getBalances();
expect(walletBalances).toEqual([
	{ assetId: BaseAssetId, amount: bn(42) },
	{ assetId: assetIdA, amount: bn(100) },
]);

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.

const blocks = await provider.getBlocks({
	last: 10,
});