I'm running the test outlined below using Hardhat. The smart contract has a function to output 'msg.sender.balance', and the test file calls this function, then waits for the next block, then calls the function again. I'd expect the balance to decrease due to gas cost, but instead it increases.

Smart contract Test2.sol:

pragma solidity ^0.8.4;

import "hardhat/console.sol";

contract Test2
    function test() public {
       console.log("msg.sender=", msg.sender, ", balance=", msg.sender.balance);

    constructor() { }

Hardhat test:

const { expect } = require("chai");
const { ethers } = require("hardhat");

describe("Test2 contract", function () {
    it("Calls test()", async function () {
        const [owner] = await ethers.getSigners();
        const test2 = await ethers.getContractFactory("Test2");
        const contract = await test2.deploy();

        await contract.test();

        // wait for next block
        await ethers.provider.send("evm_mine", []); 

        await contract.test();

The result of the test is as follows:

msg.sender= 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266 , balance= 9999948439737164068560
msg.sender= 0xf39fd6e51aad88f6f4ce6ab8827279cfffb92266 , balance= 9999953606917951780844

As you can see the balance has increased. I have no idea why this is happening so any help is much appreciated, thanks!



Your Answer

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

Browse other questions tagged or ask your own question.