Trying to write a quick function to withdraw ETH
from my contract - it's not working.
Here's the code:
function withdrawEntireBalance() payable external {
require(msg.sender == contractOwner, "ERROR! Only Contract's owner may call!");
msg.sender.transfer(address(this).balance);
}
When I say it's not working I don't mean that it's throwing errors. On that contrary: it compiles perfectly, it runs - it's just plain not doing what it's supposed to be doing. It does nothing.
I also created a getContractBalance()
function just so I can query the Contract and make sure it actually has some ETH
in its balance:
function getContractBalance() external returns(uint256 theBalance) {
return address(this).balance;
}
This function works perfectly well and I'm able to verify with 100% certainly that the Contract does indeed have ETH in its balance when I'm trying to withdraw that balance. Here's a sample output:
'getContractBalance' result is: 14700000000000000
I also tried some alternative code for the withdrawEntireBalance()
function - after having read all sorts of posts re security issues and minimum gas costs increasing (potentially causing the traditional transfer()
and send()
methods normally used for this sort of operation to fail) - it didn't work either. I'm including it here anyway so you can see what I'v gone through:
// Alternative code:
(bool success, ) = msg.sender.call.value(address(this).balance)("");
require(success, "ERROR!!!! 'withdrawEntireBalance()' - Transfer failed.");
Any ideas what's going on here?
=================
UPDATE:
As requested, here's how I'm calling my withdrawEntireBalance()
method in my javascript
code:
cashOut: function() {
console.log("\n\n=================\n==> in 'cashOut()'!");
var cashoutYo = jsApp.appContractInstance.methods.withdrawEntireBalance().call({from: jsApp.defaultAccount},
function(error, result) {
if (!error) {
console.log("Back in 'withdrawEntireBalance'!!! No ERRORS!!!");
// jsApp.contractOwner = result;
console.log("CashOut result is: " + result);
console.log("CashOut result KEYS = " + Object.keys(result));
console.log("CashOut result VALUES = " + Object.values(result));
}
else {
console.log("ERROR!!! from 'withdrawEntireBalance()' -->\n" + error);
console.log("Error KEYS = " + Object.keys(error));
console.log("Error VALUES = " + Object.values(error));
}
});
console.log("\n\n=================\n==>EXITING 'withdrawEntireBalance()'!");
}
it runs
- how? (please share your method of executing thewithdrawEntireBalance
function).