🏨CompanyInfo.sol

This proposed contract allows you to fetch data from an external API using Chainlink oracles. It sends a request with a company name and handles the response, storing the data on-chain.

Explanation of CompanyInfo.sol Contract

Imports

import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";
  • ChainlinkClient: Provides functions to interact with Chainlink nodes.

Contract Declaration

contract CompanyInfo is ChainlinkClient {
    using Chainlink for Chainlink.Request;
  • CompanyInfo Contract: Inherits from ChainlinkClient to interact with Chainlink.

  • using Chainlink for Chainlink.Request: Allows us to use the Chainlink.Request library methods.

State Variables

    uint256 public companyData;
    address private oracle;
    bytes32 private jobId;
    uint256 private fee;
  • companyData: Stores the data returned from the Chainlink request.

  • oracle: Address of the Chainlink oracle.

  • jobId: ID of the specific Chainlink job to be called.

  • fee: Fee for the Chainlink request.

Events

    event DataFulfilled(bytes32 indexed requestId, uint256 indexed data);
  • DataFulfilled: Emitted when the Chainlink request is fulfilled.

Constructor

    constructor() {
        setPublicChainlinkToken();
        oracle = 0x...; // Chainlink Oracle address
        jobId = "b0..."; // Chainlink Job ID
        fee = 0.1 * 10 ** 18; // 0.1 LINK
    }
  • constructor: Initializes the contract, sets the Chainlink token, oracle address, job ID, and fee.

Request Function

    function requestCompanyInfo(string memory companyName) public returns (bytes32 requestId) {
        Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
        request.add("companyName", companyName);
        return sendChainlinkRequestTo(oracle, request, fee);
    }
  • requestCompanyInfo: Builds and sends a Chainlink request with the company name.

Fulfillment Function

    function fulfill(bytes32 _requestId, uint256 _data) public recordChainlinkFulfillment(_requestId) {
        emit DataFulfilled(_requestId, _data);
        companyData = _data;
    }
}
  • fulfill: Callback function that handles the response from the Chainlink oracle and stores the returned data.


  • Steps:

    1. The contract sends a request to a Chainlink oracle with a company name.

    2. The Chainlink node fetches the data and returns it to the fulfill function.

    3. The fulfill function updates the companyData state variable and emits an event.

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;

import "@chainlink/contracts/src/v0.8/ChainlinkClient.sol";

contract CompanyInfo is ChainlinkClient {
    using Chainlink for Chainlink.Request;
  
    uint256 public companyData;
    address private oracle;
    bytes32 private jobId;
    uint256 private fee;

    event DataFulfilled(bytes32 indexed requestId, uint256 indexed data);

    constructor() {
        setPublicChainlinkToken();
        oracle = 0x...; // Chainlink Oracle address
        jobId = "b0..."; // Chainlink Job ID
        fee = 0.1 * 10 ** 18; // 0.1 LINK
    }

    function requestCompanyInfo(string memory companyName) public returns (bytes32 requestId) {
        Chainlink.Request memory request = buildChainlinkRequest(jobId, address(this), this.fulfill.selector);
        request.add("companyName", companyName);
        return sendChainlinkRequestTo(oracle, request, fee);
    }

    function fulfill(bytes32 _requestId, uint256 _data) public recordChainlinkFulfillment(_requestId) {
        emit DataFulfilled(_requestId, _data);
        companyData = _data;
    }
}
// i on you reviews - rosalone_labs
```

Last updated