Lesson 3: Fund Me Higher Level Discussion #1186
-
I just finished the first part of the FundMe Contract and while I follow along the code I'm a bit unclear on the math. (Solidity not using decimals is one of many things I find difficult to grasp coming from other languages). There's a lot of multiplying and dividing that to me don't seem well explained. Is everything in Solidity expressed in 'WEI' bits. Why is the USD value of ETH expressed in so many digits and am I supposed to know to divide by 8 to get there somehow? I'm enjoying the tutorials and the coding but this lack of clarity to me on the size of numbers and some sort of default value (and best how to deal with it) is a bit frustrating. I mean I get the 'using the calculator' to verify but how do you know? To me it's a pretty important concept I have to get before moving forward. |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 6 replies
-
When you make function fund() we set minimumUSD to 50$ and multiply it with 10^18, hence converting it according to wei. This is because when we use getConversionRate we also enter the value in wei and hence we get ethAmountinUSD in terms of wei according to the eth price in dollars and we compare it with 50$ using require. TLDR: In getConversionRate function we enter the value in wei and hence in return we get out dollar value multiplied with 10^18 (once put something in wei in getConversionRate) hence comparing it 50*10**18 |
Beta Was this translation helpful? Give feedback.
-
Are you saying input matches output? If in get conversion rate I entered 1ETH instead of 1WEI it would have been in ETH? THe USD price would be 2956.56056820 - with decimals, without decimals it's obviously a larger number but how do we know we have to divide by 10**8 to get that value...I sort of get the functionality but I don't understand the base logic between the return (the whole no decimcals thing in solidity seems overly complicated, is there a logic behind that?) |
Beta Was this translation helpful? Give feedback.
-
Hello @JohnEMagee well this is a tricky question, let's consider the following:
|
Beta Was this translation helpful? Give feedback.
-
Hi @JohnEMagee , I recently wrote a lengthy post exactly about this topic. Please let me know if this was helpful. #1153 |
Beta Was this translation helpful? Give feedback.
-
Thanks for everyones help the math part of it makes sense to me now but I realize there's a piece I was missing deeper that maybe I missed in the lecture. If i enter (currently) 10000 WEI the fund will reject it as too low,. If I enter 10000 GWEI, it gets rejected as too low. However, if I enter .03 ETH it will accept it. This is the variable msg.value. But for this to work msg.value must have a 'default' currency. Is msg.value automatically converted to a standard value (i'm assuming WEI/GWEI probably). That might be the key piece I was missing, if the msg.value is always standardized , (logically it seems WEI or GWEI since you can't do .03 ETH as a value) then it would make sense to me. I hope I'm right about this...and if I am right, is this covered in the solidity docs and I just haven't read them yet? |
Beta Was this translation helpful? Give feedback.
Hi @JohnEMagee , I recently wrote a lengthy post exactly about this topic. Please let me know if this was helpful. #1153