Simulate your Functions
Before making a Chainlink Functions request from your smart contract, it is always a good practice to simulate the source code off-chain to make any adjustments or corrections.
Currently, there are several options for simulating a request:
-
Chainlink Functions Hardhat Starter Kit: Use the
npx hardhat functions-simulate-scriptcommand to simulate your Functions JavaScript source code. -
Chainlink Functions Toolkit NPM package: Import this NPM package into your JavaScript/TypeScript project, then use the
simulateScriptfunction to simulate your Functions JavaScript source code.
Chainlink Functions playground
To use the Chainlink Functions Playground, enter any source code, arguments, and secrets you would like to use. Click the Run code button to view the output.
Chainlink Functions Hardhat Starter Kit
This repository comes preconfigured with Hardhat and the Chainlink Functions Toolkit NPM package, allowing you to quickly get started with Functions.
To simulate:
-
In a terminal, clone the functions-hardhat-starter-kit repository and change to the
functions-hardhat-starter-kitdirectory.git clone https://github.com/smartcontractkit/functions-hardhat-starter-kit && \ cd functions-hardhat-starter-kit -
Run
npm installto install the dependencies.npm install -
For simulation, you don't need to set up the environment variables. Run
npx hardhat functions-simulate-scriptto simulate the calculation-example.js JavaScript source code. Note: This example calculates the continuously compounding interest for a given principal amount over one month. It takes the principal amount and annual percentage yield (APY) as input arguments and uses Euler's number to compute the total amount after interest.npx hardhat functions-simulate-scriptResult:
secp256k1 unavailable, reverting to browser version Response returned by script during local simulation: 1003757
Chainlink Functions Toolkit NPM package
Follow the Simple Computation guide to learn how to import the Chainlink Functions Toolkit NPM package into your JavaScript project to simulate and execute a Functions request. You can read the Examine the code section for a detailed description of the code example.
Note: You can also import and use the Chainlink Functions Toolkit NPM package as a scripting dependency inside Remix IDE scripts in your browser.