Skip to content

Instantly share code, notes, and snippets.

@ericjaurena
Last active May 9, 2022 18:19
Embed
What would you like to do?

Steps For Using This Oracle

  • Write and deploy your Chainlink contract using the network details and example consumer contract below
  • Fund your consumer contract with LINK
  • Call your request method, as described in these docs

Network Details

Matic Mumbai Testnet

Payment Amount: 0.1 LINK
LINK Token Address: 0x326C977E6efc84E512bB9C30f76E30c160eD06FB
Oracle Address: 0xd5821b900e44db9490da9b09541bbd027fBecF4E
JobID: 29f5bf7127734d768e40c8bec5e41e6b
JobID as bytes32: 0x3239663562663731323737333464373638653430633862656335653431653662

Ethereum Kovan Testnet

Payment Amount: 0.1 LINK
LINK Token Address: 0xa36085F69e2889c224210F603D836748e7dC0088
Oracle Address: 0xfF07C97631Ff3bAb5e5e5660Cdf47AdEd8D4d4Fd
JobID: ad875918c4a94489bbe1f9e34e7986cf
JobID as bytes32: 0x6164383735393138633461393434383962626531663965333465373938366366

Request Parameters

artistId

  • The unique Chartmetric ID for the artist you wish to request.

Outputs

Statistics, a struct, with variables of:

  • youtube: a uint64 representing total Youtube subscribers
  • spotify: a uint64 representing total Spotify listeners
  • tiktok: a uint64 representing total Tiktok followers
    struct Statistics {
        uint64 youtube;
        uint64 spotify;
        uint64 tiktok;
    }

Example Consumer Contract

// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;

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

/**
 * @title A consumer contract for Chartmetric.
 * @author LinkPool.
 * @notice Interact with the GamesByDate API (sportsdataio-linkpool adapter).
 * @dev Uses @chainlink/contracts 0.4.0.
 */
contract ChartmetricConsumerOracle is ChainlinkClient {
    using Chainlink for Chainlink.Request;

    struct Statistics {
        uint64 youtube;
        uint64 spotify;
        uint64 tiktok;
    }

    mapping(bytes32 => Statistics) public requestIdStatistics;

    error FailedTransferLINK(address to, uint256 amount);

    /**
     * @param _link the LINK token address.
     * @param _oracle the Operator.sol contract address.
     */
    constructor(address _link, address _oracle) {
        setChainlinkToken(_link);
        setChainlinkOracle(_oracle);
    }

    /* ========== CONSUMER REQUEST FUNCTIONS ========== */

    /**
     * @notice Requests the youtube subscribers, spotify monthly listeners and tiktok followers of a given artist.
     * @dev Requests the 'statistics' endpoint. Result is Statistics struct (see structs).
     * @param _specId the jobID.
     * @param _payment the LINK amount in Juels (i.e. 10^18 aka 1 LINK).
     * @param _artistId the ID of the artist for which we want to query the stats (as uint256).
     */
    function requestStatistics(
        bytes32 _specId,
        uint256 _payment,
        uint256 _artistId
    ) external {
        Chainlink.Request memory req = buildChainlinkRequest(_specId, address(this), this.fulfillStatistics.selector);

        req.addUint("artistId", _artistId);

        sendChainlinkRequest(req, _payment);
    }

    /* ========== CONSUMER FULFILL FUNCTIONS ========== */

    /**
     * @notice Stores the statistics.
     * @param _requestId the request ID for fulfillment.
     * @param _result the statistics struct.
     */
    function fulfillStatistics(bytes32 _requestId, bytes32 _result) external recordChainlinkFulfillment(_requestId) {
        requestIdStatistics[_requestId] = getStatistics(_result);
    }

    function getStatistics(bytes32 _data) private pure returns (Statistics memory) {
        Statistics memory statistics = Statistics(
            uint64(bytes8(_data)),
            uint64(bytes8(_data << 64)),
            uint64(bytes8(_data << 128))
        );
        return statistics;
    }

    /* ========== OTHER FUNCTIONS ========== */

    function getOracleAddress() external view returns (address) {
        return chainlinkOracleAddress();
    }

    function setOracle(address _oracle) external {
        setChainlinkOracle(_oracle);
    }

    function withdrawLink(uint256 _amount, address payable _payee) external {
        LinkTokenInterface linkToken = LinkTokenInterface(chainlinkTokenAddress());
        _requireTransferLINK(linkToken.transfer(_payee, _amount), _payee, _amount);
    }

    function _requireTransferLINK(
        bool _success,
        address _to,
        uint256 _amount
    ) private pure {
        if (!_success) {
            revert FailedTransferLINK(_to, _amount);
        }
    }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment