From c0098121657ef3a8901b154c24cd63dd4eb9860a Mon Sep 17 00:00:00 2001 From: Peter Smith Date: Wed, 30 Oct 2024 10:08:50 +0000 Subject: [PATCH] docs: migrated `provider` docs snippets (#3357) * docs: moved provider options docs snippets * docs: moved provider instantiation docs snippets * docs: moved provider getBaseAssetId docs snippets * docs: removed un-used provider.test.ts docs snippets * docs: moved provider getCoins docs snippets * docs: removed /guide from snippet imports * docs: moved provider getResourcesToSpend docs snippets * docs: moved provider getBalances docs snippets * docs: moved provider getBlocks docs snippets * docs: moved provider getMessages docs snippets * docs: moved provider getMessageByNonce docs snippets * docs: moved provider getMessageProof docs snippets * docs: moved provider getTransactions docs snippets * feat: export `PageInfo` from `Provider` class * docs: moved provider pagination docs snippets * chore: changeset * docs: finalize the migration of provider snippets * chore: fixing tests * docs: fixed getMessageProof tests * chore: lint --- .changeset/hip-fans-scream.md | 5 + .../src/guide/provider/pagination.test.ts | 109 ------ .../src/guide/provider/provider.test.ts | 181 ---------- .../guide/provider/querying-the-chain.test.ts | 316 ------------------ .../provider/functionality/get-balances.ts | 20 ++ .../functionality/get-base-asset-id.ts | 20 ++ .../src/provider/functionality/get-blocks.ts | 21 ++ .../functionality/get-coins-from-account.ts | 18 + .../functionality/get-coins-from-provider.ts | 34 ++ .../get-message-proof-block-height.ts | 45 +++ .../get-message-proof-block-id.ts | 44 +++ .../functionality/get-messages-by-nonce.ts | 13 + .../provider/functionality/get-messages.ts | 14 + .../get-resources-to-spend-from-account.ts | 37 ++ .../get-resources-to-spend-from-provider.ts | 38 +++ .../functionality/get-transactions.ts | 11 + .../docs-snippets2/src/provider/pagination.ts | 91 +++++ .../src/provider/provider-instantiation.ts | 22 ++ .../src/provider/provider-operations.ts | 15 + .../src/provider/provider-options.ts | 71 ++++ .../src/guide/cookbook/graphql-integration.md | 2 +- apps/docs/src/guide/provider/index.md | 2 +- apps/docs/src/guide/provider/pagination.md | 14 +- .../src/guide/provider/provider-options.md | 10 +- .../src/guide/provider/querying-the-chain.md | 26 +- packages/account/src/providers/provider.ts | 10 +- .../get-transaction-summary.ts | 4 +- 27 files changed, 554 insertions(+), 639 deletions(-) create mode 100644 .changeset/hip-fans-scream.md delete mode 100644 apps/docs-snippets/src/guide/provider/pagination.test.ts delete mode 100644 apps/docs-snippets/src/guide/provider/provider.test.ts delete mode 100644 apps/docs-snippets/src/guide/provider/querying-the-chain.test.ts create mode 100644 apps/docs-snippets2/src/provider/functionality/get-balances.ts create mode 100644 apps/docs-snippets2/src/provider/functionality/get-base-asset-id.ts create mode 100644 apps/docs-snippets2/src/provider/functionality/get-blocks.ts create mode 100644 apps/docs-snippets2/src/provider/functionality/get-coins-from-account.ts create mode 100644 apps/docs-snippets2/src/provider/functionality/get-coins-from-provider.ts create mode 100644 apps/docs-snippets2/src/provider/functionality/get-message-proof-block-height.ts create mode 100644 apps/docs-snippets2/src/provider/functionality/get-message-proof-block-id.ts create mode 100644 apps/docs-snippets2/src/provider/functionality/get-messages-by-nonce.ts create mode 100644 apps/docs-snippets2/src/provider/functionality/get-messages.ts create mode 100644 apps/docs-snippets2/src/provider/functionality/get-resources-to-spend-from-account.ts create mode 100644 apps/docs-snippets2/src/provider/functionality/get-resources-to-spend-from-provider.ts create mode 100644 apps/docs-snippets2/src/provider/functionality/get-transactions.ts create mode 100644 apps/docs-snippets2/src/provider/pagination.ts create mode 100644 apps/docs-snippets2/src/provider/provider-instantiation.ts create mode 100644 apps/docs-snippets2/src/provider/provider-operations.ts create mode 100644 apps/docs-snippets2/src/provider/provider-options.ts diff --git a/.changeset/hip-fans-scream.md b/.changeset/hip-fans-scream.md new file mode 100644 index 00000000000..a46e9ea1d5a --- /dev/null +++ b/.changeset/hip-fans-scream.md @@ -0,0 +1,5 @@ +--- +"@fuel-ts/account": patch +--- + +docs: migrated `provider` docs snippets diff --git a/apps/docs-snippets/src/guide/provider/pagination.test.ts b/apps/docs-snippets/src/guide/provider/pagination.test.ts deleted file mode 100644 index d247b4571bc..00000000000 --- a/apps/docs-snippets/src/guide/provider/pagination.test.ts +++ /dev/null @@ -1,109 +0,0 @@ -import type { GqlPageInfo } from '@fuel-ts/account/dist/providers/__generated__/operations'; -import type { CursorPaginationArgs } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -/** - * @group node - * @group browser - */ -describe('querying the chain', () => { - it('pagination snippet test 1', () => { - // #region pagination-1 - const paginationArgs: CursorPaginationArgs = { - after: 'cursor', - first: 10, - before: 'cursor', - last: 10, - }; - // #endregion pagination-1 - - // #region pagination-2 - const pageInfo: GqlPageInfo = { - endCursor: 'cursor', - hasNextPage: true, - startCursor: 'cursor', - hasPreviousPage: true, - }; - // #endregion pagination-2 - - expect(paginationArgs).toBeDefined(); - expect(pageInfo).toBeDefined(); - }); - - it('pagination snippet test 2', async () => { - using launched = await launchTestNode(); - const { - provider, - wallets: [myWallet], - } = launched; - - // #region pagination-3 - - let paginationArgs: CursorPaginationArgs = { - first: 10, // It will return only the first 10 coins - }; - - const { coins, pageInfo } = await provider.getCoins( - myWallet.address, - provider.getBaseAssetId(), - paginationArgs - ); - - if (pageInfo.hasNextPage) { - paginationArgs = { - after: pageInfo.endCursor, - first: 10, - }; - // The coins array will include the next 10 coins after the last one in the previous array - await provider.getCoins(myWallet.address, provider.getBaseAssetId(), paginationArgs); - } - // #endregion pagination-3 - - // #region pagination-4 - if (pageInfo.hasPreviousPage) { - paginationArgs = { - before: pageInfo.startCursor, - last: 10, - }; - - // It will includes the previous 10 coins before the first one in the previous array - await provider.getCoins(myWallet.address, provider.getBaseAssetId(), paginationArgs); - } - // #endregion pagination-4 - - expect(paginationArgs).toBeDefined(); - expect(coins).toBeDefined(); - expect(pageInfo).toBeDefined(); - }); - - it('pagination snippet test 3', () => { - // #region pagination-5 - const paginationArgs = { after: 'cursor', first: 10 }; - // #endregion pagination-5 - - expect(paginationArgs).toBeDefined(); - }); - it('pagination snippet test 4', () => { - // #region pagination-6 - const paginationArgs = { before: 'cursor', last: 10 }; - // #endregion pagination-6 - - expect(paginationArgs).toBeDefined(); - }); - - it('pagination snippet test 5', async () => { - using launched = await launchTestNode(); - const { - provider, - wallets: [myWallet], - } = launched; - // #region pagination-7 - - // It will return the first 100 coins of the base asset - const { coins, pageInfo } = await provider.getCoins(myWallet.address); - // #endregion pagination-7 - - expect(coins).toBeDefined(); - expect(pageInfo).toBeDefined(); - }); -}); diff --git a/apps/docs-snippets/src/guide/provider/provider.test.ts b/apps/docs-snippets/src/guide/provider/provider.test.ts deleted file mode 100644 index e4da394b958..00000000000 --- a/apps/docs-snippets/src/guide/provider/provider.test.ts +++ /dev/null @@ -1,181 +0,0 @@ -import { Provider, ScriptTransactionRequest, sleep, WalletUnlocked, Address } from 'fuels'; -import { launchTestNode } from 'fuels/test-utils'; - -async function fetchSomeExternalCredentials() { - return Promise.resolve('credential'); -} - -function decorateResponseWithCustomLogic(response: Response) { - return response; -} - -/** - * @group node - * @group browser - */ -describe('Provider', () => { - it('base examples', async () => { - using launched = await launchTestNode(); - - const mockedProvider = await Provider.create(launched.provider.url); - vi.spyOn(Provider, 'create').mockResolvedValueOnce(mockedProvider); - - // #region provider-definition - // #import { Provider, WalletUnlocked }; - - const FUEL_NETWORK_URL = 'http://127.0.0.1:4000/v1/graphql'; - - // Create the provider - const provider = await Provider.create(FUEL_NETWORK_URL); - - // Querying the blockchain - const { consensusParameters } = provider.getChain(); - - // Create a new wallet - const wallet = WalletUnlocked.generate({ provider }); - - // Get the balances of the wallet (this will be empty until we have assets) - const { balances } = await wallet.getBalances(); - // [] - // #endregion provider-definition - - expect(provider).toBeDefined(); - expect(provider).toBeInstanceOf(Provider); - expect(consensusParameters).toBeDefined(); - expect(consensusParameters).toBeInstanceOf(Object); - expect(balances).toEqual([]); - }); - - test('options: requestMiddleware', async () => { - using launched = await launchTestNode(); - - const FUEL_NETWORK_URL = launched.provider.url; - - // #region options-requestMiddleware - // synchronous request middleware - await Provider.create(FUEL_NETWORK_URL, { - requestMiddleware: (request: RequestInit) => { - request.credentials = 'omit'; - - return request; - }, - }); - - // asynchronous request middleware - await Provider.create(FUEL_NETWORK_URL, { - requestMiddleware: async (request: RequestInit) => { - const credentials = await fetchSomeExternalCredentials(); - request.headers ??= {}; - (request.headers as Record).auth = credentials; - - return request; - }, - }); - // #endregion options-requestMiddleware - }); - - it('options: timeout', async () => { - using launched = await launchTestNode(); - - const FUEL_NETWORK_URL = launched.provider.url; - - // #region options-timeout - await Provider.create(FUEL_NETWORK_URL, { - timeout: 30000, // will abort if request takes 30 seconds to complete - }); - // #endregion options-timeout - }); - - it('options: retryOptions', async () => { - using launched = await launchTestNode(); - - const FUEL_NETWORK_URL = launched.provider.url; - - // #region options-retryOptions - await Provider.create(FUEL_NETWORK_URL, { - retryOptions: { - maxRetries: 5, - baseDelay: 100, - backoff: 'linear', - }, - }); - // #endregion options-retryOptions - }); - - it('options: fetch', async () => { - using launched = await launchTestNode(); - - const FUEL_NETWORK_URL = launched.provider.url; - - // #region options-fetch - await Provider.create(FUEL_NETWORK_URL, { - fetch: async (url: string, requestInit: RequestInit | undefined) => { - // do something - await sleep(100); - - // native fetch - const response = await fetch(url, requestInit); - - const updatedResponse = decorateResponseWithCustomLogic(response); - - return updatedResponse; - }, - }); - // #endregion options-fetch - }); - - it('options: resourceCacheTTL', async () => { - using launched = await launchTestNode(); - - const FUEL_NETWORK_URL = launched.provider.url; - // #region options-cache-utxo - const provider = await Provider.create(FUEL_NETWORK_URL, { - resourceCacheTTL: 5000, // cache resources (Coin's and Message's) for 5 seconds - }); - // #endregion options-cache-utxo - - expect(provider).toBeDefined(); - }); - - it('fetches the base asset ID', async () => { - const recipientAddress = Address.fromRandom(); - using launched = await launchTestNode(); - - const mockedProvider = await Provider.create(launched.provider.url); - vi.spyOn(Provider, 'create').mockResolvedValueOnce(mockedProvider); - - // #region provider-getBaseAssetId - // #import { Provider, ScriptTransactionRequest }; - - const FUEL_NETWORK_URL = 'http://127.0.0.1:4000/v1/graphql'; - - // 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); - // #endregion provider-getBaseAssetId - - expect(baseAssetId).toBeDefined(); - }); - - it('using operations', async () => { - using launched = await launchTestNode(); - - const FUEL_NETWORK_URL = launched.provider.url; - - // #region operations - const provider = await Provider.create(FUEL_NETWORK_URL); - - const chain = await provider.operations.getChain(); - const nodeInfo = await provider.operations.getNodeInfo(); - // #endregion operations - - expect(chain).toBeDefined(); - expect(nodeInfo).toBeDefined(); - }); -}); diff --git a/apps/docs-snippets/src/guide/provider/querying-the-chain.test.ts b/apps/docs-snippets/src/guide/provider/querying-the-chain.test.ts deleted file mode 100644 index 088492c3566..00000000000 --- a/apps/docs-snippets/src/guide/provider/querying-the-chain.test.ts +++ /dev/null @@ -1,316 +0,0 @@ -/* eslint-disable @typescript-eslint/no-unused-vars */ -import type { - TransactionResultMessageOutReceipt, - CoinQuantityLike, - ExcludeResourcesOption, -} from 'fuels'; -import { ScriptTransactionRequest, Provider } from 'fuels'; -import { TestAssetId, TestMessage, launchTestNode } from 'fuels/test-utils'; - -/** - * @group node - * @group browser - */ -describe('querying the chain', () => { - it('query coins', async () => { - using launched = await launchTestNode({ - walletsConfig: { - amountPerCoin: 100, - assets: [TestAssetId.A], - }, - }); - const { - provider: testProvider, - wallets: [wallet], - } = launched; - - const FUEL_NETWORK_URL = testProvider.url; - - // #region get-coins-1 - // #import { Provider }; - - const provider = await Provider.create(FUEL_NETWORK_URL); - - const assetIdA = '0x0101010101010101010101010101010101010101010101010101010101010101'; - const baseAssetId = provider.getBaseAssetId(); - - // fetches up to 100 coins from baseAssetId - const { coins, pageInfo } = await provider.getCoins(wallet.address, baseAssetId); - // [ - // { amount: bn(100), assetId: baseAssetId }, - // ... - // ] - - // fetches up to 100 coins from all assets - await provider.getCoins(wallet.address); - // [ - // { amount: bn(100), assetId: baseAssetId } - // { amount: bn(100), assetId: assetIdA } - // ... - // ] - // #endregion get-coins-1 - - // #region get-coins-2 - await wallet.getCoins(baseAssetId); - // #endregion get-coins-2 - - expect(coins).toBeDefined(); - expect(pageInfo).toBeDefined(); - }); - - it('get spendable resources', async () => { - using launched = await launchTestNode({ - walletsConfig: { - amountPerCoin: 100, - assets: [TestAssetId.A], - }, - }); - const { - provider: testProvider, - wallets: [wallet], - } = launched; - - const FUEL_NETWORK_URL = testProvider.url; - - // #region get-spendable-resources-1 - // #import { Provider, ScriptTransactionRequest, CoinQuantityLike, ExcludeResourcesOption }; - - const provider = await Provider.create(FUEL_NETWORK_URL); - const assetIdA = '0x0101010101010101010101010101010101010101010101010101010101010101'; - - const baseAssetId = provider.getBaseAssetId(); - - const quantities: CoinQuantityLike[] = [ - { amount: 32, assetId: baseAssetId, max: 42 }, - { amount: 50, assetId: assetIdA }, - ]; - - const utxoId = '0x00000000000000000000000000000000000000000000000000000000000000010001'; - const messageNonce = '0x381de90750098776c71544527fd253412908dec3d07ce9a7367bd1ba975908a0'; - const excludedIds: ExcludeResourcesOption = { - utxos: [utxoId], - messages: [messageNonce], - }; - - const spendableResources = await provider.getResourcesToSpend( - wallet.address, - quantities, - excludedIds - ); - - const tx = new ScriptTransactionRequest(); - tx.addResources(spendableResources); - // #endregion get-spendable-resources-1 - - // #region get-spendable-resources-2 - await wallet.getResourcesToSpend(spendableResources, excludedIds); - // #endregion get-spendable-resources-2 - - expect(spendableResources).toBeDefined(); - }); - - it('get balances', async () => { - using launched = await launchTestNode({ - walletsConfig: { - amountPerCoin: 100, - assets: [TestAssetId.A], - }, - }); - const { - provider: testProvider, - wallets: [wallet], - } = launched; - - const FUEL_NETWORK_URL = testProvider.url; - - // #region get-balances-1 - // #import { Provider }; - - const provider = await Provider.create(FUEL_NETWORK_URL); - - const { balances } = await provider.getBalances(wallet.address); - // [ - // { amount: bn(42), assetId: baseAssetId } // total amount of baseAssetId - // { amount: bn(100), assetId: assetIdA } // total amount of assetIdA - // ] - // #endregion get-balances-1 - - // #region get-balances-2 - await wallet.getBalances(); - // #endregion get-balances-2 - - expect(balances).toBeDefined(); - }); - - it('can getBlocks', async () => { - using launched = await launchTestNode(); - - const FUEL_NETWORK_URL = launched.provider.url; - - // #region Provider-get-blocks - // #import { Provider }; - - const provider = await Provider.create(FUEL_NETWORK_URL); - - const blockToProduce = 3; - - // Force-producing some blocks to make sure that 10 blocks exist - await provider.produceBlocks(blockToProduce); - - const { blocks } = await provider.getBlocks({ - last: blockToProduce, - }); - // #endregion Provider-get-blocks - expect(blocks.length).toBe(blockToProduce); - }); - - it('can getMessageByNonce', async () => { - using launched = await launchTestNode(); - const { provider: testProvider } = launched; - - const FUEL_NETWORK_URL = testProvider.url; - - // #region get-message-by-nonce-1 - // #import { Provider }; - - const provider = await Provider.create(FUEL_NETWORK_URL); - - const nonce = '0x381de90750098776c71544527fd253412908dec3d07ce9a7367bd1ba975908a0'; - const message = await provider.getMessageByNonce(nonce); - // #endregion get-message-by-nonce-1 - - expect(message).toBeDefined(); - expect(message?.nonce).toEqual(nonce); - }); - - it('can getMessage', async () => { - // #region Message-getMessages - // #import { TestMessage, launchTestNode }; - - // Creates a test message with an amount of 100 - const testMessage = new TestMessage({ amount: 100 }); - - // Launches a test node with the test message configured - using launched = await launchTestNode({ walletsConfig: { messages: [testMessage] } }); - const { - wallets: [wallet], - } = launched; - - // Retrieves messages from the wallet - const { messages } = await wallet.getMessages(); - // #endregion Message-getMessages - - expect(messages[0].nonce).toEqual(testMessage.nonce); - }); - - it('can getMessageProof with blockId', async () => { - // #region Message-getMessageProof-blockId - // #import { launchTestNode, TransactionResultMessageOutReceipt }; - - // Launches a test node - using launched = await launchTestNode({ - nodeOptions: { - args: ['--poa-instant', 'false', '--poa-interval-period', '1s'], - }, - }); - - const { - wallets: [sender, recipient], - provider, - } = launched; - - // Performs a withdrawal transaction from sender to recipient, thus generating a message - const withdrawTx = await sender.withdrawToBaseLayer(recipient.address.toB256(), 100); - const result = await withdrawTx.waitForResult(); - - // Waiting for a new block to be commited (1 confirmation block) - await new Promise((resolve) => { - setTimeout(() => { - resolve(true); - }, 1000); - }); - - // Retrives the latest block - const latestBlock = await provider.getBlock('latest'); - - // Retrieves the `nonce` via message out receipt from the initial transaction result - const { nonce } = result.receipts[0] as TransactionResultMessageOutReceipt; - - // Retrieves the message proof for the transaction ID and nonce using the next block Id - const messageProof = await provider.getMessageProof(result.id, nonce, latestBlock?.id); - // #endregion Message-getMessageProof-blockId - - expect(messageProof?.amount.toNumber()).toEqual(100); - expect(messageProof?.sender.toHexString()).toEqual(result.id); - }); - - it('can getMessageProof with blockHeight', async () => { - // #region Message-getMessageProof-blockHeight - // #import { launchTestNode, TransactionResultMessageOutReceipt }; - - // Launches a test node - using launched = await launchTestNode({ - nodeOptions: { - args: ['--poa-instant', 'false', '--poa-interval-period', '1s'], - }, - }); - - const { - wallets: [sender, recipient], - provider, - } = launched; - - // Performs a withdrawal transaction from sender to recipient, thus generating a message - const withdrawTx = await sender.withdrawToBaseLayer(recipient.address.toB256(), 100); - const result = await withdrawTx.waitForResult(); - - // Waiting for a new block to be commited (1 confirmation block) - await new Promise((resolve) => { - setTimeout(() => { - resolve(true); - }, 1000); - }); - - // Retrieves the `nonce` via message out receipt from the initial transaction result - const { nonce } = result.receipts[0] as TransactionResultMessageOutReceipt; - - // Retrives the latest block - const latestBlock = await provider.getBlock('latest'); - - // Retrieves the message proof for the transaction ID and nonce using the block height - const messageProof = await provider.getMessageProof( - result.id, - nonce, - undefined, - latestBlock?.height - ); - // #endregion Message-getMessageProof-blockHeight - - expect(messageProof?.amount.toNumber()).toEqual(100); - expect(messageProof?.sender.toHexString()).toEqual(result.id); - }); - - it('get transactions', async () => { - using launched = await launchTestNode(); - const { - provider: testProvider, - wallets: [wallet, receiver], - } = launched; - - const tx = await wallet.transfer(receiver.address, 100); - await tx.waitForResult(); - const FUEL_NETWORK_URL = testProvider.url; - - // #region get-transactions - // #import { Provider }; - - const provider = await Provider.create(FUEL_NETWORK_URL); - - const { transactions } = await provider.getTransactions(); - // #endregion get-transactions - - expect(transactions).toBeDefined(); - expect(transactions.length).toBe(2); - // Includes base asset minting tx - }); -}); diff --git a/apps/docs-snippets2/src/provider/functionality/get-balances.ts b/apps/docs-snippets2/src/provider/functionality/get-balances.ts new file mode 100644 index 00000000000..d56d4440005 --- /dev/null +++ b/apps/docs-snippets2/src/provider/functionality/get-balances.ts @@ -0,0 +1,20 @@ +// #region getBalances-1 +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +const { balances } = await provider.getBalances(wallet.address); +// [ +// { amount: bn(42), assetId: baseAssetId } // total amount of baseAssetId +// { amount: bn(100), assetId: assetIdA } // total amount of assetIdA +// ] +// #endregion getBalances-1 + +// #region getBalances-2 +await wallet.getBalances(); +// #endregion getBalances-2 + +console.log('balances', balances); diff --git a/apps/docs-snippets2/src/provider/functionality/get-base-asset-id.ts b/apps/docs-snippets2/src/provider/functionality/get-base-asset-id.ts new file mode 100644 index 00000000000..22b4bde5ac0 --- /dev/null +++ b/apps/docs-snippets2/src/provider/functionality/get-base-asset-id.ts @@ -0,0 +1,20 @@ +// #region getBaseAssetId +import { Address, Provider, ScriptTransactionRequest } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_ADDRESS } from '../../env'; + +// Fetch the base asset ID using the provider +const provider = await Provider.create(LOCAL_NETWORK_URL); +const baseAssetId = provider.getBaseAssetId(); +// 0x... + +// Instantiate our recipients address +const recipientAddress = Address.fromAddressOrString(WALLET_ADDRESS); + +// Create a transaction request +const transactionRequest = new ScriptTransactionRequest(); +// Use the base asset for an operation +transactionRequest.addCoinOutput(recipientAddress, 100, baseAssetId); +// #endregion getBaseAssetId + +console.log('baseAssetId', baseAssetId); diff --git a/apps/docs-snippets2/src/provider/functionality/get-blocks.ts b/apps/docs-snippets2/src/provider/functionality/get-blocks.ts new file mode 100644 index 00000000000..6fdee941436 --- /dev/null +++ b/apps/docs-snippets2/src/provider/functionality/get-blocks.ts @@ -0,0 +1,21 @@ +// #region getBlocks +import { Provider } from 'fuels'; + +import { LOCAL_NETWORK_URL } from '../../env'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); + +const blockToProduce = 3; + +// Force-producing some blocks to make sure that blocks exist +await provider.produceBlocks(blockToProduce); + +const { blocks } = await provider.getBlocks({ + last: blockToProduce, +}); +// #endregion getBlocks + +console.log( + 'blockToProduce equals blocks.length', + blockToProduce === blocks.length +); diff --git a/apps/docs-snippets2/src/provider/functionality/get-coins-from-account.ts b/apps/docs-snippets2/src/provider/functionality/get-coins-from-account.ts new file mode 100644 index 00000000000..5c7a6d06ec6 --- /dev/null +++ b/apps/docs-snippets2/src/provider/functionality/get-coins-from-account.ts @@ -0,0 +1,18 @@ +// #region getCoins-2 +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +const baseAssetId = provider.getBaseAssetId(); + +const { coins } = await wallet.getCoins(baseAssetId); +// [ +// { amount: bn(100), assetId: baseAssetId }, +// ... +// ] +// #endregion getCoins-2 + +console.log('coinsFromWallet', coins); diff --git a/apps/docs-snippets2/src/provider/functionality/get-coins-from-provider.ts b/apps/docs-snippets2/src/provider/functionality/get-coins-from-provider.ts new file mode 100644 index 00000000000..87010af5eae --- /dev/null +++ b/apps/docs-snippets2/src/provider/functionality/get-coins-from-provider.ts @@ -0,0 +1,34 @@ +/* eslint-disable @typescript-eslint/no-unused-vars */ +// #region getCoins-1 +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +const assetIdA = + '0x0101010101010101010101010101010101010101010101010101010101010101'; +const baseAssetId = provider.getBaseAssetId(); + +// Fetches up to 100 coins that have an asset ID that is equal to the base asset ID +const { coins: coinsOnlyBaseAsset } = await provider.getCoins( + wallet.address, + baseAssetId +); +// [ +// { amount: bn(100), assetId: baseAssetId }, +// ... +// ] + +// Fetches up to 100 coins - irrespective of the asset ID +const { coins: coinsAnyAsset } = await provider.getCoins(wallet.address); +// [ +// { amount: bn(100), assetId: baseAssetId } +// { amount: bn(100), assetId: assetIdA } +// ... +// ] +// #endregion getCoins-1 + +console.log('coinsOnlyBaseAsset', coinsOnlyBaseAsset); +console.log('coinsAnyAsset', coinsAnyAsset); diff --git a/apps/docs-snippets2/src/provider/functionality/get-message-proof-block-height.ts b/apps/docs-snippets2/src/provider/functionality/get-message-proof-block-height.ts new file mode 100644 index 00000000000..40dd892f57b --- /dev/null +++ b/apps/docs-snippets2/src/provider/functionality/get-message-proof-block-height.ts @@ -0,0 +1,45 @@ +// #region getMessageProof-blockHeight +import type { TransactionResultMessageOutReceipt } from 'fuels'; +import { sleep } from 'fuels'; +import { launchTestNode } from 'fuels/test-utils'; + +using launched = await launchTestNode({ + nodeOptions: { + args: ['--poa-instant', 'false', '--poa-interval-period', '1s'], + }, +}); + +const { + provider, + wallets: [sender, recipient], +} = launched; + +// Performs a withdrawal transaction from sender to recipient, thus generating a message +const withdrawTx = await sender.withdrawToBaseLayer( + recipient.address.toB256(), + 100 +); +const result = await withdrawTx.waitForResult(); + +// Waiting for a new block to be committed (1 confirmation block) +// Retrieves the latest block +await sleep(1000); +const latestBlock = await provider.getBlock('latest'); + +// Retrieves the `nonce` via message out receipt from the initial transaction result +const { nonce } = result.receipts[0] as TransactionResultMessageOutReceipt; + +// Retrieves the message proof for the transaction ID and nonce using the block height +const messageProofFromBlockHeight = await provider.getMessageProof( + result.id, + nonce, + undefined, + latestBlock?.height +); +// #endregion getMessageProof-blockHeight + +console.log('messageProofFromBlockHeight', messageProofFromBlockHeight); +console.log( + 'messageProofFromBlockHeight.amount equals 100', + messageProofFromBlockHeight?.amount.toNumber() === 100 +); diff --git a/apps/docs-snippets2/src/provider/functionality/get-message-proof-block-id.ts b/apps/docs-snippets2/src/provider/functionality/get-message-proof-block-id.ts new file mode 100644 index 00000000000..df30d6966fc --- /dev/null +++ b/apps/docs-snippets2/src/provider/functionality/get-message-proof-block-id.ts @@ -0,0 +1,44 @@ +// #region getMessageProof-blockId +import type { TransactionResultMessageOutReceipt } from 'fuels'; +import { sleep } from 'fuels'; +import { launchTestNode } from 'fuels/test-utils'; + +using launched = await launchTestNode({ + nodeOptions: { + args: ['--poa-instant', 'false', '--poa-interval-period', '1s'], + }, +}); + +const { + provider, + wallets: [sender, recipient], +} = launched; + +// Performs a withdrawal transaction from sender to recipient, thus generating a message +const withdrawTx = await sender.withdrawToBaseLayer( + recipient.address.toB256(), + 100 +); +const result = await withdrawTx.waitForResult(); + +// Waiting for a new block to be committed (1 confirmation block) +// Retrieves the latest block +await sleep(1000); +const latestBlock = await provider.getBlock('latest'); + +// Retrieves the `nonce` via message out receipt from the initial transaction result +const { nonce } = result.receipts[0] as TransactionResultMessageOutReceipt; + +// Retrieves the message proof for the transaction ID and nonce using the next block Id +const messageProofFromBlockId = await provider.getMessageProof( + result.id, + nonce, + latestBlock?.id +); +// #endregion getMessageProof-blockId + +console.log('messageProofFromBlockId', messageProofFromBlockId); +console.log( + 'messageProofFromBlockId.amount equals 100', + messageProofFromBlockId?.amount.toNumber() === 100 +); diff --git a/apps/docs-snippets2/src/provider/functionality/get-messages-by-nonce.ts b/apps/docs-snippets2/src/provider/functionality/get-messages-by-nonce.ts new file mode 100644 index 00000000000..9d92a96d6c4 --- /dev/null +++ b/apps/docs-snippets2/src/provider/functionality/get-messages-by-nonce.ts @@ -0,0 +1,13 @@ +// #region getMessageByNonce +import { Provider } from 'fuels'; + +import { LOCAL_NETWORK_URL } from '../../env'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); + +const nonce = + '0x381de90750098776c71544527fd253412908dec3d07ce9a7367bd1ba975908a0'; +const message = await provider.getMessageByNonce(nonce); +// #endregion getMessageByNonce + +console.log('message', message); diff --git a/apps/docs-snippets2/src/provider/functionality/get-messages.ts b/apps/docs-snippets2/src/provider/functionality/get-messages.ts new file mode 100644 index 00000000000..ad9bd6528d4 --- /dev/null +++ b/apps/docs-snippets2/src/provider/functionality/get-messages.ts @@ -0,0 +1,14 @@ +// #region getMessages +import { Provider, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; + +// Instantiate a provider and wallet +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); + +// Retrieves messages from the wallet +const { messages } = await wallet.getMessages(); +// #endregion getMessages + +console.log('messages', messages); diff --git a/apps/docs-snippets2/src/provider/functionality/get-resources-to-spend-from-account.ts b/apps/docs-snippets2/src/provider/functionality/get-resources-to-spend-from-account.ts new file mode 100644 index 00000000000..52b1a6715c7 --- /dev/null +++ b/apps/docs-snippets2/src/provider/functionality/get-resources-to-spend-from-account.ts @@ -0,0 +1,37 @@ +// #region getResourcesToSpend-2 +import type { CoinQuantityLike, ExcludeResourcesOption } from 'fuels'; +import { Provider, ScriptTransactionRequest, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const assetIdA = + '0x0101010101010101010101010101010101010101010101010101010101010101'; + +const baseAssetId = provider.getBaseAssetId(); + +const quantities: CoinQuantityLike[] = [ + { amount: 32, assetId: baseAssetId, max: 42 }, + { amount: 50, assetId: assetIdA }, +]; + +const utxoId = + '0x00000000000000000000000000000000000000000000000000000000000000010001'; +const messageNonce = + '0x381de90750098776c71544527fd253412908dec3d07ce9a7367bd1ba975908a0'; +const excludedIds: ExcludeResourcesOption = { + utxos: [utxoId], + messages: [messageNonce], +}; + +const spendableResources = await wallet.getResourcesToSpend( + quantities, + excludedIds +); + +const tx = new ScriptTransactionRequest(); +tx.addResources(spendableResources); +// #endregion getResourcesToSpend-2 + +console.log('spendableResources', spendableResources); diff --git a/apps/docs-snippets2/src/provider/functionality/get-resources-to-spend-from-provider.ts b/apps/docs-snippets2/src/provider/functionality/get-resources-to-spend-from-provider.ts new file mode 100644 index 00000000000..714daa7102c --- /dev/null +++ b/apps/docs-snippets2/src/provider/functionality/get-resources-to-spend-from-provider.ts @@ -0,0 +1,38 @@ +// #region getResourcesToSpend-1 +import type { CoinQuantityLike, ExcludeResourcesOption } from 'fuels'; +import { Provider, ScriptTransactionRequest, Wallet } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_PVT_KEY } from '../../env'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); +const wallet = Wallet.fromPrivateKey(WALLET_PVT_KEY, provider); +const assetIdA = + '0x0101010101010101010101010101010101010101010101010101010101010101'; + +const baseAssetId = provider.getBaseAssetId(); + +const quantities: CoinQuantityLike[] = [ + { amount: 32, assetId: baseAssetId, max: 42 }, + { amount: 50, assetId: assetIdA }, +]; + +const utxoId = + '0x00000000000000000000000000000000000000000000000000000000000000010001'; +const messageNonce = + '0x381de90750098776c71544527fd253412908dec3d07ce9a7367bd1ba975908a0'; +const excludedIds: ExcludeResourcesOption = { + utxos: [utxoId], + messages: [messageNonce], +}; + +const spendableResources = await provider.getResourcesToSpend( + wallet.address, + quantities, + excludedIds +); + +const tx = new ScriptTransactionRequest(); +tx.addResources(spendableResources); +// #endregion getResourcesToSpend-1 + +console.log('spendableResources', spendableResources); diff --git a/apps/docs-snippets2/src/provider/functionality/get-transactions.ts b/apps/docs-snippets2/src/provider/functionality/get-transactions.ts new file mode 100644 index 00000000000..cd61daf126d --- /dev/null +++ b/apps/docs-snippets2/src/provider/functionality/get-transactions.ts @@ -0,0 +1,11 @@ +// #region getTransactions +import { Provider } from 'fuels'; + +import { LOCAL_NETWORK_URL } from '../../env'; + +const provider = await Provider.create(LOCAL_NETWORK_URL); + +const { transactions } = await provider.getTransactions(); +// #endregion getTransactions + +console.log('transactions', transactions); diff --git a/apps/docs-snippets2/src/provider/pagination.ts b/apps/docs-snippets2/src/provider/pagination.ts new file mode 100644 index 00000000000..02a0353e423 --- /dev/null +++ b/apps/docs-snippets2/src/provider/pagination.ts @@ -0,0 +1,91 @@ +import { Provider } from 'fuels'; +import type { CursorPaginationArgs, PageInfo } from 'fuels'; + +import { LOCAL_NETWORK_URL, WALLET_ADDRESS } from '../env'; + +// #region pagination-args +const paginationArgsExample: CursorPaginationArgs = { + after: 'cursor', + first: 10, + before: 'cursor', + last: 10, +}; +// #endregion pagination-args +console.log('paginationArgsExample', paginationArgsExample); + +// #region pagination-page-info +const pageInfoExample: PageInfo = { + endCursor: 'cursor', + hasNextPage: true, + startCursor: 'cursor', + hasPreviousPage: true, +}; +// #endregion pagination-page-info +console.log('pageInfoExample', pageInfoExample); + +// #region pagination-next-page +const provider = await Provider.create(LOCAL_NETWORK_URL); + +let paginationArgs: CursorPaginationArgs = { + first: 10, // It will return only the first 10 coins +}; + +const { coins, pageInfo } = await provider.getCoins( + WALLET_ADDRESS, + provider.getBaseAssetId(), + paginationArgs +); + +if (pageInfo.hasNextPage) { + paginationArgs = { + after: pageInfo.endCursor, + first: 10, + }; + // The coins array will include the next 10 coins after the last one in the previous array + await provider.getCoins( + WALLET_ADDRESS, + provider.getBaseAssetId(), + paginationArgs + ); +} +// #endregion pagination-next-page + +// #region pagination-previous-page +if (pageInfo.hasPreviousPage) { + paginationArgs = { + before: pageInfo.startCursor, + last: 10, + }; + + // It will includes the previous 10 coins before the first one in the previous array + await provider.getCoins( + WALLET_ADDRESS, + provider.getBaseAssetId(), + paginationArgs + ); +} +// #endregion pagination-previous-page + +console.log('coins', coins); +console.log('pageInfo', pageInfo); + +// #region pagination-forward-pagination +const paginationArgsForward: CursorPaginationArgs = { + after: 'cursor', + first: 10, +}; +// #endregion pagination-forward-pagination +console.log('paginationArgsForward', paginationArgsForward); + +// #region pagination-backward-pagination +const paginationArgsBackwards: CursorPaginationArgs = { + before: 'cursor', + last: 10, +}; +// #endregion pagination-backward-pagination +console.log('paginationArgsBackwards', paginationArgsBackwards); + +// #region pagination-default-args +// It will return the first 100 coins for a given wallet +await provider.getCoins(WALLET_ADDRESS); +// #endregion pagination-default-args diff --git a/apps/docs-snippets2/src/provider/provider-instantiation.ts b/apps/docs-snippets2/src/provider/provider-instantiation.ts new file mode 100644 index 00000000000..c7a581d24c7 --- /dev/null +++ b/apps/docs-snippets2/src/provider/provider-instantiation.ts @@ -0,0 +1,22 @@ +// #region provider-instantiation +import { Provider, WalletUnlocked } from 'fuels'; + +import { LOCAL_NETWORK_URL } from '../env'; + +// Create the provider +const provider = await Provider.create(LOCAL_NETWORK_URL); + +// Querying the blockchain +const { consensusParameters } = provider.getChain(); + +// Create a new wallet +const wallet = WalletUnlocked.generate({ provider }); + +// Get the balances of the wallet (this will be empty until we have assets) +const { balances } = await wallet.getBalances(); +// [] +// #endregion provider-instantiation + +console.log('provider', provider); +console.log('consensusParameters', consensusParameters); +console.log('balances', balances); diff --git a/apps/docs-snippets2/src/provider/provider-operations.ts b/apps/docs-snippets2/src/provider/provider-operations.ts new file mode 100644 index 00000000000..1f7052ec389 --- /dev/null +++ b/apps/docs-snippets2/src/provider/provider-operations.ts @@ -0,0 +1,15 @@ +// #region operations +import { Provider } from 'fuels'; + +import { LOCAL_NETWORK_URL } from '../env'; + +// Create the provider +const provider = await Provider.create(LOCAL_NETWORK_URL); + +const chain = await provider.operations.getChain(); +const nodeInfo = await provider.operations.getNodeInfo(); +// #endregion operations + +console.log('provider', provider); +console.log('chain', chain); +console.log('nodeInfo', nodeInfo); diff --git a/apps/docs-snippets2/src/provider/provider-options.ts b/apps/docs-snippets2/src/provider/provider-options.ts new file mode 100644 index 00000000000..000a004550f --- /dev/null +++ b/apps/docs-snippets2/src/provider/provider-options.ts @@ -0,0 +1,71 @@ +import { Provider } from 'fuels'; + +import { LOCAL_NETWORK_URL } from '../env'; + +async function fetchSomeExternalCredentials() { + return Promise.resolve('credential'); +} + +function decorateResponseWithCustomLogic(response: Response) { + return response; +} + +const NETWORK_URL = LOCAL_NETWORK_URL; + +// #region requestMiddleware +// synchronous request middleware +await Provider.create(NETWORK_URL, { + requestMiddleware: (request: RequestInit) => { + request.credentials = 'omit'; + + return request; + }, +}); + +// asynchronous request middleware +await Provider.create(NETWORK_URL, { + requestMiddleware: async (request: RequestInit) => { + const credentials = await fetchSomeExternalCredentials(); + request.headers ??= {}; + (request.headers as Record).auth = credentials; + + return request; + }, +}); +// #endregion requestMiddleware + +// #region timeout +await Provider.create(NETWORK_URL, { + timeout: 3000, // will abort if request takes 30 seconds to complete +}); +// #endregion timeout + +// #region retryOptions +await Provider.create(NETWORK_URL, { + retryOptions: { + maxRetries: 5, + baseDelay: 100, + backoff: 'linear', + }, +}); +// #endregion retryOptions + +// #region fetch +await Provider.create(NETWORK_URL, { + fetch: async (url: string, requestInit: RequestInit | undefined) => { + // native fetch + const response = await fetch(url, requestInit); + + const updatedResponse = decorateResponseWithCustomLogic(response); + + return updatedResponse; + }, +}); +// #endregion fetch + +// #region cache-utxo +await Provider.create(NETWORK_URL, { + // Cache resources (Coin's and Message's) for 5 seconds + resourceCacheTTL: 5000, +}); +// #endregion cache-utxo diff --git a/apps/docs/src/guide/cookbook/graphql-integration.md b/apps/docs/src/guide/cookbook/graphql-integration.md index 2239c39987f..c0e668056da 100644 --- a/apps/docs/src/guide/cookbook/graphql-integration.md +++ b/apps/docs/src/guide/cookbook/graphql-integration.md @@ -7,7 +7,7 @@ The Fuel Network provides a [GraphQL API](https://docs.fuel.network/docs/graphql For its own purposes, the SDK creates custom operations based off of the API's schema and auto-generates TypeScript client code via codegen tools. The end result of this code generation are the operations available on the [`Provider`](../provider/index.md), of which some are shown below: -<<< @/../../docs-snippets/src/guide/provider/provider.test.ts#operations{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/provider-operations.ts#operations{ts:line-numbers} Note that these operations primarily serve the needs of the SDK and the `Provider`'s methods which can encapsulate calls to multiple operations, parse the responses, etc. diff --git a/apps/docs/src/guide/provider/index.md b/apps/docs/src/guide/provider/index.md index 2868ef67e49..d8b56ff8a69 100644 --- a/apps/docs/src/guide/provider/index.md +++ b/apps/docs/src/guide/provider/index.md @@ -4,6 +4,6 @@ The [`Provider`](../../api/Account/Provider.md) lets you connect to a Fuel node All higher-level abstractions (e.g. [`Wallet`](../wallets/index.md), [`Contract`](../contracts/index.md)) that interact with the blockchain go through the `Provider`, so it's used for various actions like getting a wallet's balance, deploying contracts, querying their state, etc. -<<< @/../../docs-snippets/src/guide/provider/provider.test.ts#provider-definition{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/provider-instantiation.ts#provider-instantiation{ts:line-numbers} You can find more examples of `Provider` usage [here](./querying-the-chain.md). diff --git a/apps/docs/src/guide/provider/pagination.md b/apps/docs/src/guide/provider/pagination.md index 4e073419e64..e056856d0c9 100644 --- a/apps/docs/src/guide/provider/pagination.md +++ b/apps/docs/src/guide/provider/pagination.md @@ -11,7 +11,7 @@ The pagination arguments object is used to specify the range of data you want to - `before`: A cursor pointing to a position before which you want to retrieve items. - `last`: The number of items to retrieve before the specified cursor. This is used in conjunction with the `before` argument. -<<< @/../../docs-snippets/src/guide/provider/pagination.test.ts#pagination-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/pagination.ts#pagination-args{ts:line-numbers} ## Page Info @@ -22,7 +22,7 @@ The `pageInfo` object is included in the GraphQL response for requests that supp - `startCursor`: A cursor representing the first item in the current set of results. It should be used as the `before` argument in subsequent queries to fetch the previous set of items. - `hasPreviousPage`: A boolean indicating whether there are more items available before the current set. -<<< @/../../docs-snippets/src/guide/provider/pagination.test.ts#pagination-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/pagination.ts#pagination-page-info{ts:line-numbers} ## Using Pagination @@ -36,13 +36,13 @@ One of the methods that supports pagination is the `getCoins` method. This metho Here is how you can use the `getCoins` method with pagination: -<<< @/../../docs-snippets/src/guide/provider/pagination.test.ts#pagination-3{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/pagination.ts#pagination-next-page{ts:line-numbers} ### Navigating to the Previous Page You can also use the `paginationArgs` to navigate to the previous page of results: -<<< @/../../docs-snippets/src/guide/provider/pagination.test.ts#pagination-4{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/pagination.ts#pagination-previous-page{ts:line-numbers} ## Valid Combinations @@ -50,16 +50,16 @@ You can also use the `paginationArgs` to navigate to the previous page of result Use `after` with `first` to retrieve items following a cursor. -<<< @/../../docs-snippets/src/guide/provider/pagination.test.ts#pagination-5{ts} +<<< @/../../docs-snippets2/src/provider/pagination.ts#pagination-forward-pagination{ts} - Backward Pagination: Use `before` with `last` to retrieve items preceding a cursor. -<<< @/../../docs-snippets/src/guide/provider/pagination.test.ts#pagination-6{ts} +<<< @/../../docs-snippets2/src/provider/pagination.ts#pagination-backward-pagination{ts} ## Default Behavior If neither `assetId` nor `paginationArgs` are provided, the `getCoins` method will default to the base asset ID and return the first 100 items: -<<< @/../../docs-snippets/src/guide/provider/pagination.test.ts#pagination-7{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/pagination.ts#pagination-default-args{ts:line-numbers} diff --git a/apps/docs/src/guide/provider/provider-options.md b/apps/docs/src/guide/provider/provider-options.md index 2623095aca2..4d016955247 100644 --- a/apps/docs/src/guide/provider/provider-options.md +++ b/apps/docs/src/guide/provider/provider-options.md @@ -18,19 +18,19 @@ You can provide the following settings: - `fixed`: Uses a constant delay between attempts. - `baseDelay` _(default 150ms)_ - Base time in milliseconds for the backoff strategy. -<<< @/../../docs-snippets/src/guide/provider/provider.test.ts#options-retryOptions{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/provider-options.ts#retryOptions{ts:line-numbers} ### `requestMiddleware` Allows you to modify the request object to add additional headers, modify the request's body, and much more. -<<< @/../../docs-snippets/src/guide/provider/provider.test.ts#options-requestMiddleware{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/provider-options.ts#requestMiddleware{ts:line-numbers} ### `timeout` Specify the timeout in milliseconds after which every request will be aborted. -<<< @/../../docs-snippets/src/guide/provider/provider.test.ts#options-timeout{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/provider-options.ts#timeout{ts:line-numbers} ### `fetch` @@ -38,7 +38,7 @@ Provide a custom `fetch` function that'll replace the default fetch call. _Note: If defined, `requestMiddleware`, `timeout` and `retryOptions` are applied to this custom `fetch` function as well._ -<<< @/../../docs-snippets/src/guide/provider/provider.test.ts#options-fetch{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/provider-options.ts#fetch{ts:line-numbers} ### `resourceCacheTTL` @@ -60,7 +60,7 @@ This error indicates that the resources used by the second transaction no longer To prevent this issue, the SDK sets a default cache for resources to 20 seconds. This default caching mechanism ensures that resources used in a submitted transaction are not reused in subsequent transactions within the specified time. You can control the duration of this cache using the `resourceCacheTTL` flag. If you would like to disable caching, you can pass a value of `-1` to the `resourceCacheTTL` parameter. -<<< @/../../docs-snippets/src/guide/provider/provider.test.ts#options-cache-utxo{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/provider-options.ts#cache-utxo{ts:line-numbers} **Note:** diff --git a/apps/docs/src/guide/provider/querying-the-chain.md b/apps/docs/src/guide/provider/querying-the-chain.md index b7df5b4a31a..93eb3527056 100644 --- a/apps/docs/src/guide/provider/querying-the-chain.md +++ b/apps/docs/src/guide/provider/querying-the-chain.md @@ -13,55 +13,55 @@ Let's look at a few examples below. 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. -<<< @/../../docs-snippets/src/guide/provider/provider.test.ts#provider-getBaseAssetId{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/functionality/get-base-asset-id.ts#getBaseAssetId{ts:line-numbers} ## `getCoins` Returns UTXOs coins from an account address, optionally filtered by asset ID. This method supports [pagination](./pagination.md). -<<< @/../../docs-snippets/src/guide/provider/querying-the-chain.test.ts#get-coins-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/functionality/get-coins-from-provider.ts#getCoins-1{ts:line-numbers} This method is also implemented on the `Account` class and can be used without providing the `address`: -<<< @/../../docs-snippets/src/guide/provider/querying-the-chain.test.ts#get-coins-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/functionality/get-coins-from-account.ts#getCoins-2{ts:line-numbers} ## `getResourcesToSpend` Returns spendable resources (coins or messages) for a transaction request. It accepts an optional third parameter, `excludedIds`, to exclude specific UTXO IDs or coin message nonces: -<<< @/../../docs-snippets/src/guide/provider/querying-the-chain.test.ts#get-spendable-resources-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/functionality/get-resources-to-spend-from-provider.ts#getResourcesToSpend-1{ts:line-numbers} This method is also available in the `Account` class and can be used without providing the `address`: -<<< @/../../docs-snippets/src/guide/provider/querying-the-chain.test.ts#get-spendable-resources-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/functionality/get-resources-to-spend-from-account.ts#getResourcesToSpend-2{ts:line-numbers} ## `getBalances` Returns the sum of all UTXOs coins and unspent message coins amounts for all assets. Unlike `getCoins`, it only returns the total amounts, not the individual coins: -<<< @/../../docs-snippets/src/guide/provider/querying-the-chain.test.ts#get-balances-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/functionality/get-balances.ts#getBalances-1{ts:line-numbers} This method is also available in the `Account` class and can be used without providing the `address` parameter: -<<< @/../../docs-snippets/src/guide/provider/querying-the-chain.test.ts#get-balances-2{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/functionality/get-balances.ts#getBalances-2{ts:line-numbers} ## `getBlocks` The `getBlocks` method returns blocks from the blockchain matching the given `paginationArgs` parameter, supporting [pagination](./pagination.md). The below code snippet shows how to get the last 10 blocks. -<<< @/../../docs-snippets/src/guide/provider/querying-the-chain.test.ts#Provider-get-blocks{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/functionality/get-blocks.ts#getBlocks{ts:line-numbers} ## `getMessageByNonce` You can use the `getMessageByNonce` method to retrieve a message by its nonce. -<<< @/../../docs-snippets/src/guide/provider/querying-the-chain.test.ts#get-message-by-nonce-1{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/functionality/get-messages-by-nonce.ts#getMessageByNonce{ts:line-numbers} ## `getMessages` You can use the `getMessages` method to retrieve a list of messages from the blockchain. -<<< @/../../docs-snippets/src/guide/provider/querying-the-chain.test.ts#Message-getMessages{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/functionality/get-messages.ts#getMessages{ts:line-numbers} ## `getMessageProof` @@ -69,14 +69,14 @@ A message proof is a cryptographic proof that a message was included in a block. You can retrieve a message proof by either using it's block ID: -<<< @/../../docs-snippets/src/guide/provider/querying-the-chain.test.ts#Message-getMessageProof-blockId{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/functionality/get-message-proof-block-id.ts#getMessageProof-blockId{ts:line-numbers} Or by it's block height: -<<< @/../../docs-snippets/src/guide/provider/querying-the-chain.test.ts#Message-getMessageProof-blockHeight{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/functionality/get-message-proof-block-height.ts#getMessageProof-blockHeight{ts:line-numbers} ## `getTransactions` You can use the `getTransactions` method to retrieve a list of transactions from the blockchain. This is limited to 30 transactions per page. -<<< @/../../docs-snippets/src/guide/provider/querying-the-chain.test.ts#get-transactions{ts:line-numbers} +<<< @/../../docs-snippets2/src/provider/functionality/get-transactions.ts#getTransactions{ts:line-numbers} diff --git a/packages/account/src/providers/provider.ts b/packages/account/src/providers/provider.ts index 4552f4d4f56..c5119258914 100644 --- a/packages/account/src/providers/provider.ts +++ b/packages/account/src/providers/provider.ts @@ -106,14 +106,16 @@ export type Block = { }; }; +export type PageInfo = GqlPageInfo; + export type GetCoinsResponse = { coins: Coin[]; - pageInfo: GqlPageInfo; + pageInfo: PageInfo; }; export type GetMessagesResponse = { messages: Message[]; - pageInfo: GqlPageInfo; + pageInfo: PageInfo; }; export type GetBalancesResponse = { @@ -122,12 +124,12 @@ export type GetBalancesResponse = { export type GetTransactionsResponse = { transactions: Transaction[]; - pageInfo: GqlPageInfo; + pageInfo: PageInfo; }; export type GetBlocksResponse = { blocks: Block[]; - pageInfo: GqlPageInfo; + pageInfo: PageInfo; }; /** diff --git a/packages/account/src/providers/transaction-summary/get-transaction-summary.ts b/packages/account/src/providers/transaction-summary/get-transaction-summary.ts index 579d816b403..b7af213dada 100644 --- a/packages/account/src/providers/transaction-summary/get-transaction-summary.ts +++ b/packages/account/src/providers/transaction-summary/get-transaction-summary.ts @@ -5,10 +5,10 @@ import { arrayify } from '@fuel-ts/utils'; import type { GqlGetTransactionsByOwnerQueryVariables, - GqlPageInfo, GqlReceiptFragment, } from '../__generated__/operations'; import type Provider from '../provider'; +import type { PageInfo } from '../provider'; import type { TransactionRequest } from '../transaction-request'; import type { TransactionResult } from '../transaction-response'; @@ -133,7 +133,7 @@ export interface GetTransactionsSummariesParams { export interface GetTransactionsSummariesReturns { transactions: TransactionResult[]; - pageInfo: GqlPageInfo; + pageInfo: PageInfo; } /** @hidden */