|
|
|
|
|
|
|
pragma solidity >=0.8.0;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface IERC20 {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
event Transfer(address indexed from, address indexed to, uint256 value);
|
|
|
|
|
|
|
|
|
|
|
|
event Approval(address indexed owner, address indexed spender, uint256 value);
|
|
|
|
|
|
|
|
|
|
function totalSupply() external view returns (uint256);
|
|
|
|
/**
|
|
* @dev Returns the amount of tokens owned by `account`.
|
|
*/
|
|
function balanceOf(address account) external view returns (uint256);
|
|
|
|
/**
|
|
* @dev Moves `amount` tokens from the caller's account to `to`.
|
|
*
|
|
* Returns a boolean value indicating whether the operation succeeded.
|
|
*
|
|
* Emits a {Transfer} event.
|
|
*/
|
|
function transfer(address to, uint256 amount) external returns (bool);
|
|
|
|
/**
|
|
* @dev Returns the remaining number of tokens that `spender` will be
|
|
* allowed to spend on behalf of `owner` through {transferFrom}. This is
|
|
* zero by default.
|
|
*
|
|
* This value changes when {approve} or {transferFrom} are called.
|
|
*/
|
|
function allowance(address owner, address spender) external view returns (uint256);
|
|
|
|
/**
|
|
* @dev Sets `amount` as the allowance of `spender` over the caller's tokens.
|
|
*
|
|
* Returns a boolean value indicating whether the operation succeeded.
|
|
*
|
|
* IMPORTANT: Beware that changing an allowance with this method brings the risk
|
|
* that someone may use both the old and the new allowance by unfortunate
|
|
* transaction ordering. One possible solution to mitigate this race
|
|
* condition is to first reduce the spender's allowance to 0 and set the
|
|
* desired value afterwards:
|
|
* https://github.com/ethereum/EIPs/issues/20#issuecomment-263524729
|
|
*
|
|
* Emits an {Approval} event.
|
|
*/
|
|
function approve(address spender, uint256 amount) external returns (bool);
|
|
|
|
/**
|
|
* @dev Moves `amount` tokens from `from` to `to` using the
|
|
* allowance mechanism. `amount` is then deducted from the caller's
|
|
* allowance.
|
|
*
|
|
* Returns a boolean value indicating whether the operation succeeded.
|
|
*
|
|
* Emits a {Transfer} event.
|
|
*/
|
|
function transferFrom(
|
|
address from,
|
|
address to,
|
|
uint256 amount
|
|
) external returns (bool);
|
|
}
|
|
|
|
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol)
|
|
|
|
/**
|
|
* @dev Interface for the optional metadata functions from the ERC20 standard.
|
|
*
|
|
* _Available since v4.1._
|
|
*/
|
|
interface IERC20Metadata is IERC20 {
|
|
|
|
|
|
|
|
function name() external view returns (string memory);
|
|
|
|
/**
|
|
* @dev Returns the symbol of the token.
|
|
*/
|
|
function symbol() external view returns (string memory);
|
|
|
|
/**
|
|
* @dev Returns the decimals places of the token.
|
|
*/
|
|
function decimals() external view returns (uint8);
|
|
}
|
|
|
|
// OpenZeppelin Contracts v4.4.1 (utils/Context.sol)
|
|
|
|
/**
|
|
* @dev Provides information about the current execution context, including the
|
|
* sender of the transaction and its data. While these are generally available
|
|
* via msg.sender and msg.data, they should not be accessed in such a direct
|
|
* manner, since when dealing with meta-transactions the account sending and
|
|
* paying for execution may not be the actual sender (as far as an application
|
|
* is concerned).
|
|
*
|
|
* This contract is only required for intermediate, library-like contracts.
|
|
*/
|
|
abstract contract Context {
|
|
function _msgSender() internal view virtual returns (address) {
|
|
return msg.sender;
|
|
}
|
|
|
|
function _msgData() internal view virtual returns (bytes calldata) {
|
|
return msg.data;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
contract ERC20 is Context, IERC20, IERC20Metadata {
|
|
mapping(address => uint256) private _balances;
|
|
|
|
mapping(address => mapping(address => uint256)) private _allowances;
|
|
|
|
uint256 private _totalSupply;
|
|
|
|
string private _name;
|
|
string private _symbol;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(string memory name_, string memory symbol_) {
|
|
_name = name_;
|
|
_symbol = symbol_;
|
|
}
|
|
|
|
|
|
|
|
|
|
function name() public view virtual override returns (string memory) {
|
|
return _name;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function symbol() public view virtual override returns (string memory) {
|
|
return _symbol;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function decimals() public view virtual override returns (uint8) {
|
|
return 18;
|
|
}
|
|
|
|
|
|
|
|
|
|
function totalSupply() public view virtual override returns (uint256) {
|
|
return _totalSupply;
|
|
}
|
|
|
|
|
|
|
|
|
|
function balanceOf(address account) public view virtual override returns (uint256) {
|
|
return _balances[account];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function transfer(address to, uint256 amount) public virtual override returns (bool) {
|
|
address owner = _msgSender();
|
|
_transfer(owner, to, amount);
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
function allowance(address owner, address spender) public view virtual override returns (uint256) {
|
|
return _allowances[owner][spender];
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function approve(address spender, uint256 amount) public virtual override returns (bool) {
|
|
address owner = _msgSender();
|
|
_approve(owner, spender, amount);
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function transferFrom(
|
|
address from,
|
|
address to,
|
|
uint256 amount
|
|
) public virtual override returns (bool) {
|
|
address spender = _msgSender();
|
|
_spendAllowance(from, spender, amount);
|
|
_transfer(from, to, amount);
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function increaseAllowance(address spender, uint256 addedValue) public virtual returns (bool) {
|
|
address owner = _msgSender();
|
|
_approve(owner, spender, allowance(owner, spender) + addedValue);
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function decreaseAllowance(address spender, uint256 subtractedValue) public virtual returns (bool) {
|
|
address owner = _msgSender();
|
|
uint256 currentAllowance = allowance(owner, spender);
|
|
require(currentAllowance >= subtractedValue, "ERC20: decreased allowance below zero");
|
|
unchecked {
|
|
_approve(owner, spender, currentAllowance - subtractedValue);
|
|
}
|
|
|
|
return true;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _transfer(
|
|
address from,
|
|
address to,
|
|
uint256 amount
|
|
) internal virtual {
|
|
require(from != address(0), "ERC20: transfer from the zero address");
|
|
require(to != address(0), "ERC20: transfer to the zero address");
|
|
|
|
_beforeTokenTransfer(from, to, amount);
|
|
|
|
uint256 fromBalance = _balances[from];
|
|
require(fromBalance >= amount, "ERC20: transfer amount exceeds balance");
|
|
unchecked {
|
|
_balances[from] = fromBalance - amount;
|
|
|
|
|
|
_balances[to] += amount;
|
|
}
|
|
|
|
emit Transfer(from, to, amount);
|
|
|
|
_afterTokenTransfer(from, to, amount);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _mint(address account, uint256 amount) internal virtual {
|
|
require(account != address(0), "ERC20: mint to the zero address");
|
|
|
|
_beforeTokenTransfer(address(0), account, amount);
|
|
|
|
_totalSupply += amount;
|
|
unchecked {
|
|
|
|
_balances[account] += amount;
|
|
}
|
|
emit Transfer(address(0), account, amount);
|
|
|
|
_afterTokenTransfer(address(0), account, amount);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _burn(address account, uint256 amount) internal virtual {
|
|
require(account != address(0), "ERC20: burn from the zero address");
|
|
|
|
_beforeTokenTransfer(account, address(0), amount);
|
|
|
|
uint256 accountBalance = _balances[account];
|
|
require(accountBalance >= amount, "ERC20: burn amount exceeds balance");
|
|
unchecked {
|
|
_balances[account] = accountBalance - amount;
|
|
|
|
_totalSupply -= amount;
|
|
}
|
|
|
|
emit Transfer(account, address(0), amount);
|
|
|
|
_afterTokenTransfer(account, address(0), amount);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _approve(
|
|
address owner,
|
|
address spender,
|
|
uint256 amount
|
|
) internal virtual {
|
|
require(owner != address(0), "ERC20: approve from the zero address");
|
|
require(spender != address(0), "ERC20: approve to the zero address");
|
|
|
|
_allowances[owner][spender] = amount;
|
|
emit Approval(owner, spender, amount);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _spendAllowance(
|
|
address owner,
|
|
address spender,
|
|
uint256 amount
|
|
) internal virtual {
|
|
uint256 currentAllowance = allowance(owner, spender);
|
|
if (currentAllowance != type(uint256).max) {
|
|
require(currentAllowance >= amount, "ERC20: insufficient allowance");
|
|
unchecked {
|
|
_approve(owner, spender, currentAllowance - amount);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _beforeTokenTransfer(
|
|
address from,
|
|
address to,
|
|
uint256 amount
|
|
) internal virtual {}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _afterTokenTransfer(
|
|
address from,
|
|
address to,
|
|
uint256 amount
|
|
) internal virtual {}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
interface IERC20Permit {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function permit(
|
|
address owner,
|
|
address spender,
|
|
uint256 value,
|
|
uint256 deadline,
|
|
uint8 v,
|
|
bytes32 r,
|
|
bytes32 s
|
|
) external;
|
|
|
|
/**
|
|
* @dev Returns the current nonce for `owner`. This value must be
|
|
* included whenever a signature is generated for {permit}.
|
|
*
|
|
* Every successful call to {permit} increases ``owner``'s nonce by one. This
|
|
* prevents a signature from being used multiple times.
|
|
*/
|
|
function nonces(address owner) external view returns (uint256);
|
|
|
|
/**
|
|
* @dev Returns the domain separator used in the encoding of the signature for {permit}, as defined by {EIP712}.
|
|
*/
|
|
// solhint-disable-next-line func-name-mixedcase
|
|
function DOMAIN_SEPARATOR() external view returns (bytes32);
|
|
}
|
|
|
|
// OpenZeppelin Contracts (last updated v4.7.0) (utils/cryptography/ECDSA.sol)
|
|
|
|
// OpenZeppelin Contracts (last updated v4.7.0) (utils/Strings.sol)
|
|
|
|
// OpenZeppelin Contracts (last updated v4.7.0) (utils/math/Math.sol)
|
|
|
|
/**
|
|
* @dev Standard math utilities missing in the Solidity language.
|
|
*/
|
|
library Math {
|
|
enum Rounding {
|
|
Down, // Toward negative infinity
|
|
Up, // Toward infinity
|
|
Zero // Toward zero
|
|
}
|
|
|
|
/**
|
|
* @dev Returns the largest of two numbers.
|
|
*/
|
|
function max(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
return a > b ? a : b;
|
|
}
|
|
|
|
/**
|
|
* @dev Returns the smallest of two numbers.
|
|
*/
|
|
function min(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
return a < b ? a : b;
|
|
}
|
|
|
|
/**
|
|
* @dev Returns the average of two numbers. The result is rounded towards
|
|
* zero.
|
|
*/
|
|
function average(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
// (a + b) / 2 can overflow.
|
|
return (a & b) + (a ^ b) / 2;
|
|
}
|
|
|
|
/**
|
|
* @dev Returns the ceiling of the division of two numbers.
|
|
*
|
|
* This differs from standard division with `/` in that it rounds up instead
|
|
* of rounding down.
|
|
*/
|
|
function ceilDiv(uint256 a, uint256 b) internal pure returns (uint256) {
|
|
// (a + b - 1) / b can overflow on addition, so we distribute.
|
|
return a == 0 ? 0 : (a - 1) / b + 1;
|
|
}
|
|
|
|
/**
|
|
* @notice Calculates floor(x * y / denominator) with full precision. Throws if result overflows a uint256 or denominator == 0
|
|
* @dev Original credit to Remco Bloemen under MIT license (https://xn--2-umb.com/21/muldiv)
|
|
* with further edits by Uniswap Labs also under MIT license.
|
|
*/
|
|
function mulDiv(
|
|
uint256 x,
|
|
uint256 y,
|
|
uint256 denominator
|
|
) internal pure returns (uint256 result) {
|
|
unchecked {
|
|
// 512-bit multiply [prod1 prod0] = x * y. Compute the product mod 2^256 and mod 2^256 - 1, then use
|
|
// use the Chinese Remainder Theorem to reconstruct the 512 bit result. The result is stored in two 256
|
|
// variables such that product = prod1 * 2^256 + prod0.
|
|
uint256 prod0; // Least significant 256 bits of the product
|
|
uint256 prod1; // Most significant 256 bits of the product
|
|
assembly {
|
|
let mm := mulmod(x, y, not(0))
|
|
prod0 := mul(x, y)
|
|
prod1 := sub(sub(mm, prod0), lt(mm, prod0))
|
|
}
|
|
|
|
// Handle non-overflow cases, 256 by 256 division.
|
|
if (prod1 == 0) {
|
|
return prod0 / denominator;
|
|
}
|
|
|
|
// Make sure the result is less than 2^256. Also prevents denominator == 0.
|
|
require(denominator > prod1);
|
|
|
|
///////////////////////////////////////////////
|
|
// 512 by 256 division.
|
|
///////////////////////////////////////////////
|
|
|
|
// Make division exact by subtracting the remainder from [prod1 prod0].
|
|
uint256 remainder;
|
|
assembly {
|
|
// Compute remainder using mulmod.
|
|
remainder := mulmod(x, y, denominator)
|
|
|
|
// Subtract 256 bit number from 512 bit number.
|
|
prod1 := sub(prod1, gt(remainder, prod0))
|
|
prod0 := sub(prod0, remainder)
|
|
}
|
|
|
|
// Factor powers of two out of denominator and compute largest power of two divisor of denominator. Always >= 1.
|
|
// See https://cs.stackexchange.com/q/138556/92363.
|
|
|
|
// Does not overflow because the denominator cannot be zero at this stage in the function.
|
|
uint256 twos = denominator & (~denominator + 1);
|
|
assembly {
|
|
// Divide denominator by twos.
|
|
denominator := div(denominator, twos)
|
|
|
|
// Divide [prod1 prod0] by twos.
|
|
prod0 := div(prod0, twos)
|
|
|
|
// Flip twos such that it is 2^256 / twos. If twos is zero, then it becomes one.
|
|
twos := add(div(sub(0, twos), twos), 1)
|
|
}
|
|
|
|
// Shift in bits from prod1 into prod0.
|
|
prod0 |= prod1 * twos;
|
|
|
|
// Invert denominator mod 2^256. Now that denominator is an odd number, it has an inverse modulo 2^256 such
|
|
// that denominator * inv = 1 mod 2^256. Compute the inverse by starting with a seed that is correct for
|
|
// four bits. That is, denominator * inv = 1 mod 2^4.
|
|
uint256 inverse = (3 * denominator) ^ 2;
|
|
|
|
// Use the Newton-Raphson iteration to improve the precision. Thanks to Hensel's lifting lemma, this also works
|
|
|
|
inverse *= 2 - denominator * inverse;
|
|
inverse *= 2 - denominator * inverse;
|
|
inverse *= 2 - denominator * inverse;
|
|
inverse *= 2 - denominator * inverse;
|
|
inverse *= 2 - denominator * inverse;
|
|
inverse *= 2 - denominator * inverse;
|
|
|
|
|
|
|
|
|
|
|
|
result = prod0 * inverse;
|
|
return result;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
function mulDiv(
|
|
uint256 x,
|
|
uint256 y,
|
|
uint256 denominator,
|
|
Rounding rounding
|
|
) internal pure returns (uint256) {
|
|
uint256 result = mulDiv(x, y, denominator);
|
|
if (rounding == Rounding.Up && mulmod(x, y, denominator) > 0) {
|
|
result += 1;
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function sqrt(uint256 a) internal pure returns (uint256) {
|
|
if (a == 0) {
|
|
return 0;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
uint256 result = 1 << (log2(a) >> 1);
|
|
|
|
|
|
|
|
|
|
|
|
unchecked {
|
|
result = (result + a / result) >> 1;
|
|
result = (result + a / result) >> 1;
|
|
result = (result + a / result) >> 1;
|
|
result = (result + a / result) >> 1;
|
|
result = (result + a / result) >> 1;
|
|
result = (result + a / result) >> 1;
|
|
result = (result + a / result) >> 1;
|
|
return min(result, a / result);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
function sqrt(uint256 a, Rounding rounding) internal pure returns (uint256) {
|
|
unchecked {
|
|
uint256 result = sqrt(a);
|
|
return result + (rounding == Rounding.Up && result * result < a ? 1 : 0);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function log2(uint256 value) internal pure returns (uint256) {
|
|
uint256 result = 0;
|
|
unchecked {
|
|
if (value >> 128 > 0) {
|
|
value >>= 128;
|
|
result += 128;
|
|
}
|
|
if (value >> 64 > 0) {
|
|
value >>= 64;
|
|
result += 64;
|
|
}
|
|
if (value >> 32 > 0) {
|
|
value >>= 32;
|
|
result += 32;
|
|
}
|
|
if (value >> 16 > 0) {
|
|
value >>= 16;
|
|
result += 16;
|
|
}
|
|
if (value >> 8 > 0) {
|
|
value >>= 8;
|
|
result += 8;
|
|
}
|
|
if (value >> 4 > 0) {
|
|
value >>= 4;
|
|
result += 4;
|
|
}
|
|
if (value >> 2 > 0) {
|
|
value >>= 2;
|
|
result += 2;
|
|
}
|
|
if (value >> 1 > 0) {
|
|
result += 1;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function log2(uint256 value, Rounding rounding) internal pure returns (uint256) {
|
|
unchecked {
|
|
uint256 result = log2(value);
|
|
return result + (rounding == Rounding.Up && 1 << result < value ? 1 : 0);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function log10(uint256 value) internal pure returns (uint256) {
|
|
uint256 result = 0;
|
|
unchecked {
|
|
if (value >= 10**64) {
|
|
value /= 10**64;
|
|
result += 64;
|
|
}
|
|
if (value >= 10**32) {
|
|
value /= 10**32;
|
|
result += 32;
|
|
}
|
|
if (value >= 10**16) {
|
|
value /= 10**16;
|
|
result += 16;
|
|
}
|
|
if (value >= 10**8) {
|
|
value /= 10**8;
|
|
result += 8;
|
|
}
|
|
if (value >= 10**4) {
|
|
value /= 10**4;
|
|
result += 4;
|
|
}
|
|
if (value >= 10**2) {
|
|
value /= 10**2;
|
|
result += 2;
|
|
}
|
|
if (value >= 10**1) {
|
|
result += 1;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function log10(uint256 value, Rounding rounding) internal pure returns (uint256) {
|
|
unchecked {
|
|
uint256 result = log10(value);
|
|
return result + (rounding == Rounding.Up && 10**result < value ? 1 : 0);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function log256(uint256 value) internal pure returns (uint256) {
|
|
uint256 result = 0;
|
|
unchecked {
|
|
if (value >> 128 > 0) {
|
|
value >>= 128;
|
|
result += 16;
|
|
}
|
|
if (value >> 64 > 0) {
|
|
value >>= 64;
|
|
result += 8;
|
|
}
|
|
if (value >> 32 > 0) {
|
|
value >>= 32;
|
|
result += 4;
|
|
}
|
|
if (value >> 16 > 0) {
|
|
value >>= 16;
|
|
result += 2;
|
|
}
|
|
if (value >> 8 > 0) {
|
|
result += 1;
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function log256(uint256 value, Rounding rounding) internal pure returns (uint256) {
|
|
unchecked {
|
|
uint256 result = log256(value);
|
|
return result + (rounding == Rounding.Up && 1 << (result << 3) < value ? 1 : 0);
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
library Strings {
|
|
bytes16 private constant _SYMBOLS = "0123456789abcdef";
|
|
uint8 private constant _ADDRESS_LENGTH = 20;
|
|
|
|
|
|
|
|
|
|
function toString(uint256 value) internal pure returns (string memory) {
|
|
unchecked {
|
|
uint256 length = Math.log10(value) + 1;
|
|
string memory buffer = new string(length);
|
|
uint256 ptr;
|
|
|
|
assembly {
|
|
ptr := add(buffer, add(32, length))
|
|
}
|
|
while (true) {
|
|
ptr--;
|
|
|
|
assembly {
|
|
mstore8(ptr, byte(mod(value, 10), _SYMBOLS))
|
|
}
|
|
value /= 10;
|
|
if (value == 0) break;
|
|
}
|
|
return buffer;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
function toHexString(uint256 value) internal pure returns (string memory) {
|
|
unchecked {
|
|
return toHexString(value, Math.log256(value) + 1);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
function toHexString(uint256 value, uint256 length) internal pure returns (string memory) {
|
|
bytes memory buffer = new bytes(2 * length + 2);
|
|
buffer[0] = "0";
|
|
buffer[1] = "x";
|
|
for (uint256 i = 2 * length + 1; i > 1; --i) {
|
|
buffer[i] = _SYMBOLS[value & 0xf];
|
|
value >>= 4;
|
|
}
|
|
require(value == 0, "Strings: hex length insufficient");
|
|
return string(buffer);
|
|
}
|
|
|
|
|
|
|
|
|
|
function toHexString(address addr) internal pure returns (string memory) {
|
|
return toHexString(uint256(uint160(addr)), _ADDRESS_LENGTH);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
library ECDSA {
|
|
enum RecoverError {
|
|
NoError,
|
|
InvalidSignature,
|
|
InvalidSignatureLength,
|
|
InvalidSignatureS,
|
|
InvalidSignatureV
|
|
}
|
|
|
|
function _throwError(RecoverError error) private pure {
|
|
if (error == RecoverError.NoError) {
|
|
return;
|
|
} else if (error == RecoverError.InvalidSignature) {
|
|
revert("ECDSA: invalid signature");
|
|
} else if (error == RecoverError.InvalidSignatureLength) {
|
|
revert("ECDSA: invalid signature length");
|
|
} else if (error == RecoverError.InvalidSignatureS) {
|
|
revert("ECDSA: invalid signature 's' value");
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function tryRecover(bytes32 hash, bytes memory signature) internal pure returns (address, RecoverError) {
|
|
if (signature.length == 65) {
|
|
bytes32 r;
|
|
bytes32 s;
|
|
uint8 v;
|
|
|
|
|
|
|
|
assembly {
|
|
r := mload(add(signature, 0x20))
|
|
s := mload(add(signature, 0x40))
|
|
v := byte(0, mload(add(signature, 0x60)))
|
|
}
|
|
return tryRecover(hash, v, r, s);
|
|
} else {
|
|
return (address(0), RecoverError.InvalidSignatureLength);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function recover(bytes32 hash, bytes memory signature) internal pure returns (address) {
|
|
(address recovered, RecoverError error) = tryRecover(hash, signature);
|
|
_throwError(error);
|
|
return recovered;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function tryRecover(
|
|
bytes32 hash,
|
|
bytes32 r,
|
|
bytes32 vs
|
|
) internal pure returns (address, RecoverError) {
|
|
bytes32 s = vs & bytes32(0x7fffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff);
|
|
uint8 v = uint8((uint256(vs) >> 255) + 27);
|
|
return tryRecover(hash, v, r, s);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function recover(
|
|
bytes32 hash,
|
|
bytes32 r,
|
|
bytes32 vs
|
|
) internal pure returns (address) {
|
|
(address recovered, RecoverError error) = tryRecover(hash, r, vs);
|
|
_throwError(error);
|
|
return recovered;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function tryRecover(
|
|
bytes32 hash,
|
|
uint8 v,
|
|
bytes32 r,
|
|
bytes32 s
|
|
) internal pure returns (address, RecoverError) {
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (uint256(s) > 0x7FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF5D576E7357A4501DDFE92F46681B20A0) {
|
|
return (address(0), RecoverError.InvalidSignatureS);
|
|
}
|
|
|
|
|
|
address signer = ecrecover(hash, v, r, s);
|
|
if (signer == address(0)) {
|
|
return (address(0), RecoverError.InvalidSignature);
|
|
}
|
|
|
|
return (signer, RecoverError.NoError);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function recover(
|
|
bytes32 hash,
|
|
uint8 v,
|
|
bytes32 r,
|
|
bytes32 s
|
|
) internal pure returns (address) {
|
|
(address recovered, RecoverError error) = tryRecover(hash, v, r, s);
|
|
_throwError(error);
|
|
return recovered;
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function toEthSignedMessageHash(bytes32 hash) internal pure returns (bytes32) {
|
|
|
|
|
|
return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n32", hash));
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function toEthSignedMessageHash(bytes memory s) internal pure returns (bytes32) {
|
|
return keccak256(abi.encodePacked("\x19Ethereum Signed Message:\n", Strings.toString(s.length), s));
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function toTypedDataHash(bytes32 domainSeparator, bytes32 structHash) internal pure returns (bytes32) {
|
|
return keccak256(abi.encodePacked("\x19\x01", domainSeparator, structHash));
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract EIP712 {
|
|
|
|
|
|
|
|
bytes32 private immutable _CACHED_DOMAIN_SEPARATOR;
|
|
uint256 private immutable _CACHED_CHAIN_ID;
|
|
address private immutable _CACHED_THIS;
|
|
|
|
bytes32 private immutable _HASHED_NAME;
|
|
bytes32 private immutable _HASHED_VERSION;
|
|
bytes32 private immutable _TYPE_HASH;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(string memory name, string memory version) {
|
|
bytes32 hashedName = keccak256(bytes(name));
|
|
bytes32 hashedVersion = keccak256(bytes(version));
|
|
bytes32 typeHash = keccak256(
|
|
"EIP712Domain(string name,string version,uint256 chainId,address verifyingContract)"
|
|
);
|
|
_HASHED_NAME = hashedName;
|
|
_HASHED_VERSION = hashedVersion;
|
|
_CACHED_CHAIN_ID = block.chainid;
|
|
_CACHED_DOMAIN_SEPARATOR = _buildDomainSeparator(typeHash, hashedName, hashedVersion);
|
|
_CACHED_THIS = address(this);
|
|
_TYPE_HASH = typeHash;
|
|
}
|
|
|
|
|
|
|
|
|
|
function _domainSeparatorV4() internal view returns (bytes32) {
|
|
if (address(this) == _CACHED_THIS && block.chainid == _CACHED_CHAIN_ID) {
|
|
return _CACHED_DOMAIN_SEPARATOR;
|
|
} else {
|
|
return _buildDomainSeparator(_TYPE_HASH, _HASHED_NAME, _HASHED_VERSION);
|
|
}
|
|
}
|
|
|
|
function _buildDomainSeparator(
|
|
bytes32 typeHash,
|
|
bytes32 nameHash,
|
|
bytes32 versionHash
|
|
) private view returns (bytes32) {
|
|
return keccak256(abi.encode(typeHash, nameHash, versionHash, block.chainid, address(this)));
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _hashTypedDataV4(bytes32 structHash) internal view virtual returns (bytes32) {
|
|
return ECDSA.toTypedDataHash(_domainSeparatorV4(), structHash);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
library Counters {
|
|
struct Counter {
|
|
|
|
|
|
|
|
uint256 _value;
|
|
}
|
|
|
|
function current(Counter storage counter) internal view returns (uint256) {
|
|
return counter._value;
|
|
}
|
|
|
|
function increment(Counter storage counter) internal {
|
|
unchecked {
|
|
counter._value += 1;
|
|
}
|
|
}
|
|
|
|
function decrement(Counter storage counter) internal {
|
|
uint256 value = counter._value;
|
|
require(value > 0, "Counter: decrement overflow");
|
|
unchecked {
|
|
counter._value = value - 1;
|
|
}
|
|
}
|
|
|
|
function reset(Counter storage counter) internal {
|
|
counter._value = 0;
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract ERC20Permit is ERC20, IERC20Permit, EIP712 {
|
|
using Counters for Counters.Counter;
|
|
|
|
mapping(address => Counters.Counter) private _nonces;
|
|
|
|
|
|
bytes32 private constant _PERMIT_TYPEHASH =
|
|
keccak256("Permit(address owner,address spender,uint256 value,uint256 nonce,uint256 deadline)");
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bytes32 private _PERMIT_TYPEHASH_DEPRECATED_SLOT;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
constructor(string memory name) EIP712(name, "1") {}
|
|
|
|
|
|
|
|
|
|
function permit(
|
|
address owner,
|
|
address spender,
|
|
uint256 value,
|
|
uint256 deadline,
|
|
uint8 v,
|
|
bytes32 r,
|
|
bytes32 s
|
|
) public virtual override {
|
|
require(block.timestamp <= deadline, "ERC20Permit: expired deadline");
|
|
|
|
bytes32 structHash = keccak256(abi.encode(_PERMIT_TYPEHASH, owner, spender, value, _useNonce(owner), deadline));
|
|
|
|
bytes32 hash = _hashTypedDataV4(structHash);
|
|
|
|
address signer = ECDSA.recover(hash, v, r, s);
|
|
require(signer == owner, "ERC20Permit: invalid signature");
|
|
|
|
_approve(owner, spender, value);
|
|
}
|
|
|
|
|
|
|
|
|
|
function nonces(address owner) public view virtual override returns (uint256) {
|
|
return _nonces[owner].current();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
function DOMAIN_SEPARATOR() external view override returns (bytes32) {
|
|
return _domainSeparatorV4();
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function _useNonce(address owner) internal virtual returns (uint256 current) {
|
|
Counters.Counter storage nonce = _nonces[owner];
|
|
current = nonce.current();
|
|
nonce.increment();
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
abstract contract ERC20Burnable is Context, ERC20 {
|
|
|
|
|
|
|
|
|
|
|
|
function burn(uint256 amount) public virtual {
|
|
_burn(_msgSender(), amount);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
function burnFrom(address account, uint256 amount) public virtual {
|
|
_spendAllowance(account, _msgSender(), amount);
|
|
_burn(account, amount);
|
|
}
|
|
}
|
|
|
|
|
|
|
|
contract Owned {
|
|
address public owner;
|
|
address public nominatedOwner;
|
|
|
|
constructor (address _owner) {
|
|
require(_owner != address(0), "Owner address cannot be 0");
|
|
owner = _owner;
|
|
emit OwnerChanged(address(0), _owner);
|
|
}
|
|
|
|
function nominateNewOwner(address _owner) external onlyOwner {
|
|
nominatedOwner = _owner;
|
|
emit OwnerNominated(_owner);
|
|
}
|
|
|
|
function acceptOwnership() external {
|
|
require(msg.sender == nominatedOwner, "You must be nominated before you can accept ownership");
|
|
emit OwnerChanged(owner, nominatedOwner);
|
|
owner = nominatedOwner;
|
|
nominatedOwner = address(0);
|
|
}
|
|
|
|
modifier onlyOwner {
|
|
require(msg.sender == owner, "Only the contract owner may perform this action");
|
|
_;
|
|
}
|
|
|
|
event OwnerNominated(address newOwner);
|
|
event OwnerChanged(address oldOwner, address newOwner);
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
contract ERC20PermitPermissionedMint is ERC20Permit, ERC20Burnable, Owned {
|
|
|
|
address public timelock_address;
|
|
|
|
|
|
address[] public minters_array;
|
|
mapping(address => bool) public minters;
|
|
|
|
|
|
|
|
constructor(
|
|
address _creator_address,
|
|
address _timelock_address,
|
|
string memory _name,
|
|
string memory _symbol
|
|
)
|
|
ERC20(_name, _symbol)
|
|
ERC20Permit(_name)
|
|
Owned(_creator_address)
|
|
{
|
|
timelock_address = _timelock_address;
|
|
}
|
|
|
|
|
|
|
|
modifier onlyByOwnGov() {
|
|
require(msg.sender == timelock_address || msg.sender == owner, "Not owner or timelock");
|
|
_;
|
|
}
|
|
|
|
modifier onlyMinters() {
|
|
require(minters[msg.sender] == true, "Only minters");
|
|
_;
|
|
}
|
|
|
|
|
|
|
|
|
|
function minter_burn_from(address b_address, uint256 b_amount) public onlyMinters {
|
|
super.burnFrom(b_address, b_amount);
|
|
emit TokenMinterBurned(b_address, msg.sender, b_amount);
|
|
}
|
|
|
|
|
|
function minter_mint(address m_address, uint256 m_amount) public onlyMinters {
|
|
super._mint(m_address, m_amount);
|
|
emit TokenMinterMinted(msg.sender, m_address, m_amount);
|
|
}
|
|
|
|
|
|
function addMinter(address minter_address) public onlyByOwnGov {
|
|
require(minter_address != address(0), "Zero address detected");
|
|
|
|
require(minters[minter_address] == false, "Address already exists");
|
|
minters[minter_address] = true;
|
|
minters_array.push(minter_address);
|
|
|
|
emit MinterAdded(minter_address);
|
|
}
|
|
|
|
|
|
function removeMinter(address minter_address) public onlyByOwnGov {
|
|
require(minter_address != address(0), "Zero address detected");
|
|
require(minters[minter_address] == true, "Address nonexistant");
|
|
|
|
|
|
delete minters[minter_address];
|
|
|
|
|
|
for (uint i = 0; i < minters_array.length; i++){
|
|
if (minters_array[i] == minter_address) {
|
|
minters_array[i] = address(0);
|
|
break;
|
|
}
|
|
}
|
|
|
|
emit MinterRemoved(minter_address);
|
|
}
|
|
|
|
function setTimelock(address _timelock_address) public onlyByOwnGov {
|
|
require(_timelock_address != address(0), "Zero address detected");
|
|
timelock_address = _timelock_address;
|
|
emit TimelockChanged(_timelock_address);
|
|
}
|
|
|
|
|
|
|
|
event TokenMinterBurned(address indexed from, address indexed to, uint256 amount);
|
|
event TokenMinterMinted(address indexed from, address indexed to, uint256 amount);
|
|
event MinterAdded(address minter_address);
|
|
event MinterRemoved(address minter_address);
|
|
event TimelockChanged(address timelock_address);
|
|
}
|
|
|
|
contract frxETH is ERC20PermitPermissionedMint {
|
|
|
|
|
|
constructor(
|
|
address _creator_address,
|
|
address _timelock_address
|
|
)
|
|
ERC20PermitPermissionedMint(_creator_address, _timelock_address, "Frax Ether", "frxETH")
|
|
{}
|
|
|
|
} |