Skip to content

Commit

Permalink
feat: UsingWitnet._witnetBaseFeeOverheadPercentage()
Browse files Browse the repository at this point in the history
  • Loading branch information
guidiaz committed Dec 1, 2023
1 parent b8e74c8 commit 0d9fc0c
Show file tree
Hide file tree
Showing 8 changed files with 111 additions and 71 deletions.
34 changes: 28 additions & 6 deletions contracts/apps/UsingWitnet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,32 @@ abstract contract UsingWitnet
is
IWitnetRequestBoardEvents
{
/// @dev Immutable reference to the Witnet Request Board contract.
WitnetRequestBoard internal immutable __witnet;

/// @dev Default Security-Level Agreement parameters to be fulfilled by the Witnet blockchain
/// @dev when solving a data request.
bytes32 private __witnetDefaultPackedSLA;

/// @dev Include an address to specify the WitnetRequestBoard entry point address.
/// @param _wrb The WitnetRequestBoard entry point address.
/// @dev Percentage over base fee to pay on every data request,
/// @dev as to deal with volatility of evmGasPrice and evmWitPrice during the live time of
/// @dev a data request (since being posted until a result gets reported back), at both the EVM and
/// @dev the Witnet blockchain levels, respectivelly.
uint16 private __witnetBaseFeeOverheadPercentage;

/// @param _wrb Address of the WitnetRequestBoard contract.
constructor(WitnetRequestBoard _wrb) {
require(
_wrb.specs() == type(IWitnetRequestBoard).interfaceId,
"UsingWitnet: uncompliant WitnetRequestBoard"
);
__witnet = _wrb;
__witnetDefaultPackedSLA = WitnetV2.toBytes32(WitnetV2.RadonSLA({
witnessingCommitteeSize: 10,
witnessingWitTotalReward: 10 ** 9
witnessingCommitteeSize: 10, // up to 127
witnessingWitTotalReward: 10 ** 9 // 1.0 $WIT
}));

__witnetBaseFeeOverheadPercentage = 10; // defaults to 10%
}

/// @dev Provides a convenient way for client contracts extending this to block the execution of the main logic of the
Expand All @@ -53,11 +64,14 @@ abstract contract UsingWitnet
/// @notice Estimate the minimum reward required for posting a data request, using `tx.gasprice` as a reference.
/// @dev Underestimates if the size of returned data is greater than `_resultMaxSize`.
/// @param _resultMaxSize Maximum expected size of returned data (in bytes).
function _witnetEstimateBaseFee(uint16 _resultMaxSize)
function _witnetEstimateEvmReward(uint16 _resultMaxSize)
virtual internal view
returns (uint256)
{
return __witnet.estimateBaseFee(tx.gasprice, _resultMaxSize);
return (
(100 + _witnetBaseFeeOverheadPercentage())
* __witnet.estimateBaseFee(tx.gasprice, _resultMaxSize)
) / 100;
}

function _witnetCheckQueryResultAuditTrail(uint256 _witnetQueryId)
Expand Down Expand Up @@ -89,6 +103,10 @@ abstract contract UsingWitnet
return WitnetV2.toRadonSLA(__witnetDefaultPackedSLA);
}

function _witnetBaseFeeOverheadPercentage() virtual internal view returns (uint16) {
return __witnetBaseFeeOverheadPercentage;
}

function __witnetRequestData(
uint256 _witnetEvmReward,
WitnetV2.RadonSLA memory _witnetQuerySLA,
Expand Down Expand Up @@ -116,4 +134,8 @@ abstract contract UsingWitnet
function __witnetSetDefaultSLA(WitnetV2.RadonSLA memory _defaultSLA) virtual internal {
__witnetDefaultPackedSLA = WitnetV2.toBytes32(_defaultSLA);
}

function __witnetSetBaseFeeOverheadPercentage(uint16 _baseFeeOverheadPercentage) virtual internal {
__witnetBaseFeeOverheadPercentage = _baseFeeOverheadPercentage;
}
}
14 changes: 10 additions & 4 deletions contracts/apps/UsingWitnetRandomness.sol
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,18 @@ abstract contract UsingWitnetRandomness

bytes32 internal immutable __witnetRandomnessRadHash;

