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 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,
});

Icon LinkGet messages

You can use the getMessages method to retrieve a list of messages from the blockchain.

const WALLET_A = Wallet.fromPrivateKey(
	'0x1ff16505df75735a5bcf4cb4cf839903120c181dd9be6781b82cda23543bd242',
	provider
);
const WALLET_B = Wallet.fromPrivateKey(
	'0x30bb0bc68f5d2ec3b523cee5a65503031b40679d9c72280cd8088c2cfbc34e38',
	provider
);
 
const EXPECTED_MESSAGES_A: Message[] = [
	{
	messageId: '0x9ca8b2c626327692c7a865d0bbfe6232503e8dc0f7c442abe0b864ffdcca2da9',
	sender: WALLET_B.address,
	recipient: WALLET_A.address,
	nonce: '0x0101010101010101010101010101010101010101010101010101010101010101',
	amount: bn('ffff', 'hex'),
	data: arrayify('0x'),
	daHeight: bn(0),
	},
];
const EXPECTED_MESSAGES_B: Message[] = [
	{
	messageId: '0x39578ef8c047ae994d0dadce8015559953b32fffa657c25c4c068fe4d6995a4b',
	sender: WALLET_A.address,
	recipient: WALLET_B.address,
	nonce: '0x0e1ef2963832068b0e1ef2963832068b0e1ef2963832068b0e1ef2963832068b',
	amount: bn('12704439083013451934'),
	data: arrayify('0x'),
	daHeight: bn(0),
	},
];
 
const aMessages = await WALLET_A.getMessages();
const bMessages = await WALLET_B.getMessages();
 
expect(aMessages).toStrictEqual(EXPECTED_MESSAGES_A);
expect(bMessages).toStrictEqual(EXPECTED_MESSAGES_B);

Icon LinkGet resources

You can use the getResourcesToSpend method to retrieve a list of all the resources (coins + assets) that can be spent by a given address.

const account = new Account(
	'0x09c0b2d1a486c439a87bcba6b46a7a1a23f3897cc83a94521a96da5c23bc58db',
	provider
);
const resourcesToSpend = await account.getResourcesToSpend([
	{
	amount: bn(2),
	assetId: ASSET_A,
	},
]);
expect(resourcesToSpend[0].amount.gt(2)).toBeTruthy();

Icon LinkGet message proof

A message proof is a cryptographic proof that a message was included in a block. You can use the getMessageProof method to retrieve a message proof for a given transaction ID and message ID.

const result = await tx.waitForResult();
 
// Wait for the next block to be minter on out case we are using a local provider
// so we can create a new tx to generate next block
const resp = await sender.transfer(recipient, AMOUNT, BaseAssetId, {
	gasPrice,
	gasLimit: 10_000,
});
const nextBlock = await resp.waitForResult();
 
const messageOutReceipt = <providersMod.TransactionResultMessageOutReceipt>result.receipts[0];
const messageProof = await provider.getMessageProof(
	result.gqlTransaction.id,
	messageOutReceipt.nonce,
	nextBlock.blockId
);