Skip to content

Commit

Permalink
feat: implement a realitio mock controlled by a trusted user group
Browse files Browse the repository at this point in the history
  • Loading branch information
0xferit committed Oct 14, 2024
1 parent 7478299 commit 539f31e
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 42 deletions.
8 changes: 4 additions & 4 deletions script/Counter.s.sol → script/MockRealitio.s.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
14 changes: 0 additions & 14 deletions src/Counter.sol

This file was deleted.

56 changes: 56 additions & 0 deletions src/MockRealitio.sol
Original file line number Diff line number Diff line change
@@ -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;
}
}
24 changes: 0 additions & 24 deletions test/Counter.t.sol

This file was deleted.

42 changes: 42 additions & 0 deletions test/MockRealitio.t.sol
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit 539f31e

Please sign in to comment.