0

I copy a withdrawal function from the solidity documentation and added it to my contract

    function withdraw() public onlyOwner {
      uint256 ownerBalance = address(this).balance;
      require(ownerBalance > 0, "ERRFUN");
      payable(owner()).transfer(ownerBalance);
      emit ChangeEtherBalance(ownerBalance);
    }

Obs. My contract use:

import "@openzeppelin/contracts/access/Ownable.sol";

The problem is that while testing I found out that after the function's call the balance of the contract is zero, however, the balance of the owner is lower than the balance before. Here is the code of my test and the print of the console commands:

    describe('Test withdraw() method. Sucess', () => {
    it('withdraw success', async function () {
        const ownerBalanceBefore = await web3.eth.getBalance(owner);
        const vendorBalanceBefore = await web3.eth.getBalance(this.managementCenter.address);
        console.log("owner init Balance",ownerBalanceBefore) //owner init Balance 995570313620188160000
        console.log("vendor init Balance", vendorBalanceBefore) //vendor init Balance 257200000000000
        // withdraw operation
        const txWithdraw = await this.managementCenter.withdraw({ from: owner });

        // Check that the Vendor's balance has 0 eth
        const vendorFinalBalance = await web3.eth.getBalance(this.managementCenter.address);
        expect(vendorFinalBalance).to.be.bignumber.equal(new BN(0));

        const ownerFinalBalance = await web3.eth.getBalance(owner);
        console.log("vendor Final Balance", vendorFinalBalance) //vendor Final Balance 0
        console.log("owner Final Balance",ownerFinalBalance) //owner Final Balance 995568878760188160000          
        expect(new BN(ownerFinalBalance))
            .to.be.bignumber.greaterThan(new BN(ownerBalanceBefore));            

        // Check the the owner balance has changed
        expectEvent(txWithdraw, 'ChangeEtherBalance', { value: new BN(vendorBalanceBefore)});
    });
});    

because that, line

expect(new BN(ownerFinalBalance))
        .to.be.bignumber.greaterThan(new BN(ownerBalanceBefore));  

crash.

I am usign ganache-cli and Truffle v5.4.10 (core: 5.4.10) Solidity - 0.8.7 (solc-js) Node v16.9.1 Web3.js v1.5.2

Any ideas? thanks in advance

3
  • this is just a hunch, but is there any chance _owner address is not that of owner ? balance that is being reduced can be explained by gas used in making this transaction Commented Jan 10, 2022 at 22:25
  • thanks for the replay. In this case _owner was the sender that call the constructor. But just to be sure and since I am using The Ownable contract from openzeppelin, I replace it _owner with owner(). Same result. Commented Jan 11, 2022 at 11:49
  • Maybe it is something related to the testing process? Commented Jan 11, 2022 at 11:55

1 Answer 1

1

The problem is that the amount, 257200000000000 to retrieve is smaller than the gas cost that is 84831 * 10000000000 = 8,4831×10^14.

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.