From 90f0f87a61d63b0390c6519cb5ed97082e2c8066 Mon Sep 17 00:00:00 2001 From: Jason Date: Wed, 16 Nov 2022 14:55:31 +0800 Subject: [PATCH] =?UTF-8?q?=E5=90=88=E7=BA=A6=E6=8F=90=E6=AC=BE,=E8=BD=AC?= =?UTF-8?q?=E5=90=84=E7=A7=8Dtoken?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- README.md | 6 ++- src/ERC20/TransferERC20.sol | 28 +++++++++++++ src/common/Ownable.sol | 78 ------------------------------------- src/common/Paymentable.sol | 32 +++++++++++++++ src/common/Transfer.sol | 56 ++++++++++++++++++++++++++ 5 files changed, 121 insertions(+), 79 deletions(-) create mode 100644 src/ERC20/TransferERC20.sol delete mode 100644 src/common/Ownable.sol create mode 100644 src/common/Paymentable.sol create mode 100644 src/common/Transfer.sol diff --git a/README.md b/README.md index d8478f0..3ce8330 100644 --- a/README.md +++ b/README.md @@ -6,4 +6,8 @@ ### 共享本地内容 -> remixd -s /Users/jason/Documents/Develop/Sol-Projects/ERC-DEV -u https://remix.ethereum.org \ No newline at end of file +> remixd -s /Users/jason/Documents/Develop/Sol-Projects/ERC-DEV -u https://remix.ethereum.org + +### verify code + +> 使用 standard-json-input,在 build-info里面找 \ No newline at end of file diff --git a/src/ERC20/TransferERC20.sol b/src/ERC20/TransferERC20.sol new file mode 100644 index 0000000..f46820c --- /dev/null +++ b/src/ERC20/TransferERC20.sol @@ -0,0 +1,28 @@ +// SPDX-License-Identifier: GPL-3.0 + +pragma solidity ^0.8.0; + +import "@openzeppelin/contracts/token/ERC20/ERC20.sol"; +import "@openzeppelin/contracts/access/Ownable.sol"; + +contract TransferERC20 is ERC20, Ownable { + uint8 private _decimals; + + constructor() payable ERC20("TRANSFER ERC20", "TERC20") { + _decimals = 8; + _mint(msg.sender, 33333333 * (10**8)); + } + + function decimals() public view virtual override returns (uint8) { + return _decimals; + } + + /** + * @dev 从合约中提取ERC20 + */ + function transferERC20( + address tokenAddress, + address to, + uint256 amount + ) public virtual onlyOwner {} +} diff --git a/src/common/Ownable.sol b/src/common/Ownable.sol deleted file mode 100644 index 1839068..0000000 --- a/src/common/Ownable.sol +++ /dev/null @@ -1,78 +0,0 @@ -// SPDX-License-Identifier: MIT -// OpenZeppelin Contracts (last updated v4.7.0) (access/Ownable.sol) - -pragma solidity ^0.8.0; - -import "@openzeppelin/contracts/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 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); - } -} diff --git a/src/common/Paymentable.sol b/src/common/Paymentable.sol new file mode 100644 index 0000000..daa8e5a --- /dev/null +++ b/src/common/Paymentable.sol @@ -0,0 +1,32 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.0; + +import "@openzeppelin/contracts/access/Ownable.sol"; + +abstract contract Paymentable is Ownable { + /** + * @dev 从合约中提现 + */ + function withdraw(address payable to, uint256 amount) + public + payable + virtual + onlyOwner + { + require( + to != address(0), + "WITHDRAW: Can not withdraw to empty address" + ); + require( + address(this).balance >= amount, + "WITHDRAW: Insufficient balance" + ); + to.transfer(amount); + } + + /** + * @dev 这个是为了接收ETH转账用的 + */ + receive() external payable {} +} diff --git a/src/common/Transfer.sol b/src/common/Transfer.sol new file mode 100644 index 0000000..0dbd954 --- /dev/null +++ b/src/common/Transfer.sol @@ -0,0 +1,56 @@ +// SPDX-License-Identifier: MIT + +pragma solidity ^0.8.0; + +import "@openzeppelin/contracts/access/Ownable.sol"; +import "@openzeppelin/contracts/token/ERC20/IERC20.sol"; +import "@openzeppelin/contracts/token/ERC721/IERC721.sol"; +import "@openzeppelin/contracts/token/ERC1155/IERC1155.sol"; +import "@openzeppelin/contracts/token/ERC721/utils/ERC721Holder.sol"; +import "@openzeppelin/contracts/token/ERC1155/utils/ERC1155Holder.sol"; + +abstract contract Transfer is Ownable, ERC721Holder, ERC1155Holder { + /** + * @dev 从合约中提取ERC20,因为ERC20转账不会检测是否是合约,是否可以接收代币,不用前置的一些操作 + */ + function transferERC20( + address tokenAddress, + address to, + uint256 amount + ) public virtual onlyOwner { + require(to != address(0), "TRANSFER: to the zero address"); + IERC20 token = IERC20(tokenAddress); + uint256 balance = token.balanceOf(address(this)); + require(amount <= balance, "TRANSFER: insufficient balance"); + token.transfer(to, amount); + } + + function transferERC721( + address tokenAddress, + address to, + uint256 tokenId + ) public virtual onlyOwner { + require(to != address(0), "TRANSFER: to the zero address"); + IERC721 token = IERC721(tokenAddress); + require( + address(this) == token.ownerOf(tokenId), + "TRANSFER: transfer from incorrect owner" + ); + token.transferFrom(address(this), to, tokenId); + } + + function transferERC1155( + address tokenAddress, + address to, + uint256 tokenId, + uint256 amount + ) public virtual onlyOwner { + require(to != address(0), "TRANSFER: to the zero address"); + IERC1155 token = IERC1155(tokenAddress); + require( + amount <= token.balanceOf(address(this), tokenId), + "TRANSFER: insufficient balance" + ); + token.safeTransferFrom(address(this), to, tokenId, amount, bytes("")); + } +}