npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

hardhat-chainlink

v0.2.4

Published

Integrates Chainlink into Hardhat projects

Downloads

25

Readme

Hardhat Chainlink Plugin

Integrates Chainlink into Hardhat projects.

What

This plugin will help you to use the Chainlink protocol inside your tests, scripts & tasks. This is a community initiative, so everyone is welcome to contribute. Start by opening a "Feature Request" issue.

Installation

npm install hardhat-chainlink

# or

yarn add hardhat-chainlink

Import the plugin in your hardhat.config.js:

require("hardhat-chainlink");

Or, if you are using TypeScript, in your hardhat.config.ts:

import "hardhat-chainlink";

Environment extensions

This plugin extends the Hardhat Runtime Environment by adding a chainlink field whose type is ExampleHardhatRuntimeEnvironmentField.

Usage

Full documentation is available at DOCUMENTATION.md. Here are a couple of examples of how to use the plugin.

Example 1 - Get the latest price of ETH

To get the latest price of a given asset, prepare the network field inside hardhat.config file. To add the Goerli Testnet for example, type:

  networks: {
    goerli: {
      url: GOERLI_RPC_URL,
      accounts: [PRIVATE_KEY]
    }
  }

Then import chainlink from hardhat, call the getLatestPrice function and pass it the Aggreagator contract address:

import { chainlink } from "hardhat";

const ethUsdGoerliAggregator: string = `0xD4a33860578De61DBAbDc8BFdb98FD742fA7028e`;
const ethPrice = await chainlink.getLatestPrice(ethUsdGoerliAggregator);

Make sure that your network points to the correct one (Goerli in this example), and double-check that provided aggregator address is correct. For example, if you are writing your tests with the forking feature enabled or developing scripts and tasks on a Goerli network, in which case you will run the above code snippet like this:

npx hardhat run scripts/myScript.ts --network goerli

Example 2 - Create and Manage VRF Subscriptions inside the deployment script

Start by creating VRFv2Consumer.sol contract, which you can get from the Official Chainlink Documentation.

Prepare hardhat.config file for deployment on the Goerli network, for example:

  networks: {
    goerli: {
      url: GOERLI_RPC_URL,
      accounts: [PRIVATE_KEY]
    }
  }

Usually, you will create and manage your subscriptions on the VRF Subscription Management page, but with the plugin, you can automate that process.

Expand your deployment script to include the following:

  • Creating new VRF subscriptions
  • Funding VRF subscriptions (Make sure to claim LINKs from the faucet)
  • Adding new VRF consumers
  • And optionally,
    • Getting details about VRF subscriptions
    • Checking if there is a pending outgoing VRF request
    • Removing VRF consumers
    • Canceling VRF subscriptions
// scripts/deploy.ts
import { chainlink, ethers } from "hardhat";

async function main() {
  // NOTE: If you already have an active VRF Subscription, proceed to step 3

  // Step 1: Create a new VRF Subscription
  const vrfCoordinatorAddress = `0x2Ca8E0C643bDe4C2E08ab1fA0da3401AdAD7734D`;
  const { subscriptionId } = await chainlink.createVrfSubscription(
    vrfCoordinatorAddress
  );

  // Step 2: Fund VRF Subscription
  const linkTokenAddress = `0x326C977E6efc84E512bB9C30f76E30c160eD06FB`;
  const amountInJuels = ethers.BigNumber.from(`1000000000000000000`); // 1 LINK
  await chainlink.fundVrfSubscription(
    vrfCoordinatorAddress,
    linkTokenAddress,
    amountInJuels,
    subscriptionId
  );

  // Step 3: Deploy your smart contract
  const VRFv2ConsumerFactory = await ethers.getContractFactory("VRFv2Consumer");
  const VRFv2Consumer = await VRFv2ConsumerFactory.deploy(subscriptionId);
  await VRFv2Consumer.deployed();
  console.log("VRFv2Consumer deployed to:", VRFv2Consumer.address);

  // Step 4: Add VRF Consumer contract to your VRF Subscription
  await chainlink.addVrfConsumer(
    vrfCoordinatorAddress,
    VRFv2Consumer.address,
    subscriptionId
  );
}

main().catch((error) => {
  console.error(error);
  process.exitCode = 1;
});

Run the deployment script by typing:

npx hardhat run scripts/deploy.ts --network goerli

Example 3 - Run Chainlink node on Hardhat network

To run a Chainlink Node on the Hardhat network, you will need to follow a couple of steps.

Step 1

Spin up the Hardhat network by running the following command:

npx hardhat node

Step 2

Open, and if necessary, install Docker Desktop.

Step 3

Spin up a Chainlink node using the following command, which will set up some env variables. Please do not start it from Docker Desktop.

npx hardhat chainlink:run-node

Note

IMPORTANT!

Each time this command runs, it will remove all containers and re-create them (before running docker compose up, we first run docker compose down)

This behavior is analogous to the hardhat EVM node losing all previous history each time it is restarted.

If you want to restart, only pass an additional true parameter (restartOnly) like this npx hardhat chainlink:run-node true

If you visit http://127.0.0.1:6688 in your browser, you should see the Chainlink node login page displayed.

You can use the following credentials to log in to your local Chainlink node:

To see the available tasks to interact with your node, run:

npx hardhat