From 539f31ec07af3a58ce9ae634b3c4ea9858f21621 Mon Sep 17 00:00:00 2001 From: Ferit <0xferittuncer@gmail.com> Date: Mon, 14 Oct 2024 13:27:58 +0100 Subject: [PATCH] feat: implement a realitio mock controlled by a trusted user group --- script/{Counter.s.sol => MockRealitio.s.sol} | 8 +-- src/Counter.sol | 14 ----- src/MockRealitio.sol | 56 ++++++++++++++++++++ test/Counter.t.sol | 24 --------- test/MockRealitio.t.sol | 42 +++++++++++++++ 5 files changed, 102 insertions(+), 42 deletions(-) rename script/{Counter.s.sol => MockRealitio.s.sol} (58%) delete mode 100644 src/Counter.sol create mode 100644 src/MockRealitio.sol delete mode 100644 test/Counter.t.sol create mode 100644 test/MockRealitio.t.sol diff --git a/script/Counter.s.sol b/script/MockRealitio.s.sol similarity index 58% rename from script/Counter.s.sol rename to script/MockRealitio.s.sol index cdc1fe9..f237b5f 100644 --- a/script/Counter.s.sol +++ b/script/MockRealitio.s.sol @@ -2,17 +2,17 @@ pragma solidity ^0.8.13; import {Script, console} from "forge-std/Script.sol"; -import {Counter} from "../src/Counter.sol"; +import {MockRealitio} from "../src/MockRealitio.sol"; -contract CounterScript is Script { - Counter public counter; +contract MockRealitioScript is Script { + MockRealitio public mockRealitio; function setUp() public {} function run() public { vm.startBroadcast(); - counter = new Counter(); + mockRealitio = new MockRealitio(); vm.stopBroadcast(); } diff --git a/src/Counter.sol b/src/Counter.sol deleted file mode 100644 index aded799..0000000 --- a/src/Counter.sol +++ /dev/null @@ -1,14 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -contract Counter { - uint256 public number; - - function setNumber(uint256 newNumber) public { - number = newNumber; - } - - function increment() public { - number++; - } -} diff --git a/src/MockRealitio.sol b/src/MockRealitio.sol new file mode 100644 index 0000000..7c2b17d --- /dev/null +++ b/src/MockRealitio.sol @@ -0,0 +1,56 @@ +pragma solidity ^0.8.0; + +interface IRealitio { + function getContentHash(bytes32 questionId) external view returns (bytes32); + function getOpeningTS(bytes32 questionId) external view returns (uint32); + function resultFor(bytes32 questionId) external view returns (bytes32); +} + +contract MockRealitio is IRealitio { + mapping(address => bool) public owners; + mapping(bytes32 => bytes32) private contentHashes; + mapping(bytes32 => uint32) private openingTimestamps; + mapping(bytes32 => bytes32) private results; + + constructor() { + owners[msg.sender] = true; + } + + modifier onlyOwners() { + require(owners[msg.sender], "Only owner can call this function"); + _; + } + + function addOwner(address newOwner) external onlyOwners { + owners[newOwner] = true; + } + + function removeOwner(address ownerToRemove) external onlyOwners { + require(msg.sender != ownerToRemove, "Owner cannot remove themselves"); + owners[ownerToRemove] = false; + } + + function getContentHash(bytes32 questionId) external view returns (bytes32) { + return contentHashes[questionId]; + } + + function getOpeningTS(bytes32 questionId) external view returns (uint32) { + return openingTimestamps[questionId]; + } + + function resultFor(bytes32 questionId) external view returns (bytes32) { + return results[questionId]; + } + + function setContentHash(bytes32 questionId, bytes32 contentHash) external onlyOwners { + contentHashes[questionId] = contentHash; + } + + function setOpeningTS(bytes32 questionId, uint32 openingTS) external onlyOwners { + openingTimestamps[questionId] = openingTS; + } + + function setResult(bytes32 questionId, bytes32 result) external onlyOwners { + results[questionId] = result; + } +} diff --git a/test/Counter.t.sol b/test/Counter.t.sol deleted file mode 100644 index 54b724f..0000000 --- a/test/Counter.t.sol +++ /dev/null @@ -1,24 +0,0 @@ -// SPDX-License-Identifier: UNLICENSED -pragma solidity ^0.8.13; - -import {Test, console} from "forge-std/Test.sol"; -import {Counter} from "../src/Counter.sol"; - -contract CounterTest is Test { - Counter public counter; - - function setUp() public { - counter = new Counter(); - counter.setNumber(0); - } - - function test_Increment() public { - counter.increment(); - assertEq(counter.number(), 1); - } - - function testFuzz_SetNumber(uint256 x) public { - counter.setNumber(x); - assertEq(counter.number(), x); - } -} diff --git a/test/MockRealitio.t.sol b/test/MockRealitio.t.sol new file mode 100644 index 0000000..5d832d7 --- /dev/null +++ b/test/MockRealitio.t.sol @@ -0,0 +1,42 @@ +// SPDX-License-Identifier: UNLICENSED +pragma solidity ^0.8.13; + +import {Test} from "forge-std/Test.sol"; +import "forge-std/console2.sol"; +import {MockRealitio} from "../src/MockRealitio.sol"; + +contract MockRealitioTest is Test { + MockRealitio public mockRealitio; + + function setUp() public { + mockRealitio = new MockRealitio(); + } + + function test_AddOwner() public { + address newOwner = address(0x123); + mockRealitio.addOwner(newOwner); + assertTrue(mockRealitio.owners(newOwner)); + } + + function test_RemoveOwner() public { + address ownerToRemove = address(0x456); + mockRealitio.addOwner(ownerToRemove); + mockRealitio.removeOwner(ownerToRemove); + assertFalse(mockRealitio.owners(ownerToRemove)); + } + + function testFuzz_SetContentHash(bytes32 questionId, bytes32 contentHash) public { + mockRealitio.setContentHash(questionId, contentHash); + assertEq(mockRealitio.getContentHash(questionId), contentHash); + } + + function testFuzz_SetOpeningTS(bytes32 questionId, uint32 openingTS) public { + mockRealitio.setOpeningTS(questionId, openingTS); + assertEq(mockRealitio.getOpeningTS(questionId), openingTS); + } + + function testFuzz_SetResult(bytes32 questionId, bytes32 result) public { + mockRealitio.setResult(questionId, result); + assertEq(mockRealitio.resultFor(questionId), result); + } +}