9

Running

      const receipt = await contract.mint(4)
      console.log(receipt)

gives me the following transaction receipt

{
  hash: '0xee7c081488965518d3cb8d23a13a4eabf728a6f1842c9b1249679a8b809bcf29',
  type: 2,
  accessList: [],
  blockHash: '0x741edbbc20aa711b80cbcbf6f7cf4ffed88bc042a4417ddbeb0b38d7fdf385cc',
  blockNumber: 3,
  transactionIndex: 0,
  confirmations: 1,
  from: '0x70997970C51812dc3A010C7d01b50e0d17dc79C8',
  gasPrice: BigNumber { _hex: '0x65013202', _isBigNumber: true },
  maxPriorityFeePerGas: BigNumber { _hex: '0x3b9aca00', _isBigNumber: true },
  maxFeePerGas: BigNumber { _hex: '0x8e679a04', _isBigNumber: true },
  gasLimit: BigNumber { _hex: '0x01bad518', _isBigNumber: true },
  to: '0x5FbDB2315678afecb367f032d93F642f64180aa3',
  value: BigNumber { _hex: '0x00', _isBigNumber: true },
  nonce: 0,
  data: '0xfe234aac0000000000000000000000000000000000000000000000000000000000000004',
  r: '0xc0edd7dc1a026cddeee860ab6f7d1aacf80a0c3c9ef3a119a49382662a74f39d',
  s: '0x3b1a06a0df530e153bd22852d5956e59dbcac0250eda1cdd961e71554027f60e',
  v: 1,
  creates: null,
  chainId: 31337,
  wait: [Function (anonymous)]
}

I want to run tests with waffle and would like to make sure that the refund is implemented correctly. For that I want to see if the account balance difference equals the transaction value minus the refund. The problem is that I now have to take gas costs into account. How do I accurately factor these out?

1 Answer 1

11

After a bit of searching I figured out that the receipt should be the mined object.. I also wasn't sending any value with the transaction.

      const addr1Bal = await provider.getBalance(addr1.address)
      const txValue = ethers.utils.parseEther('0.03').mul(BigNumber.from('4'))
      const tx = await contract.connect(addr1).mint(4, { value: txValue })
      const receipt = await tx.wait()
      console.log('total ether spent on gas for transaction: \t', ethers.utils.formatEther(receipt.gasUsed.mul(receipt.effectiveGasPrice)))
      console.log('balance difference minus transaction value: \t', ethers.utils.formatEther(addr1Bal.sub(await provider.getBalance(addr1.address)).sub(txValue)))

prints

total ether spent on gas for transaction:        0.000768148434753892
balance difference minus transaction value:      0.000768148434753892

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.