-
-
Save lychees/8956f010c8fe96e0d4e8619fc91a4bfb 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; | |
| // Events | |
| event Transfer(address from, address to, uint256 tokenId); | |
| event Approval(address owner, address approved, uint256 tokenId); | |
| // 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 CryptoERC20TokenPackage is ERC721{ | |
| event Bought (uint256 indexed _tokenId, address indexed _owner, uint256 _price); | |
| event Sold (uint256 indexed _tokenId, address indexed _owner, uint256 _price); | |
| event Transfer(address indexed _from, address indexed _to, uint256 _tokenId); | |
| event Approval(address indexed _owner, address indexed _approved, uint256 _tokenId); | |
| address private owner; | |
| mapping (address => bool) private admins; | |
| uint256 private tokenSize; | |
| address[] private ownerOfToken; | |
| address[] private approvedOfToken; | |
| uint256[] private ethOfToken; | |
| struct Package{ | |
| address issuer; | |
| uint256 value; | |
| } | |
| mapping(uint256 => Package[]) private package; | |
| uint256[] private packageSize; | |
| function CryptoERC20TokenPackage() public { | |
| owner = msg.sender; | |
| admins[owner] = true; | |
| } | |
| /* Modifiers */ | |
| modifier onlyOwner() { | |
| require(owner == msg.sender); | |
| _; | |
| } | |
| modifier onlyOwnerOf(uint256 _tokenId) { | |
| require(ownerOfToken[_tokenId] == 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 "cryptosanguo.pro"; | |
| } | |
| function symbol() public view returns (string _symbol) { | |
| return "CSG"; | |
| } | |
| 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]; | |
| } | |
| /* Use */ | |
| function open(uint256 _tokenId) public onlyOwnerOf(_tokenId) { | |
| for (uint256 i = 0; i < packageSize[_tokenId]; ++i) { | |
| Issuer issuer = Issuer(package[_tokenId][i].issuer); | |
| issuer.transfer(msg.sender, package[_tokenId][i].value); | |
| } | |
| ownerOfToken[_tokenId].transfer(ethOfToken[_tokenId]); | |
| ethOfToken[_tokenId] = 0; | |
| packageSize[_tokenId] = 0; | |
| } | |
| function putETH(uint256 _tokenId) public payable onlyOwnerOf(_tokenId){ | |
| ethOfToken[_tokenId] += msg.value; | |
| } | |
| function putERC20(uint256 _tokenId, address _issuer, uint256 _value) public onlyOwnerOf(_tokenId){ | |
| Issuer issuer = Issuer(_issuer); | |
| issuer.transferFrom(msg.sender, address(this), _value); | |
| for (uint256 i = 0; i < packageSize[_tokenId]; ++i) { | |
| if (issuer == package[_tokenId][i].issuer) { | |
| package[_tokenId][i].value += _value; | |
| return; | |
| } | |
| } | |
| if (packageSize[_tokenId] == package[_tokenId].length){ | |
| package[_tokenId].push(Package(_issuer, _value)); | |
| } else { | |
| package[_tokenId][packageSize[_tokenId]] = Package(_issuer, _value); | |
| } | |
| packageSize[_tokenId] += 1; | |
| } | |
| /* Issue */ | |
| function issueToken(uint256 _count) onlyAdmins() public { | |
| uint256 l = tokenSize; | |
| uint256 r = tokenSize + _count; | |
| for (uint256 i = l; i < r; i++) { | |
| ownerOfToken.push(msg.sender); | |
| packageSize.push(0); | |
| } | |
| 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.push(to); | |
| packageSize.push(0); | |
| } | |
| 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.push(msg.sender); | |
| packageSize.push(0); | |
| approve(to, i); | |
| } | |
| tokenSize += _count; | |
| } | |
| } | |
| interface Issuer { | |
| function transfer(address _to, uint256 _value) external; | |
| function transferFrom(address _from, address _to, uint256 _value) external; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment