♈
Bootcamp-2024
  • Intro
  • 1. Blockchain & Wallet Fundamentals
    • Blockchain Introduction
    • State Machines
    • Cryptography
    • Distributed Networks
    • Game Theory
    • What is Web3
    • MetaMask Wallet Installation
    • Transferring Tokens with MetaMask
  • 2. Smart Contract & Solidity Fundamentals
    • Using Remix
    • Create, compile and publish your first smart contract
    • Interact with already published smart contracts
    • Blockchain Explorer
    • Verify source code on Etherscan
  • 3. Oracles, ERC20 & Chainlink Data Feeds
    • Oracles
    • Create & Deploy ERC20
    • Data Feeds
  • 4. Cross-Chain Tokens With Chainlink CCIP
    • Setting up MetaMask
    • Getting USDC Testnet Tokens
    • Create Smart Contract In Remix
    • Compile and Deploy
    • Approve USDC
    • Send LINK to your Contract
    • Send USDC from Fuji to Sepolia
    • USDC on Sepolia
  • 5. Mentoring Session
  • 6. NFTs & Chainlink Automation
    • NFT Basics
    • Dynamic NFTs
    • Creating an NFT Smart Contract
    • Deploying Your Dynamic NFTs
  • 7. Chainlink CCIP & Cross-Chain NFT dApps
    • Create and deploy CCIP NFT Contracts
    • Mint on Source Chain
    • Fund Contract
    • Mint On Sepolia From Fuji
    • Mint from Destination 2 - Base Sepolia
  • 8. Random Numbers with Chainlink VRF
    • Introduction to Chainlink VRF
    • Hands On Game Using VRF
  • 9. Off-Chain Data with Chainlink Functions
    • Chainlink Functions Playground
    • Setting up MetaMask
    • Remix
    • Functions Subscription
    • Creating The Functions Consumer Contract
    • Sending a Request from Remix
    • City Weather and Examples
    • City Weather on Chainlink Functions
  • 10. Connecting the 🌏 with Chainlink
  • Glossary
Powered by GitBook
On this page
  • Remix
  • You Subscription on Chainlink Functions
  1. 9. Off-Chain Data with Chainlink Functions

City Weather on Chainlink Functions

PreviousCity Weather and ExamplesNext10. Connecting the 🌏 with Chainlink

Last updated 1 year ago

Remix

Go to Remix and create a new Solidity file by 1. going to the “FILE EXPLORER” and 2. clicking the “Create new file” icon:

Name the new file WeatherFunctions.sol:

WeatherFunctions.sol
// SPDX-License-Identifier: MIT
pragma solidity 0.8.19;

// Deploy on Fuji

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

