Search code examples
blockchainethereumsoliditycontractether

Can't withdraw amount from contract on Ethereum local blockchain


I am having an issue that is blocking me for days. And it all comes down to this:

I am using Ganache (TestRPC) for my local blockchain with it's default test accounts. I am connected with Metamask(on Chrome) using the first test account. (balance 100ETH).

I am creating the WithdrawalContract from solidity documentation: http://solidity.readthedocs.io/en/develop/common-patterns.html using 10ETH. Everything fine until now. Contract creation successful, my test account balance is 90ETH and Contract shows in transactions list with 10ETH.

Now I execute the withdraw() that should refund me the 10ETH amount.

Metamask shows transaction as success.

But the amount never makes it to my test account. Account balance is still 90ETH.

What am I missing? Why isn't withdrawal working for me?

PS: I do all this using remix.ethereum.org, using same account for contract creation and withdrawal. I also tried it with truffle but i got the same result.


Solution

  • I looked at the video from your comment. This is just a simple example contract on the Solidity site and is not written very well. You have to pay closer attention to the code. withdraw() uses the pendingWithdrawals mapping to determine how much the caller is allowed to transfer out of the contract. Only the becomeRichest() method is setting that mapping. The constructor is not.

    Deploy the contract without sending any ether. Then call becomeRichest(), this time sending in the ether. Then use withdraw (or, improve the code).