-
Notifications
You must be signed in to change notification settings - Fork 0
/
TellorCaller.sol
60 lines (55 loc) · 2.48 KB
/
TellorCaller.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
// SPDX-License-Identifier: MIT
pragma solidity 0.6.11;
pragma experimental ABIEncoderV2;
import "./interfaces/ITellorCaller.sol";
import "./interfaces/ITellor.sol";
/*
* This contract serves as an example of how to integrate the Tellor oracle into a Liquity-like system. It
* utilizes a best practice for using Tellor by implementing a time buffer. In addition, by caching the most
* recent value and timestamp, it also seeks to limit dispute attacks.
*
* This contract has a single external function that calls Tellor: getTellorCurrentValue().
*
* The function is called by the Liquity contract PriceFeed.sol. If any of its inner calls to Tellor revert,
* this function will revert, and PriceFeed will catch the failure and handle it accordingly.
*
*/
contract TellorCaller is ITellorCaller {
ITellor public tellor;
mapping (bytes32 => uint256) public lastStoredTimestamps;
mapping (bytes32 => uint256) public lastStoredPrices;
constructor (address payable _tellorOracleAddress) public {
tellor = ITellor(_tellorOracleAddress);
}
/*
* getTellorCurrentValue(): retrieves most recent value with a 20 minute time buffer.
* This buffer can be updated before deployment.
*
* @dev Allows the user to get the latest value with a time buffer for the queryId specified
* @param _queryId is the queryId to look up the value for
* @return ifRetrieve bool true if it is able to retrieve a value and the value's timestamp
* @return value the value retrieved, converted from bytes to a uint256 value
* @return _timestampRetrieved the value's timestamp
*/
function getTellorCurrentValue(bytes32 _queryId)
external
override
returns (
bool ifRetrieve,
uint256 value,
uint256 _timestampRetrieved
)
{
// retrieve most recent 20+ minute old value for a queryId. the time buffer allows time for a bad value to be disputed
(, bytes memory data, uint256 timestamp) = tellor.getDataBefore(_queryId, block.timestamp - 20 minutes);
uint256 _value = abi.decode(data, (uint256));
if (timestamp == 0 || _value == 0) return (false, _value, timestamp);
if (timestamp > lastStoredTimestamps[_queryId]) {
lastStoredTimestamps[_queryId] = timestamp;
lastStoredPrices[_queryId] = _value;
return (true, _value, timestamp);
} else {
return (true, lastStoredPrices[_queryId], lastStoredTimestamps[_queryId]);
}
}
}