contract WeatherFunctions is FunctionsClient {
    using FunctionsRequest for FunctionsRequest.Request;

    // State variables to store the last request ID, response, and error
    bytes32 public lastRequestId;
    bytes public lastResponse;
    bytes public lastError;

    struct RequestStatus {
        bool fulfilled; // whether the request has been successfully fulfilled
        bool exists; // whether a requestId exists
        bytes response;
        bytes err;
    }
    mapping(bytes32 => RequestStatus) public requests; /* requestId --> requestStatus */          
    bytes32[] public requestIds;

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

    // Hardcoded for Fuji
    // Supported networks https://docs.chain.link/chainlink-functions/supported-networks
    address router = 0xA9d587a00A31A52Ed70D6026794a8FC5E2F5dCb0;
    bytes32 donID =
        0x66756e2d6176616c616e6368652d66756a692d31000000000000000000000000;

    //Callback gas limit
    uint32 gasLimit = 300000;

    // Your subscription ID.
    uint64 public subscriptionId;

    // JavaScript source code
    string public source =
        "const city = args[0];"
        "const apiResponse = await Functions.makeHttpRequest({"
        "url: `https://wttr.in/${city}?format=3&m`,"
        "responseType: 'text'"
        "});"
        "if (apiResponse.error) {"
        "throw Error('Request failed');"
        "}"
        "const { data } = apiResponse;"
        "return Functions.encodeString(data);";
    string public lastCity;    
    string public lastTemperature;
    address public lastSender;

    struct CityStruct {
        address sender;
        uint timestamp;
        string name;
        string temperature;
    }
    CityStruct[] public cities;
    mapping(string => uint256) public cityIndex;
    mapping(bytes32 => string) public request_city; /* requestId --> city*/

    constructor(uint64 functionsSubscriptionId) FunctionsClient(router) {
        subscriptionId = functionsSubscriptionId;      
    }

    function getTemperature(
        string memory _city
    ) external returns (bytes32 requestId) {

        string[] memory args = new string[](1);
        args[0] = _city;

        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
        lastRequestId = _sendRequest(
            req.encodeCBOR(),
            subscriptionId,
            gasLimit,
            donID
        );
        lastCity = _city;
        request_city[lastRequestId] = _city;

        CityStruct memory auxCityStruct = CityStruct({
            sender: msg.sender,
            timestamp: 0,
            name: _city,
            temperature: ""            
        });
        cities.push(auxCityStruct);
        cityIndex[_city] = cities.length-1;

        requests[lastRequestId] = RequestStatus({
            exists: true,
            fulfilled: false,
            response: "",
            err: ""
        });
        requestIds.push(lastRequestId);

        return lastRequestId;
    }

    // Receive the weather in the city requested
    function fulfillRequest(
        bytes32 requestId,
        bytes memory response,
        bytes memory err
    ) internal override {
        require(requests[requestId].exists, "request not found");

        lastError = err;
        lastResponse = response;

        requests[requestId].fulfilled = true;
        requests[requestId].response = response;
        requests[requestId].err = err;

        string memory auxCity = request_city[requestId];
        lastTemperature = string(response);
        cities[cityIndex[auxCity]].temperature = lastTemperature;
        cities[cityIndex[auxCity]].timestamp = block.timestamp;

        // Emit an event to log the response
        emit Response(requestId, lastTemperature, lastResponse, lastError);
    }

	function getCity(string memory city) public view returns (CityStruct memory) {
    	return cities[cityIndex[city]];
	}

	function listAllCities() public view returns (CityStruct[] memory) {
    	return cities;
	}

    function listCities(uint start, uint end) public view returns (CityStruct[] memory) {
        if (end > cities.length)
            end = cities.length-1;
        require (start <= end, "start must <= end");
        uint cityCount = end - start + 1;
        CityStruct[] memory citiesAux = new CityStruct[](cityCount);

        for (uint i = start; i < (end + 1); i++) {
            citiesAux[i-start] = cities[i];
        }
        return citiesAux;
    }

}

As in the previous exercise, in the “DEPLOY & RUN TRANSACTIONS”, make sure your “ENVIRONMENT is on “Injected Provider - Metamask” and you are on Avalanche Fuji Testnet 43113:

Under the “DEPLOY” section, 1. paste in your Subscription ID we set up in the previous exercise and then 2. click the “transact” button to deploy our contract:

MetaMask will once again pop-up and ask you to confirm the transaction:

Once deployed, we need to add this contract's address as a consumer. Copy the contracts address from here in Remix:


You Subscription on Chainlink Functions

You can have multiple consumer contracts in a single Chainlink Functions Subscription (up to 100)

Now 1. paste in your WeatherFunctions.sol deployed address and then 2. click the “Add consumer” blue button:

MetaMask will pop-up for you to confirm the transaction:

Once the transaction is confirmed, refresh the page you will now see your new consumer:

Back in Remix with our deployed contract WeatherFunctions.sol, click the drop down arrow next to getTemperature:

Fill in the 1. _city argument as London, or maybe your next vacation city, and then 2. click the “transact” button and 3. MetaMask will pop-up asking you to “Confirm” the transaction:

After the transaction is complete, there are several read functions that will return you information. You asked for the weather in London, thus the lastTemperature function is what you are looking for below:

You can try any other city again. Sao Paulo is the argument for _city (#1) and thus you would do another transaction and after completion, you now see the weather in Sao Paulo (#2)! Click on the listAllCities function (#3), which returns a tuple of the history of what cities you have called for the current weather and it shows London and Sao Paulo:


Here are more resources to help you with examples and guides to get a solid expert foundation on using Chainlink functions:

The code is available at and also below:

Navigate back to and click the "Add consumer" button on the far right:

GitHub
Chainlink Functions
Chainlink Functions Service Limits
11 Ways To Use Chainlink Functions in Your Decentralized Applications
Chainlink Dev Hub | Functions resources
Deep Dive Into Chainlink Functions | Constellation
useChainlinkFunctions()
Create a new Solidity file
Create a new Solidity file
Enter your Chainlink Subscription ID and click "transact"
Confirm the transaction
Copy your contract's address
Time to add a consumer
Paste in your consumer address and add the consumer
Confirm the transaction
Successfully added another consumer to your subscription
Click the down arrow
Confirm the transaction
You get London's weather!
Weather in Sao Paulo and our history