Withdraw funds

Hi guys,

I made a smart contract, uploaded it to ganache and I am testing a withdraw function that withdraws all the funds of the contract to the owner of the contract. I tried several things but nothing seems to work.

My smart contract looks as follows:

Contract Test is Ownable, Pausable
function withdraw1() public onlyOwner
(bool success, ) = msg.sender.call{value: address(this).balance}("");

function withdraw2() public onlyOwner

None of these withdrawal functions work. The second withdrawal function doesn’t even compile. I get the following error message:

“TypeError: “send” and “transfer” are only available for objects of type “address payable”, not “address”.”

There is not much I can do with this because I can’t make the addess payable.

The first withdrawal function compiles but when I run my tests I don’t see funds moving from the contract to the owner.

At this moment I am basically stuck. Do I need to inherit from another Openzeppelin contract in order to withdraw ?

By the way, I am working on solidity version 0.8.0.

You can cast to address payable with payable(owner()), like this:


owner is a function that you need to call btw.

Thanks for the reply. It compiles. But I get the same error during my mocha test as with my first withdraw function:

“Error: Returned error: VM Exception while processing transaction: invalid opcode”

My test function is as follows:

instance = await Contract.deployed();
user1 = accounts[0];
await instance.withdraw({from:user1});

I guess the error means I am calling my withdraw function wrong but I don’t think I am. This is currently my withdraw function:

function withdraw() public onlyOwner

Please use triple backticks to surround your code in order to get proper formatting.

code here

This error depends on many factors. What is the value of owner()?

Thanks, I didn’t know about the tripple backticks.

owner() correctly returns the address of the owner of the contract.

And the address is an account with a private key, or is it a contract?

The address is an account with a private key.

Is user1 the owner?

Otherwise, I don’t know what could be the problem. If you can put together a repository where I can reproduce the issue and test it I’ll be able to figure out why it’s not working.

Hey Frangio,

Sorry for the late reply but I was on holiday. Here is the repository:

The file testfile.js contains a test that throws the error. If you comment the withdraw function call, the test runs fine. If you uncomment it, the withdraw function call throws an error:

Error: Returned error: VM Exception while processing transaction: invalid opcode

The test works fine for me in the repository you shared. There was no commented line in the tests.

Really ? Strange … Then I have no clue why it doesn’t work here :slight_smile: Thanks for letting me know that it works fine for you.