-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbreeding.sol
487 lines (400 loc) · 16.7 KB
/
breeding.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
pragma solidity ^0.4.18;
contract ERC721 {
// Required methods
function totalSupply() public view returns (uint256 total);
function balanceOf(address _owner) public view returns (uint256 balance);
function ownerOf(uint256 _tokenId) external view returns (address owner);
function approve(address _to, uint256 _tokenId) external;
function transfer(address _to, uint256 _tokenId) external;
function transferFrom(address _from, address _to, uint256 _tokenId) external;
// Events
event Approval(address owner, address approved, uint256 tokenId);
}
contract Ownable {
address public owner;
function Ownable() public {
owner = msg.sender;
}
modifier onlyOwner() {
require(msg.sender == owner);
_;
}
function transferOwnership(address newOwner) public onlyOwner {
if (newOwner != address(0)) {
owner = newOwner;
}
}
}
contract GeneScience {
function random() internal view returns (uint256) ;
function getBitMask(uint8[] index) internal pure returns (bytes32);
function mixGenes(uint256 genes1, uint256 genes2) external view returns (uint256);
function getCoolDown(uint256 genes) external view returns (uint16) ;
function variation(uint32 attID, bytes32 genes) internal view returns (bytes32);
function init_attribute() external;
function init_mixrule() external;
function init_rate() external;
function init_rate_distribution() external;
function fiveDValue(uint256 genes, bool gen0, uint8 attID) external view returns (uint8);
}
contract KittyAccessControl {
event ContractUpgrade(address newContract);
// The addresses of the accounts (or contracts) that can execute actions within each roles.
address public ceoAddress;
address public cfoAddress;
address public cooAddress;
// @dev Keeps track whether the contract is paused. When that is true, most actions are blocked
bool public paused = false;
/// @dev Access modifier for CEO-only functionality
modifier onlyCEO() {
require(msg.sender == ceoAddress);
_;
}
/// @dev Access modifier for CFO-only functionality
modifier onlyCFO() {
require(msg.sender == cfoAddress);
_;
}
/// @dev Access modifier for COO-only functionality
modifier onlyCOO() {
require(msg.sender == cooAddress);
_;
}
modifier onlyCLevel() {
require(msg.sender == cooAddress || msg.sender == ceoAddress || msg.sender == cfoAddress);
_;
}
function KittyAccessControl() public {
ceoAddress = msg.sender;
}
/// @dev Assigns a new address to act as the CEO. Only available to the current CEO.
/// @param _newCEO The address of the new CEO
function setCEO(address _newCEO) external onlyCEO {
require(_newCEO != address(0));
ceoAddress = _newCEO;
}
/// @dev Assigns a new address to act as the CFO. Only available to the current CEO.
/// @param _newCFO The address of the new CFO
function setCFO(address _newCFO) external onlyCEO {
require(_newCFO != address(0));
cfoAddress = _newCFO;
}
/// @dev Assigns a new address to act as the COO. Only available to the current CEO.
/// @param _newCOO The address of the new COO
function setCOO(address _newCOO) external onlyCEO {
require(_newCOO != address(0));
cooAddress = _newCOO;
}
/*** Pausable functionality adapted from OpenZeppelin ***/
/// @dev Modifier to allow actions only when the contract IS NOT paused
modifier whenNotPaused() {
require(!paused);
_;
}
/// @dev Modifier to allow actions only when the contract IS paused
modifier whenPaused {
require(paused);
_;
}
/// @dev Called by any "C-level" role to pause the contract. Used only when
/// a bug or exploit is detected and we need to limit damage.
function pause() external onlyCLevel whenNotPaused {
paused = true;
}
/// @dev Unpauses the smart contract. Can only be called by the CEO, since
/// one reason we may pause the contract is when CFO or COO accounts are
/// compromised.
/// @notice This is public rather than external so it can be called by
/// derived contracts.
function unpause() public onlyCEO whenPaused {
// can't unpause if contract was upgraded
paused = false;
}
}
contract KittyBase is KittyAccessControl {
/*** EVENTS ***/
event Birth(address owner, uint256 kittyId, uint256 matronId, uint256 sireId, uint256 genes);
event Transfer(address from, address to, uint256 tokenId);
struct Kitty {
uint256 genes;
uint64 birthTime;
uint64 cooldownEndBlock;
uint32 matronId;
uint32 sireId;
uint32 siringWithId;
uint16 cooldownIndex;
uint16 generation;
}
function _transfer(address _from, address _to, uint256 _tokenId) internal;
function _createKitty(uint256 _matronId,uint256 _sireId,uint256 _generation,uint256 _genes,address _owner) internal returns (uint);
function setSecondsPerBlock(uint256 secs) external;
}
contract KittyOwnership is KittyBase, ERC721 {
function _owns(address _claimant, uint256 _tokenId) public view returns (bool);
function _approvedFor(address _claimant, uint256 _tokenId) public view returns (bool);
function _approve(uint256 _tokenId, address _approved) internal;
function balanceOf(address _owner) public view returns (uint256 count);
function transfer(address _to,uint256 _tokenId) external;
function approve(address _to,uint256 _tokenId) external;
function transferFrom(address _from,address _to,uint256 _tokenId) external;
function totalSupply() public view returns (uint);
function ownerOf(uint256 _tokenId) external view returns (address owner);
function tokensOfOwner(address _owner) external view returns(uint256[] ownerTokens);
function _memcpy(uint _dest, uint _src, uint _len) private pure ;
function _toString(bytes32[4] _rawBytes, uint256 _stringLength) private pure returns (string);
function getSireAllowedTo(uint256 _tokenId) external view returns (address allowed);
function getKitty(uint256 _tokenId) external view returns (
uint256 genes,
uint256 birthTime,
uint256 cooldownEndBlock,
uint256 matronId,
uint256 sireId,
uint256 siringWithId,
uint256 cooldownIndex,
uint256 generation,
uint256 breedTimes
);
function createKitty(
uint256 _matronId,
uint256 _sireId,
uint256 _generation,
uint256 _genes,
address _owner,
bool _gen0
) external returns (uint);
function setSaleAuctionAddress(address _address) external;
function createGen0Kitty(uint256 _genes, address _owner) external returns (uint);
function setSireAllowedTo(uint256 _tokenId, address _address) external;
function setSiringWithId(uint256 _tokenId, uint32 _siringWithId) external;
function isReadyToBreed(uint256 _tokenId) external view returns (bool);
function setCooldownEndBlock(uint256 _tokenId, uint64 _blocknum) external;
function setBreedTimes(uint256 _tokenId, uint16 _breedTimes) external;
function deleteSireAllowedTo(uint256 _tokenId) external;
function deleteSiringWithId(uint256 _tokenId) external;
function testGene() external view returns (uint256);
function approveToSaleAuction(uint256 _tokenId) external;
function setSiringAuctionAddress(address _address) external;
function approveToSiringAuction(uint256 _tokenId) external;
}
contract KittyBreeding is KittyAccessControl {
event Pregnant(address owner, uint256 matronId, uint256 sireId, uint256 cooldownEndBlock);
/*** DATA TYPES ***/
struct Kitty {
// The Kitty's genetic code is packed into these 256-bits, the format is
// sooper-sekret! A cat's genes never change.
uint256 genes;
uint64 birthTime;
uint64 cooldownEndBlock;
uint32 matronId;
uint32 sireId;
uint32 siringWithId;
uint16 cooldownIndex;
uint16 generation;
uint16 breedTimes;
}
/*** CONSTANTS ***/
uint32[6] public cooldowns = [
uint32(10 minutes),
uint32(1 hours),
uint32(4 hours),
uint32(16 hours),
uint32(2 days),
uint32(7 days)
];
uint256 public secondsPerBlock = 15;
uint256 public pregnantKitties;
KittyOwnership public kittyOwnership;
GeneScience public geneScience;
address public siringaction;
function KittyBreeding() public {
ceoAddress = msg.sender;
}
function setGeneScienceAddress(address _address) external onlyCEO {
GeneScience candidateContract = GeneScience(_address);
geneScience = candidateContract;
}
function setKittyOwnership(address _address) external onlyCEO {
KittyOwnership candidateContract = KittyOwnership(_address);
kittyOwnership = candidateContract;
}
function setSiringAction(address _address) external onlyCEO {
siringaction = _address;
}
function _isSiringPermitted(uint256 _sireId, uint256 _matronId) internal view returns (bool) {
address matronOwner = kittyOwnership.ownerOf(_matronId);
address sireOwner = kittyOwnership.ownerOf(_sireId);
// Siring is okay if they have same owner, or if the matron's owner was given
// permission to breed with this sire.
return (matronOwner == sireOwner || kittyOwnership.getSireAllowedTo(_sireId) == matronOwner);
}
function _triggerCooldown(uint256 _tokenId) internal {
var (,,,,,cooldownIndex,,,breedTimes) = kittyOwnership.getKitty(_tokenId);
// Compute an estimation of the cooldown time in blocks (based on current cooldownIndex).
uint64 blocknum = uint64(cooldowns[cooldownIndex] / secondsPerBlock);
blocknum = blocknum + uint64(breedTimes) * (blocknum / 5);
kittyOwnership.setCooldownEndBlock(_tokenId, uint64(blocknum + block.number));
kittyOwnership.setBreedTimes(_tokenId, uint16(breedTimes) + 1);
}
function approveSiring(address _addr, uint256 _sireId)
external
{
require(kittyOwnership._owns(msg.sender, _sireId));
kittyOwnership.setSireAllowedTo(_sireId, _addr);
}
/// @dev Checks to see if a given Kitty is pregnant and (if so) if the gestation
/// period has passed.
function _isReadyToGiveBirth(uint32 siringWithId, uint64 cooldownEndBlock) private view returns (bool) {
return (siringWithId != 0) && (cooldownEndBlock <= uint64(block.number));
}
/// @dev Checks whether a kitty is currently pregnant.
/// @param _kittyId reference the id of the kitten, any user can inquire about it
function isPregnant(uint256 _kittyId)
public
view
returns (bool)
{
require(_kittyId > 0);
// A kitty is pregnant if and only if this field is set
var (,,,,,siringWithId,,,) = kittyOwnership.getKitty(_kittyId);
return siringWithId != 0;
}
function _isValidMatingPair(
uint256 _matronId,
uint256 _sireId
)
private
view
returns(bool)
{
// A Kitty can't breed with itself!
if (_matronId == _sireId) {
return false;
}
var (,,,matron_matronId,matron_sireId,,,,) = kittyOwnership.getKitty(_matronId);
// Kitties can't breed with their parents.
if (matron_matronId == _sireId || matron_sireId == _sireId) {
return false;
}
var (,,,sire_matronId,sire_sireId,,,,) = kittyOwnership.getKitty(_sireId);
if (sire_matronId == _matronId || sire_sireId == _matronId) {
return false;
}
// We can short circuit the sibling check (below) if either cat is
// gen zero (has a matron ID of zero).
if (sire_matronId == 0 || matron_matronId == 0) {
return true;
}
// Kitties can't breed with full or half siblings.
if (sire_matronId == matron_matronId || sire_matronId == matron_sireId) {
return false;
}
if (sire_sireId == matron_matronId || sire_sireId == matron_sireId) {
return false;
}
// Everything seems cool! Let's get DTF.
return true;
}
function _canBreedWithViaAuction(uint256 _matronId, uint256 _sireId)
external view returns (bool)
{
return _isValidMatingPair(_matronId, _sireId);
}
function canBreedWith(uint256 _matronId, uint256 _sireId)
external
view
returns(bool)
{
require(_matronId > 0);
require(_sireId > 0);
return _isValidMatingPair(_matronId, _sireId) &&
_isSiringPermitted(_sireId, _matronId);
}
/// @dev Internal utility function to initiate breeding, assumes that all breeding
/// requirements have been checked.
function _breedWith(uint256 _matronId, uint256 _sireId) internal {
// Mark the matron as pregnant, keeping track of who the sire is.
kittyOwnership.setSiringWithId(_matronId, uint32(_sireId));
// Trigger the cooldown for both parents.
_triggerCooldown(_sireId);
_triggerCooldown(_matronId);
// Clear siring permission for both parents. This may not be strictly necessary
// but it's likely to avoid confusion!
kittyOwnership.deleteSireAllowedTo(_matronId);
kittyOwnership.deleteSireAllowedTo(_sireId);
// Every time a kitty gets pregnant, counter is incremented.
pregnantKitties++;
// Emit the pregnancy event.
var (,,cooldownEndBlock,,,,,,) = kittyOwnership.getKitty(_matronId);
Pregnant(kittyOwnership.ownerOf(_matronId), _matronId, _sireId, cooldownEndBlock);
//giveBirth(_matronId);
}
function breedWithAuto(uint256 _matronId, uint256 _sireId)
external
{
require(kittyOwnership._owns(msg.sender, _matronId));
require(_isSiringPermitted(_sireId, _matronId));
breedWith(_matronId, _sireId);
}
function breedWith(uint256 _matronId, uint256 _sireId) internal
{
// Make sure matron isn't pregnant, or in the middle of a siring cooldown
require(kittyOwnership.isReadyToBreed(_matronId));
// Make sure sire isn't pregnant, or in the middle of a siring cooldown
require(kittyOwnership.isReadyToBreed(_sireId));
// Test that these cats are a valid mating pair.
require(_isValidMatingPair(
_matronId,
_sireId
));
// All checks passed, kitty gets pregnant!
_breedWith(_matronId, _sireId);
}
function breedSelfKitty(uint256 _matronId, uint256 _sireId) external
{
require(kittyOwnership._owns(msg.sender, _matronId));
require(kittyOwnership._owns(msg.sender, _sireId));
breedWith(_matronId, _sireId);
}
function giveBirthByAuction(uint256 _matronId, uint256 _sireId, address _owner)
external
returns(uint256)
{
require(msg.sender == siringaction);
return giveBirth(_matronId, _sireId, _owner);
}
function giveBirthBySelf(uint256 _matronId, uint256 _sireId)
external
returns(uint256)
{
require(kittyOwnership._owns(msg.sender, _matronId));
require(kittyOwnership._owns(msg.sender, _sireId));
var (,,,,,sireIdWith,,,) = kittyOwnership.getKitty(_matronId);
require(sireIdWith == _sireId);
return giveBirth(_matronId, _sireId, msg.sender);
}
function giveBirth(uint256 _matronId, uint256 _sireId, address _owner)
internal
returns(uint256)
{
var (matron_genes,matron_birthTime,,,,,,matron_generation,) = kittyOwnership.getKitty(_matronId);
// Check that the matron is a valid cat.
require(matron_birthTime != 0);
var (sireId_genes,,,,,,,sireId_generation,) = kittyOwnership.getKitty(_sireId);
// Determine the higher generation number of the two parents
uint16 parentGen = uint16(matron_generation);
if (sireId_generation > matron_generation) {
parentGen = uint16(sireId_generation);
}
// Call the sooper-sekret gene mixing operation.
uint256 childGenes = geneScience.mixGenes(matron_genes, sireId_genes);
// Make the new kitten!
uint256 kittenId = kittyOwnership.createKitty(_matronId, _sireId, parentGen + 1, childGenes, _owner, false);
// Clear the reference to sire from the matron (REQUIRED! Having siringWithId
// set is what marks a matron as being pregnant.)
kittyOwnership.deleteSiringWithId(_matronId);
// Every time a kitty gives birth counter is decremented.
pregnantKitties--;
// return the new kitten's ID
return kittenId;
}
}