0

This is the bit of code I added that is throwing the error,

function withdraw() public {
    address payable owner = 0x11AEA62f1575d87a509d0e25b1060B06600842Fc;
    owner.send(address[this].balance);
}

The error it throws is "Type contract MyToken is not implicitly convertible to expected type uint256"

If I change the 3rd line to owner.send(this.balance); the compiler tells me that it can't find "balance". Can any one explain what I've done wrong?

1 Answer 1

1

The payable address is intended to use with Ethereum or the main coin whatever the network you are using. Quoted from the docs

If you need a variable of type address and plan to send Ether to it, then declare its type as address payable to make this requirement visible. Also, try to make this distinction or conversion as early as possible.

At the second line, Solidity compiler cannot know whether the address is just an address or payable address ,so the correct syntax as:

address payable owner = payable(address(0x11AEA62f1575d87a509d0e25b1060B06600842Fc));

I am guessing your intention with this code as: whether someone calls the withdraw, certain account receives ethers from the contract's balance.

Actually at the third line, you've mixed parentheses with brackets. Correct syntax as:

owner.send( address(this).balance );

Just to note payable address member function send returns false when it fails for some reason, doesn't stop with an exception, docs states a warning about this;

There are some dangers in using send: The transfer fails if the call stack depth is at 1024 (this can always be forced by the caller) and it also fails if the recipient runs out of gas. So in order to make safe Ether transfers, always check the return value of send, use transfer or even better: use a pattern where the recipient withdraws the money.

2
  • Oddly owner = payable(address( caused a new error. However the brackets to parenthesis. address(this).balance fixed my issue. Commented May 28, 2021 at 21:15
  • I've got it working on remix actually, do you have transaction link ? Commented May 28, 2021 at 21:29

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.