# Data Feeds

{% embed url="<https://docs.chain.link/docs/using-chainlink-reference-contracts>" %}
Documentation - Chainlink Market and Data Feeds
{% endembed %}

{% embed url="<https://docs.chain.link/docs/reference-contracts>" %}
Chainlink Data Feeds Contract Addresses
{% endembed %}

{% embed url="<https://docs.chain.link/data-feeds/price-feeds/addresses?network=ethereum&page=1#sepolia-testnet>" %}
Data Feed Contract addresses from the Sepolia Test Network
{% endembed %}

***

### Token Shop Exercise

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

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FHS6wwEVtWeDbB0ir7Hjk%2FScreenshot%202024-04-30%20at%202.04.57%E2%80%AFAM.png?alt=media&#x26;token=d96fa40f-0588-4e93-8a79-d6f67ed5b08d" alt=""><figcaption><p>Example of the File Explorer</p></figcaption></figure>

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FImQ11vRnDfjHaQEcSomP%2FScreenshot%202024-04-30%20at%202.05.30%E2%80%AFAM.png?alt=media&#x26;token=0a7303b6-fc4e-4d90-89f6-1954cfa3b738" alt=""><figcaption><p>Example of creating a new File</p></figcaption></figure>

Name your new file TokenShop.sol

Below is the code for the Tokenshop.sol

```solidity
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

// Deploy this contract on Sepolia

import "@chainlink/contracts/src/v0.8/interfaces/AggregatorV3Interface.sol";

interface TokenInterface {
    function mint(address account, uint256 amount) external;
}

contract TokenShop {
    
	AggregatorV3Interface internal priceFeed;
	TokenInterface public minter;
	uint256 public tokenPrice = 200; //1 token = 2.00 usd, with 2 decimal places
	address public owner;
    
	constructor(address tokenAddress) {
    	minter = TokenInterface(tokenAddress);
        /**
        * Network: Sepolia
        * Aggregator: ETH/USD
        * Address: 0x694AA1769357215DE4FAC081bf1f309aDC325306
        */
        priceFeed = AggregatorV3Interface(0x694AA1769357215DE4FAC081bf1f309aDC325306);
        owner = msg.sender;
	}

	/**
 	* Returns the latest answer
 	*/
	function getChainlinkDataFeedLatestAnswer() public view returns (int) {
    	(
        	/*uint80 roundID*/,
        	int price,
        	/*uint startedAt*/,
        	/*uint timeStamp*/,
        	/*uint80 answeredInRound*/
    	) = priceFeed.latestRoundData();
    	return price;
	}

	function tokenAmount(uint256 amountETH) public view returns (uint256) {
    	//Sent amountETH, how many usd I have
    	uint256 ethUsd = uint256(getChainlinkDataFeedLatestAnswer());		//with 8 decimal places
    	uint256 amountUSD = amountETH * ethUsd / 10**18; //ETH = 18 decimal places
    	uint256 amountToken = amountUSD / tokenPrice / 10**(8/2);  //8 decimal places from ETHUSD / 2 decimal places from token 
    	return amountToken;
	} 

	receive() external payable {
    	uint256 amountToken = tokenAmount(msg.value);
    	minter.mint(msg.sender, amountToken);
	}

    modifier onlyOwner() {
        require(msg.sender == owner);
        _;
    }

    function withdraw() external onlyOwner {
        payable(owner).transfer(address(this).balance);
    }    
}
```

{% embed url="<https://github.com/solangegueiros/chainlink-bootcamp-2024/blob/main/TokenShop.sol>" %}
TokenShop.sol Source Code
{% endembed %}

***

Deploying the TokenShop Contract

Go to the Deploy & Run Transactions tab within the Remix IDE

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FMTZPIhs8Kw1SXWHlVyPH%2FScreenshot%202024-04-30%20at%202.23.59%E2%80%AFAM.png?alt=media&#x26;token=e4153218-6bfe-4658-a7ff-2d60e5955a3a" alt=""><figcaption><p>This is an example of what your Deploy &#x26; Run Transactions should look like</p></figcaption></figure>

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.

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2F692vzIbBT0yidFlcCh6b%2FScreenshot%202024-04-30%20at%202.25.37%E2%80%AFAM.png?alt=media&#x26;token=87c0c8a2-d33c-46d2-aa16-e3928b51ae66" alt=""><figcaption><p>IMPORTANT: SELECT THE TOKENSHOP</p></figcaption></figure>

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

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FnRsDKqw0KquGWA6gGs4O%2FScreenshot%202024-04-30%20at%202.27.03%E2%80%AFAM.png?alt=media&#x26;token=a57b4725-8035-45d5-9f84-c0723a5a2e67" alt=""><figcaption><p>Example of how to copy your Token.sol Contract address</p></figcaption></figure>

Paste your Token.sol Contract (which you deployed in the previous section) address as the TOKENADDRESS deployment parameter in the deploy tab&#x20;

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FAU4VRzenS0V2qi3asVXd%2FScreenshot%202024-04-30%20at%202.28.03%E2%80%AFAM.png?alt=media&#x26;token=204d594e-4bee-4eaf-aa9c-57f78738361e" alt=""><figcaption><p>Example of token address being included in TokenShop.sol for Deployment</p></figcaption></figure>

