-
Notifications
You must be signed in to change notification settings - Fork 0
/
Creature_proto.sol
130 lines (107 loc) · 4.32 KB
/
Creature_proto.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
pragma solidity ^0.4.24;
contract CreatureCont {
enum Types { fire, water, air, earth }
struct Creature {
string nickname;
Types attribute;
uint256 power;
bool breeding;
bool tradeable;
uint256 TokenId;
}
uint256 TokenId = 0;
mapping(address => Creature[]) creatures;
mapping(uint256 => address) owners;
mapping(uint256 => bool) private tokenExists;
function createCreature(string memory nick, Types t, uint256 p, bool b, bool trade) public{
creatures[msg.sender].push(Creature(nick, t, p, b, trade, TokenId));
owners[TokenId] = msg.sender;
tokenExists[TokenId] = true;
TokenId++;
}
function random() private view returns (uint256) {
return uint256(keccak256(block.timestamp, block.difficulty));
}
function createRandomCreature(string memory nick, bool trade) public{
bool b = true;
if (random() % 2 == 0) {
b = false;
}
createCreature(nick, Types(random()%4), random()%10000, b, trade);
}
// ERC721 compatible functions
function name() public view returns (string memory name) {
return "CreatureToken";
}
function symbol() public view returns (string memory symbol) {
return "CRT";
}
uint256 private total_supply = 1000;
function totalSupply() public view returns (uint256 supply) {
return total_supply;
}
function balanceOf(address _owner) public view returns (uint balance) {
return creatures[_owner].length;
}
// Functions that define ownership
function ownerOf(uint256 _tokenId) constant returns (address owner) {
require(tokenExists[_tokenId]);
return owners[_tokenId];
}
mapping(address => mapping (address => uint256)) allowed;
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 find_index(address owner, uint256 _tokenId) view returns(uint256 index) {
require(tokenExists[_tokenId]);
require(owners[_tokenId] == owner);
for (uint256 i = 0; i < balanceOf(owner); ++i) {
if (creatures[owner][i].TokenId == _tokenId) {
return i;
}
}
}
function remove(address owner, uint256 index) view {
creatures[owner][index] = creatures[owner][balanceOf(owner) - 1];
delete creatures[owner][balanceOf(owner) - 1];
}
function takeOwnership(uint256 _tokenId) {
require(tokenExists[_tokenId]);
address oldOwner = ownerOf(_tokenId);
address newOwner = msg.sender;
require(newOwner != oldOwner);
require(allowed[oldOwner][newOwner] == _tokenId);
owners[_tokenId] = newOwner;
uint256 index = find_index(oldOwner, _tokenId);
Creature c = creatures[oldOwner][index];
creatures[newOwner].push(c);
remove(oldOwner, index);
Transfer(oldOwner, newOwner, _tokenId);
}
function removeFromTokenList(address owner, uint256 _tokenId) private {
uint256 index = find_index(owner, _tokenId);
remove(owner, index);
}
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));
uint256 index = find_index(currentOwner, _tokenId);
Creature c = creatures[currentOwner][index];
creatures[newOwner].push(c);
remove(currentOwner, index);
Transfer(currentOwner, newOwner, _tokenId);
}
function tokenOfOwnerByIndex(address _owner, uint256 _index) constant returns (uint256 _tokenId) {
return creatures[_owner][_index].TokenId;
}
// Events
event Transfer(address indexed _from, address indexed _to, uint256 _tokenId);
event Approval(address indexed _owner, address indexed _approved, uint256 _tokenId);
}