# readDataFeedValueWithDapiName()
This function reads a value from a dAPI using its name. In the code example
below, \_dapiName
is a dAPI name. For on-chain smart contracts the msg.sender
argument received by the function
readDataFeedValueWithDapiName() (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 Usage
// 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 _dapiName
) external {
int224 private value;
// Calling the DapiServer for a data feed value.
value =
IDapiServer(_dapiServerContractAddress).readDataFeedValueWithDapiName(_dapiName);
}
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
See another code example of readDataFeedValueWithDapiName()
in the
data-feed-reader-example repo (opens new window).
# Parameters
readDataFeedValueWithDapiName(bytes32 _dapiName)
bytes32 dapiName
- The encoded bytes32 value of a dAPI name to retrieve a value for (no timestamp). The example below generates the encoded bytes32 value of AVAX/USD. Try it in the ethers playground (opens new window).// Encode the dapiName (such as AVAX/USD) to bytes32 ethers.utils.formatBytes32String("AVAX/USD"); // 0x415641582f555344000000000000000000000000000000000000000000000000 // Pass the above value to readDataFeedWithDapiName()
1
2
3
4
# Returns
int224 value
- The value of the dAPI.
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".