0

I am building a contract to transfer ether to the contract's owner and test it via hardhat. I found that the owner's balance doesn't change after the transfer. below is the contract code:


contract Test is ERC20,Ownable {

    constructor(uint256 price) ERC20("Test", "OMN") Ownable(msg.sender) {
        _mint(msg.sender, 10000);
    }

    function testTransfer() public payable {
        payable(owner()).transfer(msg.value);
    }
}

In the unit test, I have code:

async function deployFixture() {
    const [owner] = await ethers.getSigners();

    const Test = await ethers.getContractFactory('Test');

    const [_, addr1, addr2] = await ethers.getSigners();
    const contract = await Omniverse.deploy(1);
    return { contract, owner,addr2 };
  }

it('test transfer', async () => {
    const { contract, owner, addr2 } = await loadFixture(deployFixture);
    const addr2Contract = await contract.connect(addr2);
    const ownerWalletBalance = parseFloat(ethers.formatEther(await ethers.provider.getBalance(owner.address)));
    await addr2Contract.testTransfer({ value: 100 });
    const ownerWalletBalanceAfter = parseFloat(ethers.formatEther(await ethers.provider.getBalance(owner.address)));
    
    console.log('ownerWalletBalance', ownerWalletBalance, ownerWalletBalanceAfter);
  });

The output from above unit test is:

ownerWalletBalance 9999.995976379376 9999.995976379376

The testTransfer transfers the value from msg to the contract's owner. But the balance printed out in the log shows there are exactly the same before and after the method is called. I can't figure out what wrong in my code.

3 Answers 3

1

The value you sent in this line is in wei.

await addr2Contract.testTransfer({ value: 100 });

100 wei is equal to 0,0000000000000001 eth. It is possible that the two values are different, but your output needs to have more numbers after the decimal point.

0

The balances of owner and addr2 are getting changed, you're passing the value as 100 which is very small in terms of 18-decimals (as Wei), if you send by parsing it using ethers.parseEther('100'), then the change in balances are quite noticeable as it'll transfer 100 Ether instead of 100 Wei, like this:

it('test transfer', async () => {

        const { contract, owner, addr2 } = await loadFixture(deployFixture);

        const addr2Contract = await contract.connect(addr2); 

        const ownerWalletBalance = parseFloat(ethers.formatEther(await ethers.provider.getBalance(owner.address)));

        const addr2WalletBalance = parseFloat(ethers.formatEther(await ethers.provider.getBalance(addr2.address)));

        await addr2Contract.testTransfer({ value: ethers.parseEther('100') })

        const ownerWalletBalanceAfter = parseFloat(ethers.formatEther(await ethers.provider.getBalance(owner.address)));

        const addr2WalletBalanceAfter = parseFloat(ethers.formatEther(await ethers.provider.getBalance(addr2.address)));

        console.log('ownerWalletBalance', ownerWalletBalance, ownerWalletBalanceAfter);

        console.log('addr2WalletBalance', addr2WalletBalance, addr2WalletBalanceAfter);

});

enter image description here

-1

Try to wait a few seconds before you query the balance. It turns out that when you do await addr2Contract.testTransfer({ value: 100 }); the script moves to the next line as soon as the transaction is inserted into the blockchain.

But for you to see a change in the owner's Balance you would need to wait until the transaction is validated. Because you are using the simulated environment of Hardhat it may seem to you that the validation runs instantly, but it may not actually have been the case.

1
  • in brief: Your script might be querying the balance before the validation/confirmation of the transfer
    – DarkWinter
    Commented Mar 18 at 10:00

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.