Skip to content

Instantly share code, notes, and snippets.

@ghiliweld
Created February 3, 2018 19:26
Show Gist options
  • Star 10 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ghiliweld/95bf92efd6d3abced1fae77b13e9b968 to your computer and use it in GitHub Desktop.
Save ghiliweld/95bf92efd6d3abced1fae77b13e9b968 to your computer and use it in GitHub Desktop.
ERC-721 Library
pragma solidity ^0.4.19;
/**
* @title NonFungibleTokenLib
* @author Ghilia Weldesselasie
* SHAMELESS SELF-PLUG: https://github.com/ERC-ME/Whitepaper
*
* version 1.2.1
* Copyright (c) 2018 Ghilia Weldesselasie
* The MIT License (MIT)
*
* The Non Fungible Token Library provides functionality to create a variety of ERC721 tokens.
* Non Fungible Tokens (NFTs) shall henceforth be referred to as Deeds cause it's shorter
* The choice of the word deed comes from https://github.com/ethereum/EIPs/pull/841
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
import "./BasicMathLib.sol";
library NonFungibleTokenLib {
using BasicMathLib for uint256;
struct DeedStorage {
string name;
string symbol;
address controller;
struct Deed {
address mintedBy;
uint256 mintedAt;
}
Deed[] public deeds; // array of Deed struct
// Mapping from token ID to owner
mapping (uint256 => address) public tokenOwner;
// Mapping from token ID to approved address
mapping (uint256 => address) public tokenApprovals;
// Mapping from owner to list of owned token IDs
mapping (address => uint256[]) public ownedTokens;
}
event Transfer(address indexed from, address indexed to, uint256 value);
event Approval(address indexed owner, address indexed spender, uint256 value);
event OwnerChange(address from, address to);
event DeedMinted(address minter, uint256 tokenId)
/**
* @dev Gets the total amount of tokens stored by the contract
* @return uint256 representing the total amount of tokens
*/
function totalSupply(DeedStorage storage self) public view returns (uint256) {
return self.deeds.length;
}
/**
* @dev Gets the balance of the specified address
* @param _owner address to query the balance of
* @return uint256 representing the amount owned by the passed address
*/
function balanceOf(DeedStorage storage self, address _owner) public view returns (uint256) {
return self.ownedTokens[_owner].length;
}
/**
* @dev Gets the list of tokens owned by a given address
* @param _owner address to query the tokens of
* @return uint256[] representing the list of tokens owned by the passed address
*/
function tokensOf(DeedStorage storage self, address _owner) public view returns (uint256[]) {
return self.ownedTokens[_owner];
}
/**
* @dev Gets the owner of the specified token ID
* @param _tokenId uint256 ID of the token to query the owner of
* @return owner address currently marked as the owner of the given token ID
*/
function ownerOf(DeedStorage storage self, uint256 _tokenId) public view returns (address) {
address owner = self.tokenOwner[_tokenId];
require(owner != address(0));
return owner;
}
/**
* @dev Gets the approved address to take ownership of a given token ID
* @param _tokenId uint256 ID of the token to query the approval of
* @return address currently approved to take ownership of the given token ID
*/
function approvedFor(DeedStorage storage self, uint256 _tokenId) public view returns (address) {
return self.tokenApprovals[_tokenId];
}
/**
* @dev Transfers the ownership of a given token ID to another address
* @param _to address to receive the ownership of the given token ID
* @param _tokenId uint256 ID of the token to be transferred
*/
function transfer(address _to, uint256 _tokenId) public onlyOwnerOf(_tokenId) {
clearApprovalAndTransfer(msg.sender, _to, _tokenId);
}
/**
* @dev Approves another address to claim for the ownership of the given token ID
* @param _to address to be approved for the given token ID
* @param _tokenId uint256 ID of the token to be approved
*/
function approve(DeedStorage storage self, address _to, uint256 _tokenId) public onlyOwnerOf(_tokenId) {
address owner = ownerOf(_tokenId);
require(_to != owner);
if (approvedFor(_tokenId) != 0 || _to != 0) {
self.tokenApprovals[_tokenId] = _to;
Approval(owner, _to, _tokenId);
}
}
/**
* @dev Claims the ownership of a given token ID
* @param _tokenId uint256 ID of the token being claimed by the msg.sender
*/
function takeOwnership(uint256 _tokenId) public {
require(isApprovedFor(msg.sender, _tokenId));
clearApprovalAndTransfer(ownerOf(_tokenId), msg.sender, _tokenId);
}
/**
* @dev Mint token function
* @param _to The address that will own the minted token
* @param _tokenId uint256 ID of the token to be minted by the msg.sender
*/
function _mint(address _to) internal {
require(_to != address(0));
Deed memory deed = Deed({
mintedBy: _to,
mintedAt: uint64(now)
});
tokenId = self.deeds.push(deed) - 1;
addToken(_to, tokenId);
Transfer(0x0, _to, tokenId);
}
/**
* @dev Burns a specific token
* @param _tokenId uint256 ID of the token being burned by the msg.sender
*/
function _burn(uint256 _tokenId) internal onlyOwnerOf(_tokenId) {
if (approvedFor(_tokenId) != 0) {
clearApproval(msg.sender, _tokenId);
}
removeToken(msg.sender, _tokenId);
Transfer(msg.sender, 0x0, _tokenId);
}
/**
* @dev Tells whether the msg.sender is approved for the given token ID or not
* This function is not private so it can be extended in further implementations like the operatable ERC721
* @param _owner address of the owner to query the approval of
* @param _tokenId uint256 ID of the token to query the approval of
* @return bool whether the msg.sender is approved for the given token ID or not
*/
function isApprovedFor(address _owner, uint256 _tokenId) internal view returns (bool) {
return approvedFor(_tokenId) == _owner;
}
/**
* @dev Internal function to clear current approval and transfer the ownership of a given token ID
* @param _from address which you want to send tokens from
* @param _to address which you want to transfer the token to
* @param _tokenId uint256 ID of the token to be transferred
*/
function clearApprovalAndTransfer(address _from, address _to, uint256 _tokenId) internal {
require(_to != address(0));
require(_to != ownerOf(_tokenId));
require(ownerOf(_tokenId) == _from);
clearApproval(_from, _tokenId);
removeToken(_from, _tokenId);
addToken(_to, _tokenId);
Transfer(_from, _to, _tokenId);
}
/**
* @dev Internal function to clear current approval of a given token ID
* @param _tokenId uint256 ID of the token to be transferred
*/
function clearApproval(DeedStorage storage self, address _owner, uint256 _tokenId) private {
require(ownerOf(_tokenId) == _owner);
self.tokenApprovals[_tokenId] = 0;
Approval(_owner, 0, _tokenId);
}
/**
* @dev Internal function to add a token ID to the list of a given address
* @param _to address representing the new owner of the given token ID
* @param _tokenId uint256 ID of the token to be added to the tokens list of the given address
*/
function addToken(DeedStorage storage self, address _to, uint256 _tokenId) private {
require(self.tokenOwner[_tokenId] == address(0));
self.tokenOwner[_tokenId] = _to;
uint256 length = balanceOf(_to);
self.ownedTokens[_to].push(_tokenId);
self.ownedTokensIndex[_tokenId] = length;
}
/**
* @dev Internal function to remove a token ID from the list of a given address
* @param _from address representing the previous owner of the given token ID
* @param _tokenId uint256 ID of the token to be removed from the tokens list of the given address
*/
function removeToken(DeedStorage storage self, address _from, uint256 _tokenId) private {
require(ownerOf(_tokenId) == _from);
uint256 tokenIndex = self.ownedTokensIndex[_tokenId];
uint256 lastTokenIndex = balanceOf(_from).sub(1);
uint256 lastToken = self.ownedTokens[_from][lastTokenIndex];
self.tokenOwner[_tokenId] = 0;
self.ownedTokens[_from][tokenIndex] = lastToken;
self.ownedTokens[_from][lastTokenIndex] = 0;
// Note that this will handle single-element arrays. In that case, both tokenIndex and lastTokenIndex are going to
// be zero. Then we can make sure that we will remove _tokenId from the ownedTokens list since we are first swapping
// the lastToken to the first position, and then dropping the element placed in the last position of the list
self.ownedTokens[_from].length--;
self.ownedTokensIndex[_tokenId] = 0;
self.ownedTokensIndex[lastToken] = tokenIndex;
}
function getMinter(Deed storage self, uint256 _tokenId) public {
return self.deeds[_tokenId].mintedBy;
}
function getMintDate(Deed storage self, uint256 _tokenId) public {
return self.deeds[_tokenId].mintedAt;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment