From b33db4ef109d65c9abe1e933154b5f54bcddae2a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Guillermo=20D=C3=ADaz?= Date: Wed, 29 Nov 2023 11:52:34 +0100 Subject: [PATCH] feat: add mocking contracts --- .../WitnetRequestBoardTrustableBase.sol | 19 +++++++----- .../defaults/WitnetRequestFactoryDefault.sol | 1 - contracts/mocks/WitnetBytecodesMock.sol | 15 ++++++++++ contracts/mocks/WitnetPriceFeedsMock.sol | 15 ++++++++++ contracts/mocks/WitnetRequestBoardMock.sol | 30 +++++++++++++++++++ contracts/mocks/WitnetRequestFactoryMock.sol | 21 +++++++++++++ 6 files changed, 92 insertions(+), 9 deletions(-) create mode 100644 contracts/mocks/WitnetBytecodesMock.sol create mode 100644 contracts/mocks/WitnetPriceFeedsMock.sol create mode 100644 contracts/mocks/WitnetRequestBoardMock.sol create mode 100644 contracts/mocks/WitnetRequestFactoryMock.sol diff --git a/contracts/core/defaults/WitnetRequestBoardTrustableBase.sol b/contracts/core/defaults/WitnetRequestBoardTrustableBase.sol index 073e357a..fd4bc1ee 100644 --- a/contracts/core/defaults/WitnetRequestBoardTrustableBase.sol +++ b/contracts/core/defaults/WitnetRequestBoardTrustableBase.sol @@ -37,9 +37,9 @@ abstract contract WitnetRequestBoardTrustableBase using WitnetV2 for WitnetV2.Response; bytes4 public immutable override specs = type(IWitnetRequestBoard).interfaceId; - - WitnetRequestFactory immutable public override factory; WitnetBytecodes immutable public override registry; + + WitnetRequestFactory immutable private __factory; modifier checkCallbackRecipient(address _addr, uint96 _callbackGasLimit) { require( @@ -76,8 +76,7 @@ abstract contract WitnetRequestBoardTrustableBase "io.witnet.proxiable.board" ) { - assert(address(_factory) != address(0) && address(_registry) != address(0)); - factory = _factory; + __factory = _factory; registry = _registry; } @@ -104,6 +103,10 @@ abstract contract WitnetRequestBoardTrustableBase return bytes4(keccak256(abi.encode(address(this), block.chainid))); } + function factory() virtual override public view returns (WitnetRequestFactory) { + return __factory; + } + // ================================================================================================================ // --- Yet to be implemented virtual methods ---------------------------------------------------------------------- @@ -158,16 +161,16 @@ abstract contract WitnetRequestBoardTrustableBase __storage().base = base(); require( - address(factory).code.length > 0, + address(__factory).code.length > 0, "WitnetRequestBoardTrustableBase: inexistent factory" ); require( - factory.specs() == type(IWitnetRequestFactory).interfaceId, + __factory.specs() == type(IWitnetRequestFactory).interfaceId, "WitnetRequestBoardTrustableBase: uncompliant factory" ); require( - address(factory.witnet()) == address(this) - && address(factory.registry()) == address(registry), + address(__factory.witnet()) == address(this) + && address(__factory.registry()) == address(registry), "WitnetRequestBoardTrustableBase: discordant factory" ); diff --git a/contracts/core/defaults/WitnetRequestFactoryDefault.sol b/contracts/core/defaults/WitnetRequestFactoryDefault.sol index a3ef1b11..6bc54b6f 100644 --- a/contracts/core/defaults/WitnetRequestFactoryDefault.sol +++ b/contracts/core/defaults/WitnetRequestFactoryDefault.sol @@ -60,7 +60,6 @@ contract WitnetRequestFactoryDefault "io.witnet.requests.factory" ) { - assert(address(_witnet) != address(0) && address(_registry) != address(0)); witnet = _witnet; registry = _registry; // let logic contract be used as a factory, while avoiding further initializations: diff --git a/contracts/mocks/WitnetBytecodesMock.sol b/contracts/mocks/WitnetBytecodesMock.sol new file mode 100644 index 00000000..6b50c526 --- /dev/null +++ b/contracts/mocks/WitnetBytecodesMock.sol @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: MIT + +pragma solidity >=0.7.0 <0.9.0; +pragma experimental ABIEncoderV2; + +import "../core/defaults/WitnetBytecodesDefault.sol"; + +contract WitnetBytecodesMock is WitnetBytecodesDefault { + constructor() + WitnetBytecodesDefault( + false, + bytes32("mocked") + ) + {} +} diff --git a/contracts/mocks/WitnetPriceFeedsMock.sol b/contracts/mocks/WitnetPriceFeedsMock.sol new file mode 100644 index 00000000..1656c0bb --- /dev/null +++ b/contracts/mocks/WitnetPriceFeedsMock.sol @@ -0,0 +1,15 @@ +// SPDX-License-Identifier: MIT + +pragma solidity >=0.7.0 <0.9.0; +pragma experimental ABIEncoderV2; + +import "../apps/WitnetPriceFeeds.sol"; + +contract WitnetPriceFeedsMock is WitnetPriceFeeds { + constructor(WitnetRequestBoard _wrb) + WitnetPriceFeeds( + msg.sender, + _wrb + ) + {} +} diff --git a/contracts/mocks/WitnetRequestBoardMock.sol b/contracts/mocks/WitnetRequestBoardMock.sol new file mode 100644 index 00000000..03ba15c8 --- /dev/null +++ b/contracts/mocks/WitnetRequestBoardMock.sol @@ -0,0 +1,30 @@ +// SPDX-License-Identifier: MIT + +pragma solidity >=0.7.0 <0.9.0; +pragma experimental ABIEncoderV2; + +import "./WitnetRequestFactoryMock.sol"; +import "../core/defaults/WitnetRequestBoardTrustableDefault.sol"; + +contract WitnetRequestBoardMock + is + WitnetRequestBoardTrustableDefault +{ + WitnetRequestFactory private __factory; + + constructor(WitnetBytecodesMock _registry) + WitnetRequestBoardTrustableDefault( + WitnetRequestFactory(address(0)), + WitnetBytecodes(address(_registry)), + false, + bytes32("mocked"), + 60000, 65000, 70000, 20000 + ) + { + __factory = WitnetRequestFactory(address(new WitnetRequestFactoryMock(_registry))); + } + + function factory() override public view returns (WitnetRequestFactory) { + return __factory; + } +} \ No newline at end of file diff --git a/contracts/mocks/WitnetRequestFactoryMock.sol b/contracts/mocks/WitnetRequestFactoryMock.sol new file mode 100644 index 00000000..00481991 --- /dev/null +++ b/contracts/mocks/WitnetRequestFactoryMock.sol @@ -0,0 +1,21 @@ +// SPDX-License-Identifier: MIT + +pragma solidity >=0.7.0 <0.9.0; +pragma experimental ABIEncoderV2; + +import "./WitnetBytecodesMock.sol"; +import "../core/defaults/WitnetRequestFactoryDefault.sol"; + +contract WitnetRequestFactoryMock + is + WitnetRequestFactoryDefault +{ + constructor (WitnetBytecodesMock _registry) + WitnetRequestFactoryDefault( + WitnetRequestBoard(msg.sender), + WitnetBytecodes(address(_registry)), + false, + bytes32("mocked") + ) + {} +} \ No newline at end of file