Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions data-feeds/getting-started/hardhat/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
node_modules
.env
.env.enc
/build

# Hardhat files
/cache
/artifacts

# TypeChain files
/typechain
/typechain-types

# solidity-coverage files
/coverage
/coverage.json
3 changes: 3 additions & 0 deletions data-feeds/getting-started/hardhat/.npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
hardhat.config.js
scripts
test
11 changes: 11 additions & 0 deletions data-feeds/getting-started/hardhat/.prettierignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
node_modules
artifacts
cache
coverage*
gasReporterOutput.json
img
.env
.*
README.md
coverage.json
deployments
20 changes: 20 additions & 0 deletions data-feeds/getting-started/hardhat/.prettierrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"tabWidth": 4,
"useTabs": false,
"semi": false,
"singleQuote": false,
"printWidth": 100,
"overrides": [
{
"files": "*.sol",
"options": {
"printWidth": 80,
"tabWidth": 4,
"useTabs": false,
"singleQuote": false,
"bracketSpacing": false,
"explicitTypes": "always"
}
}
]
}
22 changes: 22 additions & 0 deletions data-feeds/getting-started/hardhat/.solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
{
"extends": "solhint:recommended",
"plugins": ["prettier", "chainlink-solidity"],
"rules": {
"prettier/prettier": "error",
"compiler-version": ["error", ">=0.4.24"],
"const-name-snakecase": "off",
"constructor-syntax": "error",
"var-name-mixedcase": "off",
"func-named-parameters": "off",
"immutable-vars-naming": "off",
"no-inline-assembly": "off",
"no-unused-import": "error",
"func-visibility": ["error", { "ignoreConstructors": true }],
"quotes": ["error", "double"],
"reason-string": ["warn", { "maxLength": 64 }],
"chainlink-solidity/prefix-internal-functions-with-underscore": "warn",
"chainlink-solidity/prefix-private-functions-with-underscore": "warn",
"chainlink-solidity/prefix-storage-variables-with-s-underscore": "warn",
"chainlink-solidity/prefix-immutable-variables-with-i": "warn"
}
}
1 change: 1 addition & 0 deletions data-feeds/getting-started/hardhat/.solhintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules
98 changes: 98 additions & 0 deletions data-feeds/getting-started/hardhat/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
# Getting Started with Data Feeds

