I was following a tutorial on building a simple Solidity program for Ethereum transactions between a smart contract and account. However, I didn't understand the following code:-
balanceReceived[msg.sender].payments[balanceReceived[msg.sender].numPayments] = payment;
Here's the full code:-
pragma solidity >=0.5.11 <0.7.0;
contract MappingsStructExample {
struct Payment {
uint amount;
uint timestamps;
}
struct Balance {
uint totalBalance;
uint numPayments;
mapping (uint => Payment) payments;
}
mapping(address => Balance)public balanceReceived;
function getBalance() public view returns(uint) {
return address(this).balance;
}
function sendMoney() public payable {
balanceReceived[msg.sender].totalBalance += msg.value;
Payment memory payment = Payment(msg.value, now);//this stores
balanceReceived[msg.sender].payments[balanceReceived[msg.sender].numPayments] = payment;
balanceReceived[msg.sender].numPayments++;
}
function withdrawMoney(address payable _to, uint _amount) public {
require(_amount <= balanceReceived[msg.sender].totalBalance, "not enough funds");
balanceReceived[msg.sender].totalBalance -= _amount;
_to.transfer(_amount);
}
function withdrawAllMoney(address payable _to) public {
uint balanceToSend = balanceReceived[msg.sender].totalBalance;
balanceReceived[msg.sender].totalBalance = 0;
_to.transfer(balanceToSend);
}
}
If I were to remove the code, the program won't have an impact. The program will still keep track of the payments made from the account as well as keep track of the amount being sent. Therefore, is it important to keep the code?