1

I am going through a tutorial at the moment and have just covered the basics of mappings and structs which I feel like I understand. However, in the next section of the tute we put together mappings and structs and I become a bit lost in the section I bolded ** **. Would someone be able to very slowly take me through the logic so I might be able to better understand what's happening?

        uint amount;
        uint timestamp;
    }

    struct Balance {
        uint totalBalance;
        uint numDeposits;
        mapping(uint => Transaction) deposits;
        uint numWithdrawals;
        mapping(uint => Transaction) withdrawals;
    }

    mapping(address => Balance) public balanceReceived;


    function getBalance(address _addr) public view returns(uint) {
        return balanceReceived[_addr].totalBalance;
    }

    function depositMoney() public payable {
        balanceReceived[msg.sender].totalBalance += msg.value;

        **Transaction memory deposit = Transaction(msg.value, block.timestamp);
        balanceReceived[msg.sender].deposits[balanceReceived[msg.sender].numDeposits] = deposit;**
        balanceReceived[msg.sender].numDeposits++;
    } ```

1 Answer 1

1

That function can be refactored as follows:

function depositMoney() public payable {
        balanceReceived[msg.sender].totalBalance += msg.value;

      // Creates an instace of `Transaction` struct with `amount` = msg.value and `timestamp` = block.timestamp.
      // A code liket his is also possible: `Transaction memory deposit = Transaction({amount: msg.value, timestamp: block.timestamp});`
      Transaction memory deposit = Transaction(msg.value, block.timestamp);
      
      // Get the Balance struct instance in the `balanceReceived` mapping under the `msg.sender` address.
      Balance storage balance = balanceReceived[msg.sender];

      // Assign the new `deposit` Transaction struct instance to this `balance.deposits` mapping.
      balance.deposits[balance.numDeposits] = deposit;

      // Increase the `numDeposits` so next time when we do `balance.deposits[balance.numDeposits] = deposit;` as the line of code above,
      // we set a new 'deposite' to a new `numDeposits`.
      balance.numDeposits++;

    }

The line of code like:

Transaction memory deposit = Transaction(msg.value, block.timestamp);

is just creating a Transaction struct instance in memory with the msg.value and block.timestamp. It could also be done more explicit like this:

Transaction memory deposit = Transaction({amount: msg.value, timestamp: block.timestamp});

Line balanceReceived[msg.sender].deposits[balanceReceived[msg.sender].numDeposits] = deposit; is really doing a bunch of things that could be refactored to:

// Get the Balance struct instance in the `balanceReceived` mapping under the `msg.sender` address.
Balance storage balance = balanceReceived[msg.sender];

// Assign the new `deposit` Transaction struct instance to this `balance.deposits` mapping.
balance.deposits[balance.numDeposits] = deposit;

As you can see, it simply gets the Balance struct instace of the msg.sender from the balanceReceived mapping to update it.

It assigns the new Transaction deposit instance to the deposits mapping inside the Balance struct instance, where the key is the value of numDeposits, which is a property that is increased everytime you add a new deposit so next time you can save another deposit in a difference 'index'.

And then finally it is increasing the numDeposits property:

balance.numDeposits++;

That's it.

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.