You can use Chainlink Data Feeds to connect your smart contracts to asset pricing data like the [ETH / USD feed](https://data.chain.link/feeds/ethereum/mainnet/eth-usd). These data feeds use data aggregated from many independent Chainlink node operators. Each price feed has an onchain address and functions that enable contracts to read pricing data from that address.

## Before you begin

This guide uses the [Hardhat](https://hardhat.org/) development environment to deploy and interact with the contracts. To learn more about Hardhat, read the [Hardhat Documentation](https://hardhat.org/hardhat-runner/docs/getting-started).

### Requirements

- **Git**: Make sure you have Git installed. You can check your current version by running <CopyText text="git --version" code/> in your terminal and download the latest version from the official [Git website](https://git-scm.com/book/en/v2/Getting-Started-Installing-Git) if necessary.
- **Nodejs** and **npm**: [Install the latest release of Node.js 20](https://nodejs.org/en/download/). Optionally, you can use the nvm package to switch between Node.js versions with `nvm use 20`. To ensure you are running the correct version in a terminal, type `node -v`.
```bash
$ node -v
v20.11.0
```
- **Testnet funds**: This guide requires testnet ETH on Ethereum Sepolia. If necessary, go to [faucets.chain.link](https://faucets.chain.link/sepolia) and get testnet ETH on Ethereum Sepolia.

## Examine the sample contract

This [example contract](https://github.com/smartcontractkit/smart-contract-examples/blob/main/data-feeds/getting-started/hardhat/contracts/DataConsumerV3.sol) obtains the latest price answer from the [BTC / USD feed](https://docs.chain.link/data-feeds/price-feeds/addresses) on the Sepolia testnet, but you can modify it to read any of the different [Types of Data Feeds](https://docs.chain.link/data-feeds#types-of-data-feeds).

The contract has the following components:

- The `import` line imports an interface named `AggregatorV3Interface`. Interfaces define functions without their implementation, which leaves inheriting contracts to define the actual implementation themselves. In this case, `AggregatorV3Interface` defines that all v3 Aggregators have the function `latestRoundData`. You can [see the complete code](https://github.com/smartcontractkit/chainlink/blob/master/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol) for the `AggregatorV3Interface` on GitHub.

- The `constructor() {}` initializes an interface object named `dataFeed` that uses `AggregatorV3Interface` and connects specifically to a proxy aggregator contract that is already deployed at `0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43`. The interface allows your contract to run functions on that deployed aggregator contract.

- The `getChainlinkDataFeedLatestAnswer()` function calls your `dataFeed` object and runs the `latestRoundData()` function. When you deploy the contract, it initializes the `dataFeed` object to point to the aggregator at `0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43`, which is the proxy address for the Sepolia BTC / USD data feed. Your contract connects to that address and executes the function. The aggregator connects with several oracle nodes and aggregates the pricing data from those nodes. The response from the aggregator includes several variables, but `getChainlinkDataFeedLatestAnswer()` returns only the `answer` variable.

## Tutorial

### Setup

1. Clone the repository that contains the Hardhat project setup for this guide. This repository contains the Solidity contract and the Hardhat configuration files you need to deploy and interact with the contract.

```bash
git clone https://github.com/smartcontractkit/smart-contract-examples.git
cd data-feeds/getting-started/hardhat
```

1. Install all the dependencies:

```bash
npm install
```

1. Set an encryption password for your environment variables. This password needs to be set each time you create or restart a terminal shell session.

```bash
npx env-enc set-pw
```

1. Set the required environment variables using the following command:

```bash
npx env-enc set
```

- `PRIVATE_KEY`: The private key for your testnet wallet that will deploy and interact with the contracts. If you use MetaMask, follow the instructions to [Export a Private Key](https://support.metamask.io/hc/en-us/articles/360015289632-How-to-export-an-account-s-private-key).
- `ETHEREUM_SEPOLIA_RPC_URL`: The Remote Procedure Call (RPC) URL for the Ethereum Sepolia network. You can obtain one by creating an account on [Alchemy](https://www.alchemy.com/) or [Infura](https://www.infura.io/) and setting up an Ethereum Sepolia project.

### Deploy the `DataConsumerV3` contract

Execute the following command to deploy the `DataConsumerV3` contract on the Ethereum Sepolia testnet:

```bash
npx hardhat deployDataConsumerV3 --network ethereumSepolia
```

After a few seconds, the transaction completes. Expect output similar to the following in your terminal:

```bash
ℹ Compiling contracts...
Compiled 2 Solidity files successfully (evm target: paris).
ℹ Starting deployment of DataConsumerV3 with account: 0x45C90FBb5acC1a5c156a401B56Fea55e69E7669d
✔ DataConsumerV3 deployed at: 0xcbEAC520915727e2cf242feA77EEEEEb319A43bB on ethereumSepolia
```

Save the deployed contract address. You will use this address later.

### Get the latest answer from the aggregator contract

Execute the following command to get the latest answer from the aggregator contract:

```bash
npx hardhat getLatestAnswer --data-consumer-v3 <DataConsumerV3Address> --network ethereumSepolia
```

Replace `<DataConsumerV3Address>` with the address of the `DataConsumerV3` contract you saved earlier.

Expect output similar to the following in your terminal:

```bash
✔ Latest BTC / USD Data Feed answer: 6292416053902
```

In this example, the answer is the latest price. The returned answer is an integer, so it is missing its decimal point. You can find the correct number of decimal places for this answer on the [Price Feed addresses](https://docs.chain.link/data-feeds/price-feeds/addresses) page by clicking the **Show more details** checkbox. The answer on the BTC / USD feed uses 8 decimal places, so an answer of `6292416053902` indicates a BTC / USD price of `62924.16053902`. Each feed uses a different number of decimal places for answers.
50 changes: 50 additions & 0 deletions data-feeds/getting-started/hardhat/contracts/DataConsumerV3.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.7;

import {AggregatorV3Interface} from "@chainlink/contracts/src/v0.8/shared/interfaces/AggregatorV3Interface.sol";

/**
* THIS IS AN EXAMPLE CONTRACT THAT USES HARDCODED
* VALUES FOR CLARITY.
* THIS IS AN EXAMPLE CONTRACT THAT USES UN-AUDITED CODE.
* DO NOT USE THIS CODE IN PRODUCTION.
*/

/**
* If you are reading data feeds on L2 networks, you must
* check the latest answer from the L2 Sequencer Uptime
* Feed to ensure that the data is accurate in the event
* of an L2 sequencer outage. See the
* https://docs.chain.link/data-feeds/l2-sequencer-feeds
* page for details.
*/
contract DataConsumerV3 {
AggregatorV3Interface internal dataFeed;

/**
* Network: Sepolia
* Aggregator: BTC/USD
* Address: 0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43
*/
constructor() {
dataFeed = AggregatorV3Interface(0x1b44F3514812d835EB1BDB0acB33d3fA3351Ee43);
}

/**
* Returns the latest answer.
*/
function getChainlinkDataFeedLatestAnswer() public view returns (int256) {
// prettier-ignore
(
/* uint80 roundID */
,
int256 answer,
/*uint startedAt*/
,
/*uint timeStamp*/
,
/*uint80 answeredInRound*/
) = dataFeed.latestRoundData();
return answer;
}
}
38 changes: 38 additions & 0 deletions data-feeds/getting-started/hardhat/hardhat.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
require("@chainlink/env-enc").config()
require("@nomicfoundation/hardhat-toolbox")
require("./tasks")

const PRIVATE_KEY = process.env.PRIVATE_KEY || ""

const COMPILER_SETTINGS = {
optimizer: {
enabled: true,
runs: 1000000,
},
metadata: {
bytecodeHash: "none",
},
}

module.exports = {
solidity: {
compilers: [
{
version: "0.8.19",
settings: COMPILER_SETTINGS,
},
],
},
networks: {
ethereumSepolia: {
url: process.env.ETHEREUM_SEPOLIA_RPC_URL || "UNSET",
accounts: [PRIVATE_KEY],
chainId: 11155111,
},
},
paths: {
sources: "./contracts",
cache: "./build/cache",
artifacts: "./build/artifacts",
},
}
Empty file.
Loading