> For the complete documentation index, see [llms.txt](https://cll-devrel.gitbook.io/bootcamp-2024/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://cll-devrel.gitbook.io/bootcamp-2024/9.-off-chain-data-with-chainlink-functions/remix.md).

# Remix

Ethereum's Online IDE

{% embed url="<https://remix.ethereum.org/>" fullWidth="false" %}

### Connect MetaMask to Remix

In Remix, Go to the "DEPLOY & RUN TRANSACTIONS" icon on left side:

<div align="left"><figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2Fvo6MVk1wlVGPEjueiwUc%2Ffunctions42.png?alt=media&amp;token=372d5c7e-dba0-40fe-bb14-76af3f5dc6bc" alt=""><figcaption><p>DEPLOY &#x26; RUN TRANSACTIONS ICON</p></figcaption></figure></div>

In the “ENVIRONMENT” drop-down menu, select “Injected Provider - MetaMask”:

<div align="left"><figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FuMTrLwlQM9xXO4FYL6xF%2Ffunctions43.png?alt=media&amp;token=952c183c-1210-4823-bc04-632b4e8225fb" alt=""><figcaption><p>Select Injected Provider - MetaMask</p></figcaption></figure></div>

Make sure you are on Custom (43113) network, which is Avalanche Fuji Testnet:

<div align="left"><figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FnNdXJftZvze0EhP99K6J%2Ffunctions44.png?alt=media&amp;token=d7c0fdb9-c129-48b6-9f1b-f82c84a18933" alt=""><figcaption><p>Connected to (43113) - Avalanche Fuji Testnet</p></figcaption></figure></div>

***

### Creating your Smart Contract

Create a smart contract called `GettingStartedFunctionsConsumer.sol`. First, go to the 1. “FILE EXPLORER” icon and then click the 2. “Create new file” icon:

<div align="left"><figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FzglhzZ6DPYf0mYoKIGPt%2Fremix6.png?alt=media&amp;token=b7307b70-9e7c-4e20-bd15-f9600f552ef2" alt=""><figcaption><p>Create a new Solidity file</p></figcaption></figure></div>

Name the new Solidity file `GettingStartedFunctionsConsumer.sol` and a new tab will open in Remix:

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2F8CedlVrglCw1YOE9xPq6%2Ffunctions12.png?alt=media&amp;token=9fe78ec1-a244-4398-b9eb-52b5f5ab8102" alt=""><figcaption><p>Blank Solidity file</p></figcaption></figure>

***

### Adding Code&#x20;

Go to [Getting Started | Chainlink Documentation](https://docs.chain.link/chainlink-functions/getting-started/#deploy-a-functions-consumer-contract-on-sepolia/) and you will see a "Open in Remix" button, click that and Remix will open with the code loaded.

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FKgMc9d84TTJ0WwYo1bSV%2Fsepolia.png?alt=media&amp;token=da6bb11b-70a3-4948-8585-6313deaa9db2" alt=""><figcaption></figcaption></figure>

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2F0DSrUIzRxwryqDsA7MSQ%2FremixFunctions.png?alt=media&amp;token=85d51cb9-343b-46d6-a1ef-93068b699437" alt=""><figcaption><p>GettingStartedFunctionsConsumer.sol</p></figcaption></figure>

Since contract we copied over  was written for deployment on Sepolia, you will need to change configuration parameters like the `address router` and `donID` to the relevant addresses for Avalanche Fuji Testnet.  \
\
Specifically:

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FEZh2FAAysOgt8g3m6Fjr%2FremixFunctions2.png?alt=media&amp;token=c0306b75-e6d2-4096-b333-61489b06a33d" alt=""><figcaption></figcaption></figure>

Update the `address router` and `donID` by copying and pasting the following addresses into `GettingStartedFunctionsConsumer.sol`:

{% code title="address router" %}

```solidity
0xA9d587a00A31A52Ed70D6026794a8FC5E2F5dCb0
```

{% endcode %}

{% code title="donID" %}

```solidity
0x66756e2d6176616c616e6368652d66756a692d31000000000000000000000000
```

{% endcode %}

You can get these configuration details from [Supported Networks | Chainlink Documentation](https://docs.chain.link/chainlink-functions/supported-networks#avalanche-fuji-testnet/). Always cross-check that for the correct configs!

3. Copy the full code below and paste into `GettingStartedFunctionsConsumer.sol`:

{% code title="GettingStartedFunctionsConsumer.sol" %}

````solidity
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

// Deploy on Fuji

import {FunctionsClient} from "@chainlink/contracts@1.1.0/src/v0.8/functions/v1_0_0/FunctionsClient.sol";
import {ConfirmedOwner} from "@chainlink/contracts@1.1.0/src/v0.8/shared/access/ConfirmedOwner.sol";
import {FunctionsRequest} from "@chainlink/contracts@1.1.0/src/v0.8/functions/v1_0_0/libraries/FunctionsRequest.sol";

/**
 * Request testnet LINK and ETH here: https://faucets.chain.link/
 * Find information on LINK Token Contracts and get the latest ETH and LINK faucets here: https://docs.chain.link/resources/link-token-contracts/
 */

/**
 * @title GettingStartedFunctionsConsumer
 * @notice This is an example contract to show how to make HTTP requests using Chainlink
 * @dev This contract uses hardcoded values and should not be used in production.
 */
contract GettingStartedFunctionsConsumer is FunctionsClient, ConfirmedOwner {
    using FunctionsRequest for FunctionsRequest.Request;

    // State variables to store the last request ID, response, and error
    bytes32 public s_lastRequestId;
    bytes public s_lastResponse;
    bytes public s_lastError;

    // Custom error type
    error UnexpectedRequestID(bytes32 requestId);

    // Event to log responses
    event Response(
        bytes32 indexed requestId,
        string character,
        bytes response,
        bytes err
    );

    // Router address - Hardcoded for Fuji
    // Check to get the router address for your supported network https://docs.chain.link/chainlink-functions/supported-networks
    address router = 0xA9d587a00A31A52Ed70D6026794a8FC5E2F5dCb0;

    // JavaScript source code
    // Fetch character name from the Star Wars API.
    // Documentation: https://swapi.info/people
    string source =
        "const characterId = args[0];"
        "const apiResponse = await Functions.makeHttpRequest({"
        "url: `https://swapi.info/api/people/${characterId}/`"
        "});"
        "if (apiResponse.error) {"
        "throw Error('Request failed');"
        "}"
        "const { data } = apiResponse;"
        "return Functions.encodeString(data.name);";

    //Callback gas limit
    uint32 gasLimit = 300000;

    // donID - Hardcoded for Fuji
    // Check to get the donID for your supported network https://docs.chain.link/chainlink-functions/supported-networks
    bytes32 donID =
        0x66756e2d6176616c616e6368652d66756a692d31000000000000000000000000;

    // State variable to store the returned character information
    string public character;

    /**
     * @notice Initializes the contract with the Chainlink router address and sets the contract owner
     */
    constructor() FunctionsClient(router) ConfirmedOwner(msg.sender) {}

    /**
     * @notice Sends an HTTP request for character information
     * @param subscriptionId The ID for the Chainlink subscription
     * @param args The arguments to pass to the HTTP request
     * @return requestId The ID of the request
     */
    function sendRequest(
        uint64 subscriptionId,
        string[] calldata args
    ) external onlyOwner returns (bytes32 requestId) {
        FunctionsRequest.Request memory req;
        req.initializeRequestForInlineJavaScript(source); // Initialize the request with JS code
        if (args.length > 0) req.setArgs(args); // Set the arguments for the request

        // Send the request and store the request ID
        s_lastRequestId = _sendRequest(
            req.encodeCBOR(),
            subscriptionId,
            gasLimit,
            donID
        );

        return s_lastRequestId;
    }

    /**
     * @notice Callback function for fulfilling a request
     * @param requestId The ID of the request to fulfill
     * @param response The HTTP response data
     * @param err Any errors from the Functions request
     */
    function fulfillRequest(
        bytes32 requestId,
        bytes memory response,
        bytes memory err
    ) internal override {
        if (s_lastRequestId != requestId) {
            revert UnexpectedRequestID(requestId); // Check if request IDs match
        }
        // Update the contract's state variables with the response and any errors
        s_lastResponse = response;
        character = string(response);
        s_lastError = err;

        // Emit an event to log the response
        emit Response(requestId, character, s_lastResponse, s_lastError);
    }
}
```
````

{% endcode %}

***

### Compile your Contract

You will need to compile your contract, so navigate to the 1. “SOLIDITY COMPILER” icon and then 2. click on the “Compile” button. You will see a green check when it has compiled successfully on the icon.

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FUvfI2vjU5zDqzrq02TvM%2Ffunctions45.png?alt=media&amp;token=628d3996-29ad-4b2f-bb28-59a1dcdf16b1" alt=""><figcaption><p>Successful compiled contract</p></figcaption></figure>

***

### Deploy your Smart Contract

Time to deploy your contract, perform the following steps:

1. Go to the "DEPLOY & RUN TRANSACTIONS" tab, and make sure you're connected to Fuji
2. Click the "Deploy" button at the bottom
3. MetaMask will pop-up and click the "Confirm" button

<figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FsZee1RVSNa2JalnjbSoU%2Fdeploy2.png?alt=media&amp;token=dc2491a0-c813-4e61-ab08-f6877ce52187" alt=""><figcaption><p>Deployment</p></figcaption></figure>

Upon a successful deployment, you will see your contract at the bottom the of "DEPLOY & RUN TRANSACTIONS" section:

<div align="left"><figure><img src="https://62720068-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FNQrWPKDHvXg0HQjwzHbY%2Fuploads%2FMkSPs0nPqKCA9kOwf3TY%2Fdeployed.png?alt=media&amp;token=773789f8-00f7-4ad0-af45-ff056f175751" alt=""><figcaption><p>Successfully deployed your contract</p></figcaption></figure></div>

In the next section, we will create a Chainlink Functions Subscription.