Confirm your transaction in Metamask

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FxOgA0FsQu1QyjFzFq7hZ%2FScreenshot%202024-04-30%20at%202.29.54%E2%80%AFAM.png?alt=media&#x26;token=c7cb2753-52d1-4c9a-9a60-393282d19734" alt=""><figcaption><p>Example MetaMask Transaction Confirmation</p></figcaption></figure>

***

&#x20;

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”

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2Fnmi7jSOx7ToKCIKpPbJ6%2FScreenshot%202024-04-30%20at%202.33.14%E2%80%AFAM.png?alt=media&#x26;token=896339ea-df2a-4f8c-93c2-f6214c824d94" alt=""><figcaption><p>MINTER_ROLE Call</p></figcaption></figure>

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"

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FZFQ43crIg65sIIRJjoIU%2FScreenshot%202024-04-30%20at%202.34.21%E2%80%AFAM.png?alt=media&#x26;token=fa17baea-1dca-4447-9b38-28e0c2539adb" alt=""><figcaption><p>Example of grantRole</p></figcaption></figure>

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FnP4hgcKRMwopLbMzVU2J%2FScreenshot%202024-04-30%20at%202.35.43%E2%80%AFAM.png?alt=media&#x26;token=df211760-6349-46a5-8d63-ec46812f9f62" alt=""><figcaption><p>This is an example of how to copy your TokenShop Contract Address for the account: parameter in last image</p></figcaption></figure>

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"

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FhM74NAj0CzO29aMijBiK%2FScreenshot%202024-04-30%20at%202.40.17%E2%80%AFAM.png?alt=media&#x26;token=235ec4c1-c9ec-42c7-b6da-4e83567f6fa5" alt=""><figcaption><p>Example of the hasRole call with fields inputted</p></figcaption></figure>

We are expecting a boolean response of `true`

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FR4nAROottFyqgsvWt74x%2FScreenshot%202024-04-30%20at%202.40.57%E2%80%AFAM.png?alt=media&#x26;token=35d9652a-1075-440a-86ae-a7175199fcde" alt=""><figcaption><p>Example of completed confirmation of authorization</p></figcaption></figure>

***

### Getting Price Data from Chainlink Price Feeds

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&#x20;

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FTmhNHysl8bFl589CIHZN%2FScreenshot%202024-04-30%20at%202.42.55%E2%80%AFAM.png?alt=media&#x26;token=5d11b0fe-a00e-4021-a2c1-2adb49fe5758" alt=""><figcaption><p>Example of getChainlinkDataFeedLatestAnswer</p></figcaption></figure>

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2Fok68QZO6Z2t4jQ9NaJES%2FScreenshot%202024-04-30%20at%202.43.23%E2%80%AFAM.png?alt=media&#x26;token=2cd809a2-a32f-4cd4-b2a5-8db603ea24c7" alt=""><figcaption><p>This is an example of a return value from clicking the getChainlinkDataFeedLatestAnswer</p></figcaption></figure>

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](https://docs.chain.link/data-feeds/price-feeds/addresses?network=ethereum\&page=1#sepolia-testnet) (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](https://docs.chain.link/data-feeds/price-feeds/addresses?network=ethereum\&page=1#sepolia-testnet), 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).&#x20;

***

Buying tokens on metamask

Open your Metamask and send 0.01 ETH to your TokenShop.sol Address

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2F1X4zdkqttWmUD59EKAz4%2FScreenshot%202024-04-30%20at%202.50.18%E2%80%AFAM.png?alt=media&#x26;token=81a5ffa3-3a2c-49ce-af9d-8e55d604e01b" alt=""><figcaption><p>The address at the top and beginning with 0xD1 is my TokenShop.sol Address, The amount I chose was .01 ETH</p></figcaption></figure>

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FgKW94LINKEfcj89v7njM%2FScreenshot%202024-04-30%20at%202.51.27%E2%80%AFAM.png?alt=media&#x26;token=5ff56b0a-7a08-4995-b436-5dd9b3c4dccc" alt=""><figcaption><p>Confirm the transaction</p></figcaption></figure>

You can check your wallet's token balance on Metamask & through Remix IDE in the Token.sol Deployed Contracts UI.&#x20;

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FAJ4ctlBUjSChVS58JHAb%2FScreenshot%202024-04-30%20at%202.53.00%E2%80%AFAM.png?alt=media&#x26;token=f770a057-d441-400d-a3e6-4389eb900c2f" alt=""><figcaption><p>What it looks like in my imported Tokens list in Metamask</p></figcaption></figure>

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2Ffhzm0ikJTKXieMfjHhbe%2FScreenshot%202024-04-30%20at%202.53.37%E2%80%AFAM.png?alt=media&#x26;token=f0cf8a7f-aeb8-4ad3-9911-8d5ee67eebc7" alt=""><figcaption><p>Using REMIX IDE - balanceOf call in the Token.sol dropdown menu</p></figcaption></figure>

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.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://cll-devrel.gitbook.io/bootcamp-2024/3.-oracles-erc20-and-chainlink-data-feeds/data-feeds.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
