A tutorial that teaches how to use Chainlink Data Feeds to access real-world data, such as asset prices, directly from your smart contracts on the Base testnet.
This tutorial will guide you through the process of creating a smart contract on Base that utilizes Chainlink Data Feeds to access real-world data, such as asset prices, directly from your smart contracts.
By the end of this tutorial you should be able to do the following:
This tutorial requires you to have Foundry installed.
curl -L https://foundry.paradigm.xyz | bash
foundryup
, to install the latest (nightly) build of FoundryFor more information, see the Foundry Book installation guide.
In order to deploy a smart contract, you will first need a wallet. You can create a wallet by downloading the Coinbase Wallet browser extension.
Deploying contracts to the blockchain requires a gas fee. Therefore, you will need to fund your wallet with ETH to cover those gas fees.
For this tutorial, you will be deploying a contract to the Base Goerli test network. You can fund your wallet with Base Goerli ETH using one of the faucets listed on the Base Network Faucets page.
Accurate price data is essential in DeFi applications. However, blockchain networks lack the capability to directly fetch external real-world data, leading to the “Oracle Problem”.
Chainlink Data Feeds offer a solution to this problem by serving as a secure middleware layer that bridges the gap between real-world asset prices and onchain smart contracts.
Before you can begin writing smart contracts for Base and consuming Chainlink data feeds, you need to set up your development environment by creating a Foundry project.
To create a new Foundry project, first create a new directory:
Then run:
This will create a Foundry project, which has the following basic layout:
To use Chainlink’s data feeds within your project, you need to install Chainlink smart contracts as a project dependency using forge install
.
To install Chainlink smart contracts, run:
Once installed, update your foundry.toml
file by appending the following line:
Once your project has been created and dependencies have been installed, you can now start writing a smart contract.
The Solidity code below defines a smart contract named DataConsumerV3
. The code uses the AggregatorV3Interface
interface from the Chainlink contracts library to provide access to price feed data.
The smart contract passes an address to AggregatorV3Interface
. This address (0xcD2A119bD1F7DF95d706DE6F2057fDD45A0503E2
) corresponds to the ETH/USD
price feed on the Base Goerli network.
Chainlink provides a number of price feeds for Base. For a list of available price feeds on Base, visit the Chainlink documentation.
In your project, add the code provided above to a new file named src/DataConsumerV3.sol
, and delete the src/Counter.sol
contract that was generated with the project. (you can also delete the test/Counter.t.sol
and script/Counter.s.sol
files).
To compile the new smart contract, run:
Before you can deploy your smart contract to the Base network, you will need to set up a wallet to be used as the deployer.
To do so, you can use the cast wallet import
command to import the private key of the wallet into Foundry’s securely encrypted keystore:
After running the command above, you will be prompted to enter your private key, as well as a password for signing transactions.
For instructions on how to get your private key from Coinbase Wallet, visit the Coinbase Wallet documentation.
It is critical that you do NOT commit this to a public repo.
To confirm that the wallet was imported as the deployer
account in your Foundry project, run:
To setup your environment for deploying to the Base network, create an .env
file in the home directory of your project, and add the RPC URL for the Base Goerli testnet:
Once the .env
file has been created, run the following command to load the environment variables in the current command line session:
With your contract compiled and environment setup, you are ready to deploy the smart contract to the Base Goerli Testnet!
For deploying a single smart contract using Foundry, you can use the forge create
command. The command requires you to specify the smart contract you want to deploy, an RPC URL of the network you want to deploy to, and the account you want to deploy with.
To deploy the DataConsumerV3
smart contract to the Base Goerli test network, run the following command:
When prompted, enter the password that you set earlier, when you imported your wallet’s private key.
Your wallet must be funded with ETH on the Base Goerli Testnet to cover the gas fees associated with the smart contract deployment. Otherwise, the deployment will fail.
To get testnet ETH for Base Goerli, see the prerequisites.
After running the command above, the contract will be deployed on the Base Goerli test network. You can view the deployment status and contract by using a block explorer.
Foundry provides the cast
command-line tool that can be used to interact with the smart contract that was deployed and call the getLatestPrice()
function to fetch the latest price of ETH.
To call the getLatestPrice()
function of the smart contract, run:
You should receive the latest ETH / USD
price in hexadecimal form.
Congratulations! You have successfully deployed and interacted with a smart contract that consumes a Chainlink price feed on the Base blockchain network.
To learn more about Oracles and using Chainlink to access real-world data within your smart contracts on Base, check out the following resources: