Last active
May 6, 2018 08:07
-
-
Save lychees/1a5506aa39f9ed6eac3609e2c2e29043 to your computer and use it in GitHub Desktop.
钱袋
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| pragma solidity ^0.4.21; | |
| /// @title Interface for contracts conforming to ERC-721: Non-Fungible Tokens | |
| /// @author MinakoKojima (https://github.com/lychees) | |
| contract ERC721 { | |
| // Required methods | |
| function totalSupply() public view returns (uint256 total); | |
| function balanceOf(address _owner) public view returns (uint256 balance); | |
| function ownerOf(uint256 _tokenId) public view returns (address owner); | |
| function approve(address _to, uint256 _tokenId) public; | |
| function transfer(address _to, uint256 _tokenId) public; | |
| function transferFrom(address _from, address _to, uint256 _tokenId) public; | |
| // Optional | |
| function name() public view returns (string _name); | |
| function symbol() public view returns (string _symbol); | |
| // function tokensOfOwner(address _owner) external view returns (uint256[] tokenIds); | |
| // function tokenMetadata(uint256 _tokenId, string _preferredTransport) public view returns (string infoUrl); | |
| // ERC-165 Compatibility (https://github.com/ethereum/EIPs/issues/165) | |
| // function supportsInterface(bytes4 _interfaceID) external view returns (bool); | |
| } | |
| contract ERC20Package is ERC721{ | |
| event Transfer(address indexed _from, address indexed _to, uint256 _tokenId); | |
| event Approval(address indexed _owner, address indexed _approved, uint256 _tokenId); | |
| event Put(address indexed addr, address indexed issuer, uint256 amount); | |
| event Open(address indexed addr); | |
| address private owner; | |
| mapping (address => bool) private admins; | |
| uint256 private tokenSize; | |
| mapping (uint256 => address) private ownerOfToken; | |
| mapping (uint256 => address) private approvedOfToken; | |
| struct Package { | |
| address issuer; | |
| uint256 amount; | |
| } | |
| mapping (uint256 => Package[]) private package; | |
| uint256[] private packageSize; | |
| function ERC20Package() public { | |
| owner = msg.sender; | |
| admins[owner] = true; | |
| } | |
| /* Modifiers */ | |
| modifier onlyOwner() { | |
| require(owner == msg.sender); | |
| _; | |
| } | |
| modifier onlyAdmins() { | |
| require(admins[msg.sender]); | |
| _; | |
| } | |
| /* Owner */ | |
| function setOwner (address _owner) onlyOwner() public { | |
| owner = _owner; | |
| } | |
| function addAdmin (address _admin) onlyOwner() public { | |
| admins[_admin] = true; | |
| } | |
| function removeAdmin (address _admin) onlyOwner() public { | |
| delete admins[_admin]; | |
| } | |
| /* Withdraw */ | |
| /* | |
| NOTICE: These functions withdraw the developer's cut which is left | |
| in the contract by `buy`. User funds are immediately sent to the old | |
| owner in `buy`, no user funds are left in the contract. | |
| */ | |
| function withdrawAll () onlyAdmins() public { | |
| msg.sender.transfer(address(this).balance); | |
| } | |
| function withdrawAmount (uint256 _amount) onlyAdmins() public { | |
| msg.sender.transfer(_amount); | |
| } | |
| /* ERC721 */ | |
| function name() public view returns (string _name) { | |
| return "ERC20Package"; | |
| } | |
| function symbol() public view returns (string _symbol) { | |
| return "E2P"; | |
| } | |
| function totalSupply() public view returns (uint256 _totalSupply) { | |
| return tokenSize; | |
| } | |
| function balanceOf (address _owner) public view returns (uint256 _balance) { | |
| uint256 counter = 0; | |
| for (uint256 i = 0; i < tokenSize; i++) { | |
| if (ownerOf(i) == _owner) { | |
| counter++; | |
| } | |
| } | |
| return counter; | |
| } | |
| function ownerOf (uint256 _tokenId) public view returns (address _owner) { | |
| return ownerOfToken[_tokenId]; | |
| } | |
| function tokensOf (address _owner) public view returns (uint256[] _tokenIds) { | |
| uint256[] memory Tokens = new uint256[](balanceOf(_owner)); | |
| uint256 TokenCounter = 0; | |
| for (uint256 i = 0; i < tokenSize; i++) { | |
| if (ownerOf(i) == _owner) { | |
| Tokens[TokenCounter] = i; | |
| TokenCounter += 1; | |
| } | |
| } | |
| return Tokens; | |
| } | |
| function approvedFor(uint256 _tokenId) public view returns (address _approved) { | |
| return approvedOfToken[_tokenId]; | |
| } | |
| function approve(address _to, uint256 _tokenId) public { | |
| require(msg.sender != _to); | |
| require(ownerOf(_tokenId) == msg.sender); | |
| if (_to == 0) { | |
| if (approvedOfToken[_tokenId] != 0) { | |
| delete approvedOfToken[_tokenId]; | |
| emit Approval(msg.sender, 0, _tokenId); | |
| } | |
| } else { | |
| approvedOfToken[_tokenId] = _to; | |
| emit Approval(msg.sender, _to, _tokenId); | |
| } | |
| } | |
| /* Transferring a country to another owner will entitle the new owner the profits from `buy` */ | |
| function transfer(address _to, uint256 _tokenId) public { | |
| require(msg.sender == ownerOf(_tokenId)); | |
| _transfer(msg.sender, _to, _tokenId); | |
| } | |
| function transferFrom(address _from, address _to, uint256 _tokenId) public { | |
| require(approvedFor(_tokenId) == msg.sender); | |
| _transfer(_from, _to, _tokenId); | |
| } | |
| function _transfer(address _from, address _to, uint256 _tokenId) internal { | |
| require(ownerOf(_tokenId) == _from); | |
| require(_to != address(0)); | |
| require(_to != address(this)); | |
| ownerOfToken[_tokenId] = _to; | |
| approvedOfToken[_tokenId] = 0; | |
| emit Transfer(_from, _to, _tokenId); | |
| } | |
| /* Read */ | |
| function isAdmin (address _admin) public view returns (bool _isAdmin) { | |
| return admins[_admin]; | |
| } | |
| function allOf (uint256 _tokenId) external view returns (address _owner) { | |
| return (ownerOf(_tokenId)); | |
| } | |
| /* Read */ | |
| function getAllPackage(uint256 _tokenId) public view returns (address[] _issuer, uint256[] _amount) { | |
| address[] memory ISSUER = new address[](packageSize[_tokenId]); | |
| uint256[] memory AMOUNT = new uint256[](packageSize[_tokenId]); | |
| for (uint i = 0; i < packageSize[_tokenId]; i++) { | |
| ISSUER[i] = package[_tokenId][i].issuer; | |
| AMOUNT[i] = package[_tokenId][i].amount; | |
| } | |
| return (ISSUER, AMOUNT); | |
| } | |
| /* Util */ | |
| function isContract(address addr) internal view returns (bool) { | |
| uint size; | |
| assembly { size := extcodesize(addr) } // solium-disable-line | |
| return size > 0; | |
| } | |
| function put(uint256 _tokenId, address _issuer, uint256 _amount) public { | |
| Issuer issuer = Issuer(_issuer); | |
| issuer.transferFrom(msg.sender, address(this), _amount); | |
| if (packageSize[_tokenId] >= package[_tokenId].length) { | |
| package[_tokenId].push(Package(_issuer, _amount)); | |
| } else { | |
| package[_tokenId][packageSize[_tokenId]].issuer = _issuer; | |
| package[_tokenId][packageSize[_tokenId]].amount = _amount; | |
| } | |
| packageSize[_tokenId] += 1; | |
| } | |
| function open(uint256 _tokenId) public { | |
| require(msg.sender == ownerOfToken[_tokenId]); | |
| require(packageSize[_tokenId] > 0); | |
| emit Open(msg.sender); | |
| /* transfer */ | |
| for (uint256 i = 0; i < packageSize[_tokenId]; i++) { | |
| Issuer issuer = Issuer(package[_tokenId][i].issuer); | |
| issuer.transfer(msg.sender, package[_tokenId][i].amount); | |
| } | |
| /* remove */ | |
| packageSize[_tokenId] = 0; | |
| } | |
| /* Issue */ | |
| function issueToken(uint256 _count) onlyAdmins() public { | |
| uint256 l = tokenSize; | |
| uint256 r = tokenSize + _count; | |
| for (uint256 i = l; i < r; i++) { | |
| ownerOfToken[i] = msg.sender; | |
| } | |
| tokenSize += _count; | |
| } | |
| function issueTokenAndTransfer(uint256 _count, address to) onlyAdmins() public { | |
| uint256 l = tokenSize; | |
| uint256 r = tokenSize + _count; | |
| for (uint256 i = l; i < r; i++) { | |
| ownerOfToken[i] = to; | |
| } | |
| tokenSize += _count; | |
| } | |
| function issueTokenAndApprove(uint256 _count, address to) onlyAdmins() public { | |
| uint256 l = tokenSize; | |
| uint256 r = tokenSize + _count; | |
| for (uint256 i = l; i < r; i++) { | |
| ownerOfToken[i] = msg.sender; | |
| approve(to, i); | |
| } | |
| tokenSize += _count; | |
| } | |
| } | |
| interface Issuer { | |
| function transferFrom(address _from, address _to, uint256 _tokenId) external; | |
| function transfer(address _to, uint256 _tokenId) external; | |
| function ownerOf (uint256 _tokenId) external view returns (address _owner); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment