Replies: 1 comment 1 reply
-
Hello @n4n0b1t3 Wow you really went deep on this, I would like to add some anotations.
|
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
One thing in advance. I thought for a while about sharing this article at all. Most of you will probably grab your head and say, 'that's logical'. For me, it wasn't and I had to chew through it step by step to understand how we came to this conclusion. This article is for all of you who, like me, are baffled that you can add 18 zeros to a 2600$ value without getting filthy rich.
This article is about the function getEntranceFee() which appears a few times in the Youtube course "Solidity, Blockchain, and Smart Contract Course - Beginner to Expert Python Tutorial" by Patrick Collins. The GitHub repository can be found here: https://github.com/smartcontractkit/full-blockchain-solidity-course-py
I have a habit in lessons like this when I already understand the basics, of developing the code of a lesson myself before it is explained. This helps me to see which parts I haven't understood yet, and it helps me to improve my memory of the syntax. In the process, I keep coming across parts of the code where it makes sense for me to write down each step in prose before writing a line of code. In this article, I go into the calculation that is necessary to determine whether the transferred value in WEI corresponds to the given price in USD.
Planning
When will this function be used?
A player enters the lottery by sending the entrance fee in WEI.
What is my objective?
Return the minimum amount in WEI the player needs to pay in order to participate.
What is the high-level process?
Step one is easy: The entrance fee is set by me in the constructor as a static variable.
set up the data feed to get the current exchange rate.
Where to find the address for the Ethereum Data Feeds?
https://docs.chain.link/docs/ethereum-addresses/
What value will be returned, let's get a feel for it.
OK, today is the 24. February 2022, how does 269782000000 relate to the current ETH / USD exchange rate?
What kind of value even is this? It is not USD, it is not WEI ...
The importance of decimals.
When you copy the data feed address, you will find the information of DEC in the column next to it, for this data feed a DEC value of 8 is displayed. Here is my understanding: It tells me how many decimal places this number has, in other words: In our case with 8 decimals I find the position of the decimal point by counting from the last digit of the returned value 8 digits backward.
So this gives me 2697.82000000
https://coinmarketcap.com/currencies/ethereum/ gives me $2,682.11 per one Ethereum. Nice, this is pretty close considering the enormous fluctuation of cryptocurrencies.
using the exchange rate
Before we continue let's make an everyday comparison. Say I am on a business trip in Shanghai and I want to buy a nice Huawei Phone. At the time of writing 1 EUR = 7.11161 CNY (Chinese Yen). The phone is priced at 2100 CNY. I would just need to divide the CNY price by the exchange rate and arrive at the price in EUR.
2100 / 7.11161 = 295,29 EUR.
The only difference in our calculation is that I have to use WEI and I am just not used to dealing with large numbers.
Adding a lot of zeros
Let's assume our lottery ticket price would be 120 USD. According to our example above we would simply have to divide it by the exchange rate to arrive at the price in ETH. However, our exchange rate and our ticket price are currently not comparable, one is in USD and one is our strange exchange rate return value from chainlink.
Let's lift them both to the same level so we can start using them. Here is a short explanation of the logic behind it.
Like one dollar can be divided by 100 cents, one Ethereum can be divided by a Quintillion WEI. The scientific notation would be 1e18, so a 1 followed by 18 zeros. So if you have on one side of the equation a value in ETH and on the other side a value in USD and want to convert ETH to WEI, you need to do this on both sides so the equation stays the same.
ETH 1 = 2682 USD
WEI 1e18 = USD-WEI 2682 * 1e18
We notice that the large number USD-WEI is the equivalent of 2682 USD.
Let's do this in solidity.
First, we get the exchange rate thingy with the 8 decimals from our chainlink oracle price feed, named here ethUsdPriceFeed.
(, int256 price, , , ) = ethUsdPriceFeed.latestRoundData();
Let's check this out in the brownie console:
As you can see, the second value in the list returned is our desired exchange rate. Solidity allows us to extract only this value with the syntax seen above. So with the first solidity line, we get in the variable
price
which contains our rate thingy with the 8 decimals.Let's produce from this price our exchange rate.
uint256 exchangeRate= uint256(price) * 10**10;
We are adding here 10 zeros to the price increasing the 8 existing decimals to 18 thus becoming the comparable USD-WEI equivalent. This becomes now our usable exchange rate.
Remember this is neither WEI, nor USD, nor ETH, this is something else we created but which is comparable to WEI.
The next line of code is combining two things
In the constructor the value entryFeeInUSD is initiated like so:
entryFeeInUsdWei = 120 * (10**18);
In our getEntranceFee() function the following calculation appears.
uint256 entryFeeInWEI = (entryFeeInUsdWei * 10**18) / exchangeRate;
Here is the reasoning:
The calculated value x relates to 1 ETH like the entrance fee of 120 USD to our exchange rate of 2698 USD. Converting ETH to WEI and USD to a USD-WEI equivalent.
This equals
To double-check this, I find it quite convenient to use the brownie console for calculations like so:
The result 44477390659747968 can be converted to ETH with https://eth-converter.com/ and the Ethereum equivalent can be checked at any marketplace, for example, coinbase https://www.coinbase.com/converter/eth/usd
At the time of writing 0,044 ETH is exactly 120 USD.
just for fun, here are the large numbers:
The final code
python examples
Here are some test snippets I used while writing this article. I find it interesting how complicated some steps are when what you want to achieve is so straightforward. Keep in mind that the price feed was fluctuating during the writing, and the values are not always the same use above.
Let's try to convert the 8 decimal USD 247223226886 into a 18 decimal USD-WEI equivalent.
The first thing I tried was:
247223226886 * 10e10
It is 10 to the power of 10 and not 18 because the chainlink return value already has 8 of the 18 decimals. Get it?
Let's try this with Decimal
WTF?
more googling ...
Beta Was this translation helpful? Give feedback.
All reactions