0

I've deployed a simple wallet contract.

The withdraw function goes like this..

function withdraw() external payable {
    require(msg.sender == owner, "Only Owner can withdraw");
    payable(msg.sender).transfer(msg.value);
}

In this case, the msg.value is not transferred to the owner.

But when I pass the value as a parameter to the function, it is transferred.
Tried something like this

function withdraw(uint256 _amount) external payable {
    require(msg.sender == owner, "Only Owner can withdraw");
    payable(msg.sender).transfer(_amount);
}

The same goes with when I try to transfer with Call method.

Could someone explain it?
Thanks

1 Answer 1

1

This code doesn't make sense. You're trying to withdraw the amount you're sending to the contract at the time of calling the function withdraw. In other words, your money will just make a round trip, from the owner to the contract and back to the owner. Therefore, the balance of the contract doesn't change.

I assume you are trying to withdraw the entire balance of the contract. If that's the case, you need this:

payable(msg.sender).transfer(address(this).balance);

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.