5

Transaction response and transaction receipt are different from one another?? When to use which one?? in ethers.js

3

2 Answers 2

6

transactionReceipt is available only if the block is mined, it includes gasCost, gasUsed.

transactionResponse get u details like to, from, data etc. Here transaction is not mined yet.

4

A transactionResponse object is what gets returned by awaiting an initial transactional interaction with a blockchain/contract. For this example, I'm getting a "Token" contract of my deployments and calling transfer() on it:

const { deployments, ethers } = require("hardhat");

const [ owner, account1 ] = await ethers.getSigners(); 
await deployments.fixture(["Token"]); 
const token = await ethers.getContract("Token", owner);

const transactionResponse = await token.transfer(account1.address, 1); // initial transaction response

The transactionResponse object will hold the initial response from the local blockchain. Keep in mind that the transaction hasn't been mined yet. The object you receive back looks like that:

{
  hash: '0x0032de9b9af85f72b2b1b236b69240fa09f2672b059f95600b200ae9f7360d4b',
  type: 2,
  accessList: [],
  blockHash: '0x13872d79194559ec6cdd959f7c7f991f99b0073016129f77ab5c365b04b8bdb5',
  blockNumber: 2,
  transactionIndex: 0,
  confirmations: 1,
  from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
  gasPrice: BigNumber { _hex: '0x69a495c0', _isBigNumber: true },
  maxPriorityFeePerGas: BigNumber { _hex: '0x3b9aca00', _isBigNumber: true },
  maxFeePerGas: BigNumber { _hex: '0x97ae6180', _isBigNumber: true },
  gasLimit: BigNumber { _hex: '0x01bada98', _isBigNumber: true },
  to: '0x5FbDB2315678afecb367f032d93F642f64180aa3',
  value: BigNumber { _hex: '0x00', _isBigNumber: true },
  nonce: 1,
  data: '0xa9059cbb00000000000000000000000070997970c51812dc3a010c7d01b50e0d17dc79c80000000000000000000000000000000000000000000000000000000000000002',
  r: '0xd74f88e3d36e92fb32468cc4c77f2b3e72f577ffd0e9c7c274a726edb3dce6f6',
  s: '0x4b0b2648df3a3db8f2b86f64e02937970cfefd7bba59a14c0ce3f866cddd3024',
  v: 1,
  creates: null,
  chainId: 313374,
  wait: [Function (anonymous)]
}

This transactionResponse object has a method wait(), which takes a number of blocks as a parameter and returns a promise, which resolves after the number of blocks specified have been mined returning a transactionReceipt:

const transactionReceipt = await transactionResponse.wait(1);

The transactionReceipt object returned looks like that:

{
  to: '0x5FbDB2315678afecb367f032d93F642f64180aa3',
  from: '0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266',
  contractAddress: null,
  transactionIndex: 0,
  gasUsed: BigNumber { _hex: '0xcd5d', _isBigNumber: true },
  logsBloom: '0x00000000000000000002000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000008000000000000000000000000000000000000000000000840000000000000000100000000000000000000000000000010000000000000000000000000000000000000000000000000000000000000000200000000000000000000000000000000000000000000000000000000000000000000000000000042000000200000000000000000000000002000000000000000000000000000000000000000000000000000000001000000000000000000000000000000',
  blockHash: '0x13872d79194559ec6cdd959f7c7f991f99b0073016129f77ab5c365b04b8bdb5',
  transactionHash: '0x0032de9b9af85f72b2b1b236b69240fa09f2672b059f95600b200ae9f7360d4b',
  logs: [
    {
      transactionIndex: 0,
      blockNumber: 2,
      transactionHash: '0x0032de9b9af85f72b2b1b236b69240fa09f2672b059f95600b200ae9f7360d4b',
      address: '0x5FbDB2315678afecb367f032d93F642f64180aa3',
      topics: [Array],
      data: '0x0000000000000000000000000000000000000000000000000000000000000002',
      logIndex: 0,
      blockHash: '0x13872d79194559ec6cdd959f7c7f991f99b0073016129f77ab5c365b04b8bdb5'
    }
  ],
  blockNumber: 2,
  confirmations: 1,
  cumulativeGasUsed: BigNumber { _hex: '0xcd5d', _isBigNumber: true },
  effectiveGasPrice: BigNumber { _hex: '0x69a495c0', _isBigNumber: true },
  status: 1,
  type: 2,
  byzantium: true,
  events: [
    {
      transactionIndex: 0,
      blockNumber: 2,
      transactionHash: '0x0032de9b9af85f72b2b1b236b69240fa09f2672b059f95600b200ae9f7360d4b',
      address: '0x5FbDB2315678afecb367f032d93F642f64180aa3',
      topics: [Array],
      data: '0x0000000000000000000000000000000000000000000000000000000000000002',
      logIndex: 0,
      blockHash: '0x13872d79194559ec6cdd959f7c7f991f99b0073016129f77ab5c365b04b8bdb5',
      args: [Array],
      decode: [Function (anonymous)],
      event: 'Transfer',
      eventSignature: 'Transfer(address,address,uint256)',
      removeListener: [Function (anonymous)],
      getBlock: [Function (anonymous)],
      getTransaction: [Function (anonymous)],
      getTransactionReceipt: [Function (anonymous)]
    }
  ]
}

The transactionReceipt object holds all of the data pertaining to the actual mined transaction, including, but not limited to gasUsed, Logs, Events, and so on.

To summarize, we send transaction to blockchain and get Response, as soon as it gets mined we get Receipt

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.