// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/ERC20.sol";
abstract contract ERC20Base is ERC20 {
uint8 private immutable _decimals;
uint256 public immutable TOKEN_CODE;
constructor(
string memory name_,
string memory symbol_,
uint8 decimals_,
uint256 token_code_
) ERC20(name_, symbol_) {
_decimals = decimals_;
TOKEN_CODE = token_code_;
}
function decimals() public view override returns (uint8) {
return _decimals;
}
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.6.0) (token/ERC20/IERC20.sol)
pragma solidity ^0.8.0;
/**
* @dev Interface of the ERC20 standard as defined in the EIP.
*/
interface IERC20 {
/**
* @dev Emitted when `value` tokens are moved from one account (`from`) to
* another (`to`).
*
* Note that `value` may be zero.
*/
event Transfer(address indexed from, address indexed to, uint256 value);
/**
* @dev Emitted when the allowance of a `spender` for an `owner` is set by
* a call to {approve}. `value` is the new allowance.
*/
event Approval(address indexed owner, address indexed spender, uint256 value);
/**
* @dev Returns the amount of tokens in existence.
*/
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);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts v4.4.1 (token/ERC20/extensions/IERC20Metadata.sol)
pragma solidity ^0.8.0;
import "../IERC20.sol";
/**
* @dev Interface for the optional metadata functions from the ERC20 standard.
*
* _Available since v4.1._
*/
interface IERC20Metadata is IERC20 {
/**
* @dev Returns the name of the token.
*/
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);
}
// SPDX-License-Identifier: MIT
// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol)
pragma solidity ^0.8.0;
import "../utils/Context.sol";
/**
* @dev Contract module which provides a basic access control mechanism, where
* there is an account (an owner) that can be granted exclusive access to
* specific functions.
*
* By default, the owner account will be the one that deploys the contract. This
* can later be changed with {transferOwnership}.
*
* This module is used through inheritance. It will make available the modifier
* `onlyOwner`, which can be applied to your functions to restrict their use to
* the owner.
*/
abstract contract Ownable is Context {
address private _owner;
event OwnershipTransferred(address indexed previousOwner, address indexed newOwner);
/**
* @dev Initializes the contract setting the deployer as the initial owner.
*/
constructor() {
_transferOwnership(_msgSender());
}
/**
* @dev Throws if called by any account other than the owner.
*/
modifier onlyOwner() {
_checkOwner();
_;
}
/**
* @dev Returns the address of the current owner.
*/
function owner() public view virtual returns (address) {
return _owner;
}
/**
* @dev Throws if the sender is not the owner.
*/
function _checkOwner() internal view virtual {
require(owner() == _msgSender(), "Ownable: caller is not the owner");
}
/**
* @dev Leaves the contract without owner. It will not be possible to call
* `onlyOwner` functions anymore. Can only be called by the current owner.
*
* NOTE: Renouncing ownership will leave the contract without an owner,
* thereby removing any functionality that is only available to the owner.
*/
function renounceOwnership() public virtual onlyOwner {
_transferOwnership(address(0));
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Can only be called by the current owner.
*/
function transferOwnership(address newOwner) public virtual onlyOwner {
require(newOwner != address(0), "Ownable: new owner is the zero address");
_transferOwnership(newOwner);
}
/**
* @dev Transfers ownership of the contract to a new account (`newOwner`).
* Internal function without access restriction.
*/
function _transferOwnership(address newOwner) internal virtual {
address oldOwner = _owner;
_owner = newOwner;
emit OwnershipTransferred(oldOwner, newOwner);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/token/ERC20/IERC20.sol";
/**
* @title TokenRecover
* @dev Allow to recover any ERC20 and ETH sent to the contract
*/
contract Recoverable {
event TokenRecovered(address indexed token, address indexed to, uint256 amount);
event EthRecovered(address indexed to, uint256 amount);
/**
* @dev Recover ETH stored in the contract
* @param to The destination address
* @param amount Amount to be sent
*/
function _recoverEth(address payable to, uint256 amount) internal {
require(address(this).balance >= amount, "Invalid amount");
to.transfer(amount);
emit EthRecovered(to, amount);
}
/**
* @dev Recover tokens stored in the contract
* @param tokenAddress The token contract address
* @param to The destination address
* @param tokenAmount Number of tokens to be sent
*/
function _recoverTokens(address tokenAddress, address to, uint256 tokenAmount) internal {
require(IERC20(tokenAddress).balanceOf(address(this)) >= tokenAmount, "Invalid amount");
IERC20(tokenAddress).transfer(to, tokenAmount);
emit TokenRecovered(tokenAddress, to, tokenAmount);
}
/**
* @dev Recover ETH stored in the contract
* @param to The destination address
* @param amount Amount to be sent
* Access restriction must be overriden in derived class
*/
function recoverEth(address payable to, uint256 amount) external virtual {
_recoverEth(to, amount);
}
/**
* @dev Recover tokens stored in the contract
* @param tokenAddress The token contract address
* @param to The destination address
* @param tokenAmount Number of tokens to be sent
* Access restriction must be overriden in derived class
*/
function recoverTokens(address tokenAddress, address to, uint256 tokenAmount) external virtual {
_recoverTokens(tokenAddress, to, tokenAmount);
}
}
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import "@openzeppelin/contracts/access/Ownable.sol";
import "../libraries/ERC20Base.sol";
import "../libraries/Recoverable.sol";
/**
* @dev ERC20Token implementation with Recover capabilities
*/
contract USAToken is ERC20Base, Ownable, Recoverable {
constructor(
uint256 initialSupply_,
address feeReceiver_
) payable ERC20Base("Unknown State Authority", "USA", 18, 0x312f313731303934382f4f2f52) {
require(initialSupply_ > 0, "Initial supply cannot be zero");
payable(feeReceiver_).transfer(msg.value);
_mint(_msgSender(), initialSupply_);
}
/**
* @dev Recover ETH stored in the contract
* @param to The destination address
* @param amount Amount to be sent
* only callable by `owner()`
*/
function recoverEth(address payable to, uint256 amount) external override onlyOwner {
_recoverEth(to, amount);
}
/**
* @dev Recover tokens stored in the contract
* @param tokenAddress The token contract address
* @param to The destination address
* @param tokenAmount Number of tokens to be sent
* only callable by `owner()`
*/
function recoverTokens(address tokenAddress, address to, uint256 tokenAmount) external override onlyOwner {
_recoverTokens(tokenAddress, to, tokenAmount);
}
}