Skip to content

Commit

Permalink
chore: refactor UsingWitnet internal methods
Browse files Browse the repository at this point in the history
  • Loading branch information
guidiaz committed Nov 30, 2023
1 parent ff0e351 commit b8e74c8
Show file tree
Hide file tree
Showing 6 changed files with 95 additions and 17 deletions.
24 changes: 24 additions & 0 deletions contracts/apps/UsingWitnet.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}
}
11 changes: 1 addition & 10 deletions contracts/apps/UsingWitnetRandomness.sol
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ abstract contract UsingWitnetRandomness
using WitnetV2 for WitnetV2.RadonSLA;

bytes32 internal immutable __witnetRandomnessRadHash;
bytes32 private __defaultRandomizePackedSLA;

constructor(
WitnetRequestBoard _wrb,
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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();
}
}
15 changes: 14 additions & 1 deletion contracts/apps/UsingWitnetRequest.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -22,6 +25,7 @@ abstract contract UsingWitnetRequest
dataRequest = _witnetRequest;
__witnetResultMaxSize = _witnetRequest.resultDataMaxSize();
__witnetRequestRadHash = _witnetRequest.radHash();
__witnetSetDefaultSLA(_defaultSLA);
}

function _witnetEstimateBaseFee()
Expand All @@ -47,4 +51,13 @@ abstract contract UsingWitnetRequest
);
}

function __witnetRequestData(uint256 _witnetEvmReward)
virtual internal returns (uint256)
{
return __witnetRequestData(
_witnetEvmReward,
_witnetDefaultSLA(),
__witnetRequestRadHash
);
}
}
18 changes: 17 additions & 1 deletion contracts/apps/UsingWitnetRequestTemplate.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand All @@ -20,6 +23,7 @@ abstract contract UsingWitnetRequestTemplate
);
dataRequestTemplate = _requestTemplate;
__witnetResultMaxSize = _requestTemplate.resultDataMaxSize();
__witnetSetDefaultSLA(_defaultSLA);
}

function _witnetBuildRadHash(string[][] memory _witnetRequestArgs)
Expand Down Expand Up @@ -58,4 +62,16 @@ abstract contract UsingWitnetRequestTemplate
);
}

function __witnetRequestData(
uint256 _witnetEvmReward,
string[][] memory _witnetRequestArgs
)
virtual internal returns (uint256)
{
return __witnetRequestData(
_witnetEvmReward,
_witnetBuildRadHash(_witnetRequestArgs)
);
}

}
8 changes: 6 additions & 2 deletions contracts/apps/WitnetRequestConsumer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
36 changes: 33 additions & 3 deletions contracts/apps/WitnetRequestTemplateConsumer.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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,
Expand All @@ -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)
);
}

}

0 comments on commit b8e74c8

Please sign in to comment.