-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add tests, remove the dependecy from core (using mocks)
- Loading branch information
Andres Adjimann
committed
Aug 16, 2024
1 parent
f1abe4b
commit 85e4a44
Showing
21 changed files
with
2,127 additions
and
1,254 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,83 @@ | ||
// SPDX-License-Identifier: MIT | ||
// solhint-disable one-contract-per-file | ||
pragma solidity 0.8.15; | ||
|
||
import {Ownable} from "@openzeppelin/contracts-0.8.15/access/Ownable.sol"; | ||
import {ERC20} from "@openzeppelin/contracts-0.8.15/token/ERC20/ERC20.sol"; | ||
|
||
contract MockERC20 is ERC20 { | ||
constructor(string memory _name, string memory _symbol, uint256 _initialSupply) ERC20(_name, _symbol) { | ||
contract MockERC20 is ERC20, Ownable { | ||
|
||
constructor(uint256 _initialSupply) ERC20("MOCKTOKEN", "MOCK") { | ||
_mint(msg.sender, _initialSupply * 1e18); | ||
} | ||
|
||
function donateTo(address recipient, uint256 amount) external onlyOwner { | ||
_mint(recipient, amount); | ||
} | ||
|
||
/// @dev instead of using approve and call we use this method directly for testing. | ||
function mint( | ||
MintInterface target, | ||
address _wallet, | ||
uint256 _amount, | ||
uint256 _signatureId, | ||
bytes calldata _signature | ||
) external { | ||
target.mint(_wallet, _amount, _signatureId, _signature); | ||
} | ||
/// @notice Approve `target` to spend `amount` and call it with data. | ||
/// @param target The address to be given rights to transfer and destination of the call. | ||
/// @param amount The number of tokens allowed. | ||
/// @param data The bytes for the call. | ||
/// @return The data of the call. | ||
function approveAndCall( | ||
address target, | ||
uint256 amount, | ||
bytes calldata data | ||
) external payable returns (bytes memory) { | ||
require(doFirstParamEqualsAddress(data, _msgSender()), "FIRST_PARAM_NOT_SENDER"); | ||
|
||
_approve(_msgSender(), target, amount); | ||
|
||
// solhint-disable-next-line avoid-low-level-calls | ||
(bool success, bytes memory returnData) = target.call{value : msg.value}(data); | ||
if (success) { | ||
return returnData; | ||
} | ||
if (returnData.length > 0) { | ||
// The easiest way to bubble the revert reason is using memory via assembly | ||
/// @solidity memory-safe-assembly | ||
assembly { | ||
let returndata_size := mload(returnData) | ||
revert(add(32, returnData), returndata_size) | ||
} | ||
} else { | ||
revert("Empty error from destination"); | ||
} | ||
} | ||
|
||
function doFirstParamEqualsAddress(bytes memory data, address _address) | ||
internal | ||
pure | ||
returns (bool) | ||
{ | ||
if (data.length < (36 + 32)) { | ||
return false; | ||
} | ||
uint256 value; | ||
assembly { | ||
value := mload(add(data, 36)) | ||
} | ||
return value == uint160(_address); | ||
} | ||
|
||
} | ||
|
||
interface MintInterface { | ||
function mint( | ||
address _wallet, | ||
uint256 _amount, | ||
uint256 _signatureId, | ||
bytes calldata _signature | ||
) external; | ||
} |
99 changes: 99 additions & 0 deletions
99
packages/avatar/contracts/mocks/MockOperatorFilterRegistry.sol
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.15; | ||
|
||
import {Ownable} from "@openzeppelin/contracts-0.8.15/access/Ownable.sol"; | ||
|
||
contract MockOperatorFilterRegistry { | ||
/// @notice Emitted when a registration is updated. | ||
event RegistrationUpdated(address indexed registrant, bool indexed registered); | ||
|
||
/// @notice Emitted when the caller is not the address or EIP-173 "owner()" | ||
error OnlyAddressOrOwner(); | ||
/// @notice Emitted when trying to register and the contract is not ownable (EIP-173 "owner()") | ||
error NotOwnable(); | ||
/// @notice Emitted when the registrant is already registered. | ||
error AlreadyRegistered(); | ||
/// @notice Emitted when an address is filtered. | ||
error AddressFiltered(address filtered); | ||
/// @notice Emitted when a codeHash is filtered. | ||
error CodeHashFiltered(address account, bytes32 codeHash); | ||
|
||
mapping(address => address) private _registrations; | ||
|
||
bool public revertWithAddressFiltered; | ||
|
||
bool public revertWithCodeHashFiltered; | ||
/** | ||
* @notice Restricts method caller to the address or EIP-173 "owner()" | ||
*/ | ||
modifier onlyAddressOrOwner(address addr) { | ||
if (msg.sender != addr) { | ||
try Ownable(addr).owner() returns (address owner) { | ||
if (msg.sender != owner) { | ||
revert OnlyAddressOrOwner(); | ||
} | ||
} catch (bytes memory reason) { | ||
if (reason.length == 0) { | ||
revert NotOwnable(); | ||
} else { | ||
/// @solidity memory-safe-assembly | ||
assembly { | ||
revert(add(32, reason), mload(reason)) | ||
} | ||
} | ||
} | ||
} | ||
_; | ||
} | ||
|
||
function doRevert(bool _revertWithAddressFiltered, bool _revertWithCodeHashFiltered) external { | ||
revertWithAddressFiltered = _revertWithAddressFiltered; | ||
revertWithCodeHashFiltered = _revertWithCodeHashFiltered; | ||
} | ||
|
||
/** | ||
* @notice Registers an address with the registry. May be called by address itself or by EIP-173 owner. | ||
*/ | ||
function register(address registrant) external onlyAddressOrOwner(registrant) { | ||
if (_registrations[registrant] != address(0)) { | ||
revert AlreadyRegistered(); | ||
} | ||
_registrations[registrant] = registrant; | ||
emit RegistrationUpdated(registrant, true); | ||
} | ||
|
||
/** | ||
* @notice Returns true if an address has registered | ||
*/ | ||
function isRegistered(address registrant) external view returns (bool) { | ||
return _registrations[registrant] != address(0); | ||
} | ||
|
||
/** | ||
* @notice Returns true if operator is not filtered for a given token, either by address or codeHash. Also returns | ||
* true if supplied registrant address is not registered. | ||
* Note that this method will *revert* if an operator or its codehash is filtered with an error that is | ||
* more informational than a false boolean, so smart contracts that query this method for informational | ||
* purposes will need to wrap in a try/catch or perform a low-level staticcall in order to handle the case | ||
* that an operator is filtered. | ||
*/ | ||
function isOperatorAllowed(address registrant, address operator) external view returns (bool) { | ||
address registration = _registrations[registrant]; | ||
if (registration != address(0)) { | ||
if (revertWithAddressFiltered) { | ||
revert AddressFiltered(operator); | ||
} else if (revertWithCodeHashFiltered) { | ||
bytes32 codeHash = operator.codehash; | ||
revert CodeHashFiltered(operator, codeHash); | ||
} | ||
} | ||
return true; | ||
} | ||
|
||
/** | ||
* @dev Convenience method to compute the code hash of an arbitrary contract | ||
*/ | ||
function codeHashOf(address a) external view returns (bytes32) { | ||
return a.codehash; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,7 +67,6 @@ | |
"@openzeppelin/contracts-upgradeable": "^4.9.2", | ||
"@openzeppelin/contracts-upgradeable-0.8.13": "npm:@openzeppelin/[email protected]", | ||
"@release-it/keep-a-changelog": "^5.0.0", | ||
"@sandbox-smart-contracts/core": "0.0.1", | ||
"@typechain/ethers-v6": "^0.4.0", | ||
"@typechain/hardhat": "^8.0.0", | ||
"@types/chai": "^4.3.5", | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
85e4a44
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Coverage for this commit
Coverage Report