Skip to content

Commit

Permalink
Chainlink Functions NPM import: mainnet support and explain _send ove…
Browse files Browse the repository at this point in the history
…rride in the tutorial (#1713)

* explain _send override

* explain _send override

* fix disclaimer

* fix disclaimer

* support imports on mainnet
  • Loading branch information
aelmanaa authored Jan 12, 2024
1 parent ac035af commit a185c22
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 6 deletions.
4 changes: 4 additions & 0 deletions src/content/chainlink-functions/resources/release-notes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ title: "Chainlink Functions Release Notes"

import { Aside } from "@components"

## Module imports supported on mainnet - 2024-01-12

- You can use external module imports with Chainlink Functions source code on mainnet 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.

## Arbitrum Mainnet support - 2024-01-10

Chainlink Functions is available on [Arbitrum Mainnet](/chainlink-functions/supported-networks#arbitrum-mainnet).
Expand Down
15 changes: 9 additions & 6 deletions src/content/chainlink-functions/tutorials/importing-packages.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,11 @@ This tutorial demonstrates how to import modules and use them with your Function
- 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">
Using module imports as part of your Functions source code is supported only on testnets at this time. Running the same code on mainnet will not succeed.

Users are fully responsible for any code that they import. Chainlink Functions provides no guarantees of the validity, availability, or security of any code that a user chooses to import. For any use cases seeking to secure value, fully vet any dependencies that are being used or completely avoid importing dependencies to avoid risk from a compromised dependency or repository.

Users are fully responsible for any dependencies their JavaScript source code imports. Chainlink is not responsible
for any imported dependencies and provides no guarantees of the validity, availability or security of any libraries a
user chooses to import or the repositories from which these dependencies are downloaded. Developers are advised to
fully vet any imported dependencies or avoid dependencies altogether to avoid any risks associated with a compromised
library or a compromised repository from which the dependency is downloaded.
</Aside>

<ChainlinkFunctions section="prerequisites-guides" />
Expand Down Expand Up @@ -135,7 +136,9 @@ The Decentralized Oracle Network will run the [JavaScript code](https://github.c

<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.
The example `source.js` file uses a 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 inherited to override the JsonRpcProvider [`_send` method](https://docs.ethers.org/v6/api/providers/jsonrpc/#JsonRpcProvider). This customization is necessary because Deno does not natively support Node.js modules like [http](https://nodejs.org/api/http.html) or [https](https://nodejs.org/api/https.html). We override the `_send` method to use the [fetch API](https://deno.land/[email protected]?s=fetch), which is the standard way to make HTTP(s) requests in Deno. **Note**: The `url` passed in the constructor is the URL of the JSON RPC provider.

```javascript
// Chainlink Functions compatible Ethers JSON RPC provider class
Expand All @@ -156,7 +159,7 @@ class FunctionsJsonRpcProvider extends ethers.JsonRpcProvider {
}
```

After the class is extended, you can initialize the provider object and await the response.
After the class is extended, you can initialize the provider object with the `RPC_URL` and await the response.

```javascript
const provider = new FunctionsJsonRpcProvider(RPC_URL)
Expand Down
3 changes: 3 additions & 0 deletions src/features/chainlink-functions/common/DenoImportNotes.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@ 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.

It is important to understand that importing an NPM package into Deno does not automatically ensure full compatibility. Deno and Node.js have distinct architectures and module systems. While some NPM packages might function without issues, others may need modifications or overrides, especially those relying on Node.js-specific APIs or features Deno does not support.

</Aside>

1 comment on commit a185c22

@vercel
Copy link

@vercel vercel bot commented on a185c22 Jan 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.