-
-
Save netkiller/4f4ab321154e62740d2b132e3766708a to your computer and use it in GitHub Desktop.
My implementation of the ERC721 token standard. WARNING: THIS CODE IS FOR EDUCATIONAL PURPOSES. DO NOT DEPLOY TO THE NETWORK.
This file contains 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.19; | |
contract ERC721 { | |
string constant private tokenName = "My ERC721 Token"; | |
string constant private tokenSymbol = "MET"; | |
uint256 constant private totalTokens = 1000000; | |
mapping(address => uint) private balances; | |
mapping(uint256 => address) private tokenOwners; | |
mapping(uint256 => bool) private tokenExists; | |
mapping(address => mapping (address => uint256)) private allowed; | |
mapping(address => mapping(uint256 => uint256)) private ownerTokens; | |
mapping(uint256 => string) tokenLinks; | |
function removeFromTokenList(address owner, uint256 _tokenId) private { | |
for(uint256 i = 0;ownerTokens[owner][i] != _tokenId;i++){ | |
ownerTokens[owner][i] = 0; | |
} | |
} | |
function name() public constant returns (string){ | |
return tokenName; | |
} | |
function symbol() public constant returns (string) { | |
return tokenSymbol; | |
} | |
function totalSupply() public constant returns (uint256){ | |
return totalTokens; | |
} | |
function balanceOf(address _owner) constant returns (uint){ | |
return balances[_owner]; | |
} | |
function ownerOf(uint256 _tokenId) constant returns (address){ | |
require(tokenExists[_tokenId]); | |
return tokenOwners[_tokenId]; | |
} | |
function approve(address _to, uint256 _tokenId){ | |
require(msg.sender == ownerOf(_tokenId)); | |
require(msg.sender != _to); | |
allowed[msg.sender][_to] = _tokenId; | |
Approval(msg.sender, _to, _tokenId); | |
} | |
function takeOwnership(uint256 _tokenId){ | |
require(tokenExists[_tokenId]); | |
address oldOwner = ownerOf(_tokenId); | |
address newOwner = msg.sender; | |
require(newOwner != oldOwner); | |
require(allowed[oldOwner][newOwner] == _tokenId); | |
balances[oldOwner] -= 1; | |
tokenOwners[_tokenId] = newOwner; | |
balances[oldOwner] += 1; | |
Transfer(oldOwner, newOwner, _tokenId); | |
} | |
function transfer(address _to, uint256 _tokenId){ | |
address currentOwner = msg.sender; | |
address newOwner = _to; | |
require(tokenExists[_tokenId]); | |
require(currentOwner == ownerOf(_tokenId)); | |
require(currentOwner != newOwner); | |
require(newOwner != address(0)); | |
removeFromTokenList(_tokenId); | |
balances[currentOwner] -= 1; | |
tokenOwners[_tokenId] = newOwner; | |
balances[newOwner] += 1; | |
Transfer(currentOwner, newOwner, _tokenId); | |
} | |
function tokenOfOwnerByIndex(address _owner, uint256 _index) constant returns (uint tokenId){ | |
return ownerTokens[_owner][_index]; | |
} | |
function tokenMetadata(uint256 _tokenId) constant returns (string infoUrl){ | |
return tokenLinks[_tokenId]; | |
} | |
event Transfer(address indexed _from, address indexed _to, uint256 _tokenId); | |
event Approval(address indexed _owner, address indexed _approved, uint256 _tokenId); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment