|
| 1 | +// SPDX-License-Identifier: MIT |
| 2 | + |
| 3 | +pragma solidity >=0.7.0 <0.9.0; |
| 4 | +pragma experimental ABIEncoderV2; |
| 5 | + |
| 6 | +import "./WitnetRequestBoardV2.sol"; |
| 7 | + |
| 8 | +/// @title The UsingWitnetV2 contract |
| 9 | +/// @dev Witnet-aware contracts can inherit from this contract in order to interact with Witnet. |
| 10 | +/// @author The Witnet Foundation. |
| 11 | +abstract contract UsingWitnetV2 { |
| 12 | + |
| 13 | + /// @dev Immutable address to the WitnetRequestBoardV2 contract. |
| 14 | + WitnetRequestBoardV2 public immutable witnet; |
| 15 | + |
| 16 | + /// @dev Include an address to specify the WitnetRequestBoard entry point address. |
| 17 | + /// @param _wrb The WitnetRequestBoard entry point address. |
| 18 | + constructor(WitnetRequestBoardV2 _wrb) |
| 19 | + { |
| 20 | + require( |
| 21 | + _wrb.class() == type(WitnetRequestBoardV2).interfaceId, |
| 22 | + "UsingWitnetV2: uncompliant request board" |
| 23 | + ); |
| 24 | + witnet = _wrb; |
| 25 | + } |
| 26 | + |
| 27 | + /// @dev Provides a convenient way for client contracts extending this to block the execution of the main logic of the |
| 28 | + /// @dev contract until a particular data query has been successfully solved and reported by Witnet, |
| 29 | + /// @dev either with an error or successfully. |
| 30 | + modifier witnetQueryInStatus(bytes32 _queryHash, WitnetV2.QueryStatus _queryStatus) { |
| 31 | + require( |
| 32 | + witnet.checkQueryStatus(_queryHash) == _queryStatus, |
| 33 | + "UsingWitnetV2: unexpected query status"); |
| 34 | + _; |
| 35 | + } |
| 36 | + |
| 37 | + /// @notice Returns EVM gas price within the context of current transaction. |
| 38 | + function _getTxGasPrice() virtual internal view returns (uint256) { |
| 39 | + return tx.gasprice; |
| 40 | + } |
| 41 | + |
| 42 | + /// @notice Estimate the minimum reward in EVM/wei required for posting the described Witnet data request. |
| 43 | + /// @param _radHash The hash of the query's data request part (previously registered in `witnet.registry()`). |
| 44 | + /// @param _slaParams The query's SLA parameters. |
| 45 | + /// @param _witEvmPrice The price of 1 nanoWit in EVM/wei to be used when estimating query rewards. |
| 46 | + /// @param _maxEvmGasPrice The maximum EVM gas price willing to pay upon result reporting. |
| 47 | + function _witnetEstimateQueryReward( |
| 48 | + bytes32 _radHash, |
| 49 | + WitnetV2.RadonSLAv2 memory _slaParams, |
| 50 | + uint256 _witEvmPrice, |
| 51 | + uint256 _maxEvmGasPrice |
| 52 | + ) |
| 53 | + internal view |
| 54 | + returns (uint256) |
| 55 | + { |
| 56 | + return witnet.estimateQueryReward(_radHash, _slaParams, _witEvmPrice, _maxEvmGasPrice, 0); |
| 57 | + } |
| 58 | + |
| 59 | + /// @notice Post some data request to be eventually solved by the Witnet decentralized oracle network. |
| 60 | + /// @dev Enough EVM coins need to be provided as to cover for the implicit cost and bridge rewarding. |
| 61 | + /// @param _radHash The hash of the query's data request part (previously registered in `witnet.registry()`). |
| 62 | + /// @param _slaParams The query's SLA parameters. |
| 63 | + /// @param _witEvmPrice The price of 1 nanoWit in EVM/wei to be used when estimating query rewards. |
| 64 | + /// @param _maxEvmGasPrice The maximum EVM gas price willing to pay upon result reporting. |
| 65 | + /// @return _queryHash The unique identifier of the new data query. |
| 66 | + /// @return _queryReward The actual amount escrowed into the WRB as query reward. |
| 67 | + function _witnetPostQuery( |
| 68 | + bytes32 _radHash, |
| 69 | + WitnetV2.RadonSLAv2 memory _slaParams, |
| 70 | + uint256 _witEvmPrice, |
| 71 | + uint256 _maxEvmGasPrice |
| 72 | + ) |
| 73 | + virtual internal |
| 74 | + returns (bytes32 _queryHash, uint256 _queryReward) |
| 75 | + { |
| 76 | + _queryReward = _witnetEstimateQueryReward(_radHash, _slaParams, _witEvmPrice, _maxEvmGasPrice); |
| 77 | + require(_queryReward <= msg.value, "UsingWitnetV2: EVM reward too low"); |
| 78 | + _queryHash = witnet.postQuery{value: _queryReward}(_radHash, _slaParams); |
| 79 | + } |
| 80 | + |
| 81 | + /// @notice Post some data request to be eventually solved by the Witnet decentralized oracle network. |
| 82 | + /// @dev Enough EVM coins need to be provided as to cover for the implicit cost and bridge rewarding. |
| 83 | + /// @dev Implicitly sets `tx.gasprice` as the maximum EVM gas price expected to be paid upon result reporting. |
| 84 | + /// @param _radHash The hash of the query's data request part (previously registered in `witnet.registry()`). |
| 85 | + /// @param _slaParams The query's SLA parameters. |
| 86 | + /// @param _witEvmPrice The price of 1 nanoWit in EVM/wei to be used when estimating query rewards. |
| 87 | + /// @return _queryHash The unique identifier of the new data query. |
| 88 | + /// @return _queryReward The actual amount escrowed into the WRB as query reward. |
| 89 | + function _witnetPostQuery(bytes32 _radHash, WitnetV2.RadonSLAv2 memory _slaParams, uint256 _witEvmPrice) |
| 90 | + virtual internal |
| 91 | + returns (bytes32 _queryHash, uint256 _queryReward) |
| 92 | + { |
| 93 | + return _witnetPostQuery(_radHash, _slaParams, _witEvmPrice, _getTxGasPrice()); |
| 94 | + } |
| 95 | +} |
0 commit comments