> ## Documentation Index
> Fetch the complete documentation index at: https://www.diadata.org/docs/llms.txt
> Use this file to discover all available pages before exploring further.

# getValue Method

> There are two ways that you can access the oracle in your dApp. You can either declare an IDIAOracleV2 interface or import the solidity library.

## Using DIAOracleV2 Interface

The following is an example of how to retrieve price value from a standard DIA oracle. For the purpose of this example, we will be using the following demo oracle on Ethereum: [0xCD5F...f3cB](https://sepolia.etherscan.io/address/0xcd5f78206ca1ff96ff4c043c61a2299b2febf3cb).

<Steps>
  <Step>
    Access any DIA oracle smart contract.
  </Step>

  <Step>
    Call `getValue(pair_name)` with `pair_name` being the full pair name such as `BTC/USD`. You can use the "Read" section on Etherscan to execute this call.
  </Step>

  <Step>
    The response of the call contains four values:

    1. The current asset's price in USD with a fix-comma notation of 8 decimals.

    2. The [UNIX timestamp](https://www.unixtimestamp.com/) of the last oracle update.

    ```solidity theme={"system"}
    pragma solidity ^0.8.13;

    interface IDIAOracleV2 {
        function getValue(string memory) external view returns (uint128,
                 uint128);
    }

    contract DIAOracleSample {

        address diaOracle;

        constructor(address _oracle) {
            diaOracle = _oracle;
        }

        function getPrice(string memory key)
        external
        view
        returns (
            uint128 latestPrice,
            uint128 timestampOflatestPrice
        ) {
            (latestPrice, timestampOflatestPrice) =
                     IDIAOracleV2(diaOracle).getValue(key);
        }
    }
    ```
  </Step>
</Steps>

Find the detailed description of the functions and how to run tests on our GitHub page:

<Frame caption="Solidity integration example">
  <Card title="GitHub - diadata-org/DIA-integration-sample" href="https://github.com/diadata-org/DIA-integration-sample" icon="github" iconType="solid" horizontal />
</Frame>

## Using Solidity Library

DIA has a dedicated Solidity library to facilitate the integration of DIA oracles in your own contracts. The library consists of two functions, `getPrice` and `getPriceIfNotOlderThan`.

### Access the library

```solidity theme={"system"}
import { DIAOracleLib } from "./libraries/DIAOracleLib.sol";
```

### Methods

#### getPrice

```solidity theme={"system"}
function getPrice(
        address oracle,
        string memory key
        )
        public
        view
        returns (uint128 latestPrice, uint128 timestampOflatestPrice);
```

Returns the price of a specified asset along with the update timestamp.

Parameters:

| Name   | Type    | Description                                  |
| ------ | ------- | -------------------------------------------- |
| oracle | address | Address of the oracle that we want to use    |
| key    | string  | The asset that we want to use e.g. "ETH/USD" |

Return Values:

| Name                   | Type    | Description                                            |
| ---------------------- | ------- | ------------------------------------------------------ |
| latestPrice            | uint128 | Price of the specified asset returned by the DIAOracle |
| timestampOflatestPrice | uint128 | The update timestamp of the latest price               |

#### getPriceIfNotOlderThan

```solidity theme={"system"}
function getPriceIfNotOlderThan(
        address oracle,
        string memory key,
        uint128 maxTimePassed
        )
        public
        view
        returns (uint128 price, bool inTime)
    {
```

Checks if the oracle price is older than `maxTimePassed`

Parameters:

| Name          | Type    | Description                                                                   |
| ------------- | ------- | ----------------------------------------------------------------------------- |
| oracle        | address | Address of the oracle that we want to use                                     |
| key           | string  | The asset that we want to use e.g. "ETH/USD"                                  |
| maxTimePassed | uint128 | The maximum acceptable time passed in seconds since the the price was updated |

Return Values:

| Name   | Type    | Description                                                                                            |
| ------ | ------- | ------------------------------------------------------------------------------------------------------ |
| price  | uint128 | Price of the specified asset returned by the DIAOracle                                                 |
| inTime | bool    | A boolian that is `true` if the price was updated at most maxTimePassed seconds ago, otherwise `false` |

Find a detailed integration example and how to run a test on our GitHub page:

<Card title="GitHub - diadata-org/DIAOracleLib" href="https://github.com/diadata-org/DIAOracleLib" icon="github" iconType="solid" horizontal />

### Sample contract

```solidity theme={"system"}
pragma solidity ^0.8.13;

import { DIAOracleLib } from "./libraries/DIAOracleLib.sol";

contract DIAOracleSample {
    error PriceTooOld();

    address diaOracle;

    constructor(address _oracle) {
        diaOracle = _oracle;
    }

    function getPrice(
        string memory key
    ) external view returns (uint128 latestPrice) {
        (latestPrice, ) = DIAOracleLib.getPrice(diaOracle, key);
    }

    function checkPriceAge(
        string memory key,
        uint128 maxTimePassed
    ) external view returns (uint128 price) {
        bool inTime;
        (price, inTime) = DIAOracleLib.getPriceIfNotOlderThan(
            diaOracle,
            key,
            maxTimePassed
        );

        if (!inTime) revert PriceTooOld();
    }
}
```
