NEW

CCIP is now live for all developers. See what's new.

Back

Chainlink Functions Demo App

Learn how to build applications that use Chainlink Functions by running your own version of the Chainlink Functions Demo App.

Overview

The Chainlink Functions Demo App is designed to run on the Mumbai testnet (Polygon). It uses Chainlink Functions. The functionality allows users to donate MATIC to their favorite GitHub creators. Authors of those repositories can then claim their donations. Donations are made in an amount of MATIC per amount of Stars the repository has.

Chainlink Functions is used to determine the total donation amount by multiplying the MATIC amount by the star count. There's no back-end involved in the whole donation process.

Objective

You will create and manage a ledger contract to be used by the dApp. Tracking interaction between accounts gives a better insight into how the dApp functions. Using a different wallet for contract creation and dApp usage is preferable.

Before you begin

Before you start this tutorial, complete the following items:

  • If you are new to smart contract development, learn how to Deploy Your First Smart Contract.
  • Set up a cryptocurrency wallet such as MetaMask.
  • To deploy this contract on testnets, ensure the deployer account has testnet ERC-677 LINK. Use the LINK faucet to retrieve testnet LINK.
  • Install Node.js.
  • Install pnpm: npm install -g pnpm
  • Optional: If you want to verify your contracts onchain, create an account on Polygonscan and get an API key. Note that you'll need to create an account for the main network, which also works for the testnet.

Steps to implement

Run these from the project directory where you've cloned this repo.

  1. Clone the repo and change directories:

    git clone https://github.com/smartcontractkit/chainlink-functions-demo-app.git && cd chainlink-functions-demo-app
    
  2. Install the dependencies using pnpm:

    pnpm install
    
  3. Create a .env file from the template:

    cp .env.example .env
    
  4. In the .env file specify the following variables:

    • PRIVATE_KEY - Private key of your wallet used for deploying contracts.
    • NEXT_PUBLIC_GA_TRACKING_ID - Optional - Set to your Google Analytics tracking id to enable GA.
    • POLYGONSCAN_API_KEY - Optional - API key for Polygonscan to verify and read contracts.
  5. Generate and build all required files:

    pnpm build
    
  6. Deploy the Ledger contract:

    npx hardhat project:deploy
    
  7. The deployment script prints the deployed contract address to your terminal. Add the address in the NEXT_PUBLIC_CONTRACT_ADDRESS environment variable to your .env file.

  8. Optionally, you can verify the contract. This allows you to decode the bytecode on Polygonscan. Verify the contract with npx hardhat verify:

    npx hardhat verify --constructor-args arguments.js $NEXT_PUBLIC_CONTRACT_ADDRESS
    

    Replace $NEXT_PUBLIC_CONTRACT_ADDRESS with your contract address if you don't have the address in your shell environment.

  9. Create a Chainlink Functions subscription and fund it using the project:fund script:

    npx hardhat project:fund
    

    For more information on how to manage your subscription, read the official documentation.

  10. Store the subscription ID in the NEXT_PUBLIC_SUBSCRIPTION_ID environment variable in your .env file.

Run the example

  1. Run the dev server:

    pnpm dev
    
  2. In the terminal output, you'll see a URL with a port number. Open this URL in your browser to see the UI running locally.

Stay updated on the latest Chainlink news