Skip to content

Commit

Permalink
chore: support BN input in block fetch methods (#3700)
Browse files Browse the repository at this point in the history
  • Loading branch information
Torres-ssf authored Feb 12, 2025
1 parent 7b79da3 commit 6d3cafb
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/shaggy-ravens-switch.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@fuel-ts/account": patch
---

chore: support BN input in block fetch methods
62 changes: 61 additions & 1 deletion packages/account/src/providers/provider.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ import { setupTestProviderAndWallets, launchNode, TestMessage } from '../test-ut
import type { Coin } from './coin';
import { coinQuantityfy } from './coin-quantity';
import type { Message } from './message';
import type { ChainInfo, CursorPaginationArgs, NodeInfo } from './provider';
import type { Block, ChainInfo, CursorPaginationArgs, NodeInfo } from './provider';
import Provider, {
BLOCKS_PAGE_SIZE_LIMIT,
DEFAULT_RESOURCE_CACHE_TTL,
Expand Down Expand Up @@ -1046,6 +1046,66 @@ describe('Provider', () => {
});
});

it('should ensure getBlockWithTransactions supports different parameters types', async () => {
using launched = await setupTestProviderAndWallets();
const {
provider,
wallets: [sender],
} = launched;

const baseAssetId = await provider.getBaseAssetId();

const tx = await sender.transfer(sender.address, 1, baseAssetId);
const { blockId } = await tx.waitForResult();

expect(blockId).toBeDefined();

const block = (await provider.getBlockWithTransactions('latest')) as Block;
expect(block).toBeDefined();

let sameBlock = await provider.getBlockWithTransactions(blockId as string);
expect(block).toStrictEqual(sameBlock);

sameBlock = await provider.getBlockWithTransactions(block.height.toString());
expect(block).toStrictEqual(sameBlock);

sameBlock = await provider.getBlockWithTransactions(block.height.toNumber());
expect(block).toStrictEqual(sameBlock);

sameBlock = await provider.getBlockWithTransactions(block.height);
expect(block).toStrictEqual(sameBlock);
});

it('should ensure getBlock supports different parameters types', async () => {
using launched = await setupTestProviderAndWallets();
const {
provider,
wallets: [sender],
} = launched;

const baseAssetId = await provider.getBaseAssetId();

const tx = await sender.transfer(sender.address, 1, baseAssetId);
const { blockId } = await tx.waitForResult();

expect(blockId).toBeDefined();

const block = (await provider.getBlock('latest')) as Block;
expect(block).toBeDefined();

let sameBlock = await provider.getBlock(blockId as string);
expect(block).toStrictEqual(sameBlock);

sameBlock = await provider.getBlock(block.height.toNumber());
expect(block).toStrictEqual(sameBlock);

sameBlock = await provider.getBlock(block.height.toString());
expect(block).toStrictEqual(sameBlock);

sameBlock = await provider.getBlock(block.height);
expect(block).toStrictEqual(sameBlock);
});

it('can getMessageProof with all data', async () => {
// Create a mock provider to return the message proof
// It test mainly types and converstions
Expand Down
14 changes: 8 additions & 6 deletions packages/account/src/providers/provider.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import type { AddressInput } from '@fuel-ts/address';
import { Address } from '@fuel-ts/address';
import { Address, isB256 } from '@fuel-ts/address';
import { ErrorCode, FuelError } from '@fuel-ts/errors';
import type { BN } from '@fuel-ts/math';
import type { BigNumberish, BN } from '@fuel-ts/math';
import { bn } from '@fuel-ts/math';
import type { Transaction } from '@fuel-ts/transactions';
import { InputType, InputMessageCoder, TransactionCoder } from '@fuel-ts/transactions';
Expand Down Expand Up @@ -1592,7 +1592,7 @@ export default class Provider {
* @param idOrHeight - ID or height of the block.
* @returns A promise that resolves to the block or null.
*/
async getBlock(idOrHeight: string | number | 'latest'): Promise<Block | null> {
async getBlock(idOrHeight: BigNumberish | 'latest'): Promise<Block | null> {
let block: GqlBlockFragment | undefined | null;

if (idOrHeight === 'latest') {
Expand All @@ -1601,7 +1601,7 @@ export default class Provider {
} = await this.operations.getLatestBlock();
block = latestBlock;
} else {
const isblockId = typeof idOrHeight === 'string' && idOrHeight.length === 66;
const isblockId = typeof idOrHeight === 'string' && isB256(idOrHeight);
const variables = isblockId
? { blockId: idOrHeight }
: { height: bn(idOrHeight).toString(10) };
Expand Down Expand Up @@ -1677,15 +1677,17 @@ export default class Provider {
*/
async getBlockWithTransactions(
/** ID or height of the block */
idOrHeight: string | number | 'latest'
idOrHeight: BigNumberish | 'latest'
): Promise<(Block & { transactions: Transaction[] }) | null> {
let variables;
if (typeof idOrHeight === 'number') {
variables = { blockHeight: bn(idOrHeight).toString(10) };
} else if (idOrHeight === 'latest') {
variables = { blockHeight: (await this.getBlockNumber()).toString() };
} else {
} else if (typeof idOrHeight === 'string' && isB256(idOrHeight)) {
variables = { blockId: idOrHeight };
} else {
variables = { blockHeight: bn(idOrHeight).toString() };
}

const { block } = await this.operations.getBlockWithTransactions(variables);
Expand Down

0 comments on commit 6d3cafb

Please sign in to comment.