Instantly share code, notes, and snippets.

View takeOwnership
function isApprovedFor(address _owner, uint256 _tokenId)
internal view returns (bool) {
return approvedFor(_tokenId) == _owner;
}
function takeOwnership(uint256 _tokenId) public {
require(isApprovedFor(msg.sender, _tokenId));
// Logic to clear approval for token transfer
View approve
mapping (uint256 => address) private tokenApprovals;
modifier onlyOwnerOf(uint256 _tokenId) {
require(ownerOf(_tokenId) == msg.sender);
_;
}
function approvedFor(uint256 _tokenId)
public view returns (address) {
return tokenApprovals[_tokenId];
View transfer
modifier onlyOwnerOf(uint256 _tokenId) {
require(ownerOf(_tokenId) == msg.sender);
_;
}
function transfer(address _to, uint256 _tokenId)
public onlyOwnerOf(_tokenId) {
// Logic to clear approval for token transfer
// Logic to remove token from current token owner
View ownerOf
mapping (uint256 => address) private tokenOwner;
function ownerOf(uint256 _tokenId) public view returns (address) {
address owner = tokenOwner[_tokenId];
require(owner != address(0));
return owner;
}
View balanceOf
mapping (address => uint256[]) private ownedTokens;
function balanceOf(address _owner) public view returns (uint256) {
return ownedTokens[_owner].length;
}
View ERC721
contract ERC721 {
event Transfer(
address indexed _from,
address indexed _to,
uint256 _tokenId
);
event Approval(
address indexed _owner,
address indexed _approved,
View transferFrom
function transferFrom(
address _from,
address _to,
uint256 _value,
bytes _data
) public returns (bool) {
require(_to != address(this));
super.transferFrom(_from, _to, _value);
View transfer
function transfer(address _to, uint256 _value, bytes _data)
public returns (bool) {
require(_to != address(this));
require(super.transfer(_to, _value));
require(_to.call(_data));
return true;
}
View approve
function approve(address _spender, uint256 _value, bytes _data)
public returns (bool) {
require(_spender != address(this));
require(super.approve(_spender, _value));
require(_spender.call(_data));
return true;
}
View ERC827
contract ERC827 is ERC20 {
function approve(
address _spender,
uint256 _value,
bytes _data
) public returns (bool);
function transfer(
address _to,
uint256 _value,