加密猫的demo
This commit is contained in:
30
src/Online/Access.sol
Normal file
30
src/Online/Access.sol
Normal file
@@ -0,0 +1,30 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Pausable.sol";
|
||||
import "@openzeppelin/contracts/access/Ownable.sol";
|
||||
|
||||
abstract contract Access is ERC721Pausable, Ownable {
|
||||
function pause() public virtual onlyOwner {
|
||||
super._pause();
|
||||
}
|
||||
|
||||
function unpause() public virtual onlyOwner {
|
||||
super._unpause();
|
||||
}
|
||||
|
||||
/**
|
||||
* @dev 这个是为了接收ETH转账用的
|
||||
*/
|
||||
receive() external payable virtual {}
|
||||
|
||||
function withdrawBalance() external onlyOwner {
|
||||
// uint256 balance = this.balance;
|
||||
// // Subtract all the currently pregnant kittens we have, plus 1 of margin.
|
||||
// uint256 subtractFees = (pregnantKitties + 1) * autoBirthFee;
|
||||
// if (balance > subtractFees) {
|
||||
// cfoAddress.transfer(balance - subtractFees);
|
||||
// }
|
||||
}
|
||||
}
|
||||
1720
src/Online/CoolKitty.sol
Normal file
1720
src/Online/CoolKitty.sol
Normal file
File diff suppressed because it is too large
Load Diff
1973
src/Online/CryptoKittyCore.sol
Normal file
1973
src/Online/CryptoKittyCore.sol
Normal file
File diff suppressed because it is too large
Load Diff
57
src/Online/Kitty.sol
Normal file
57
src/Online/Kitty.sol
Normal file
@@ -0,0 +1,57 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "./KittyController.sol";
|
||||
|
||||
contract HelloKitty is KittyController {
|
||||
/// 初始化猫
|
||||
constructor() ERC721("CryptoKitties", "CK") {
|
||||
_createKitty(0, 0, 0, (2**256) - 1, _msgSender());
|
||||
}
|
||||
|
||||
function _createKitty(
|
||||
uint256 _matronId,
|
||||
uint256 _sireId,
|
||||
uint256 _generation,
|
||||
uint256 _genes,
|
||||
address _owner
|
||||
) internal returns (uint256) {
|
||||
require(_matronId == uint256(uint32(_matronId)));
|
||||
require(_sireId == uint256(uint32(_sireId)));
|
||||
require(_generation == uint256(uint16(_generation)));
|
||||
|
||||
// New kitty starts with the same cooldown as parent gen/2
|
||||
uint16 cooldownIndex = uint16(_generation / 2);
|
||||
if (cooldownIndex > 13) {
|
||||
cooldownIndex = 13;
|
||||
}
|
||||
|
||||
Kitty memory _kitty = Kitty({
|
||||
genes: _genes,
|
||||
birthTime: uint64(block.timestamp),
|
||||
cooldownEndBlock: 0,
|
||||
matronId: uint32(_matronId),
|
||||
sireId: uint32(_sireId),
|
||||
siringWithId: 0,
|
||||
cooldownIndex: cooldownIndex,
|
||||
generation: uint16(_generation)
|
||||
});
|
||||
kitties.push(_kitty);
|
||||
uint256 newKittenId = 1;
|
||||
// uint256 newKittenId = kitties.push(_kitty) - 1;
|
||||
// require(newKittenId == uint256(uint32(newKittenId)));
|
||||
|
||||
emit Birth(
|
||||
_owner,
|
||||
newKittenId,
|
||||
uint256(_kitty.matronId),
|
||||
uint256(_kitty.sireId),
|
||||
_kitty.genes
|
||||
);
|
||||
|
||||
super._mint(_owner, newKittenId);
|
||||
|
||||
return newKittenId;
|
||||
}
|
||||
}
|
||||
52
src/Online/KittyController.sol
Normal file
52
src/Online/KittyController.sol
Normal file
@@ -0,0 +1,52 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
pragma solidity ^0.8.0;
|
||||
|
||||
import "@openzeppelin/contracts/access/Ownable.sol";
|
||||
import "@openzeppelin/contracts/token/ERC721/ERC721.sol";
|
||||
import "@openzeppelin/contracts/token/ERC721/extensions/ERC721Pausable.sol";
|
||||
import "./Access.sol";
|
||||
|
||||
abstract contract KittyController is ERC721, Access {
|
||||
struct Kitty {
|
||||
// 猫的基因
|
||||
uint256 genes;
|
||||
// 出块时间戳
|
||||
uint64 birthTime;
|
||||
// 再次繁育冷却时间
|
||||
uint64 cooldownEndBlock;
|
||||
// 猫的母系ID
|
||||
uint32 matronId;
|
||||
// 父系ID
|
||||
uint32 sireId;
|
||||
// 对于怀孕的母猫,设置为公猫的ID,否则为零。这里的一个非零值是我们如何知道猫怀孕了。用于在出生时检索新小猫的遗传物质。
|
||||
uint32 siringWithId;
|
||||
// 每繁殖一次,递增一次,无论公母
|
||||
uint16 cooldownIndex;
|
||||
// 世代号(i.e. max(matron.generation, sire.generation) + 1)
|
||||
uint16 generation;
|
||||
}
|
||||
|
||||
Kitty[] kitties;
|
||||
|
||||
event Birth(
|
||||
address owner,
|
||||
uint256 kittyId,
|
||||
uint256 matronId,
|
||||
uint256 sireId,
|
||||
uint256 genes
|
||||
);
|
||||
|
||||
function getKitty(uint256 _id) public view returns (Kitty memory) {
|
||||
return kitties[_id];
|
||||
}
|
||||
|
||||
function _beforeTokenTransfer(
|
||||
address from,
|
||||
address to,
|
||||
uint256 firstTokenId,
|
||||
uint256 batchSize
|
||||
) internal virtual override(ERC721, ERC721Pausable) {
|
||||
super._beforeTokenTransfer(from, to, firstTokenId, batchSize);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user