Skip to content

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 8 commits into from
Jan 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
"Numberish",
"NUON",
"preact",
"preconfigured",
"quickstarts",
"Sepolia",
"tbody",
Expand Down
4 changes: 2 additions & 2 deletions src/config/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -560,8 +560,8 @@ export const SIDEBAR: Partial<Record<Sections, SectionEntry[]>> = {
url: "chainlink-functions/resources/secrets",
},
{
title: "Functions playground",
url: "chainlink-functions/resources/playground",
title: "Simulate your Functions",
url: "chainlink-functions/resources/simulation",
},
{
title: "Managing Subscriptions",
Expand Down
12 changes: 0 additions & 12 deletions src/content/chainlink-functions/resources/playground.mdx

This file was deleted.

78 changes: 78 additions & 0 deletions src/content/chainlink-functions/resources/simulation.mdx
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.
21 changes: 13 additions & 8 deletions src/features/redirects/redirects.json
Original file line number Diff line number Diff line change
Expand Up @@ -1801,42 +1801,42 @@
"statuscode": 301
},
{
"source": "/chainlink-automation/flexible-upkeeps",
"source": "chainlink-automation/flexible-upkeeps",
"destination": "chainlink-automation/guides/flexible-upkeeps",
"statuscode": 301
},
{
"source": "/chainlink-automation/job-scheduler",
"source": "chainlink-automation/job-scheduler",
"destination": "chainlink-automation/guides/job-scheduler",
"statuscode": 301
},
{
"source": "/chainlink-automation/manage-upkeeps",
"source": "chainlink-automation/manage-upkeeps",
"destination": "chainlink-automation/guides/manage-upkeeps",
"statuscode": 301
},
{
"source": "/chainlink-automation/automation-architecture",
"source": "chainlink-automation/automation-architecture",
"destination": "chainlink-automation/concepts/automation-architecture",
"statuscode": 301
},
{
"source": "/chainlink-automation/automation-concepts",
"source": "chainlink-automation/automation-concepts",
"destination": "chainlink-automation/concepts/automation-concepts",
"statuscode": 301
},
{
"source": "/chainlink-automation/automation-release-notes",
"source": "chainlink-automation/automation-release-notes",
"destination": "chainlink-automation/overview/automation-release-notes",
"statuscode": 301
},
{
"source": "/chainlink-automation/getting-started",
"source": "chainlink-automation/getting-started",
"destination": "chainlink-automation/overview/getting-started",
"statuscode": 301
},
{
"source": "/chainlink-automation/automation-economics",
"source": "chainlink-automation/automation-economics",
"destination": "chainlink-automation/overview/automation-economics",
"statuscode": 301
},
Expand All @@ -1849,6 +1849,11 @@
"source": "ccip/supported-networks/testnet",
"destination": "ccip/supported-networks/v1_2_0/testnet",
"statuscode": 301
},
{
"source": "chainlink-functions/resources/playground",
"destination": "chainlink-functions/resources/simulation",
"statuscode": 301
}
]
}