Skip to content

Commit

Permalink
refactor: IWRB.estimateQueryEarnings -> IWRBReporter.estimateQueryEar…
Browse files Browse the repository at this point in the history
…nings
  • Loading branch information
guidiaz committed Dec 1, 2023
1 parent 0d9fc0c commit 71e7973
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 33 deletions.
46 changes: 20 additions & 26 deletions contracts/core/defaults/WitnetRequestBoardTrustableBase.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
8 changes: 1 addition & 7 deletions contracts/interfaces/V2/IWitnetRequestBoard.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions contracts/interfaces/V2/IWitnetRequestBoardReporter.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down

0 comments on commit 71e7973

Please sign in to comment.