Skip to content

Commit

Permalink
feat: unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ashitakah committed Aug 2, 2024
1 parent f7b882a commit ddde6e6
Show file tree
Hide file tree
Showing 5 changed files with 195 additions and 146 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@
"package.json": "sort-package-json"
},
"dependencies": {
"@defi-wonderland/prophet-core-contracts": "^0.0.0-e73df4c1",
"@defi-wonderland/prophet-modules-contracts": "^0.0.0-2c7deca8"
"@defi-wonderland/prophet-core-contracts": "0.0.0-c25103ea",
"@defi-wonderland/prophet-modules-contracts": "0.0.0-ee716ee0"
},
"devDependencies": {
"@commitlint/cli": "19.3.0",
Expand Down
77 changes: 58 additions & 19 deletions src/contracts/EBORequestCreator.sol
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.26;

import {IEBORequestCreator} from 'interfaces/IEBORequestCreator.sol';
import {EnumerableSet} from '@openzeppelin/contracts/utils/structs/EnumerableSet.sol';
import {IEBORequestCreator, IOracle} from 'interfaces/IEBORequestCreator.sol';

contract EBORequestCreator is IEBORequestCreator {
// /// @inheritdoc IEBORequestCreator
// IOracle public oracle;
using EnumerableSet for EnumerableSet.Bytes32Set;

/// @inheritdoc IEBORequestCreator
IOracle public oracle;

/// @inheritdoc IEBORequestCreator
address public owner;
Expand All @@ -16,11 +19,12 @@ contract EBORequestCreator is IEBORequestCreator {
/// @inheritdoc IEBORequestCreator
uint256 public reward;

/// @inheritdoc IEBORequestCreator
mapping(string _chainId => bool _approved) public chainIds;
EnumerableSet.Bytes32Set internal _chainIdsAllowed;

constructor(address _owner) {
// oracle = _oracle;
mapping(uint256 _epoch => EnumerableSet.Bytes32Set _chainIds) internal _epochChainIds;

constructor(IOracle _oracle, address _owner) {
oracle = _oracle;
owner = _owner;
reward = 0;
}
Expand All @@ -42,32 +46,59 @@ contract EBORequestCreator is IEBORequestCreator {
}

/// @inheritdoc IEBORequestCreator
function createRequest(
address _requester,
address _target,
bytes calldata _data,
uint256 _value,
uint256 _nonce
) external returns (bytes32 _requestId) {
// emit RequestCreated(_requestId, _requester, _target, _data, _value, _nonce);
function createRequests(uint256 _epoch, string[] calldata _chainIds) external {
bytes32 _encodedChainId;

EnumerableSet.Bytes32Set storage _epochEncodedChainIds = _epochChainIds[_epoch];

for (uint256 _i; _i < _chainIds.length; _i++) {
_encodedChainId = _encodeChainId(_chainIds[_i]);
if (!_chainIdsAllowed.contains(_encodedChainId)) revert EBORequestCreator_ChainNotAdded();

if (!_epochEncodedChainIds.contains(_encodedChainId)) {
_epochChainIds[_epoch].add(_encodedChainId);

// TODO: COMPLETE THE REQUEST CREATION WITH THE PROPER MODULES
IOracle.Request memory _request = IOracle.Request({
nonce: 0,
requester: msg.sender,
requestModule: address(0),
responseModule: address(0),
disputeModule: address(0),
resolutionModule: address(0),
finalityModule: address(0),
requestModuleData: '',
responseModuleData: '',
disputeModuleData: '',
resolutionModuleData: '',
finalityModuleData: ''
});

bytes32 _requestId = oracle.createRequest(_request, bytes32(0));

emit RequestCreated(_requestId, _epoch, _chainIds[_i]);
}
}
}

/// @inheritdoc IEBORequestCreator
function addChain(string calldata _chainId) external onlyOwner {
if (chainIds[_chainId]) {
bytes32 _encodedChainId = _encodeChainId(_chainId);
if (_chainIdsAllowed.contains(_encodedChainId)) {
revert EBORequestCreator_ChainAlreadyAdded();
}
chainIds[_chainId] = true;
_chainIdsAllowed.add(_encodedChainId);

emit ChainAdded(_chainId);
}

/// @inheritdoc IEBORequestCreator
function removeChain(string calldata _chainId) external onlyOwner {
if (!chainIds[_chainId]) {
bytes32 _encodedChainId = _encodeChainId(_chainId);
if (!_chainIdsAllowed.contains(_encodedChainId)) {
revert EBORequestCreator_ChainNotAdded();
}
chainIds[_chainId] = false;
_chainIdsAllowed.remove(_encodedChainId);

emit ChainRemoved(_chainId);
}
Expand All @@ -79,6 +110,14 @@ contract EBORequestCreator is IEBORequestCreator {
emit RewardSet(_oldReward, _reward);
}

/**
* @notice Encodes the chain id
* @dev The chain id is hashed to have a enumerable set to avoid duplicates
*/
function _encodeChainId(string calldata _chainId) internal pure returns (bytes32 _encodedChainId) {
_encodedChainId = keccak256(abi.encodePacked(_chainId));
}

/**
* @notice Checks if the sender is the owner
*/
Expand Down
40 changes: 12 additions & 28 deletions src/interfaces/IEBORequestCreator.sol
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity 0.8.26;

// import {IOracle} from '@defi-wonderland/prophet-core-contracts/solidity/interfaces/IOracle.sol';
import {IOracle} from '@defi-wonderland/prophet-core-contracts/solidity/interfaces/IOracle.sol';

interface IEBORequestCreator {
/*///////////////////////////////////////////////////////////////
Expand All @@ -23,10 +23,11 @@ interface IEBORequestCreator {

/**
* @notice Emitted when a request is created
* @param _requestId The id of the request
* @param _epoch The epoch of the request
* @param _chainId The chain id of the request
*/
event RequestCreated(uint256 indexed _epoch, string indexed _chainId);
event RequestCreated(bytes32 indexed _requestId, uint256 indexed _epoch, string indexed _chainId);

/**
* @notice Emitted when a chain is added
Expand Down Expand Up @@ -75,10 +76,10 @@ interface IEBORequestCreator {
VARIABLES
//////////////////////////////////////////////////////////////*/

// /**
// * @notice The oracle contract
// */
// function oracle() external view returns (IOracle _oracle);
/**
* @notice The oracle contract
*/
function oracle() external view returns (IOracle _oracle);

/**
* @notice The owner of the contract
Expand All @@ -98,13 +99,6 @@ interface IEBORequestCreator {
*/
function reward() external view returns (uint256 _reward);

/**
* @notice The chain ids
* @param _chainId The chain id to check
* @return _approved The chain id is approved
*/
function chainIds(string calldata _chainId) external view returns (bool _approved);

/*///////////////////////////////////////////////////////////////
LOGIC
//////////////////////////////////////////////////////////////*/
Expand All @@ -121,21 +115,11 @@ interface IEBORequestCreator {
function acceptPendingOwner() external;

/**
* @notice Create a request
* @param _requester The address of the requester
* @param _target The address of the target
* @param _data The data of the request
* @param _value The value of the request
* @param _nonce The nonce of the request
* @return _requestId The id of the request
*/
function createRequest(
address _requester,
address _target,
bytes calldata _data,
uint256 _value,
uint256 _nonce
) external returns (bytes32 _requestId);
* @notice Create requests
* @param _epoch The epoch of the request
* @param _chainIds The chain ids to update
*/
function createRequests(uint256 _epoch, string[] calldata _chainIds) external;

/**
* @notice Add a chain to the allowed chains which can be updated
Expand Down
Loading

0 comments on commit ddde6e6

Please sign in to comment.