Solidity
Learn how to set up solidity integration with Fetch Oracle.
Last updated
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.3;
import "usingfetch/contracts/UsingFetch.sol";
contract ExampleContract is UsingFetch {
constructor(address payable _fetchAddress) UsingFetch(_fetchAddress) {}
function getBtcSpotPrice() external view returns(uint256) {
bytes memory _queryData = abi.encode("SpotPrice", abi.encode("btc", "usd"));
bytes32 _queryId = keccak256(_queryData);
(bytes memory _value, uint256 _timestampRetrieved) =
getDataBefore(_queryId, block.timestamp - 20 minutes);
if (_timestampRetrieved == 0) return 0;
require(block.timestamp - _timestampRetrieved < 24 hours, "Data timestamp is more than 24 hours.");
return abi.decode(_value, (uint256));
}
}