Skip to content

Instantly share code, notes, and snippets.

@hskang9
Created March 3, 2019 20:19
Show Gist options
  • Save hskang9/7c2c5fe6cbdcd6303004117346e0c0bf to your computer and use it in GitHub Desktop.
Save hskang9/7c2c5fe6cbdcd6303004117346e0c0bf to your computer and use it in GitHub Desktop.
ERC721Example.sol
pragma solidity ^0.4.23;
import '../node_modules/openzeppelin-solidity/contracts/token/ERC721/ERC721.sol';
contract StarNotary is ERC721 {
struct Star {
string name;
uint256 id;
string story;
string dec;
string mag;
string cent;
}
mapping(uint256 => Star) public tokenIdToStarInfo;
mapping(uint256 => uint256) public starsForSale;
mapping(bytes32 => uint256) public uniqueness;
event starClaimed(string name, string story, string ra, string dec, string mag);
function checkIfStarExist(bytes32 _coordHash) public returns(bool) {
return uniqueness[_coordHash] != 0;
}
function tokenIdToStarInfo(uint256 _tokenId) view public returns (string name, string story, string cent, string dec, string mag) {
Star memory info = tokenIdToStarInfo[_tokenId];
return (info.name, info.story, info.cent, info.dec, info.mag);
}
function createStar(string _name, string _story, string _cent, string _dec, string _mag, uint256 _tokenId) public {
require(_tokenId != 0);
bytes32 coordHash = keccak256(_dec, _mag, _cent);
require(checkIfStarExist(coordHash) != true, "Star already registered");
Star memory newStar = Star(_name, _tokenId, _story, _dec, _mag, _cent);
tokenIdToStarInfo[_tokenId] = newStar;
uniqueness[coordHash] = _tokenId;
_mint(msg.sender, _tokenId);
emit staarClaimed(_name, _story, _cent, _dec, _mag);
}
function putStarUpForSale(uint256 _tokenId, uint256 _price) public {
require(this.ownerOf(_tokenId) == msg.sender, "You are not the owner of this star");
starsForSale[_tokenId] = _price;
}
function buyStar(uint256 _tokenId) public payable {
require(starsForSale[_tokenId] > 0);
uint256 starCost = starsForSale[_tokenId];
address starOwner = this.ownerOf(_tokenId);
require(msg.value >= starCost);
_removeTokenFrom(starOwner, _tokenId);
_addTokenTo(msg.sender, _tokenId);
starOwner.transfer(starCost);
if(msg.value > starCost) {
msg.sender.transfer(msg.value - starCost);
}
}
function mint(uint256 tokenId) public {
super._mint(msg.sender, tokenId);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment