feat: Add foundry environment and CI
This commit is contained in:
234
foundry/lib/forge-std/src/mocks/MockERC20.sol
Normal file
234
foundry/lib/forge-std/src/mocks/MockERC20.sol
Normal file
@@ -0,0 +1,234 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity >=0.6.2 <0.9.0;
|
||||
|
||||
import {IERC20} from "../interfaces/IERC20.sol";
|
||||
|
||||
/// @notice This is a mock contract of the ERC20 standard for testing purposes only, it SHOULD NOT be used in production.
|
||||
/// @dev Forked from: https://github.com/transmissions11/solmate/blob/0384dbaaa4fcb5715738a9254a7c0a4cb62cf458/src/tokens/ERC20.sol
|
||||
contract MockERC20 is IERC20 {
|
||||
/*//////////////////////////////////////////////////////////////
|
||||
METADATA STORAGE
|
||||
//////////////////////////////////////////////////////////////*/
|
||||
|
||||
string internal _name;
|
||||
|
||||
string internal _symbol;
|
||||
|
||||
uint8 internal _decimals;
|
||||
|
||||
function name() external view override returns (string memory) {
|
||||
return _name;
|
||||
}
|
||||
|
||||
function symbol() external view override returns (string memory) {
|
||||
return _symbol;
|
||||
}
|
||||
|
||||
function decimals() external view override returns (uint8) {
|
||||
return _decimals;
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////
|
||||
ERC20 STORAGE
|
||||
//////////////////////////////////////////////////////////////*/
|
||||
|
||||
uint256 internal _totalSupply;
|
||||
|
||||
mapping(address => uint256) internal _balanceOf;
|
||||
|
||||
mapping(address => mapping(address => uint256)) internal _allowance;
|
||||
|
||||
function totalSupply() external view override returns (uint256) {
|
||||
return _totalSupply;
|
||||
}
|
||||
|
||||
function balanceOf(address owner) external view override returns (uint256) {
|
||||
return _balanceOf[owner];
|
||||
}
|
||||
|
||||
function allowance(address owner, address spender) external view override returns (uint256) {
|
||||
return _allowance[owner][spender];
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////
|
||||
EIP-2612 STORAGE
|
||||
//////////////////////////////////////////////////////////////*/
|
||||
|
||||
uint256 internal INITIAL_CHAIN_ID;
|
||||
|
||||
bytes32 internal INITIAL_DOMAIN_SEPARATOR;
|
||||
|
||||
mapping(address => uint256) public nonces;
|
||||
|
||||
/*//////////////////////////////////////////////////////////////
|
||||
INITIALIZE
|
||||
//////////////////////////////////////////////////////////////*/
|
||||
|
||||
/// @dev A bool to track whether the contract has been initialized.
|
||||
bool private initialized;
|
||||
|
||||
/// @dev To hide constructor warnings across solc versions due to different constructor visibility requirements and
|
||||
/// syntaxes, we add an initialization function that can be called only once.
|
||||
function initialize(string memory name_, string memory symbol_, uint8 decimals_) public {
|
||||
require(!initialized, "ALREADY_INITIALIZED");
|
||||
|
||||
_name = name_;
|
||||
_symbol = symbol_;
|
||||
_decimals = decimals_;
|
||||
|
||||
INITIAL_CHAIN_ID = _pureChainId();
|
||||
INITIAL_DOMAIN_SEPARATOR = computeDomainSeparator();
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////
|
||||
ERC20 LOGIC
|
||||
//////////////////////////////////////////////////////////////*/
|
||||
|
||||
function approve(address spender, uint256 amount) public virtual override returns (bool) {
|
||||
_allowance[msg.sender][spender] = amount;
|
||||
|
||||
emit Approval(msg.sender, spender, amount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function transfer(address to, uint256 amount) public virtual override returns (bool) {
|
||||
_balanceOf[msg.sender] = _sub(_balanceOf[msg.sender], amount);
|
||||
_balanceOf[to] = _add(_balanceOf[to], amount);
|
||||
|
||||
emit Transfer(msg.sender, to, amount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function transferFrom(address from, address to, uint256 amount) public virtual override returns (bool) {
|
||||
uint256 allowed = _allowance[from][msg.sender]; // Saves gas for limited approvals.
|
||||
|
||||
if (allowed != ~uint256(0)) _allowance[from][msg.sender] = _sub(allowed, amount);
|
||||
|
||||
_balanceOf[from] = _sub(_balanceOf[from], amount);
|
||||
_balanceOf[to] = _add(_balanceOf[to], amount);
|
||||
|
||||
emit Transfer(from, to, amount);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////
|
||||
EIP-2612 LOGIC
|
||||
//////////////////////////////////////////////////////////////*/
|
||||
|
||||
function permit(address owner, address spender, uint256 value, uint256 deadline, uint8 v, bytes32 r, bytes32 s)
|
||||
public
|
||||
virtual
|
||||
{
|
||||
require(deadline >= block.timestamp, "PERMIT_DEADLINE_EXPIRED");
|
||||
|
||||
address recoveredAddress = ecrecover(
|
||||
keccak256(
|
||||
abi.encodePacked(
|
||||
"\x19\x01",
|
||||
DOMAIN_SEPARATOR(),
|
||||
keccak256(
|
||||
abi.encode(
|
||||
keccak256(
|
||||
"Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
|
||||
),
|
||||
owner,
|
||||
spender,
|
||||
value,
|
||||
nonces[owner]++,
|
||||
deadline
|
||||
)
|
||||
)
|
||||
)
|
||||
),
|
||||
v,
|
||||
r,
|
||||
s
|
||||
);
|
||||
|
||||
require(recoveredAddress != address(0) && recoveredAddress == owner, "INVALID_SIGNER");
|
||||
|
||||
_allowance[recoveredAddress][spender] = value;
|
||||
|
||||
emit Approval(owner, spender, value);
|
||||
}
|
||||
|
||||
function DOMAIN_SEPARATOR() public view virtual returns (bytes32) {
|
||||
return _pureChainId() == INITIAL_CHAIN_ID ? INITIAL_DOMAIN_SEPARATOR : computeDomainSeparator();
|
||||
}
|
||||
|
||||
function computeDomainSeparator() internal view virtual returns (bytes32) {
|
||||
return keccak256(
|
||||
abi.encode(
|
||||
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
|
||||
keccak256(bytes(_name)),
|
||||
keccak256("1"),
|
||||
_pureChainId(),
|
||||
address(this)
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////
|
||||
INTERNAL MINT/BURN LOGIC
|
||||
//////////////////////////////////////////////////////////////*/
|
||||
|
||||
function _mint(address to, uint256 amount) internal virtual {
|
||||
_totalSupply = _add(_totalSupply, amount);
|
||||
_balanceOf[to] = _add(_balanceOf[to], amount);
|
||||
|
||||
emit Transfer(address(0), to, amount);
|
||||
}
|
||||
|
||||
function _burn(address from, uint256 amount) internal virtual {
|
||||
_balanceOf[from] = _sub(_balanceOf[from], amount);
|
||||
_totalSupply = _sub(_totalSupply, amount);
|
||||
|
||||
emit Transfer(from, address(0), amount);
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////
|
||||
INTERNAL SAFE MATH LOGIC
|
||||
//////////////////////////////////////////////////////////////*/
|
||||
|
||||
function _add(uint256 a, uint256 b) internal pure returns (uint256) {
|
||||
uint256 c = a + b;
|
||||
require(c >= a, "ERC20: addition overflow");
|
||||
return c;
|
||||
}
|
||||
|
||||
function _sub(uint256 a, uint256 b) internal pure returns (uint256) {
|
||||
require(a >= b, "ERC20: subtraction underflow");
|
||||
return a - b;
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////
|
||||
HELPERS
|
||||
//////////////////////////////////////////////////////////////*/
|
||||
|
||||
// We use this complex approach of `_viewChainId` and `_pureChainId` to ensure there are no
|
||||
// compiler warnings when accessing chain ID in any solidity version supported by forge-std. We
|
||||
// can't simply access the chain ID in a normal view or pure function because the solc View Pure
|
||||
// Checker changed `chainid` from pure to view in 0.8.0.
|
||||
function _viewChainId() private view returns (uint256 chainId) {
|
||||
// Assembly required since `block.chainid` was introduced in 0.8.0.
|
||||
assembly {
|
||||
chainId := chainid()
|
||||
}
|
||||
|
||||
address(this); // Silence warnings in older Solc versions.
|
||||
}
|
||||
|
||||
function _pureChainId() private pure returns (uint256 chainId) {
|
||||
function() internal view returns (uint256) fnIn = _viewChainId;
|
||||
function() internal pure returns (uint256) pureChainId;
|
||||
assembly {
|
||||
pureChainId := fnIn
|
||||
}
|
||||
chainId = pureChainId();
|
||||
}
|
||||
}
|
||||
231
foundry/lib/forge-std/src/mocks/MockERC721.sol
Normal file
231
foundry/lib/forge-std/src/mocks/MockERC721.sol
Normal file
@@ -0,0 +1,231 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
pragma solidity >=0.6.2 <0.9.0;
|
||||
|
||||
import {IERC721Metadata, IERC721TokenReceiver} from "../interfaces/IERC721.sol";
|
||||
|
||||
/// @notice This is a mock contract of the ERC721 standard for testing purposes only, it SHOULD NOT be used in production.
|
||||
/// @dev Forked from: https://github.com/transmissions11/solmate/blob/0384dbaaa4fcb5715738a9254a7c0a4cb62cf458/src/tokens/ERC721.sol
|
||||
contract MockERC721 is IERC721Metadata {
|
||||
/*//////////////////////////////////////////////////////////////
|
||||
METADATA STORAGE/LOGIC
|
||||
//////////////////////////////////////////////////////////////*/
|
||||
|
||||
string internal _name;
|
||||
|
||||
string internal _symbol;
|
||||
|
||||
function name() external view override returns (string memory) {
|
||||
return _name;
|
||||
}
|
||||
|
||||
function symbol() external view override returns (string memory) {
|
||||
return _symbol;
|
||||
}
|
||||
|
||||
function tokenURI(uint256 id) public view virtual override returns (string memory) {}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////
|
||||
ERC721 BALANCE/OWNER STORAGE
|
||||
//////////////////////////////////////////////////////////////*/
|
||||
|
||||
mapping(uint256 => address) internal _ownerOf;
|
||||
|
||||
mapping(address => uint256) internal _balanceOf;
|
||||
|
||||
function ownerOf(uint256 id) public view virtual override returns (address owner) {
|
||||
require((owner = _ownerOf[id]) != address(0), "NOT_MINTED");
|
||||
}
|
||||
|
||||
function balanceOf(address owner) public view virtual override returns (uint256) {
|
||||
require(owner != address(0), "ZERO_ADDRESS");
|
||||
|
||||
return _balanceOf[owner];
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////
|
||||
ERC721 APPROVAL STORAGE
|
||||
//////////////////////////////////////////////////////////////*/
|
||||
|
||||
mapping(uint256 => address) internal _getApproved;
|
||||
|
||||
mapping(address => mapping(address => bool)) internal _isApprovedForAll;
|
||||
|
||||
function getApproved(uint256 id) public view virtual override returns (address) {
|
||||
return _getApproved[id];
|
||||
}
|
||||
|
||||
function isApprovedForAll(address owner, address operator) public view virtual override returns (bool) {
|
||||
return _isApprovedForAll[owner][operator];
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////
|
||||
INITIALIZE
|
||||
//////////////////////////////////////////////////////////////*/
|
||||
|
||||
/// @dev A bool to track whether the contract has been initialized.
|
||||
bool private initialized;
|
||||
|
||||
/// @dev To hide constructor warnings across solc versions due to different constructor visibility requirements and
|
||||
/// syntaxes, we add an initialization function that can be called only once.
|
||||
function initialize(string memory name_, string memory symbol_) public {
|
||||
require(!initialized, "ALREADY_INITIALIZED");
|
||||
|
||||
_name = name_;
|
||||
_symbol = symbol_;
|
||||
|
||||
initialized = true;
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////
|
||||
ERC721 LOGIC
|
||||
//////////////////////////////////////////////////////////////*/
|
||||
|
||||
function approve(address spender, uint256 id) public payable virtual override {
|
||||
address owner = _ownerOf[id];
|
||||
|
||||
require(msg.sender == owner || _isApprovedForAll[owner][msg.sender], "NOT_AUTHORIZED");
|
||||
|
||||
_getApproved[id] = spender;
|
||||
|
||||
emit Approval(owner, spender, id);
|
||||
}
|
||||
|
||||
function setApprovalForAll(address operator, bool approved) public virtual override {
|
||||
_isApprovedForAll[msg.sender][operator] = approved;
|
||||
|
||||
emit ApprovalForAll(msg.sender, operator, approved);
|
||||
}
|
||||
|
||||
function transferFrom(address from, address to, uint256 id) public payable virtual override {
|
||||
require(from == _ownerOf[id], "WRONG_FROM");
|
||||
|
||||
require(to != address(0), "INVALID_RECIPIENT");
|
||||
|
||||
require(
|
||||
msg.sender == from || _isApprovedForAll[from][msg.sender] || msg.sender == _getApproved[id],
|
||||
"NOT_AUTHORIZED"
|
||||
);
|
||||
|
||||
// Underflow of the sender's balance is impossible because we check for
|
||||
// ownership above and the recipient's balance can't realistically overflow.
|
||||
_balanceOf[from]--;
|
||||
|
||||
_balanceOf[to]++;
|
||||
|
||||
_ownerOf[id] = to;
|
||||
|
||||
delete _getApproved[id];
|
||||
|
||||
emit Transfer(from, to, id);
|
||||
}
|
||||
|
||||
function safeTransferFrom(address from, address to, uint256 id) public payable virtual override {
|
||||
transferFrom(from, to, id);
|
||||
|
||||
require(
|
||||
!_isContract(to)
|
||||
|| IERC721TokenReceiver(to).onERC721Received(msg.sender, from, id, "")
|
||||
== IERC721TokenReceiver.onERC721Received.selector,
|
||||
"UNSAFE_RECIPIENT"
|
||||
);
|
||||
}
|
||||
|
||||
function safeTransferFrom(address from, address to, uint256 id, bytes memory data)
|
||||
public
|
||||
payable
|
||||
virtual
|
||||
override
|
||||
{
|
||||
transferFrom(from, to, id);
|
||||
|
||||
require(
|
||||
!_isContract(to)
|
||||
|| IERC721TokenReceiver(to).onERC721Received(msg.sender, from, id, data)
|
||||
== IERC721TokenReceiver.onERC721Received.selector,
|
||||
"UNSAFE_RECIPIENT"
|
||||
);
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////
|
||||
ERC165 LOGIC
|
||||
//////////////////////////////////////////////////////////////*/
|
||||
|
||||
function supportsInterface(bytes4 interfaceId) public view virtual override returns (bool) {
|
||||
return interfaceId == 0x01ffc9a7 // ERC165 Interface ID for ERC165
|
||||
|| interfaceId == 0x80ac58cd // ERC165 Interface ID for ERC721
|
||||
|| interfaceId == 0x5b5e139f; // ERC165 Interface ID for ERC721Metadata
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////
|
||||
INTERNAL MINT/BURN LOGIC
|
||||
//////////////////////////////////////////////////////////////*/
|
||||
|
||||
function _mint(address to, uint256 id) internal virtual {
|
||||
require(to != address(0), "INVALID_RECIPIENT");
|
||||
|
||||
require(_ownerOf[id] == address(0), "ALREADY_MINTED");
|
||||
|
||||
// Counter overflow is incredibly unrealistic.
|
||||
|
||||
_balanceOf[to]++;
|
||||
|
||||
_ownerOf[id] = to;
|
||||
|
||||
emit Transfer(address(0), to, id);
|
||||
}
|
||||
|
||||
function _burn(uint256 id) internal virtual {
|
||||
address owner = _ownerOf[id];
|
||||
|
||||
require(owner != address(0), "NOT_MINTED");
|
||||
|
||||
_balanceOf[owner]--;
|
||||
|
||||
delete _ownerOf[id];
|
||||
|
||||
delete _getApproved[id];
|
||||
|
||||
emit Transfer(owner, address(0), id);
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////
|
||||
INTERNAL SAFE MINT LOGIC
|
||||
//////////////////////////////////////////////////////////////*/
|
||||
|
||||
function _safeMint(address to, uint256 id) internal virtual {
|
||||
_mint(to, id);
|
||||
|
||||
require(
|
||||
!_isContract(to)
|
||||
|| IERC721TokenReceiver(to).onERC721Received(msg.sender, address(0), id, "")
|
||||
== IERC721TokenReceiver.onERC721Received.selector,
|
||||
"UNSAFE_RECIPIENT"
|
||||
);
|
||||
}
|
||||
|
||||
function _safeMint(address to, uint256 id, bytes memory data) internal virtual {
|
||||
_mint(to, id);
|
||||
|
||||
require(
|
||||
!_isContract(to)
|
||||
|| IERC721TokenReceiver(to).onERC721Received(msg.sender, address(0), id, data)
|
||||
== IERC721TokenReceiver.onERC721Received.selector,
|
||||
"UNSAFE_RECIPIENT"
|
||||
);
|
||||
}
|
||||
|
||||
/*//////////////////////////////////////////////////////////////
|
||||
HELPERS
|
||||
//////////////////////////////////////////////////////////////*/
|
||||
|
||||
function _isContract(address _addr) private view returns (bool) {
|
||||
uint256 codeLength;
|
||||
|
||||
// Assembly required for versions < 0.8.0 to check extcodesize.
|
||||
assembly {
|
||||
codeLength := extcodesize(_addr)
|
||||
}
|
||||
|
||||
return codeLength > 0;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user