Skip to content

Commit

Permalink
refactor: improve helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
gas1cent committed Nov 9, 2023
1 parent a1c68cf commit 1a36f0c
Showing 1 changed file with 58 additions and 16 deletions.
74 changes: 58 additions & 16 deletions solidity/test/utils/Helpers.sol
Original file line number Diff line number Diff line change
Expand Up @@ -6,41 +6,53 @@ import {DSTestPlus} from '@defi-wonderland/solidity-utils/solidity/test/DSTestPl
import {IOracle} from '@defi-wonderland/prophet-core-contracts/solidity/interfaces/IOracle.sol';

import {IAccountingExtension} from '../../interfaces/extensions/IAccountingExtension.sol';

import {TestConstants} from '../utils/TestConstants.sol';
import {TestConstants} from './TestConstants.sol';

contract Helpers is DSTestPlus, TestConstants {
IERC20 public usdc = IERC20(label(USDC_ADDRESS, 'USDC'));
// 100% random sequence of bytes representing request, response, or dispute id
bytes32 public mockId = bytes32('69');

// Placeholder addresses
address public disputer = makeAddr('disputer');
address public proposer = makeAddr('proposer');

// Mocks objects
IOracle.Request public mockRequest;
IOracle.Response public mockResponse = IOracle.Response({proposer: proposer, requestId: mockId, response: bytes('')});
IOracle.Dispute public mockDispute =
IOracle.Dispute({disputer: disputer, responseId: mockId, proposer: proposer, requestId: mockId});

// Shared events that all modules emit
event RequestFinalized(bytes32 indexed _requestId, IOracle.Response _response, address _finalizer);

modifier assumeFuzzable(address _address) {
_assumeFuzzable(_address);
_;
}

/**
* @notice Ensures that a fuzzed address can be used for deployment and calls
*
* @param _address The address to check
*/
function _assumeFuzzable(address _address) internal pure {
assumeNotForgeAddress(_address);
assumeNotZeroAddress(_address);
assumeNotPrecompile(_address);
}

/**
* @notice Sets up a mock and expects a call to it
*
* @param _receiver The address to have a mock on
* @param _calldata The calldata to mock and expect
* @param _returned The data to return from the mocked call
*/
function _mockAndExpect(address _receiver, bytes memory _calldata, bytes memory _returned) internal {
vm.mockCall(_receiver, _calldata, _returned);
vm.expectCall(_receiver, _calldata);
}

function _getMockDispute(
bytes32 _requestId,
address _disputer,
address _proposer
) internal view returns (IOracle.Dispute memory _dispute) {
_dispute = IOracle.Dispute({
disputer: _disputer,
responseId: bytes32('response'),
proposer: _proposer,
requestId: _requestId
});
}

function _forBondDepositERC20(
IAccountingExtension _accountingExtension,
address _depositor,
Expand All @@ -55,4 +67,34 @@ contract Helpers is DSTestPlus, TestConstants {
_accountingExtension.deposit(_token, _depositAmount);
vm.stopPrank();
}

/**
* @notice Computes the ID of a given request as it's done in the Oracle
*
* @param _request The request to compute the ID for
* @return _id The ID of the request
*/
function _getId(IOracle.Request memory _request) internal pure returns (bytes32 _id) {
_id = keccak256(abi.encode(_request));
}

/**
* @notice Computes the ID of a given response as it's done in the Oracle
*
* @param _response The response to compute the ID for
* @return _id The ID of the response
*/
function _getId(IOracle.Response memory _response) internal pure returns (bytes32 _id) {
_id = keccak256(abi.encode(_response));
}

/**
* @notice Computes the ID of a given dispute as it's done in the Oracle
*
* @param _dispute The dispute to compute the ID for
* @return _id The ID of the dispute
*/
function _getId(IOracle.Dispute memory _dispute) internal pure returns (bytes32 _id) {
_id = keccak256(abi.encode(_dispute));
}
}

0 comments on commit 1a36f0c

Please sign in to comment.