In Remix, Go to the "DEPLOY & RUN TRANSACTIONS" icon on left side:
In the “ENVIRONMENT” drop-down menu, select “Injected Provider - MetaMask”:
Make sure you are on Custom (43113) network, which is Avalanche Fuji Testnet:
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:
Name the new Solidity file GettingStartedFunctionsConsumer.sol and a new tab will open in Remix:
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:
Update the address router and donID by copying and pasting the following addresses into GettingStartedFunctionsConsumer.sol:
Copy the full code below and paste into GettingStartedFunctionsConsumer.sol:
GettingStartedFunctionsConsumer.sol
// SPDX-License-Identifier: MITpragmasolidity 0.8.19;// Deploy on Fujiimport {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. */contractGettingStartedFunctionsConsumerisFunctionsClient, ConfirmedOwner {usingFunctionsRequestforFunctionsRequest.Request;// State variables to store the last request ID, response, and errorbytes32public s_lastRequestId;bytespublic s_lastResponse;bytespublic s_lastError;// Custom error typeerrorUnexpectedRequestID(bytes32 requestId);// Event to log responseseventResponse(bytes32indexed 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-networksaddress router =0xA9d587a00A31A52Ed70D6026794a8FC5E2F5dCb0;// JavaScript source code// Fetch character name from the Star Wars API.// Documentation: https://swapi.info/peoplestring 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 limituint32 gasLimit =300000;// donID - Hardcoded for Fuji// Check to get the donID for your supported network https://docs.chain.link/chainlink-functions/supported-networksbytes32 donID =0x66756e2d6176616c616e6368652d66756a692d31000000000000000000000000;// State variable to store the returned character informationstringpublic 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 */functionsendRequest(uint64 subscriptionId,string[] calldata args ) externalonlyOwnerreturns (bytes32 requestId) { FunctionsRequest.Request memory req; req.initializeRequestForInlineJavaScript(source); // Initialize the request with JS codeif (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 */functionfulfillRequest(bytes32 requestId,bytesmemory response,bytesmemory err ) internaloverride {if (s_lastRequestId != requestId) {revertUnexpectedRequestID(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 responseemitResponse(requestId, character, s_lastResponse, s_lastError); }}```
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.
Deploy your Smart Contract
Time to deploy your contract, perform the following steps:
Go to the "DEPLOY & RUN TRANSACTIONS" tab, and make sure you're connected to Fuji
Click the "Deploy" button at the bottom
MetaMask will pop-up and click the "Confirm" button
Upon a successful deployment, you will see your contract at the bottom the of "DEPLOY & RUN TRANSACTIONS" section:
In the next section, we will create a Chainlink Functions Subscription.