📂 dApp Developers

# readDataFeedValueWithId()

Table of Contents

This function uses IDs.

Be sure to understand the difference between using a name or ID. See dAPI Names.

This function reads a value directly from a Beacon or Beacon set using its ID. In the code example below, _datafeedId is a Beacon or Beacon set ID. For on-chain smart contracts the msg.sender argument received by the function readDataFeedValueWithId() (opens new window) must have read access for the dAPI requested.

Calling from off-chain code (using a library such as ether.js) is not subject to coverage policies. Off-chain code is beyond the scope of this doc.

# Example Code

// SPDX-License-Identifier: MIT
pragma solidity 0.8.9;
import "@api3/airnode-protocol-v1/contracts/dapis/interfaces/IDapiServer.sol";
contract mySmartContract {
    function myGetDataFeedValue(
        address _dapiServerContractAddress,
        bytes32 _datafeedId
    ) external {
        int224 private value;
        // Calling the DapiServer for a data feed value.
        value =
            IDapiServer(_dapiServerContractAddress).readDataFeedValueWithId(_datafeedId);
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

See another code example of readDataFeedValueWithId() in the data-feed-reader-example repo (opens new window).

# Parameters

readDataFeedValueWithId(bytes32 _datafeedId)

  • bytes32 datafeedId - The ID of a Beacon or Beacon set to retrieve a value for.

# Returns

  • int224 value - The value of the Beacon or Beacon set.

Please note:

The DapiServer.sol contract casts the reported data point to int224. If this is a problem (because the reported data may not fit into 224 bits or it is of a completely different type such as bytes32), do not use this contract and implement a customized version instead. The contract casts the timestamps to uint32, which means it will not work work past-2106 in the current form. If this is an issue, consider casting the timestamps to a larger type.

If the timestamp is 0 then the function will revert with "Data feed does not exist".

Last Updated: 9/8/2022, 6:10:42 AM