Data Feeds
In this section we will learn about Chainlink Data Feeds. Here are some reference resources on Data feeds.
Last updated
In this section we will learn about Chainlink Data Feeds. Here are some reference resources on Data feeds.
Last updated
In this exercise we will be creating the TokenShop Smart Contract where other people will be able to buy our newly created token.
Open the REMIX IDE and open the second Icon titled - FILE EXPLORER
Name your new file TokenShop.sol
Below is the code for the Tokenshop.sol
Deploying the TokenShop Contract
Go to the Deploy & Run Transactions tab within the Remix IDE
You want to make sure you select the TokenShop.sol Contract (not the TokenInterface called TokenShop.sol) from the contract field in the deployment tab. You will receive an error if you choose the TokenShop's Interface file.
After it's deployed to Sepolia, and you see the transaction details in Remix's console sub-window, copy your Token.sol Contract address from the Deployed Contracts UI in Remix
Paste your Token.sol Contract (which you deployed in the previous section) address as the TOKENADDRESS deployment parameter in the deploy tab
Confirm your transaction in Metamask
Giving Token Shop Token-minting rights
On the Token.Sol dropdown in the deployed contracts section, you are looking for the MINTER_ROLE
property. Click it to read the data from your smart contract. It should be “0x9f2df0fed2c77648de5860a4cc508cd0818c85b8b8a1ab4ceeef8d981c8956a6”
For your knowledge it is the hash of the word “MINTER_ROLE”
Next, you are going to run paste that MINTER_ROLE hash string into the grantRole() function
along with the TokenShop Address:
role
: 0x9f2df0fed2c77648de5860a4cc508cd0818c85b8b8a1ab4ceeef8d981c8956a6
account
: "Your tokenshop address"
Doing this will authorize your TokenShop to mint your newly created token.
We will now double-check and confirm that your TokenShop has indeed been authorized
In your Token.sol Dropdown menu you will want to find the hasRole
function. You will open this up and see that it requires two parameters. These are as follows
role
: 0x9f2df0fed2c77648de5860a4cc508cd0818c85b8b8a1ab4ceeef8d981c8956a6
account
: "your TokenShop address"
We are expecting a boolean response of true
Now we are going to use the Chainlink USD/ETH price feed that we have referenced inside our TokenShop Contract.
Go to your TokenShop.sol deployed contract dropdown and find the getChainlinkDataFeedLatestAnswer()
function. You can hover your mouse over the buttons to see the full name : it should show something like this
Notice the integer returned is quite large, that is because solidity does handle decimal points. Note that you need to know how many decimal points a given feed has. You can find this data in the Price Feeds Documentation (make sure "Show More Details" is checked)
The integer we got back needs to be converted. Since the ETH/USD price feed's data has 8 decimal places, we can see that the price as per the screenshot is $3125.42553378 (divide the returned value by 10^8 to add the decimals back in).
Buying tokens on metamask
Open your Metamask and send 0.01 ETH to your TokenShop.sol Address
You can check your wallet's token balance on Metamask & through Remix IDE in the Token.sol Deployed Contracts UI.
Token Shop Exercise - In this exercise we will be creating the TokenShop.sol Contract where other people will be able to buy our newly created token.