Search code examples
ethereumsolidity

No values being returned


Exploring blockchain using Ethereum and solidity via Remix and I encounter this code:


pragma solidity ^0.8.1;

contract AccountsDemo{
    
    address public whoDeposited;
    uint public depositAmt;
    uint public accountBalance;
    
    
    function deposit() public payable{
        
        whoDeposited = msg.sender;
        depositAmt = msg.value;
        
        accountBalance += address(this).balance 
    }
}

According to the book I'm using, calling deposit should result in accountBalance and depositAmt being increased. When I try, nothing happens. The transactions are recorded on the blockchain but no increase happens. The book uses an older version of solidity - 0.6.0 not sure if this is the cause of my frustration.

Any idea what I'm missing here?


Solution

  • What your current deposit function does:

    1. whoDeposited = msg.sender;

      Stores the sender address to property whoDeposited. Mind that the value of whoDeposited is overwritten every time you execute the deposit() function.

    2. depositAmt = msg.value;

      Stores the transaction value to property depositAmt. Mind that the value of depositAmt is overwritten every time you execute the deposit() function.

    3. accountBalance += address(this).balance

      Increments value of property accountBalance by current contract balance. The current contract balance (address(this).balance) already reflects the transaction value that was send while executing the deposit() function.

      So if your contract already had balance of 10 wei, accountBalance was 10, and now you were sending 2 wei... During the execution, address(this).balance is now 12 (because of the 2 wei that you're sending), and then accountBalance += 12, which is a result you don't want.

      By the way, your snippet has a syntax error and it wouldn't compile because of the missing semicolon at the end of line.


    To achieve your goal - store the current contract balance in both depositAmt and accountBalance (so they are always going to have the same value, also same as address(this).balance), you can do this:

    depositAmt += msg.value;
    accountBalance = address(this).balance;
    

    I switched the += and = operators. So now, when you send a value to your deposit() function:

    1. whoDeposited = msg.sender;

      The behavior stays the same as described above.

    2. depositAmt += msg.value;

      Every time you execute the deposit() function, the value of depositAmt increments by the value of the transaction. Which effectively reflects the current balance of your contract.

    3. accountBalance = address(this).balance;

      Every time you execute the deposit() function, the value of accountBalance is set to the current contract balance. The current contract balance already reflects the value you sent in the current transaction.


    Note: I used the word "always" few times in this answer. It's not really always - there are some edge cases like funding your contract address from a selfdestruct function or prior to the contract deployment, where the address(this).balance is going to be different from your depositAmt and accountBalance. But most likely, you're not going to meet these edge cases in the beginning, so you can consider it "always" for now. It's just good to know that there are cases where this snippet won't work as expected.