full-blockchain-solidity-course-js icon indicating copy to clipboard operation
full-blockchain-solidity-course-js copied to clipboard

addressToAmoundFunded[msg.sender] = msg.value; is changed into some typo in the video by was not shown through the demo

Open mmuaaz opened this issue 3 years ago • 4 comments

Lesson

Lesson 4

Could you please leave a link to the timestamp in the video where this error occurs? (You can right click a video and "copy video URL at current time")

No response

Operating System

Windows

Describe the bug

addressToAmoundFunded[msg.sender] = msg.value; this code was shown to be the one working and made sense but in the rest of the video I was able to spot the same code changed into addressToAmoundFunded[msg.sender] += msg.value; the later code is also working but I dont get it, its not shown in the video why that + is here, so I had to rewatch almost 30 min of the video like 2 times and still not understand why it is here and whether it should be here or not, but it works the same.

mmuaaz avatar Jul 18 '22 21:07 mmuaaz

Ok so at this point in time the code is addressToAmoundFunded[msg.sender] = msg.value; and I right click the video link at current time for anyone to view: https://youtu.be/gyMwXuJrbJQ?t=15826 at this point https://youtu.be/gyMwXuJrbJQ?t=16464 the code is same and right after 15 seconds

https://youtu.be/gyMwXuJrbJQ?t=16474 at this point, the code is changed to addressToAmoundFunded[msg.sender] += msg.value;, that plus sign is confusing

mmuaaz avatar Jul 19 '22 19:07 mmuaaz

At some point he commented on the need to account for this situation but did not provide the solution. I guess he fixed it in the repo code. In C-like languages the += means 'add to whatever was already there'. The first time an address sends us eth addressToAmoundFunded[msg.sender] += msg.value; will create a new entry in the mapping. I believe new entries are initialized to zero, and the +=will immediately add msg.value to it.
If the same address sends us some eth a second time, then the entry on the mapping already exists with some value and we add msg.value to what's already there.

AVDel7 avatar Jul 20 '22 12:07 AVDel7

the explanation make sense, but you are guessing i think that it was may be due to C-like language logic as you said you believe new entries are initialized to zero, may be we need a proper guidance on this one still???

mmuaaz avatar Jul 24 '22 17:07 mmuaaz

"The concept of “undefined” or “null” values does not exist in Solidity, but newly declared variables always have a default value dependent on its type."

  • https://docs.soliditylang.org/en/v0.8.14/types.html

"The default value for the uint or int types is 0."

  • https://docs.soliditylang.org/en/v0.8.14/control-structures.html#default-value

"Mapping is a reference type as arrays and structs. Following is the syntax to declare a mapping type." "mapping(_KeyType => _ValueType)"

  • https://www.tutorialspoint.com/solidity/solidity_mappings.htm

addressToAmoundFunded[msg.sender] += msg.value

With the provided information let's dissect the example above that there is some confusion about. addressToAmoundFunded is a mapping data type.

mapping(address => uint256) public addressToAmountFunded;

This means that addressToAmoundFunded has two data types that it references. The msg.sender the address part of addressToAmoundFunded and msg.value is the uint256 part. Since we know that uint256 has a default value of 0, it makes sense to shorten the code and just use addressToAmoundFunded[msg.sender] += msg.value. The += just means to add what's left of the sign what's to the right of it and assign the result of that to the variable.

addressToAmoundFunded[msg.sender] += msg.value is just a shorter way of writing addressToAmoundFunded[msg.sender] = msg.value + addressToAmoundFunded[msg.sender].

I think I went over everything. Let me know if you need anymore clarification about anything else.

cxiong22 avatar Jul 29 '22 02:07 cxiong22

Is this solved? If so, please close the issue. :)

anannyenaik avatar Sep 15 '22 04:09 anannyenaik