Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into token-test-improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
nkuba committed Sep 23, 2021
2 parents 431fb38 + 4985bcf commit 5624f29
Show file tree
Hide file tree
Showing 14 changed files with 550 additions and 89 deletions.
84 changes: 84 additions & 0 deletions contracts/clone/CloneFactory.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.5;

/*
The MIT License (MIT)
Copyright (c) 2018 Murray Software, LLC.
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

/// @notice Implementation of [EIP-1167] based on [clone-factory]
/// source code.
///
/// EIP 1167: https://eips.ethereum.org/EIPS/eip-1167
// Original implementation: https://github.com/optionality/clone-factory
// Modified to use ^0.8.5; instead of ^0.4.23 solidity version.
/* solhint-disable no-inline-assembly */
abstract contract CloneFactory {
/// @notice Creates EIP-1167 clone of the contract under the provided
/// `target` address. Returns address of the created clone.
/// @dev In specific circumstances, such as the `target` contract destroyed,
/// create opcode may return 0x0 address. The code calling this
/// function should handle this corner case properly.
function createClone(address target) internal returns (address result) {
bytes20 targetBytes = bytes20(target);
assembly {
let clone := mload(0x40)
mstore(
clone,
0x3d602d80600a3d3981f3363d3d373d3d3d363d73000000000000000000000000
)
mstore(add(clone, 0x14), targetBytes)
mstore(
add(clone, 0x28),
0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000
)
result := create(0, clone, 0x37)
}
}

/// @notice Checks if the contract under the `query` address is a EIP-1167
/// clone of the contract under `target` address.
function isClone(address target, address query)
internal
view
returns (bool result)
{
bytes20 targetBytes = bytes20(target);
assembly {
let clone := mload(0x40)
mstore(
clone,
0x363d3d373d3d3d363d7300000000000000000000000000000000000000000000
)
mstore(add(clone, 0xa), targetBytes)
mstore(
add(clone, 0x1e),
0x5af43d82803e903d91602b57fd5bf30000000000000000000000000000000000
)

let other := add(clone, 0x40)
extcodecopy(query, other, 0, 0x2d)
result := and(
eq(mload(clone), mload(other)),
eq(mload(add(clone, 0xd)), mload(add(other, 0xd)))
)
}
}
}
21 changes: 21 additions & 0 deletions contracts/test/clone/CloneFactoryStub.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.4;

import "../../clone/CloneFactory.sol";

contract CloneFactoryStub is CloneFactory {
event CloneCreated(address cloneAddress);

function createClonePublic(address target) external {
emit CloneCreated(createClone(target));
}

function isClonePublic(address target, address query)
external
view
returns (bool)
{
return isClone(target, query);
}
}
11 changes: 11 additions & 0 deletions contracts/test/clone/Cloneable.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.4;

contract Cloneable {
uint256 public value;

function initialize(uint256 _value) external {
value = _value;
}
}
21 changes: 21 additions & 0 deletions contracts/test/token/ERC20WithPermitStub.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.4;

import "../../token/ERC20WithPermit.sol";

contract ERC20WithPermitStub is ERC20WithPermit {
event BeforeTokenTransferCalled(address from, address to, uint256 amount);

constructor(string memory _name, string memory _symbol)
ERC20WithPermit(_name, _symbol)
{}

function beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal override {
emit BeforeTokenTransferCalled(from, to, amount);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

pragma solidity ^0.8.4;

import "../token/IReceiveApproval.sol";
import "../../token/IReceiveApproval.sol";

contract ReceiveApprovalStub is IReceiveApproval {
bool public shouldRevert;
Expand Down
12 changes: 12 additions & 0 deletions contracts/test/token/TestERC20.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.4;

import "../../token/ERC20WithPermit.sol";

contract TestERC20 is ERC20WithPermit {
string public constant NAME = "Test ERC20 Token";
string public constant SYMBOL = "TT";

constructor() ERC20WithPermit(NAME, SYMBOL) {}
}
16 changes: 16 additions & 0 deletions contracts/test/token/TestERC721.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.4;

import "@openzeppelin/contracts/token/ERC721/ERC721.sol";

contract TestERC721 is ERC721 {
string public constant NAME = "Test ERC721 Token";
string public constant SYMBOL = "TT";

constructor() ERC721(NAME, SYMBOL) {}

function mint(address to, uint256 tokenId) public {
_mint(to, tokenId);
}
}
76 changes: 48 additions & 28 deletions contracts/token/ERC20WithPermit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,17 @@ contract ERC20WithPermit is IERC20WithPermit, Ownable {
/// @notice Returns the current nonce for EIP2612 permission for the
/// provided token owner for a replay protection. Used to construct
/// EIP2612 signature provided to `permit` function.
mapping(address => uint256) public override nonces;
mapping(address => uint256) public override nonce;

uint256 public immutable cachedChainId;
bytes32 public immutable cachedDomainSeparator;

/// @notice Returns EIP2612 Permit message hash. Used to construct EIP2612
/// signature provided to `permit` function.
// keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
bytes32 public constant override PERMIT_TYPEHASH =
0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9;
keccak256(
"Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)"
);

/// @notice The amount of tokens in existence.
uint256 public override totalSupply;
Expand All @@ -54,7 +55,7 @@ contract ERC20WithPermit is IERC20WithPermit, Ownable {
name = _name;
symbol = _symbol;

cachedChainId = chainId();
cachedChainId = block.chainid;
cachedDomainSeparator = buildDomainSeparator();
}

Expand All @@ -72,29 +73,29 @@ contract ERC20WithPermit is IERC20WithPermit, Ownable {
return true;
}

/// @notice Moves `amount` tokens from `sender` to `recipient` using the
/// @notice Moves `amount` tokens from `spender` to `recipient` using the
/// allowance mechanism. `amount` is then deducted from the caller's
/// allowance unless the allowance was made for `type(uint256).max`.
/// @return True if the operation succeeded, reverts otherwise.
/// @dev Requirements:
/// - `sender` and `recipient` cannot be the zero address,
/// - `sender` must have a balance of at least `amount`,
/// - the caller must have allowance for `sender`'s tokens of at least
/// - `spender` and `recipient` cannot be the zero address,
/// - `spender` must have a balance of at least `amount`,
/// - the caller must have allowance for `spender`'s tokens of at least
/// `amount`.
function transferFrom(
address sender,
address spender,
address recipient,
uint256 amount
) external override returns (bool) {
uint256 currentAllowance = allowance[sender][msg.sender];
uint256 currentAllowance = allowance[spender][msg.sender];
if (currentAllowance != type(uint256).max) {
require(
currentAllowance >= amount,
"Transfer amount exceeds allowance"
);
_approve(sender, msg.sender, currentAllowance - amount);
_approve(spender, msg.sender, currentAllowance - amount);
}
_transfer(sender, recipient, amount);
_transfer(spender, recipient, amount);
return true;
}

Expand Down Expand Up @@ -140,7 +141,7 @@ contract ERC20WithPermit is IERC20WithPermit, Ownable {
owner,
spender,
amount,
nonces[owner]++,
nonce[owner]++,
deadline
)
)
Expand All @@ -160,6 +161,9 @@ contract ERC20WithPermit is IERC20WithPermit, Ownable {
/// - `recipient` cannot be the zero address.
function mint(address recipient, uint256 amount) external onlyOwner {
require(recipient != address(0), "Mint to the zero address");

beforeTokenTransfer(address(0), recipient, amount);

totalSupply += amount;
balanceOf[recipient] += amount;
emit Transfer(address(0), recipient, amount);
Expand Down Expand Up @@ -250,33 +254,56 @@ contract ERC20WithPermit is IERC20WithPermit, Ownable {
// To address this issue, we check the cached chain ID against the
// current one and in case they are different, we build domain separator
// from scratch.
if (chainId() == cachedChainId) {
if (block.chainid == cachedChainId) {
return cachedDomainSeparator;
} else {
return buildDomainSeparator();
}
}

/// @dev Hook that is called before any transfer of tokens. This includes
/// minting and burning.
///
/// Calling conditions:
/// - when `from` and `to` are both non-zero, `amount` of `from`'s tokens
/// will be to transferred to `to`.
/// - when `from` is zero, `amount` tokens will be minted for `to`.
/// - when `to` is zero, `amount` of ``from``'s tokens will be burned.
/// - `from` and `to` are never both zero.
// slither-disable-next-line dead-code
function beforeTokenTransfer(
address from,
address to,
uint256 amount
) internal virtual {}

function _burn(address account, uint256 amount) internal {
uint256 currentBalance = balanceOf[account];
require(currentBalance >= amount, "Burn amount exceeds balance");

beforeTokenTransfer(account, address(0), amount);

balanceOf[account] = currentBalance - amount;
totalSupply -= amount;
emit Transfer(account, address(0), amount);
}

function _transfer(
address sender,
address spender,
address recipient,
uint256 amount
) private {
require(sender != address(0), "Transfer from the zero address");
require(spender != address(0), "Transfer from the zero address");
require(recipient != address(0), "Transfer to the zero address");
uint256 senderBalance = balanceOf[sender];
require(senderBalance >= amount, "Transfer amount exceeds balance");
balanceOf[sender] = senderBalance - amount;
require(recipient != address(this), "Transfer to the token address");

beforeTokenTransfer(spender, recipient, amount);

uint256 spenderBalance = balanceOf[spender];
require(spenderBalance >= amount, "Transfer amount exceeds balance");
balanceOf[spender] = spenderBalance - amount;
balanceOf[recipient] += amount;
emit Transfer(sender, recipient, amount);
emit Transfer(spender, recipient, amount);
}

function _approve(
Expand All @@ -299,16 +326,9 @@ contract ERC20WithPermit is IERC20WithPermit, Ownable {
),
keccak256(bytes(name)),
keccak256(bytes("1")),
chainId(),
block.chainid,
address(this)
)
);
}

function chainId() private view returns (uint256 id) {
/* solhint-disable-next-line no-inline-assembly */
assembly {
id := chainid()
}
}
}
2 changes: 1 addition & 1 deletion contracts/token/IERC20WithPermit.sol
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ interface IERC20WithPermit is IERC20, IERC20Metadata, IApproveAndCall {
/// @notice Returns the current nonce for EIP2612 permission for the
/// provided token owner for a replay protection. Used to construct
/// EIP2612 signature provided to `permit` function.
function nonces(address owner) external view returns (uint256);
function nonce(address owner) external view returns (uint256);

/// @notice Returns EIP2612 Permit message hash. Used to construct EIP2612
/// signature provided to `permit` function.
Expand Down
33 changes: 33 additions & 0 deletions contracts/token/MisfundRecovery.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.4;

import "@openzeppelin/contracts/access/Ownable.sol";
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
import "@openzeppelin/contracts/token/ERC20/utils/SafeERC20.sol";
import "@openzeppelin/contracts/token/ERC721/IERC721.sol";

/// @title MisfundRecovery
/// @notice Allows the owner of the token contract extending MisfundRecovery
/// to recover any ERC20 and ERC721 sent mistakenly to the token
/// contract address.
contract MisfundRecovery is Ownable {
using SafeERC20 for IERC20;

function recoverERC20(
IERC20 token,
address recipient,
uint256 amount
) external onlyOwner {
token.safeTransfer(recipient, amount);
}

function recoverERC721(
IERC721 token,
address recipient,
uint256 tokenId,
bytes calldata data
) external onlyOwner {
token.safeTransferFrom(address(this), recipient, tokenId, data);
}
}
Loading

0 comments on commit 5624f29

Please sign in to comment.