ethers.js: v6: getBlock doesn't return transaction objects

Ethers Beta Version

6.0.0-beta-exports.11

Describe the Problem

v5

import { ethers } from 'ethers'

(async () => {
  const env = "https://eth-mainnet.g.alchemy.com/v2/A"
  const provider = new ethers.providers.JsonRpcProvider(env)
  const block = await provider.getBlockWithTransactions(16483624)
  console.log('transactions', block.transactions)
})();
transactions [
  {
    hash: '0x7297bb1c144ebfabb5fc677e8cbc7f0ae7023e627e137aa50fa8fac2f486764b',
    type: 2,
    accessList: [],
    blockHash: '0x8be58b1fa71c51059dbfc4a54b1586643baccfdff5e2d468310558c0d6755d79',
    blockNumber: 16483624,
    transactionIndex: 0,
    confirmations: 133,
    from: '0xA1889Cb28dDd0A67dEFF8ce9C409B6A9A091860e',
    gasPrice: BigNumber { _hex: '0x0ca9544e4a', _isBigNumber: true },
    maxPriorityFeePerGas: BigNumber { _hex: '0x096fc5f0db', _isBigNumber: true },
    maxFeePerGas: BigNumber { _hex: '0x18667f1ed1', _isBigNumber: true },
    gasLimit: BigNumber { _hex: '0x0b71b0', _isBigNumber: true },
    to: '0x000000000dFDe7deaF24138722987c9a6991e2D4',
    value: BigNumber { _hex: '0x00', _isBigNumber: true },
    nonce: 28709,
    data: '0xc18a84bc000000000000000000000000820fbab984bed73ece56592f8fee5e1d1ceed5c0000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000e4827b042500000000000000000000000000000000000000000000000000000000000000800000000000000000000000000000000000000000000000c0d93fe19ed2d80ae00000000000000000000000000000000000000000000000000000000002a5e7fa000000000000000000000000000000000000000000000000000000000004e262000000000000000000000000000000000000000000000000000000000000002b3472a5a71965499acd81997a54bba8d852c6e53d000bb82260fac5e5542a773aa44fbcfedf7c193bc2c59900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000',
    r: '0xd78b05e017bb666590c830197cb3a5269b7d547ab719416a83f623a7c9957510',
    s: '0x3898fd243d51c0362d4d5af70f7850fbde97f09722f6c774ac0367643963caa6',
    v: 1,
    creates: null,
    chainId: 1,
    wait: [Function (anonymous)]
  },
  {
    hash: '0x86705a6bf2e183a6bf7e37b333191345d8ee1da4a44fcefcaf5f8af322453660',
    type: 2,
    accessList: [],
    blockHash: '0x8be58b1fa71c51059dbfc4a54b1586643baccfdff5e2d468310558c0d6755d79',
    blockNumber: 16483624,
    transactionIndex: 1,
    confirmations: 133,
    from: '0x32F95Fd3279BcABb912266eb65A2e2D27fa1DF9B',
    gasPrice: BigNumber { _hex: '0x038e31d86f', _isBigNumber: true },
    maxPriorityFeePerGas: BigNumber { _hex: '0x54a37b00', _isBigNumber: true },
    maxFeePerGas: BigNumber { _hex: '0x06b162b5ac', _isBigNumber: true },
    gasLimit: BigNumber { _hex: '0x086470', _isBigNumber: true },
    to: '0xFb5185b7f8C61F815b57de679bbC857f510352f7',
    value: BigNumber { _hex: '0x00', _isBigNumber: true },
    nonce: 335,
    data: '0xeb139bac0000000000000000000000000000000000000000000000000000000000fb8528000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000000000000000000002a000000000000000000000000000000000000000000000000000000000000005400000000000000000000000000000000000000000000000000000009b86b56064000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000dc63269ea166b70d4780b3a11f5c825c2b761b01000000000000000000000000b0b0e4ceefde71209a113144b5924fae90f06798000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001bc16d674ec80000000000000000000000000000000000000000000cf7604ca309decc466b0a4000000000a4000000a4000000a4000000a400000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000014000000000000000000000000000000000000000000000000000000000000000a4bf15fcd8000000000000000000000000303389f541ff2d620e42832f180a08e767b28e10000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000242cc2878d0063dae24900000000050000b0b0e4ceefde71209a113144b5924fae90f0679800000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000001111111254eeb25477b68fb85ed929f73a9605820000000000000000000000000dba3dfee43d9b6450c716c58fdae8d3be37fdc900000000000000000000000003f7724180aa6b939894b5ca4314783b0b36b32900000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c02aaa39b223fe8d0a0e5c4f27ead9083c756cc2000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000220000000000000000000000000000000000000000000000000019d83547cf1c5b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000c12dcf3eb4d00000000000000000000000000000000000000000000000000000000000000000000000000003000000000000000000000000000000000000000000000000000000000007f2ca000000000000000000000000000000000000000000000000000000047641ce730000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000017c0000000000000000000000000000000000000000000000000000000000000041d51d904609b1fa6cb9d9d6004972512f34c4e6dd90985097184a09a1395981633debdad0ff94e5f6550e69f63f53781429d4388dc5dda4b872a53193e18827f91b000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000004000000000000000000000000a0b86991c6218b36c1d19d4a2e9eb0ce3606eb480000000000000000000000006b175474e89094c44da98b954eedeac495271d0f000000000000000000000000dac17f958d2ee523a2206206994597c13d831ec70000000000000000000000004fabb145d64652a948d72533023f6e7a623c7c53',
    r: '0x3bea885f1c1d63f718839a5a51634b3af242c3a3a323c70b17ff40ec5ba7d936',
    s: '0x097eacf5c93265867cd6d20826541e80c218274f3868454a642948f57e159b4b',
    v: 0,
    creates: null,
    chainId: 1,
    wait: [Function (anonymous)]
  },

v6

import { ethers } from 'ethers'

(async () => {
  const env = "https://eth-mainnet.g.alchemy.com/v2/A"
  const provider = new ethers.JsonRpcProvider(env)
  const block = await provider.getBlock(16483624, true)
  console.log('transactions', block.transactions)
})();
transactions [
  '0x7297bb1c144ebfabb5fc677e8cbc7f0ae7023e627e137aa50fa8fac2f486764b',
  '0x86705a6bf2e183a6bf7e37b333191345d8ee1da4a44fcefcaf5f8af322453660',
  '0x33528446fe7a19411b9184c93cf47b0a8f7d69e043c39ae61de0ab31c6f13f6e',
  '0x2aee9bfdccfcae6e0440db4fdd26117bc2b760ec2372fdf2cf4ab3a98103b89c',
  '0x13f1122843a2e64bc5ef55ba537a9030fb52d59b8423db9cbeac296f5da5081c',
  '0x63416356827de8c7efcab84803484ac16b21839b6f864441c7dff0d734c3129a',
  '0x3828786ec6cb3ff17e9765c395b5ae5a5c38ebb6cc901d3a93f8e8e597ecbdc6',

Code Snippet

No response

Errors

No response

Environment

node.js

Environment (Other)

No response

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 19 (8 by maintainers)

Most upvoted comments

LOL! This took some time to track down to discover it is working as I intended. I definitely need to add this to the migration. This was written a long time ago. 😃

The result is a Block instance. The transactions are returned with the RPC and are included, but accessed using the block.getTransaction(index), so that there is no difference in the Block object.

const block = await getBlock(123, true);
const txs = await Promise.all(block.transactions.map(hash => block.getTransaction(hash)));

If you didn’t pass true in for prefetchTxs, you can still do the same thing, it will just fetch each one as requested, which would be way more expensive.

Does that make more sense? I’m also open to other possible APIs. The goal is to make a single sensible object that comes back regardless of the request.

Maybe add a getAllTransactions(): Promise<TransactionResponse>? Which would just return the internal transactions if true was specified and internally call the getBlock with true if prefetch wasn’t enabled?

So for anyone who comes here and searches for a solution to retrieve an array of TransactionResponse objects, that’s how to solve it:

import * as dotenv from "dotenv";
import { JsonRpcProvider } from "ethers";

dotenv.config();

// eslint-disable-next-line @typescript-eslint/no-explicit-any
let blockPayload: Array<any> = []; // Array to aggregate all transactions of a single block.

const provider = new JsonRpcProvider(process.env.ETH_MAINNET_URL); // Connecting to the Ethereum mainnet RPC.

// Get the latest block from the network, where `blockPayload` is an array of `TransactionResponse` objects.
const block = await provider.getBlock("latest", true);
if (block) {
  blockPayload = block.prefetchedTransactions;
}

You can also use provider.getBlock("latest", true) rather than fetching the block number first. 😃

It depends which you need. The .prefetchedTransactions returns them all as an Array. The getPrefetchedTransaction(indexOrHash) will get you the transaction that matches the index (if a number is given for indexOrHash) or the transaction that matches the hash (if a string is given).

@pcaversaccio That error means you are using .prefetchedTransactions(); it is a getter, so you should use .prefetchedTransactions (i.e. no ()). It isn’t a function. 😃

I think this is resolved now, so closing. Feel free to continue discussions though.

Thanks! 😃