Skip to content

Commit

Permalink
Functions - Guides with Automation small fixes (#1857)
Browse files Browse the repository at this point in the history
* Update Functions-Automation guides

* Apply suggestions from code review

Co-authored-by: Amine E. <aelmanaa@users.noreply.github.com>

---------

Co-authored-by: Amine E. <aelmanaa@users.noreply.github.com>
  • Loading branch information
khadni and aelmanaa authored Apr 2, 2024
1 parent 5a5b6f7 commit c9feef8
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ After you deploy and set up your contract, Chainlink Automation triggers your fu
Chainlink Functions account.
</Aside>

<ChainlinkFunctions section="prerequisites-guides" />
<ChainlinkFunctions section="prerequisites-guides-with-automation" />

## Tutorial

Expand All @@ -47,36 +47,36 @@ You can locate the scripts used in this tutorial in the [_examples/10-automate-f
npx env-enc set
```

### Deploy an Automated Functions Consumer contract
### Deploy a Functions Consumer contract

<Aside type="caution">
When using Chainlink Automation, developers should be mindful of the risks of Automation attempting to perform upkeeps
indefinitely if a previous upkeep fails to update due to reversion. To learn more, read the [Automation best
practices](/chainlink-automation/concepts/best-practice).
</Aside>

1. Deploy a Functions consumer contract on _Polygon Mumbai_:
Deploy a Functions consumer contract on _Polygon Mumbai_:

1. Open the [CustomAutomatedFunctionsConsumerExample.sol](https://remix.ethereum.org/#url=https://docs.chain.link/samples/ChainlinkFunctions/CustomAutomatedFunctionsConsumerExample.sol) in Remix.

{" "}
1. Open the [CustomAutomatedFunctionsConsumerExample.sol](https://remix.ethereum.org/#url=https://docs.chain.link/samples/ChainlinkFunctions/CustomAutomatedFunctionsConsumerExample.sol) in Remix.

<CodeSample src="samples/ChainlinkFunctions/CustomAutomatedFunctionsConsumerExample.sol" showButtonOnly={true} />

1. Compile the contract.
1. Open MetaMask and select the _Polygon Mumbai_ network.
1. In Remix under the **Deploy & Run Transactions** tab, select _Injected Provider - MetaMask_ in the **Environment** list. Remix will use the MetaMask wallet to communicate with _Polygon Mumbai_.
1. Under the **Deploy** section, fill in the router address for your specific blockchain. You can find this address on the [Supported Networks](/chainlink-functions/supported-networks) page. For _Polygon Mumbai_, the router address is <CopyText text="0x6E2dc0F9DB014aE19888F539E59285D2Ea04244C" code/>.
1. Click the **Deploy** button to deploy the contract. MetaMask prompts you to confirm the transaction. Check the transaction details to make sure you are deploying the contract to _Polygon Mumbai_.
1. After you confirm the transaction, the contract address appears in the Deployed Contracts list. Copy your contract address.
1. Compile the contract.
1. Open MetaMask and select the _Polygon Mumbai_ network.
1. In Remix under the **Deploy & Run Transactions** tab, select _Injected Provider - MetaMask_ in the **Environment** list. Remix will use the MetaMask wallet to communicate with _Polygon Mumbai_.
1. Under the **Deploy** section, fill in the router address for your specific blockchain. You can find this address on the [Supported Networks](/chainlink-functions/supported-networks) page. For _Polygon Mumbai_, the router address is <CopyText text="0x6E2dc0F9DB014aE19888F539E59285D2Ea04244C" code/>.
1. Click the **Deploy** button to deploy the contract. MetaMask prompts you to confirm the transaction. Check the transaction details to make sure you are deploying the contract to _Polygon Mumbai_.
1. After you confirm the transaction, the contract address appears in the Deployed Contracts list. Copy your contract address.

### Add your Consumer contract to your Functions subscription

1. Create a Chainlink Functions subscription and add your contract as an approved consumer contract. **Note**: If you followed the previous tutorials, then you can reuse your existing subscription.
Add your contract as an approved consumer contract to your Functions subscription using the [Chainlink Functions Subscription Manager](https://functions.chain.link/).

### Configure your Automation Consumer contract
### Configure your Consumer contract

Configure the request details by calling the `updateRequest` function. This step stores the encoded request (source code, reference to encrypted secrets if any, arguments), gas limit, subscription ID, and job ID in the contract storage (see [Examine the code](#examine-the-code)). To do so, follow these steps:

1. On a terminal, go to the [_Functions tutorials_ directory](https://github.com/smartcontractkit/smart-contract-examples/tree/main/).
1. On a terminal, change directories to the [_10-automate-functions_](https://github.com/smartcontractkit/smart-contract-examples/tree/main/functions-examples/examples/10-automate-functions) directory.
1. Open [updateRequest.js](https://github.com/smartcontractkit/smart-contract-examples/blob/main/functions-examples/examples/10-automate-functions/updateRequest.js) and replace the consumer contract address and the subscription ID with your own values:

```javascript
Expand All @@ -92,6 +92,7 @@ The consumer contract that you deployed is designed to be used with a **custom l

- Trigger: Custom logic
- Target contract address: The address of the Chainlink Functions consumer contract that you deployed
- Name: Give your upkeep a name
- Check data: Leave this field blank
- Gas limit: <CopyText text="1000000" code/>
- Starting balance (LINK): <CopyText text="1" code/>
Expand Down Expand Up @@ -120,26 +121,26 @@ Click on your upkeep to fetch de details:

On your terminal, run the [readLatest](https://github.com/smartcontractkit/smart-contract-examples/blob/main/functions-examples/examples/10-automate-functions/readLatest.js) to read the latest received response:

1. Open `readLatest.js` and replace the consumer contract address with your own values:
1. Open `readLatest.js` and replace the consumer contract address with your own values:

```javascript
const consumerAddress = "0x5abE77Ba2aE8918bfD96e2e382d5f213f10D39fA" // REPLACE this with your Functions consumer address
```

1. Run the [readLatest](https://github.com/smartcontractkit/smart-contract-examples/blob/main/functions-examples/examples/10-automate-functions/readLatest.js) script.
1. Run the [readLatest](https://github.com/smartcontractkit/smart-contract-examples/blob/main/functions-examples/examples/10-automate-functions/readLatest.js) script.

```shell
node examples/10-automate-functions/readLatest.js
```
```shell
node examples/10-automate-functions/readLatest.js
```

Example:
Output Example:

```text
$ node examples/10-automate-functions/readLatest.js
secp256k1 unavailable, reverting to browser version
Last request ID is 0x310d57a7af34ae4ce565f5745ff46fe2706e96b25b3172ada60cc60f4603b38e
✅ Decoded response to uint256: 2625865n
```
```text
$ node examples/10-automate-functions/readLatest.js
secp256k1 unavailable, reverting to browser version
Last request ID is 0x310d57a7af34ae4ce565f5745ff46fe2706e96b25b3172ada60cc60f4603b38e
✅ Decoded response to uint256: 2625865n
```

### Clean up

Expand Down
Loading

0 comments on commit c9feef8

Please sign in to comment.