From b8e74c830bc6a0aa83103a112ad1c13fb8be5944 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20D=C3=ADaz?= Date: Thu, 30 Nov 2023 18:01:16 +0100 Subject: [PATCH] chore: refactor UsingWitnet internal methods --- contracts/apps/UsingWitnet.sol | 24 +++++++++++++ contracts/apps/UsingWitnetRandomness.sol | 11 +----- contracts/apps/UsingWitnetRequest.sol | 15 +++++++- contracts/apps/UsingWitnetRequestTemplate.sol | 18 +++++++++- contracts/apps/WitnetRequestConsumer.sol | 8 +++-- .../apps/WitnetRequestTemplateConsumer.sol | 36 +++++++++++++++++-- 6 files changed, 95 insertions(+), 17 deletions(-) diff --git a/contracts/apps/UsingWitnet.sol b/contracts/apps/UsingWitnet.sol index f19147db..928f5c24 100644 --- a/contracts/apps/UsingWitnet.sol +++ b/contracts/apps/UsingWitnet.sol @@ -13,6 +13,7 @@ abstract contract UsingWitnet IWitnetRequestBoardEvents { WitnetRequestBoard internal immutable __witnet; + bytes32 private __witnetDefaultPackedSLA; /// @dev Include an address to specify the WitnetRequestBoard entry point address. /// @param _wrb The WitnetRequestBoard entry point address. @@ -22,6 +23,10 @@ abstract contract UsingWitnet "UsingWitnet: uncompliant WitnetRequestBoard" ); __witnet = _wrb; + __witnetDefaultPackedSLA = WitnetV2.toBytes32(WitnetV2.RadonSLA({ + witnessingCommitteeSize: 10, + witnessingWitTotalReward: 10 ** 9 + })); } /// @dev Provides a convenient way for client contracts extending this to block the execution of the main logic of the @@ -80,6 +85,10 @@ abstract contract UsingWitnet return __witnet.getQueryResultError(_witnetQueryId); } + function _witnetDefaultSLA() virtual internal view returns (WitnetV2.RadonSLA memory) { + return WitnetV2.toRadonSLA(__witnetDefaultPackedSLA); + } + function __witnetRequestData( uint256 _witnetEvmReward, WitnetV2.RadonSLA memory _witnetQuerySLA, @@ -92,4 +101,19 @@ abstract contract UsingWitnet _witnetQuerySLA ); } + + function __witnetRequestData( + uint256 _witnetEvmReward, + bytes32 _witnetRadHash + ) + virtual internal returns (uint256) + { + return __witnet.postRequest{value: _witnetEvmReward}( + _witnetRadHash, + _witnetDefaultSLA() + ); + } + function __witnetSetDefaultSLA(WitnetV2.RadonSLA memory _defaultSLA) virtual internal { + __witnetDefaultPackedSLA = WitnetV2.toBytes32(_defaultSLA); + } } diff --git a/contracts/apps/UsingWitnetRandomness.sol b/contracts/apps/UsingWitnetRandomness.sol index 79908f1e..d7b3c9a0 100644 --- a/contracts/apps/UsingWitnetRandomness.sol +++ b/contracts/apps/UsingWitnetRandomness.sol @@ -16,7 +16,6 @@ abstract contract UsingWitnetRandomness using WitnetV2 for WitnetV2.RadonSLA; bytes32 internal immutable __witnetRandomnessRadHash; - bytes32 private __defaultRandomizePackedSLA; constructor( WitnetRequestBoard _wrb, @@ -59,11 +58,7 @@ abstract contract UsingWitnetRandomness ).radHash(); } // Settle default randomize SLA: - __defaultRandomizePackedSLA = _defaultSLA.toBytes32(); - } - - function _witnetDefaultSLA() virtual internal view returns (WitnetV2.RadonSLA memory) { - return __defaultRandomizePackedSLA.toRadonSLA(); + __witnetSetDefaultSLA(_defaultSLA); } function _witnetEstimateRandomizeBaseFee() internal view returns (uint256) { @@ -104,8 +99,4 @@ abstract contract UsingWitnetRandomness function _witnetReadRandomizeFromResultValue(WitnetCBOR.CBOR calldata cborValue) internal pure returns (bytes32) { return cborValue.readBytes().toBytes32(); } - - function __witnetSettleDefaultSLA(WitnetV2.RadonSLA calldata sla) virtual internal { - __defaultRandomizePackedSLA = sla.toBytes32(); - } } \ No newline at end of file diff --git a/contracts/apps/UsingWitnetRequest.sol b/contracts/apps/UsingWitnetRequest.sol index 3e75ca49..3d29baca 100644 --- a/contracts/apps/UsingWitnetRequest.sol +++ b/contracts/apps/UsingWitnetRequest.sol @@ -12,7 +12,10 @@ abstract contract UsingWitnetRequest bytes32 immutable internal __witnetRequestRadHash; uint16 immutable internal __witnetResultMaxSize; - constructor (WitnetRequest _witnetRequest) + constructor ( + WitnetRequest _witnetRequest, + WitnetV2.RadonSLA memory _defaultSLA + ) UsingWitnet(_witnetRequest.witnet()) { require( @@ -22,6 +25,7 @@ abstract contract UsingWitnetRequest dataRequest = _witnetRequest; __witnetResultMaxSize = _witnetRequest.resultDataMaxSize(); __witnetRequestRadHash = _witnetRequest.radHash(); + __witnetSetDefaultSLA(_defaultSLA); } function _witnetEstimateBaseFee() @@ -47,4 +51,13 @@ abstract contract UsingWitnetRequest ); } + function __witnetRequestData(uint256 _witnetEvmReward) + virtual internal returns (uint256) + { + return __witnetRequestData( + _witnetEvmReward, + _witnetDefaultSLA(), + __witnetRequestRadHash + ); + } } \ No newline at end of file diff --git a/contracts/apps/UsingWitnetRequestTemplate.sol b/contracts/apps/UsingWitnetRequestTemplate.sol index c95d591d..17af88bb 100644 --- a/contracts/apps/UsingWitnetRequestTemplate.sol +++ b/contracts/apps/UsingWitnetRequestTemplate.sol @@ -11,7 +11,10 @@ abstract contract UsingWitnetRequestTemplate uint16 immutable internal __witnetResultMaxSize; - constructor (WitnetRequestTemplate _requestTemplate) + constructor ( + WitnetRequestTemplate _requestTemplate, + WitnetV2.RadonSLA memory _defaultSLA + ) UsingWitnet(_requestTemplate.witnet()) { require( @@ -20,6 +23,7 @@ abstract contract UsingWitnetRequestTemplate ); dataRequestTemplate = _requestTemplate; __witnetResultMaxSize = _requestTemplate.resultDataMaxSize(); + __witnetSetDefaultSLA(_defaultSLA); } function _witnetBuildRadHash(string[][] memory _witnetRequestArgs) @@ -58,4 +62,16 @@ abstract contract UsingWitnetRequestTemplate ); } + function __witnetRequestData( + uint256 _witnetEvmReward, + string[][] memory _witnetRequestArgs + ) + virtual internal returns (uint256) + { + return __witnetRequestData( + _witnetEvmReward, + _witnetBuildRadHash(_witnetRequestArgs) + ); + } + } \ No newline at end of file diff --git a/contracts/apps/WitnetRequestConsumer.sol b/contracts/apps/WitnetRequestConsumer.sol index e8f4e0ec..4b9ca68c 100644 --- a/contracts/apps/WitnetRequestConsumer.sol +++ b/contracts/apps/WitnetRequestConsumer.sol @@ -12,8 +12,12 @@ abstract contract WitnetRequestConsumer using WitnetCBOR for WitnetCBOR.CBOR; using WitnetCBOR for WitnetCBOR.CBOR[]; - constructor(WitnetRequest _witnetRequest, uint96 _callbackGasLimit) - UsingWitnetRequest(_witnetRequest) + constructor( + WitnetRequest _witnetRequest, + uint96 _callbackGasLimit, + WitnetV2.RadonSLA memory _defaultSLA + ) + UsingWitnetRequest(_witnetRequest, _defaultSLA) WitnetConsumer(_callbackGasLimit) { require( diff --git a/contracts/apps/WitnetRequestTemplateConsumer.sol b/contracts/apps/WitnetRequestTemplateConsumer.sol index 7adad22c..01195b6f 100644 --- a/contracts/apps/WitnetRequestTemplateConsumer.sol +++ b/contracts/apps/WitnetRequestTemplateConsumer.sol @@ -12,8 +12,12 @@ abstract contract WitnetRequestTemplateConsumer using WitnetCBOR for WitnetCBOR.CBOR; using WitnetCBOR for WitnetCBOR.CBOR[]; - constructor(WitnetRequestTemplate _requestTemplate, uint96 _callbackGasLimit) - UsingWitnetRequestTemplate(_requestTemplate) + constructor( + WitnetRequestTemplate _requestTemplate, + uint96 _callbackGasLimit, + WitnetV2.RadonSLA memory _defaultSLA + ) + UsingWitnetRequestTemplate(_requestTemplate, _defaultSLA) WitnetConsumer(_callbackGasLimit) { require( @@ -55,6 +59,20 @@ abstract contract WitnetRequestTemplateConsumer ); } + function __witnetRequestData( + uint256 _witnetEvmReward, + bytes32 _witnetRadHash + ) + virtual override internal + returns (uint256) + { + return WitnetConsumer.__witnetRequestData( + _witnetEvmReward, + _witnetDefaultSLA(), + _witnetRadHash + ); + } + function __witnetRequestData( uint256 _witnetEvmReward, WitnetV2.RadonSLA memory _witnetQuerySLA, @@ -70,6 +88,18 @@ abstract contract WitnetRequestTemplateConsumer ); } - + function __witnetRequestData( + uint256 _witnetEvmReward, + string[][] memory _witnetRequestArgs + ) + virtual override internal + returns (uint256) + { + return WitnetConsumer.__witnetRequestData( + _witnetEvmReward, + _witnetDefaultSLA(), + _witnetBuildRadHash(_witnetRequestArgs) + ); + } }