Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add balance function wipes out the previous balance. [Step 8 - withdraw, transfer] #2

Open
srapper142 opened this issue Nov 14, 2021 · 0 comments

Comments

@srapper142
Copy link

Just going through the tutorial and noticed that when adding funds to an account with an existing balance, the new amount replaces the old amount. In a banking model I would expect that after adding funds to the account the new balance would be the original plus the amount added.

Tutorial Step 8 -
https://openquest.xyz/?github_url=https://raw.githubusercontent.com/CreatorOS/Building-a-bank-with-Solidity-that-isnt-a-toy-For-beginners/main
withdraw, transfer uses sample code https://remix.ethereum.org/#version=soljson-v0.8.4+commit.c7e474f2.js&optimize=false&runs=200&gist=a89ad8b401426cb74656979d6dd6e58a&evmVersion=null

subquest-8b.sol

   function addBalance() public payable {
        balances[msg.sender] = msg.value;
        totalContractBalance = totalContractBalance + msg.value;
        depositTimestamps[msg.sender] = block.timestamp;
    }

I think modified as follows makes more sense,

    function addBalance() public payable {
+       balances[msg.sender] = balances[msg.sender] + msg.value;
-        balances[msg.sender] = msg.value;
        totalContractBalance = totalContractBalance + msg.value;
        depositTimestamps[msg.sender] = block.timestamp;
    }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant