Oracle

This tutorial will guide you through the process of creating a smart contract on Glide that utilizes Chainlink Data Feeds to access real-world data, such as asset prices, directly from your smart contracts.

Objectives

By the end of this tutorial you should be able to do the following:

  • Set up a smart contract project for Glide using Foundry

  • Install the Chainlink smart contracts

  • Consume a Chainlink price data feed within your smart contract

  • Deploy and test your smart contracts on Glide

Prerequisites

Foundry

This tutorial requires you to have Foundry installed.

  • From the command-line (terminal), run: curl -L https://foundry.paradigm.xyz | bash

  • Then run foundryup, to install the latest (nightly) build of Foundry

For more information, see the Foundry Book installation guide.

Wallet funds

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 Glide network. You can fund your wallet with GLDR using one of the faucets listed on the Glide 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.


Creating a project

Before you can begin writing smart contracts for Glide 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:

mkdir myproject

Then run:

cd myproject
forge init

This will create a Foundry project, which has the following basic layout:

.
├── foundry.toml
├── script
│   └── Counter.s.sol
├── src
│   └── Counter.sol
└── test
    └── Counter.t.sol

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:

forge install smartcontractkit/chainlink --no-commit

Once installed, update your foundry.toml file by appending the following line:

remappings = ['@chainlink/contracts/=lib/chainlink/contracts']

Writing and compiling the Smart Contract

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 example on the Glide network.

   // SPDX-License-Identifier: MIT
   pragma solidity ^0.8.0;

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

   contract DataConsumerV3 {
       AggregatorV3Interface internal priceFeed;

      /**
       * Network: Glide
       * Aggregator: ETH/USD
       * Address: 0xcD2A119bD1F7DF95d706DE6F2057fDD45A0503E2
       */
       constructor() {
           priceFeed = AggregatorV3Interface(0xcD2A119bD1F7DF95d706DE6F2057fDD45A0503E2);
       }

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

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:

forge build     

Deploying the smart contract

Setting up your wallet as the deployer

Before you can deploy your smart contract to the Glide 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:

cast wallet import deployer --interactive

After running the command above, you will be prompted to enter your private key, as well as a password for signing transactions.

To confirm that the wallet was imported as the deployer account in your Foundry project, run:

cast wallet list

Setting up environment variables for Glide

To setup your environment for deploying to the Glide network, create an .env file in the home directory of your project, and add the RPC URL for the Glide:

GLIDE_GOERLI_RPC="https://rpc-api.glideprotocol.xyz"

Once the .env file has been created, run the following command to load the environment variables in the current command line session:

source .env

Deploying the smart contract to Glide

With your contract compiled and environment setup, you are ready to deploy the smart contract to the Glide!

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 Glide, run the following command:

forge create ./src/DataConsumerV3.sol:DataConsumerV3 --rpc-url $GLIDE_RPC --account deployer

When prompted, enter the password that you set earlier, when you imported your wallet's private key.

Interacting with the Smart Contract

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:

cast call <DEPLOYED_ADDRESS> --rpc-url $GLIDE_RPC "getLatestPrice()"

You should receive the latest ETH / USD price in hexadecimal form.

Conclusion

Congratulations! You have successfully deployed and interacted with a smart contract that consumes a Chainlink price feed on the Glide blockchain network.

To learn more about Oracles and using Chainlink to access real-world data within your smart contracts on Glide, check out the following resources:

Last updated