合约提款,转各种token

This commit is contained in:
2022-11-16 14:55:31 +08:00
parent 7a750289ce
commit 90f0f87a61
5 changed files with 121 additions and 79 deletions

View File

@@ -6,4 +6,8 @@
### 共享本地内容 ### 共享本地内容
> remixd -s /Users/jason/Documents/Develop/Sol-Projects/ERC-DEV -u https://remix.ethereum.org > remixd -s /Users/jason/Documents/Develop/Sol-Projects/ERC-DEV -u https://remix.ethereum.org
### verify code
> 使用 standard-json-input在 build-info里面找

View File

@@ -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 {}
}

View File

@@ -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);
}
}

View File

@@ -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 {}
}

56
src/common/Transfer.sol Normal file
View File

@@ -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(""));
}
}