Skip to content

Commit

Permalink
small fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
aelmanaa committed Dec 14, 2023
1 parent 280fb29 commit f9730a6
Show file tree
Hide file tree
Showing 11 changed files with 63 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/config/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -542,6 +542,10 @@ export const SIDEBAR: Partial<Record<Sections, SectionEntry[]>> = {
title: "Automate your Functions (Custom Logic Automation)",
url: "chainlink-functions/tutorials/automate-functions-custom-logic",
},
{
title: "Using Imports with Functions",
url: "chainlink-functions/tutorials/importing-packages",
},
],
},
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import { Aside } from "@components"

## Module import support on testnets - 2023-12-15

You can use module imports with Chainlink Functions source code on testnet networks. See the [Using Imports with Functions](/chainlink-functions/tutorials/importing-packages) tutorial to see and example of how to import and use imported modules with your Functions source code. This feature requires the [Functions Toolkit NPM package](https://www.npmjs.com/package/@chainlink/functions-toolkit/v/0.2.7) `v0.2.7` or later.
You can use module imports with Chainlink Functions source code on testnet networks. See the [Using Imports with Functions](/chainlink-functions/tutorials/importing-packages) tutorial to see an example of how to import and use imported modules with your Functions source code. This feature requires the [Functions Toolkit NPM package](https://www.npmjs.com/package/@chainlink/functions-toolkit/v/0.2.7) `v0.2.7` or later.

This feature is available only on testnets. Modules will not import or execute on Functions requests for mainnet networks at this time.

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,9 @@ To run the example:

#### source.js

The Decentralized Oracle Network will run the [JavaScript code](https://github.com/smartcontractkit/smart-contract-examples/blob/main/functions-examples/examples/3-custom-response/source.js). The code is self-explanatory and has comments to help you understand all the steps. **Note**: Functions requests with custom source code can use vanilla [Deno](https://deno.land/) but cannot use any require statements. Import statements and imported modules are supported only on testnets.
The Decentralized Oracle Network will run the [JavaScript code](https://github.com/smartcontractkit/smart-contract-examples/blob/main/functions-examples/examples/3-custom-response/source.js). The code is self-explanatory and has comments to help you understand all the steps.

<ChainlinkFunctions section="deno-importe-notes" />

This JavaScript source code uses [Functions.makeHttpRequest](/chainlink-functions/api-reference/javascript-source#http-requests) to make HTTP requests. To request the `ETH/USD` price, the source code calls the `https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD` URL. If you read the [Functions.makeHttpRequest](/chainlink-functions/api-reference/javascript-source#http-requests) documentation, you see that you must provide the following parameters:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,9 @@ To run the example:

#### source.js

The Decentralized Oracle Network will run the [JavaScript code](https://github.com/smartcontractkit/smart-contract-examples/blob/main/functions-examples/examples/8-multiple-apis/source.js). The code is self-explanatory and has comments to help you understand all the steps. **Note**: Functions requests with custom source code can use vanilla [Deno](https://deno.land/) but cannot use any require statements. Import statements and imported modules are supported only on testnets.
The Decentralized Oracle Network will run the [JavaScript code](https://github.com/smartcontractkit/smart-contract-examples/blob/main/functions-examples/examples/8-multiple-apis/source.js). The code is self-explanatory and has comments to help you understand all the steps.

<ChainlinkFunctions section="deno-importe-notes" />

This JavaScript source code uses [Functions.makeHttpRequest](/chainlink-functions/api-reference/javascript-source#http-requests) to make HTTP requests. The source code fetches the BTC/USD price from different data sources: `https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest/`, `https://api.coingecko.com/api/v3/simple/price`, and `https://api.coinpaprika.com/v1/tickers/btc-bitcoin` and then calculate the median price. you can read the API docs of [CoinMarketCap](https://coinmarketcap.com/api/documentation/v1/), [CoinGecko](https://www.coingecko.com/en/api/documentation), and [CoinPaprika](https://api.coinpaprika.com/) for details.

Expand Down
4 changes: 3 additions & 1 deletion src/content/chainlink-functions/tutorials/api-post-data.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@ To run the example:

#### source.js

The Decentralized Oracle Network will run the [JavaScript code](https://github.com/smartcontractkit/smart-contract-examples/blob/main/functions-examples/examples/4-post-data/source.js). The code is self-explanatory and has comments to help you understand all the steps. **Note**: Functions requests with custom source code can use vanilla [Deno](https://deno.land/) but cannot use any require statements. Import statements and imported modules are supported only on testnets.
The Decentralized Oracle Network will run the [JavaScript code](https://github.com/smartcontractkit/smart-contract-examples/blob/main/functions-examples/examples/4-post-data/source.js). The code is self-explanatory and has comments to help you understand all the steps.

<ChainlinkFunctions section="deno-importe-notes" />

This JavaScript source code uses [Functions.makeHttpRequest](/chainlink-functions/api-reference/javascript-source#http-requests) to make HTTP requests. To request the `JP` country information, the source code calls the `https://countries.trevorblades.com/` URL and provides the query data in the HTTP request body. If you read the [Functions.makeHttpRequest](/chainlink-functions/api-reference/javascript-source#http-requests) documentation, you see that you must provide the following parameters:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@ To run the example:

#### source.js

The Decentralized Oracle Network will run the [JavaScript code](https://github.com/smartcontractkit/smart-contract-examples/blob/main/functions-examples/examples/2-call-api/source.js). The code is self-explanatory and has comments to help you understand all the steps. **Note**: Functions requests with custom source code can use vanilla [Deno](https://deno.land/) but cannot use any require statements. Import statements and imported modules are supported only on testnets.
The Decentralized Oracle Network will run the [JavaScript code](https://github.com/smartcontractkit/smart-contract-examples/blob/main/functions-examples/examples/2-call-api/source.js). The code is self-explanatory and has comments to help you understand all the steps.

<ChainlinkFunctions section="deno-importe-notes" />

This JavaScript source code uses [Functions.makeHttpRequest](/chainlink-functions/api-reference/javascript-source#http-requests) to make HTTP requests. To request the `ETH/USD` price, the source code calls the `https://min-api.cryptocompare.com/data/pricemultifull?fsyms=ETH&tsyms=USD` URL. If you read the [Functions.makeHttpRequest](/chainlink-functions/api-reference/javascript-source#http-requests) documentation, you see that you must provide the following parameters:

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,9 @@ To run the example:

#### source.js

The Decentralized Oracle Network will run the [JavaScript code](https://github.com/smartcontractkit/smart-contract-examples/blob/main/functions-examples/examples/5-use-secrets-threshold/source.js). The code is self-explanatory and has comments to help you understand all the steps. **Note**: Functions requests with custom source code can use vanilla [Deno](https://deno.land/) but cannot use any require statements. Import statements and imported modules are supported only on testnets.
The Decentralized Oracle Network will run the [JavaScript code](https://github.com/smartcontractkit/smart-contract-examples/blob/main/functions-examples/examples/5-use-secrets-threshold/source.js). The code is self-explanatory and has comments to help you understand all the steps.

<ChainlinkFunctions section="deno-importe-notes" />

This JavaScript source code uses [Functions.makeHttpRequest](/chainlink-functions/api-reference/javascript-source#http-requests) to make HTTP requests. To request the `BTC` asset price, the source code calls the `https://pro-api.coinmarketcap.com/v1/cryptocurrency/quotes/latest/` URL. If you read the [Functions.makeHttpRequest](/chainlink-functions/api-reference/javascript-source#http-requests) documentation, you see that you must provide the following parameters:

Expand Down
39 changes: 31 additions & 8 deletions src/content/chainlink-functions/tutorials/importing-packages.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,31 @@ This tutorial demonstrates how to import modules and use them with your Function
- Up to 20 imports total are supported.
- Deno supports [ESM compatible NPM imports](https://docs.deno.com/runtime/manual/node/npm_specifiers) and some [standard Node modules](https://docs.deno.com/runtime/manual/node/node_specifiers). See the [Compatability List](https://docs.deno.com/runtime/manual/node/compatibility) for details.
- Third-party modules are imported at runtime, so import statements must use asynchronous logic like the following examples:
- Importing from `deno.land`: `const { escape } = await import("https://deno.land/std/regexp/mod.ts");`
- ESM-compatible packages: `const { format } = await import("npm:date-fns");`
- Standard Node modules: `const path = await import("node:path");`
- CDN imports: `const lodash = await import("http://cdn.skypack.dev/lodash");`

- Importing from `deno.land`:

```javascript
const { escape } = await import("https://deno.land/std/regexp/mod.ts")
```

- ESM-compatible packages:

```javascript
const { format } = await import("npm:date-fns")
```

- Standard Node modules:

```javascript
const path = await import("node:path")
```

- CDN imports:

```javascript
const lodash = await import("http://cdn.skypack.dev/lodash")
```

- Imported modules abide by all sandbox restrictions and do not have access to the file system, environment variables, or any other Deno permissions.

<Aside type="caution">
Expand Down Expand Up @@ -110,7 +131,9 @@ To run the example:

#### source.js

The Decentralized Oracle Network will run the [JavaScript code](https://github.com/smartcontractkit/smart-contract-examples/blob/main/functions-examples/examples/11-package-imports/source.js). The code is self-explanatory and has comments to help you understand all the steps. **Note**: Functions requests with custom source code can use vanilla [Deno](https://deno.land/) but cannot use any require statements. Import statements and imported modules are supported only on testnets.
The Decentralized Oracle Network will run the [JavaScript code](https://github.com/smartcontractkit/smart-contract-examples/blob/main/functions-examples/examples/11-package-imports/source.js). The code is self-explanatory and has comments to help you understand all the steps.

<ChainlinkFunctions section="deno-importe-notes" />

The example `source.js` file uses an Ethers JSON RPC call to the [`latestRoundData()` function](/data-feeds/api-reference#latestrounddata) of a [Chainlink Data Feed](/data-feeds). The request requires a few modifications to work in the Chainlink Functions environment. For example, the `JsonRpcProvider` class must be modified to handle the request asynchronously.

Expand All @@ -122,7 +145,7 @@ class FunctionsJsonRpcProvider extends ethers.JsonRpcProvider {
super(url)
this.url = url
}
async _send(payload: ethers.JsonRpcPayload | Array<ethers.JsonRpcPayload>): Promise<Array<ethers.JsonRpcResult>> {
async _send(payload) {
let resp = await fetch(this.url, {
method: "POST",
headers: { "Content-Type": "application/json" },
Expand Down Expand Up @@ -162,8 +185,8 @@ The script imports:
The script has two hardcoded values that you have to change using your own Functions consumer contract and subscription ID:

```javascript
const consumerAddress = "0x8dFf78B7EE3128D00E90611FBeD20A71397064D9" // REPLACE this with your Functions consumer address
const subscriptionId = 3 // REPLACE this with your subscription ID
const consumerAddress = "0x91257aa1c6b7f382759c357fbc53c565c80f7fee" // REPLACE this with your Functions consumer address
const subscriptionId = 38 // REPLACE this with your subscription ID
```

The primary function that the script executes is `makeRequestMumbai`. This function can be broken into five main parts:
Expand Down
1 change: 1 addition & 0 deletions src/content/chainlink-functions/tutorials/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,4 @@ import { Aside } from "@components"
- [Encode request data off-chain](/chainlink-functions/tutorials/encode-request-offchain)
- [Automate your Functions (Time-based Automation)](/chainlink-functions/tutorials/automate-functions)
- [Automate your Functions (Custom Logic Automation)](/chainlink-functions/tutorials/automate-functions-custom-logic)
- [Using Imports with Functions](/chainlink-functions/tutorials/importing-packages)
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,20 @@ const functionsConsumer = await Astro.glob("./FunctionsConsumer.mdx")
const automatedFunctionsConsumer = await Astro.glob("./AutomatedFunctionsConsumer.mdx")
const customAutomatedFunctionsConsumer = await Astro.glob("./CustomAutomatedFunctionsConsumer.mdx")
const guidesPrerequisites = await Astro.glob("./GuidesPrerequisites.mdx")
const denoImportNotes = await Astro.glob("./DenoImportNotes.mdx")
const FunctionsConsumerComponent = functionsConsumer[0].Content
const AutomatedFunctionsConsumerComponent = automatedFunctionsConsumer[0].Content
const CustomAutomatedFunctionsConsumerComponent = customAutomatedFunctionsConsumer[0].Content
const GuidesPrerequisitesComponent = guidesPrerequisites[0].Content
const DenoImportNotesComponent = denoImportNotes[0].Content
export type Props = {
section?:
| "functions-consumer"
| "automated-functions-consumer"
| "custom-automated-functions-consumer"
| "prerequisites-guides"
| "deno-importe-notes"
}
const { section } = Astro.props as Props
---
Expand All @@ -25,3 +28,5 @@ const { section } = Astro.props as Props
{section === "custom-automated-functions-consumer" && <CustomAutomatedFunctionsConsumerComponent />}

{section === "prerequisites-guides" && <GuidesPrerequisitesComponent />}

{section === "deno-importe-notes" && <DenoImportNotesComponent />}
6 changes: 6 additions & 0 deletions src/features/chainlink-functions/common/DenoImportNotes.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Aside } from "@components"

<Aside type="note">
Functions requests with custom source code can use vanilla [Deno](https://deno.com/). Import statements and imported
modules are supported only on testnets. You cannot use any require statements on mainnets at the moment.
</Aside>

0 comments on commit f9730a6

Please sign in to comment.