-
Notifications
You must be signed in to change notification settings - Fork 439
improve simulation guide #1711
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
improve simulation guide #1711
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
21344f4
improve simulation guide
aelmanaa 955e23b
Merge branch 'main' into functions/simulator
aelmanaa 1d3a9ea
Apply suggestions from code review
aelmanaa 9783eca
clarifications
aelmanaa b27beba
Merge branch 'main' into functions/simulator
aelmanaa 822add1
clarifications
aelmanaa eba5c35
clarifications
aelmanaa ae70ee4
Merge branch 'main' into functions/simulator
aelmanaa File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,6 +40,7 @@ | |
"Numberish", | ||
"NUON", | ||
"preact", | ||
"preconfigured", | ||
"quickstarts", | ||
"Sepolia", | ||
"tbody", | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
--- | ||
section: chainlinkFunctions | ||
date: Last Modified | ||
title: "Simulate your Functions" | ||
--- | ||
|
||
import { ClickToZoom, Aside } from "@components" | ||
|
||
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 playground](https://functions.chain.link/playground). | ||
|
||
- [Chainlink Functions Hardhat Starter Kit](https://github.com/smartcontractkit/functions-hardhat-starter-kit): Use the `npx hardhat functions-simulate-script` command to simulate your Functions JavaScript source code. | ||
|
||
- [Chainlink Functions Toolkit NPM package](https://github.com/smartcontractkit/functions-toolkit): Import this NPM package into your JavaScript/TypeScript project, then use the `simulateScript` function to simulate your Functions JavaScript source code. | ||
|
||
## Chainlink Functions playground | ||
|
||
<Aside type="caution"> | ||
The Chainlink Functions playground can simulate Chainlink Functions within the browser. There may be differences to | ||
the real Chainlink Functions environment. For instance, you are limited to vanilla JavaScript and cannot use all the | ||
features of Chainlink Functions, such as imports of external modules. To access all the features, use the [Chainlink | ||
Functions Hardhat Starter Kit](https://github.com/smartcontractkit/functions-hardhat-starter-kit) or [Chainlink | ||
Functions Toolkit NPM package](https://github.com/smartcontractkit/functions-toolkit). | ||
</Aside> | ||
|
||
To use the [Chainlink Functions | ||
Playground](https://functions.chain.link/playground), enter any source code, arguments, and secrets you would like to use. Click the **Run code** button to view the output. | ||
|
||
<ClickToZoom src="/images/chainlink-functions/functions-playground.png" /> | ||
|
||
## Chainlink Functions Hardhat Starter Kit | ||
|
||
This repository comes preconfigured with [Hardhat](https://hardhat.org/) and the [Chainlink Functions Toolkit NPM package](https://github.com/smartcontractkit/functions-toolkit), allowing you to quickly get started with Functions. | ||
|
||
To simulate: | ||
|
||
1. In a terminal, clone the [functions-hardhat-starter-kit repository](https://github.com/smartcontractkit/functions-hardhat-starter-kit) and change to the `functions-hardhat-starter-kit` directory. | ||
|
||
```shell | ||
git clone https://github.com/smartcontractkit/functions-hardhat-starter-kit && \ | ||
cd functions-hardhat-starter-kit | ||
``` | ||
|
||
1. Run `npm install` to install the dependencies. | ||
|
||
```shell | ||
npm install | ||
``` | ||
|
||
1. For simulation, you don't need to set up the environment variables. Run `npx hardhat functions-simulate-script` to simulate the [calculation-example.js](https://github.com/smartcontractkit/functions-hardhat-starter-kit/blob/main/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](https://www.investopedia.com/terms/e/eulers-constant.asp) to compute the total amount after interest. | ||
|
||
```shell | ||
npx hardhat functions-simulate-script | ||
``` | ||
|
||
Result: | ||
|
||
```text | ||
secp256k1 unavailable, reverting to browser version | ||
|
||
|
||
Response returned by script during local simulation: 1003757 | ||
``` | ||
|
||
<Aside type="note"> | ||
The JavaScript source code is set up in the | ||
[Functions-request-config.js](https://github.com/smartcontractkit/functions-hardhat-starter-kit/blob/main/Functions-request-config.js) | ||
file. To simulate your own JavaScript source code, modify `source` to replace `./calculation-example.js` with the | ||
relative path to your file. | ||
</Aside> | ||
|
||
## Chainlink Functions Toolkit NPM package | ||
|
||
Follow the [Simple Computation](/chainlink-functions/tutorials/simple-computation) guide to learn how to import the [Chainlink Functions Toolkit NPM package](https://github.com/smartcontractkit/functions-toolkit) into your JavaScript project to simulate and execute a Functions request. | ||
You can read the [Examine the code section](/chainlink-functions/tutorials/simple-computation#javascript-example) for a detailed description of the code example. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.