-
-
Save sherlock-shi-x/a5d2483023caf4429f642c93e0b1f8ae to your computer and use it in GitHub Desktop.
sponsorToken.sol
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.20; | |
library SafeMath { | |
/** | |
* @dev Multiplies two numbers, throws on overflow. | |
*/ | |
function mul(uint256 a, uint256 b) internal pure returns (uint256) { | |
if (a == 0) { | |
return 0; | |
} | |
uint256 c = a * b; | |
assert(c / a == b); | |
return c; | |
} | |
/** | |
* @dev Integer division of two numbers, truncating the quotient. | |
*/ | |
function div(uint256 a, uint256 b) internal pure returns (uint256) { | |
// assert(b > 0); // Solidity automatically throws when dividing by 0 | |
uint256 c = a / b; | |
// assert(a == b * c + a % b); // There is no case in which this doesn't hold | |
return c; | |
} | |
/** | |
* @dev Substracts two numbers, throws on overflow (i.e. if subtrahend is greater than minuend). | |
*/ | |
function sub(uint256 a, uint256 b) internal pure returns (uint256) { | |
assert(b <= a); | |
return a - b; | |
} | |
/** | |
* @dev Adds two numbers, throws on overflow. | |
*/ | |
function add(uint256 a, uint256 b) internal pure returns (uint256) { | |
uint256 c = a + b; | |
assert(c >= a); | |
return c; | |
} | |
} | |
/// @title Interface for contracts conforming to ERC-721: Non-Fungible Tokens | |
/// @author Dieter Shirley <[email protected]> (https://github.com/dete) | |
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 SponsorToken is ERC721{ | |
using SafeMath for uint256; | |
event Bought (uint256 indexed _itemId, address indexed _owner, uint256 _price); | |
event Sold (uint256 indexed _itemId, 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; | |
uint256 counter; | |
mapping (uint256 => address) private ownerOfItem; | |
mapping (uint256 => uint256) private priceOfItem; | |
mapping (uint256 => address) private approvedOfItem; | |
mapping (uint256 => address) private creatorOfItem; | |
mapping (uint256 => uint256) private parentOfItem; | |
mapping (uint256 => uint256) private balanceOfToken; | |
mapping (uint256 => uint256) private freeOfItem; | |
function SponsorToken () public { | |
owner = msg.sender; | |
creatorOfItem[counter] = ownerOfItem[counter] = msg.sender; | |
priceOfItem[counter] = 1 ether; | |
parentOfItem[counter] = 0; | |
freeOfItem[counter] = now + 120; | |
counter += 1; | |
} | |
/* Modifiers */ | |
modifier onlyOwner(uint256 _tokenId) { | |
require(ownerOfItem[_tokenId] == msg.sender); | |
_; | |
} | |
modifier onlyCreator(uint256 _tokenId) { | |
require(creatorOfItem[_tokenId] == msg.sender); | |
_; | |
} | |
/* Owner */ | |
function setCreator (address _creator, uint _tokenId) onlyCreator(_tokenId) public { | |
creatorOfItem[_tokenId] = _creator; | |
} | |
/* Withdraw */ | |
function withdrawAllFromToken (uint256 _tokenId) onlyCreator(_tokenId) public { | |
uint256 t = balanceOfToken[_tokenId]; | |
uint256 r = t / 20; | |
balanceOfToken[_tokenId] = 0; | |
balanceOfToken[parentOfItem[_tokenId]] += r; | |
msg.sender.transfer(t - r); | |
} | |
function withdrawAmountFromToken (uint256 _tokenId, uint256 t) onlyCreator(_tokenId) public { | |
if (t > balanceOfToken[_tokenId]) t = balanceOfToken[_tokenId]; | |
uint256 r = t / 20; | |
balanceOfToken[_tokenId] = 0; | |
balanceOfToken[parentOfItem[_tokenId]] += r; | |
msg.sender.transfer(t - r); | |
} | |
function withdrawAll() public { | |
require(msg.sender == owner); | |
owner.transfer(this.balance); | |
} | |
/* Buying */ | |
function calculateNextPrice (uint256 _price) public view returns (uint256 _nextPrice) { | |
return _price.mul(117).div(98); | |
} | |
function calculateDevCut (uint256 _price) public view returns (uint256 _devCut) { | |
return _price.div(20); // 5% | |
} | |
function buy (uint256 _itemId) payable public { | |
require(priceOf(_itemId) > 0); | |
require(ownerOf(_itemId) != address(0)); | |
require(msg.value >= priceOf(_itemId)); | |
require(ownerOf(_itemId) != msg.sender); | |
require(!isContract(msg.sender)); | |
require(msg.sender != address(0)); | |
address oldOwner = ownerOf(_itemId); | |
address newOwner = msg.sender; | |
uint256 price = priceOf(_itemId); | |
uint256 excess = msg.value.sub(price); | |
_transfer(oldOwner, newOwner, _itemId); | |
priceOfItem[_itemId] = nextPriceOf(_itemId); | |
Bought(_itemId, newOwner, price); | |
Sold(_itemId, oldOwner, price); | |
// Devevloper's cut which is left in contract and accesed by | |
// `withdrawAll` and `withdrawAmountTo` methods. | |
uint256 devCut = calculateDevCut(price); | |
// Transfer payment to old owner minus the developer's cut. | |
oldOwner.transfer(price.sub(devCut)); | |
uint256 shareHolderCut = devCut.div(20); | |
ownerOfItem[parentOfItem[_itemId]].transfer(shareHolderCut); | |
balanceOfToken[_itemId] += devCut.sub(shareHolderCut); | |
if (excess > 0) { | |
newOwner.transfer(excess); | |
} | |
} | |
/* ERC721 */ | |
function name() public view returns (string name) { | |
return "sponsortoken.io"; | |
} | |
function symbol() public view returns (string symbol) { | |
return "SST"; | |
} | |
function totalSupply() public view returns (uint256 _totalSupply) { | |
return counter; | |
} | |
function balanceOf (address _owner) public view returns (uint256 _balance) { | |
uint256 counter = 0; | |
for (uint256 i = 0; i < counter; i++) { | |
if (ownerOf(i) == _owner) { | |
counter++; | |
} | |
} | |
return counter; | |
} | |
function ownerOf (uint256 _itemId) public view returns (address _owner) { | |
return ownerOfItem[_itemId]; | |
} | |
function creatorOf (uint256 _itemId) public view returns (address _creator) { | |
return creatorOfItem[_itemId]; | |
} | |
function parentOf (uint256 _tokenId) public view returns (uint256 _parent) { | |
return parentOfItem[_tokenId]; | |
} | |
function freeOf (uint256 _tokenId) public view returns (uint256 _free) { | |
return freeOfItem[_tokenId]; | |
} | |
function balanceFromToken (uint256 _tokenId) public view returns (uint256 _balance) { | |
return balanceOfToken[_tokenId]; | |
} | |
function tokensOf (address _owner) public view returns (uint256[] _tokenIds) { | |
uint256[] memory items = new uint256[](balanceOf(_owner)); | |
uint256 itemCounter = 0; | |
for (uint256 i = 0; i < counter; i++) { | |
if (ownerOf(i) == _owner) { | |
items[itemCounter] = i; | |
itemCounter += 1; | |
} | |
} | |
return items; | |
} | |
function tokenExists (uint256 _itemId) public view returns (bool _exists) { | |
return priceOf(_itemId) > 0; | |
} | |
function approvedFor(uint256 _itemId) public view returns (address _approved) { | |
return approvedOfItem[_itemId]; | |
} | |
function approve(address _to, uint256 _itemId) public { | |
require(msg.sender != _to); | |
require(tokenExists(_itemId)); | |
require(ownerOf(_itemId) == msg.sender); | |
if (_to == 0) { | |
if (approvedOfItem[_itemId] != 0) { | |
delete approvedOfItem[_itemId]; | |
Approval(msg.sender, 0, _itemId); | |
} | |
} else { | |
approvedOfItem[_itemId] = _to; | |
Approval(msg.sender, _to, _itemId); | |
} | |
} | |
/* Transferring a country to another owner will entitle the new owner the profits from `buy` */ | |
function transfer(address _to, uint256 _itemId) public { | |
require(msg.sender == ownerOf(_itemId)); | |
_transfer(msg.sender, _to, _itemId); | |
} | |
function transferFrom(address _from, address _to, uint256 _itemId) public { | |
require(approvedFor(_itemId) == msg.sender); | |
_transfer(_from, _to, _itemId); | |
} | |
function _transfer(address _from, address _to, uint256 _itemId) internal { | |
require(tokenExists(_itemId)); | |
require(ownerOf(_itemId) == _from); | |
require(_to != address(0)); | |
require(_to != address(this)); | |
ownerOfItem[_itemId] = _to; | |
approvedOfItem[_itemId] = 0; | |
Transfer(_from, _to, _itemId); | |
} | |
/* Read */ | |
function priceOf (uint256 _itemId) public view returns (uint256 _price) { | |
return priceOfItem[_itemId]; | |
} | |
function nextPriceOf (uint256 _itemId) public view returns (uint256 _nextPrice) { | |
return calculateNextPrice(priceOf(_itemId)); | |
} | |
function allOf (uint256 _itemId) external view returns (address _owner, address _creator, uint256 _price, uint256 _nextPrice) { | |
return (ownerOfItem[_itemId], creatorOfItem[_itemId], priceOfItem[_itemId], nextPriceOf(_itemId)); | |
} | |
/* Util */ | |
function isContract(address addr) internal view returns (bool) { | |
uint size; | |
assembly { size := extcodesize(addr) } // solium-disable-line | |
return size > 0; | |
} | |
function changePrice(uint256 _itemId, uint256 _price) onlyOwner(_itemId) public { | |
require(now >= freeOfItem[_itemId]); | |
priceOfItem[_itemId] = _price; | |
} | |
function issueToken(uint256 _price, uint256 _frozen, uint256 _parent) public { | |
require(_parent <= counter); | |
creatorOfItem[counter] = ownerOfItem[counter] = msg.sender; | |
priceOfItem[counter] = _price; | |
parentOfItem[counter] = _parent; | |
freeOfItem[counter] = now + _frozen; | |
counter += 1; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment