-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOracleClientExample.sol
43 lines (35 loc) · 1.39 KB
/
OracleClientExample.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
/**
**** Disclaimer and limitations ****
*
* RIFUSDOracle queries rif price from only one datasource (CoinGecko API).
* Users should always check lastExecutionTimestamp from the oracle to know how old the price obtained is.
* The oracle updates rif price every minute, but if for any reason it stopped working then the returned price could be deprecated.
* Price obtained from the oracle is in millicents (1 dollar = 10^5 millicents).
*
* Use at your own risk
*
* This is just an example, it is NOT intended to be used in production environments
*/
pragma solidity ^0.5.0;
import "./IRIFUSDOracle.sol";
contract OracleClientExample {
IRIFUSDOracle private oracle;
uint private _oldestTimeAcceptedInSeconds;
function() external payable {}
modifier rifPriceIsRecent() {
uint lastExecutionTimestamp = oracle.lastExecutionTimestamp();
require(
now - lastExecutionTimestamp <= _oldestTimeAcceptedInSeconds,
"Rif price from oracle is older than acceptable"
);
_;
}
constructor(address oracleAddress) public {
oracle = IRIFUSDOracle(oracleAddress);
_oldestTimeAcceptedInSeconds = 7200; //2 hours
}
function calculatePrice(uint rifs) public rifPriceIsRecent returns(uint) {
uint rifPriceInMillicents = oracle.rifTokenPriceInMillicents();
return rifPriceInMillicents * rifs;
}
}