Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: oracle contract #6

Merged
merged 2 commits into from
Nov 20, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions solidity/contracts/BlockHeaderOracle.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity =0.8.19;

/**
* @title BlockHeaderOracle
* @notice This contract's purpose is to return the latest stored L1 block header and timestamp
* @notice Every X minutes a "magical" off-chain agent provides the latest block header and timestamp
*/
contract BlockHeaderOracle {
/**
* @notice Emits when the block header and timestamp are updated
*/
event BlockHeaderUpdated(bytes _blockHeader, uint256 _blockTimestamp, uint256 _blockNumber);

/**
* @notice The block header
*/
bytes public blockHeader;

/**
* @notice The block timestamp of the latest block header
*/
uint256 public blockTimestamp;

/**
* @notice Updates the block header and timestamp
* @param _blockHeader The block header
* @param _blockTimestamp The block timestamp
* @param _blockNumber The block number
*/
function updateBlockHeader(bytes memory _blockHeader, uint256 _blockTimestamp, uint256 _blockNumber) external {
blockHeader = _blockHeader;
blockTimestamp = _blockTimestamp;
excaliborr marked this conversation as resolved.
Show resolved Hide resolved

emit BlockHeaderUpdated(_blockHeader, _blockTimestamp, _blockNumber);
}

/**
* @notice Returns the latest block header and timestamp
* @return _blockHeader The block header
* @return _blockTimestamp The block timestamp
*/
function getLatestBlockHeader() external view returns (bytes memory _blockHeader, uint256 _blockTimestamp) {
return (blockHeader, blockTimestamp);
}
}
9 changes: 9 additions & 0 deletions solidity/test/e2e/Common.sol
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@ import {DSTestPlus} from '@defi-wonderland/solidity-utils/solidity/test/DSTestPl
import {IERC20} from 'isolmate/interfaces/tokens/IERC20.sol';
import {SafeProxy} from 'safe-contracts/proxies/SafeProxy.sol';
import {Enum} from 'safe-contracts/common/Enum.sol';

import {StorageMirror} from 'contracts/StorageMirror.sol';
import {UpdateStorageMirrorGuard} from 'contracts/UpdateStorageMirrorGuard.sol';
import {GuardCallbackModule} from 'contracts/GuardCallbackModule.sol';
import {BlockHeaderOracle} from 'contracts/BlockHeaderOracle.sol';

import {IGuardCallbackModule} from 'interfaces/IGuardCallbackModule.sol';
import {ISafe} from 'interfaces/ISafe.sol';

import {IGnosisSafeProxyFactory} from 'test/e2e/IGnosisSafeProxyFactory.sol';
import {TestConstants} from 'test/utils/TestConstants.sol';
import {ContractDeploymentAddress} from 'test/utils/ContractDeploymentAddress.sol';
Expand All @@ -25,6 +29,7 @@ contract CommonE2EBase is DSTestPlus, TestConstants {
StorageMirror public storageMirror;
UpdateStorageMirrorGuard public updateStorageMirrorGuard;
GuardCallbackModule public guardCallbackModule;
BlockHeaderOracle public oracle;
ISafe public safe;
IGnosisSafeProxyFactory public gnosisSafeProxyFactory = IGnosisSafeProxyFactory(GNOSIS_SAFE_PROXY_FACTORY);

Expand Down Expand Up @@ -52,6 +57,10 @@ contract CommonE2EBase is DSTestPlus, TestConstants {
updateStorageMirrorGuard = new UpdateStorageMirrorGuard(guardCallbackModule); // deployer nonce 2
label(address(updateStorageMirrorGuard), 'UpdateStorageMirrorGuard');

vm.prank(deployer);
oracle = new BlockHeaderOracle(); // deployer nonce 3
label(address(oracle), 'MockOracle');

// Make sure the theoritical address was calculated correctly
assert(address(updateStorageMirrorGuard) == _updateStorageMirrorGuardTheoriticalAddress);

Expand Down
39 changes: 39 additions & 0 deletions solidity/test/unit/MockOracle.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity >=0.8.4 <0.9.0;

import {Test} from 'forge-std/Test.sol';
import {BlockHeaderOracle} from 'contracts/BlockHeaderOracle.sol';

abstract contract Base is Test {
event BlockHeaderUpdated(bytes _blockHeader, uint256 _blockTimestamp, uint256 _blockNumber);

BlockHeaderOracle public oracle;

function setUp() public {
oracle = new BlockHeaderOracle();
}
}

contract UnitBlockHeaderOracle is Base {
function testUpdateBlockHeader(bytes memory _blockHeader, uint256 _blockTimestamp, uint256 _blockNumber) public {
vm.expectEmit(true, true, true, true);
emit BlockHeaderUpdated(_blockHeader, _blockTimestamp, _blockNumber);
oracle.updateBlockHeader(_blockHeader, _blockTimestamp, _blockNumber);

assertEq(_blockHeader, oracle.blockHeader(), 'Block header should be saved');
assertEq(_blockTimestamp, oracle.blockTimestamp(), 'Block timestamp should be saved');
}

function testGetLatestBlockHeader() public {
bytes memory _blockHeader = '0x1234';
uint256 _blockTimestamp = 1234;
uint256 _blockNumber = 1234;

oracle.updateBlockHeader(_blockHeader, _blockTimestamp, _blockNumber);

(bytes memory _savedBlockHeader, uint256 _savedBlockTimestamp) = oracle.getLatestBlockHeader();

assertEq(_blockHeader, _savedBlockHeader, 'Block header should be saved');
assertEq(_blockTimestamp, _savedBlockTimestamp, 'Block timestamp should be saved');
}
}