From 71e7973ea444047c6990602614241706aa119499 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20D=C3=ADaz?= Date: Fri, 1 Dec 2023 10:54:20 +0100 Subject: [PATCH] refactor: IWRB.estimateQueryEarnings -> IWRBReporter.estimateQueryEarnings --- .../WitnetRequestBoardTrustableBase.sol | 46 ++++++++----------- .../interfaces/V2/IWitnetRequestBoard.sol | 8 +--- .../V2/IWitnetRequestBoardReporter.sol | 6 +++ 3 files changed, 27 insertions(+), 33 deletions(-) diff --git a/contracts/core/defaults/WitnetRequestBoardTrustableBase.sol b/contracts/core/defaults/WitnetRequestBoardTrustableBase.sol index 5819d6b2..f8056eb5 100644 --- a/contracts/core/defaults/WitnetRequestBoardTrustableBase.sol +++ b/contracts/core/defaults/WitnetRequestBoardTrustableBase.sol @@ -213,32 +213,6 @@ abstract contract WitnetRequestBoardTrustableBase _resultMaxSize ); } - - /// @notice Estimates the actual earnings (or loss), in WEI, that a reporter would get by reporting result to given query, - /// @notice based on the gas price of the calling transaction. Data requesters should consider upgrading the reward on - /// @notice queries providing no actual earnings. - /// @dev Fails if the query does not exist, or if deleted. - function estimateQueryEarnings(uint256 _witnetQueryId, uint256 _gasPrice) - virtual override - external view - returns (int256 _earnings) - { - WitnetV2.Request storage __request = __seekQueryRequest(_witnetQueryId); - - _earnings = int(__request.evmReward); - uint96 _callbackGasLimit = __request.unpackCallbackGasLimit(); - if (_callbackGasLimit > 0) { - _earnings -= int(estimateBaseFeeWithCallback( - _gasPrice, - _callbackGasLimit - )); - } else { - _earnings -= int(estimateBaseFee( - _gasPrice, - __request.RAD - )); - } - } /// Retrieves copy of all response data related to a previously posted request, removing the whole query from storage. /// @dev Fails if the `_witnetQueryId` is not in 'Reported' status, or called from an address different to @@ -554,6 +528,26 @@ abstract contract WitnetRequestBoardTrustableBase // ================================================================================================================ // --- Full implementation of IWitnetRequestBoardReporter --------------------------------------------------------- + /// @notice Estimates the actual earnings (or loss), in WEI, that a reporter would get by reporting result to given query, + /// @notice based on the gas price of the calling transaction. Data requesters should consider upgrading the reward on + /// @notice queries providing no actual earnings. + /// @dev Fails if the query does not exist, or if deleted. + function estimateQueryEarnings(uint256[] calldata _witnetQueryIds, uint256 _gasPrice) + virtual override + external view + returns (int256 _earnings) + { + uint256 _expenses; uint256 _revenues; + for (uint _ix = 0; _ix < _witnetQueryIds.length; _ix ++) { + if (_statusOf(_witnetQueryIds[_ix]) == WitnetV2.QueryStatus.Posted) { + WitnetV2.Request storage __request = __seekQueryRequest(_witnetQueryIds[_ix]); + _revenues += __request.evmReward; + _expenses += _gasPrice * __request.unpackCallbackGasLimit(); + } + } + return int256(_revenues) - int256(_expenses); + } + /// Reports the Witnet-provable result to a previously posted request. /// @dev Will assume `block.timestamp` as the timestamp at which the request was solved. /// @dev Fails if: diff --git a/contracts/interfaces/V2/IWitnetRequestBoard.sol b/contracts/interfaces/V2/IWitnetRequestBoard.sol index b90bdc04..aa4ec2d1 100644 --- a/contracts/interfaces/V2/IWitnetRequestBoard.sol +++ b/contracts/interfaces/V2/IWitnetRequestBoard.sol @@ -21,13 +21,7 @@ interface IWitnetRequestBoard { /// @param gasPrice Expected gas price to pay upon posting the data request. /// @param callbackGasLimit Maximum gas to be spent when reporting the data request result. function estimateBaseFeeWithCallback(uint256 gasPrice, uint96 callbackGasLimit) external view returns (uint256); - - /// @notice Estimates the actual earnings (or loss), in WEI, that a reporter would get by reporting result to given query, - /// @notice based on the gas price of the calling transaction. Data requesters should consider upgrading the reward on - /// @notice queries providing no actual earnings. - /// @dev Fails if the query does not exist, or if deleted. - function estimateQueryEarnings(uint256 queryId, uint256 gasPrice) external view returns (int256); - + /// @notice Retrieves a copy of all Witnet-provable data related to a previously posted request, /// removing the whole query from the WRB storage. /// @dev Fails if the query was not in 'Reported' status, or called from an address different to diff --git a/contracts/interfaces/V2/IWitnetRequestBoardReporter.sol b/contracts/interfaces/V2/IWitnetRequestBoardReporter.sol index ede92a21..3be90abd 100644 --- a/contracts/interfaces/V2/IWitnetRequestBoardReporter.sol +++ b/contracts/interfaces/V2/IWitnetRequestBoardReporter.sol @@ -6,6 +6,12 @@ pragma solidity >=0.7.0 <0.9.0; /// @author The Witnet Foundation. interface IWitnetRequestBoardReporter { + /// @notice Estimates the actual earnings (or loss), in WEI, that a reporter would get by reporting result to given query, + /// @notice based on the gas price of the calling transaction. Data requesters should consider upgrading the reward on + /// @notice queries providing no actual earnings. + /// @dev Fails if the query does not exist, or if deleted. + function estimateQueryEarnings(uint256[] calldata queryIds, uint256 gasPrice) external view returns (int256); + /// @notice Reports the Witnet-provided result to a previously posted request. /// @dev Will assume `block.timestamp` as the timestamp at which the request was solved. /// @dev Fails if: