ERC20Diff

Created Diff never expires
13 removals
176 lines
2 additions
165 lines
// SPDX-License-Identifier: AGPL-3.0-only
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity ^0.8.7;
pragma solidity ^0.8.7;


import { IERC20 } from "./interfaces/IERC20.sol";
import { IERC20 } from "./interfaces/IERC20.sol";


/*
/*
███████╗██████╗ ██████╗ ██████╗ ██████╗
███████╗██████╗ ██████╗ ██████╗ ██████╗
██╔════╝██╔══██╗██╔════╝ ╚════██╗██╔═████╗
██╔════╝██╔══██╗██╔════╝ ╚════██╗██╔═████╗
█████╗ ██████╔╝██║ █████╔╝██║██╔██║
█████╗ ██████╔╝██║ █████╔╝██║██╔██║
██╔══╝ ██╔══██╗██║ ██╔═══╝ ████╔╝██║
██╔══╝ ██╔══██╗██║ ██╔═══╝ ████╔╝██║
███████╗██║ ██║╚██████╗ ███████╗╚██████╔╝
███████╗██║ ██║╚██████╗ ███████╗╚██████╔╝
╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝ ╚═════╝
╚══════╝╚═╝ ╚═╝ ╚═════╝ ╚══════╝ ╚═════╝
*/
*/


/**
/**
* @title Modern ERC-20 implementation.
* @title Modern ERC-20 implementation.
* @dev Acknowledgements to Solmate, OpenZeppelin, and DSS for inspiring this code.
* @dev Acknowledgements to Solmate, OpenZeppelin, and DSS for inspiring this code.
*/
*/
contract ERC20 is IERC20 {
abstract contract ERC20Proxied is IERC20 {


/**************************************************************************************************************************************/
/**************************************************************************************************************************************/
/*** ERC-20 ***/
/*** ERC-20 ***/
/**************************************************************************************************************************************/
/**************************************************************************************************************************************/


string public override name;
string public override name;
string public override symbol;
string public override symbol;


uint8 public immutable override decimals;
uint8 public override decimals;


uint256 public override totalSupply;
uint256 public override totalSupply;


mapping(address => uint256) public override balanceOf;
mapping(address => uint256) public override balanceOf;


mapping(address => mapping(address => uint256)) public override allowance;
mapping(address => mapping(address => uint256)) public override allowance;


/**************************************************************************************************************************************/
/**************************************************************************************************************************************/
/*** ERC-2612 ***/
/*** ERC-2612 ***/
/**************************************************************************************************************************************/
/**************************************************************************************************************************************/


// PERMIT_TYPEHASH = keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
// PERMIT_TYPEHASH = keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
bytes32 public constant override PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9;
bytes32 public constant override PERMIT_TYPEHASH = 0x6e71edae12b1b97f4d1f60370fef10105fa2faae0126114a169c64845d6126c9;


mapping(address => uint256) public override nonces;
mapping(address => uint256) public override nonces;


/**
* @param name_ The name of the token.
* @param symbol_ The symbol of the token.
* @param decimals_ The decimal precision used by the token.
*/
constructor(string memory name_, string memory symbol_, uint8 decimals_) {
name = name_;
symbol = symbol_;
decimals = decimals_;
}

/**************************************************************************************************************************************/
/**************************************************************************************************************************************/
/*** External Functions ***/
/*** External Functions ***/
/**************************************************************************************************************************************/
/**************************************************************************************************************************************/


function approve(address spender_, uint256 amount_) public virtual override returns (bool success_) {
function approve(address spender_, uint256 amount_) public virtual override returns (bool success_) {
_approve(msg.sender, spender_, amount_);
_approve(msg.sender, spender_, amount_);
return true;
return true;
}
}


function decreaseAllowance(address spender_, uint256 subtractedAmount_) public virtual override returns (bool success_) {
function decreaseAllowance(address spender_, uint256 subtractedAmount_) public virtual override returns (bool success_) {
_decreaseAllowance(msg.sender, spender_, subtractedAmount_);
_decreaseAllowance(msg.sender, spender_, subtractedAmount_);
return true;
return true;
}
}


function increaseAllowance(address spender_, uint256 addedAmount_) public virtual override returns (bool success_) {
function increaseAllowance(address spender_, uint256 addedAmount_) public virtual override returns (bool success_) {
_approve(msg.sender, spender_, allowance[msg.sender][spender_] + addedAmount_);
_approve(msg.sender, spender_, allowance[msg.sender][spender_] + addedAmount_);
return true;
return true;
}
}


function permit(address owner_, address spender_, uint256 amount_, uint256 deadline_, uint8 v_, bytes32 r_, bytes32 s_)
function permit(address owner_, address spender_, uint256 amount_, uint256 deadline_, uint8 v_, bytes32 r_, bytes32 s_)
public virtual override
public virtual override
{
{
require(deadline_ >= block.timestamp, "ERC20:P:EXPIRED");
require(deadline_ >= block.timestamp, "ERC20:P:EXPIRED");


// Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
// Appendix F in the Ethereum Yellow paper (https://ethereum.github.io/yellowpaper/paper.pdf), defines
// the valid range for s in (301): 0 < s < secp256k1n ÷ 2 + 1, and for v in (302): v ∈ {27, 28}.
// the valid range for s in (301): 0 < s < secp256k1n ÷ 2 + 1, and for v in (302): v ∈ {27, 28}.
require(
require(
uint256(s_) <= uint256(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) &&
uint256(s_) <= uint256(0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) &&
(v_ == 27 || v_ == 28),
(v_ == 27 || v_ == 28),
"ERC20:P:MALLEABLE"
"ERC20:P:MALLEABLE"
);
);


// Nonce realistically cannot overflow.
// Nonce realistically cannot overflow.
unchecked {
unchecked {
bytes32 digest_ = keccak256(
bytes32 digest_ = keccak256(
abi.encodePacked(
abi.encodePacked(
"\x19\x01",
"\x19\x01",
DOMAIN_SEPARATOR(),
DOMAIN_SEPARATOR(),
keccak256(abi.encode(PERMIT_TYPEHASH, owner_, spender_, amount_, nonces[owner_]++, deadline_))
keccak256(abi.encode(PERMIT_TYPEHASH, owner_, spender_, amount_, nonces[owner_]++, deadline_))
)
)
);
);


address recoveredAddress_ = ecrecover(digest_, v_, r_, s_);
address recoveredAddress_ = ecrecover(digest_, v_, r_, s_);


require(recoveredAddress_ == owner_ && owner_ != address(0), "ERC20:P:INVALID_SIGNATURE");
require(recoveredAddress_ == owner_ && owner_ != address(0), "ERC20:P:INVALID_SIGNATURE");
}
}


_approve(owner_, spender_, amount_);
_approve(owner_, spender_, amount_);
}
}


function transfer(address recipient_, uint256 amount_) public virtual override returns (bool success_) {
function transfer(address recipient_, uint256 amount_) public virtual override returns (bool success_) {
_transfer(msg.sender, recipient_, amount_);
_transfer(msg.sender, recipient_, amount_);
return true;
return true;
}
}


function transferFrom(address owner_, address recipient_, uint256 amount_) public virtual override returns (bool success_) {
function transferFrom(address owner_, address recipient_, uint256 amount_) public virtual override returns (bool success_) {
_decreaseAllowance(owner_, msg.sender, amount_);
_decreaseAllowance(owner_, msg.sender, amount_);
_transfer(owner_, recipient_, amount_);
_transfer(owner_, recipient_, amount_);
return true;
return true;
}
}


/**************************************************************************************************************************************/
/**************************************************************************************************************************************/
/*** View Functions ***/
/*** View Functions ***/
/**************************************************************************************************************************************/
/**************************************************************************************************************************************/


function DOMAIN_SEPARATOR() public view override returns (bytes32 domainSeparator_) {
function DOMAIN_SEPARATOR() public view override returns (bytes32 domainSeparator_) {
return keccak256(
return keccak256(
abi.encode(
abi.encode(
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256("EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"),
keccak256(bytes(name)),
keccak256(bytes(name)),
keccak256(bytes("1")),
keccak256(bytes("1")),
block.chainid,
block.chainid,
address(this)
address(this)
)
)
);
);
}
}


/**************************************************************************************************************************************/
/**************************************************************************************************************************************/
/*** Internal Functions ***/
/*** Internal Functions ***/
/**************************************************************************************************************************************/
/**************************************************************************************************************************************/


function _approve(address owner_, address spender_, uint256 amount_) internal {
function _approve(address owner_, address spender_, uint256 amount_) internal {
emit Approval(owner_, spender_, allowance[owner_][spender_] = amount_);
emit Approval(owner_, spender_, allowance[owner_][spender_] = amount_);
}
}


function _burn(address owner_, uint256 amount_) internal {
function _burn(address owner_, uint256 amount_) internal {
balanceOf[owner_] -= amount_;
balanceOf[owner_] -= amount_;


// Cannot underflow because a user's balance will never be larger than the total supply.
// Cannot underflow because a user's balance will never be larger than the total supply.
unchecked { totalSupply -= amount_; }
unchecked { totalSupply -= amount_; }


emit Transfer(owner_, address(0), amount_);
emit Transfer(owner_, address(0), amount_);
}
}


function _decreaseAllowance(address owner_, address spender_, uint256 subtractedAmount_) internal {
function _decreaseAllowance(address owner_, address spender_, uint256 subtractedAmount_) internal {
uint256 spenderAllowance = allowance[owner_][spender_]; // Cache to memory.
uint256 spenderAllowance = allowance[owner_][spender_]; // Cache to memory.


if (spenderAllowance != type(uint256).max) {
if (spenderAllowance != type(uint256).max) {
_approve(owner_, spender_, spenderAllowance - subtractedAmount_);
_approve(owner_, spender_, spenderAllowance - subtractedAmount_);
}
}
}
}


function _mint(address recipient_, uint256 amount_) internal {
function _mint(address recipient_, uint256 amount_) internal {
totalSupply += amount_;
totalSupply += amount_;


// Cannot overflow because totalSupply would first overflow in the statement above.
// Cannot overflow because totalSupply would first overflow in the statement above.
unchecked { balanceOf[recipient_] += amount_; }
unchecked { balanceOf[recipient_] += amount_; }


emit Transfer(address(0), recipient_, amount_);
emit Transfer(address(0), recipient_, amount_);
}
}


function _transfer(address owner_, address recipient_, uint256 amount_) internal {
function _transfer(address owner_, address recipient_, uint256 amount_) internal {
balanceOf[owner_] -= amount_;
balanceOf[owner_] -= amount_;


// Cannot overflow because minting prevents overflow of totalSupply, and sum of user balances == totalSupply.
// Cannot overflow because minting prevents overflow of totalSupply, and sum of user balances == totalSupply.
unchecked { balanceOf[recipient_] += amount_; }
unchecked { balanceOf[recipient_] += amount_; }


emit Transfer(owner_, recipient_, amount_);
emit Transfer(owner_, recipient_, amount_);
}
}


}
}