/// @param _wrb Address of the WitnetRequestBoard contract.
/// @param _baseFeeOverheadPercentage Percentage over base fee to pay as on every data request.
/// @param _callbackGasLimit Maximum gas to be spent by the IWitnetConsumer's callback methods.
/// @param _defaultSLA Default Security-Level Agreement parameters to be fulfilled by the Witnet blockchain.
constructor(
WitnetRequestBoard _wrb,
uint96 _randomizeCallbackGasLimit,
uint16 _baseFeeOverheadPercentage,
uint96 _callbackGasLimit,
WitnetV2.RadonSLA memory _defaultSLA
)
UsingWitnet(_wrb)
WitnetConsumer(_randomizeCallbackGasLimit)
WitnetConsumer(_callbackGasLimit)
{
// On-chain building of the Witnet Randomness Request:
{
Expand Down Expand Up @@ -59,10 +64,11 @@ abstract contract UsingWitnetRandomness
}
// Settle default randomize SLA:
__witnetSetDefaultSLA(_defaultSLA);
__witnetSetBaseFeeOverheadPercentage(_baseFeeOverheadPercentage);
}

function _witnetEstimateRandomizeBaseFee() internal view returns (uint256) {
return _witnetEstimateBaseFee(32);
function _witnetEstimateEvmReward() virtual override internal view returns (uint256) {
return _witnetEstimateEvmReward(32);
}

function _witnetRandomUniformUint32(uint32 _range, uint256 _nonce, bytes32 _seed) internal pure returns (uint32) {
Expand Down
12 changes: 7 additions & 5 deletions contracts/apps/UsingWitnetRequest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@ abstract contract UsingWitnetRequest
bytes32 immutable internal __witnetRequestRadHash;
uint16 immutable internal __witnetResultMaxSize;

/// @param _witnetRequest Address of the WitnetRequest contract containing the actual data request.
/// @param _baseFeeOverheadPercentage Percentage over base fee to pay as on every data request.
/// @param _defaultSLA Default Security-Level Agreement parameters to be fulfilled by the Witnet blockchain.
constructor (
WitnetRequest _witnetRequest,
uint16 _baseFeeOverheadPercentage,
WitnetV2.RadonSLA memory _defaultSLA
)
UsingWitnet(_witnetRequest.witnet())
Expand All @@ -26,16 +30,14 @@ abstract contract UsingWitnetRequest
__witnetResultMaxSize = _witnetRequest.resultDataMaxSize();
__witnetRequestRadHash = _witnetRequest.radHash();
__witnetSetDefaultSLA(_defaultSLA);
__witnetSetBaseFeeOverheadPercentage(_baseFeeOverheadPercentage);
}

function _witnetEstimateBaseFee()
function _witnetEstimateEvmReward()
virtual internal view
returns (uint256)
{
return __witnet.estimateBaseFee(
tx.gasprice,
__witnetResultMaxSize
);
return _witnetEstimateEvmReward(__witnetResultMaxSize);
}

function __witnetRequestData(
Expand Down
22 changes: 12 additions & 10 deletions contracts/apps/UsingWitnetRequestTemplate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,24 @@ abstract contract UsingWitnetRequestTemplate

uint16 immutable internal __witnetResultMaxSize;

/// @param _witnetRequestTemplate Address of the WitnetRequestTemplate from which actual data requests will get built.
/// @param _baseFeeOverheadPercentage Percentage over base fee to pay as on every data request.
/// @param _defaultSLA Default Security-Level Agreement parameters to be fulfilled by the Witnet blockchain.
constructor (
WitnetRequestTemplate _requestTemplate,
WitnetRequestTemplate _witnetRequestTemplate,
uint16 _baseFeeOverheadPercentage,
WitnetV2.RadonSLA memory _defaultSLA
)
UsingWitnet(_requestTemplate.witnet())
UsingWitnet(_witnetRequestTemplate.witnet())
{
require(
_requestTemplate.specs() == type(WitnetRequestTemplate).interfaceId,
_witnetRequestTemplate.specs() == type(WitnetRequestTemplate).interfaceId,
"UsingWitnetRequestTemplate: uncompliant WitnetRequestTemplate"
);
dataRequestTemplate = _requestTemplate;
__witnetResultMaxSize = _requestTemplate.resultDataMaxSize();
dataRequestTemplate = _witnetRequestTemplate;
__witnetResultMaxSize = _witnetRequestTemplate.resultDataMaxSize();
__witnetSetDefaultSLA(_defaultSLA);
__witnetSetBaseFeeOverheadPercentage(_baseFeeOverheadPercentage);
}

function _witnetBuildRadHash(string[][] memory _witnetRequestArgs)
Expand All @@ -38,14 +43,11 @@ abstract contract UsingWitnetRequestTemplate
return WitnetRequest(dataRequestTemplate.buildRequest(_witnetRequestArgs));
}

function _witnetEstimateBaseFee()
function _witnetEstimateEvmReward()
virtual internal view
returns (uint256)
{
return __witnet.estimateBaseFee(
tx.gasprice,
__witnetResultMaxSize
);
return _witnetEstimateEvmReward(__witnetResultMaxSize);
}

function __witnetRequestData(
Expand Down
44 changes: 28 additions & 16 deletions contracts/apps/WitnetConsumer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,18 @@ abstract contract WitnetConsumer
is
IWitnetConsumer,
UsingWitnet
{
uint96 private immutable __witnetReportCallbackGasLimit;
{
/// @dev Maximum gas to be spent by the IWitnetConsumer's callback methods.
uint96 private immutable __witnetCallbackGasLimit;

modifier onlyFromWitnet {
require(msg.sender == address(__witnet), "WitnetConsumer: unauthorized");
_;
}

constructor (uint96 _maxCallbackGas) {
__witnetReportCallbackGasLimit = _maxCallbackGas;
/// @param _callbackGasLimit Maximum gas to be spent by the IWitnetConsumer's callback methods.
constructor (uint96 _callbackGasLimit) {
__witnetCallbackGasLimit = _callbackGasLimit;
}


Expand All @@ -32,31 +34,41 @@ abstract contract WitnetConsumer
/// ===============================================================================================================
/// --- WitnetConsumer virtual methods ----------------------------------------------------------------------------

function _witnetEstimateBaseFee(uint16)
function _witnetCallbackGasLimit()
virtual internal view
returns (uint96)
{
return __witnetCallbackGasLimit;
}

function _witnetEstimateEvmReward() virtual internal view returns (uint256) {
return (
(100 + _witnetBaseFeeOverheadPercentage())
* __witnet.estimateBaseFeeWithCallback(
tx.gasprice,
_witnetCallbackGasLimit()
)
) / 100;
}

function _witnetEstimateEvmReward(uint16)
virtual override internal view
returns (uint256)
{
return _witnetEstimateBaseFeeWithCallback(_witnetReportCallbackGasLimit());
return _witnetEstimateEvmReward();
}


/// @notice Estimate the minimum reward required for posting a data request, using `tx.gasprice` as a reference.
/// @dev Underestimates if the size of returned data is greater than `_resultMaxSize`.
/// @param _callbackGasLimit Maximum gas to be spent when reporting the data request result.
function _witnetEstimateBaseFeeWithCallback(uint96 _callbackGasLimit)
function _witnetEstimateEvmRewardWithCallback(uint96 _callbackGasLimit)
virtual internal view
returns (uint256)
{
return __witnet.estimateBaseFeeWithCallback(tx.gasprice, _callbackGasLimit);
}

function _witnetReportCallbackGasLimit()
virtual internal view
returns (uint96)
{
return __witnetReportCallbackGasLimit;
}

function __witnetRequestData(
uint256 _witnetEvmReward,
WitnetV2.RadonSLA memory _witnetQuerySLA,
Expand All @@ -68,7 +80,7 @@ abstract contract WitnetConsumer
return __witnet.postRequestWithCallback{value: _witnetEvmReward}(
_witnetRadHash,
_witnetQuerySLA,
__witnetReportCallbackGasLimit
__witnetCallbackGasLimit
);
}

Expand All @@ -82,7 +94,7 @@ abstract contract WitnetConsumer
return __witnet.postRequestWithCallback{value: _witnetEvmReward}(
_witnetRequestBytecode,
_witnetQuerySLA,
__witnetReportCallbackGasLimit
__witnetCallbackGasLimit
);
}

Expand Down
27 changes: 13 additions & 14 deletions contracts/apps/WitnetRequestConsumer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,36 +11,35 @@ abstract contract WitnetRequestConsumer
{
using WitnetCBOR for WitnetCBOR.CBOR;
using WitnetCBOR for WitnetCBOR.CBOR[];


/// @param _witnetRequest Address of the WitnetRequest contract containing the actual data request.
/// @param _baseFeeOverheadPercentage Percentage over base fee to pay as on every data request.
/// @param _callbackGasLimit Maximum gas to be spent by the IWitnetConsumer's callback methods.
/// @param _defaultSLA Default Security-Level Agreement parameters to be fulfilled by the Witnet blockchain.
constructor(
WitnetRequest _witnetRequest,
uint16 _baseFeeOverheadPercentage,
uint96 _callbackGasLimit,
WitnetV2.RadonSLA memory _defaultSLA
)
UsingWitnetRequest(_witnetRequest, _defaultSLA)
UsingWitnetRequest(_witnetRequest, _baseFeeOverheadPercentage, _defaultSLA)
WitnetConsumer(_callbackGasLimit)
{
require(
_witnetEstimateBaseFeeWithCallback(_callbackGasLimit)
> UsingWitnetRequest._witnetEstimateBaseFee(),
"WitnetRequestConsumer: insufficient callback gas limit"
);
}
{}

function _witnetEstimateBaseFee()
virtual override
function _witnetEstimateEvmReward()
virtual override(UsingWitnetRequest, WitnetConsumer)
internal view
returns (uint256)
{
return WitnetConsumer._witnetEstimateBaseFee(__witnetResultMaxSize);
return WitnetConsumer._witnetEstimateEvmReward();
}

function _witnetEstimateBaseFee(uint16 _resultMaxSize)
function _witnetEstimateEvmReward(uint16)
virtual override(UsingWitnet, WitnetConsumer)
internal view
returns (uint256)
{
return WitnetConsumer._witnetEstimateBaseFee(_resultMaxSize);
return WitnetConsumer._witnetEstimateEvmReward();
}

function __witnetRequestData(
Expand Down
28 changes: 13 additions & 15 deletions contracts/apps/WitnetRequestTemplateConsumer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -12,36 +12,34 @@ abstract contract WitnetRequestTemplateConsumer
using WitnetCBOR for WitnetCBOR.CBOR;
using WitnetCBOR for WitnetCBOR.CBOR[];

/// @param _witnetRequestTemplate Address of the WitnetRequestTemplate from which actual data requests will get built.
/// @param _baseFeeOverheadPercentage Percentage over base fee to pay as on every data request.
/// @param _callbackGasLimit Maximum gas to be spent by the IWitnetConsumer's callback methods.
/// @param _defaultSLA Default Security-Level Agreement parameters to be fulfilled by the Witnet blockchain.
constructor(
WitnetRequestTemplate _requestTemplate,
WitnetRequestTemplate _witnetRequestTemplate,
uint16 _baseFeeOverheadPercentage,
uint96 _callbackGasLimit,
WitnetV2.RadonSLA memory _defaultSLA
)
UsingWitnetRequestTemplate(_requestTemplate, _defaultSLA)
UsingWitnetRequestTemplate(_witnetRequestTemplate, _baseFeeOverheadPercentage, _defaultSLA)
WitnetConsumer(_callbackGasLimit)
{
require(
_witnetEstimateBaseFeeWithCallback(_callbackGasLimit)
> UsingWitnetRequestTemplate._witnetEstimateBaseFee(),
"WitnetRequestTemplateConsumer: insufficient callback gas limit"
);

}
{}

function _witnetEstimateBaseFee()
virtual override
function _witnetEstimateEvmReward()
virtual override(UsingWitnetRequestTemplate, WitnetConsumer)
internal view
returns (uint256)
{
return WitnetConsumer._witnetEstimateBaseFee(__witnetResultMaxSize);
return WitnetConsumer._witnetEstimateEvmReward(__witnetResultMaxSize);
}

function _witnetEstimateBaseFee(uint16 _resultMaxSize)
function _witnetEstimateEvmReward(uint16)
virtual override(UsingWitnet, WitnetConsumer)
internal view
returns (uint256)
{
return WitnetConsumer._witnetEstimateBaseFee(_resultMaxSize);
return WitnetConsumer._witnetEstimateEvmReward();
}

function __witnetRequestData(
Expand Down
1 change: 0 additions & 1 deletion contracts/interfaces/V2/IWitnetConsumer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ interface IWitnetConsumer {
/// @dev It should revert if called from any other address different to the WitnetRequestBoard being used
/// @dev by the WitnetConsumer contract.
/// @param witnetQueryId The unique identifier of the Witnet query being reported.
/// @param witnetQueryId The unique identifier of the Witnet query being reported.
/// @param witnetResultTallyHash Hash of the commit/reveal witnessing act that took place in the Witnet blockahin.
/// @param witnetResultTimestamp Timestamp at which the reported value was captured by the Witnet blockchain.
/// @param witnetEvmFinalityBlock EVM block at which the provided data can be considered to be final.
Expand Down

0 comments on commit 0d9fc0c

Please sign in to comment.