Blog Details

  • Home
  • Ethereum: Create/set “subId” using createSubscription() and get “subId” vrfCoordinatorV2_5Mock call via javascript in hardhat environment
Roha Khan February 1, 2025 0 Comments

I can provide an article based on the given specification. Please note that I assume that the createSubscription(), getSubId() functions are already implemented and available in the JavaScript code.

Ethereum: Creating and Configuring Sub-Identities with VRF Coordinators

Ethereum: Create/set

In this article, we will look at how to create and configure sub-identities using VRF (Verifiable Random Function) coordinators in a hardhat environment.

Overview of the Hardhat Environment

Hardhat is a popular Ethereum development framework that allows us to build and deploy smart contracts on the Ethereum network. It provides an easy-to-use interface for interacting with the Ethereum blockchain, making it ideal for rapid prototyping and development.

Creating and Configuring Subids with VRF Coordinators

To create subids with hardhat VRF Coordinators, we need to do the following:

  • Import the required dependencies
  • Configure our network configuration
  • Create an instance of “VRFCoordinator”
  • Call the “createSubscription()” function and pass it as an argument to the “setSubId” method
  • Get the subid with the “getSubId” method

Hardhat Deployment Script (deploy.js)

const { network, ethers } = require("@nomiclab/hardhat");

// Configure our network configuration

const networks = {

hardhat: {

chainid: 1337,

// Add additional network configurations as needed

},

};

// Import the VRF Coordinator library

const vrfCoordinatorV2_5Mock = require("./vrfCoordinatorV2_5Mock");

async function main() {

const { getChainId } = await ethers.getContractFactory("yourcontractname");

const contractInstance = await getChainId();

// Create a subscription instance using the VRF Coordinator

const subscriptionInstance = await vrfCoordinatorV2_5Mock.createSubscription({

chainId: contractInstance.chainId,

provider: network.provider,

});

// Set the subId using the createSubscription() function

await subscriptionInstance.setSubId("your-subId");

console.log(await vrfCoordinatorV2_5Mock.getSubId());

// Get the subid using the getSubId method

const subid = await vrfCoordinatorV2_5Mock.getSubId();

console.log(subid);

}

main();

In this script, we first import the necessary dependencies and set up our network configuration. Then, we create an instance of “VRFCoordinator” with VRF Coordinator version 2.5.

Next, we create a subscription instance using the “createSubscription()” function and pass it as an argument to the “setSubId” method. This sets the support for the specified contract.

Next, we log the subid using the “getSubId” method and get the subid by calling the “getSubId” function directly on the VRF Coordinator instance.

Note: Replace “yourcontractname” with the actual name of your Ethereum contract and replace “yoursubid” with the desired subid value. This script assumes that a VRF Coordinator version 2.5 model implementation is available.

Leave Comment