Skip to content

Instantly share code, notes, and snippets.

@embedovator
Created July 4, 2019 15:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save embedovator/201703ca3af273564ae732139b1aff5a to your computer and use it in GitHub Desktop.
Save embedovator/201703ca3af273564ae732139b1aff5a to your computer and use it in GitHub Desktop.
Created using remix-ide: Realtime Ethereum Contract Compiler and Runtime. Load this file by pasting this gists URL or ID at https://remix.ethereum.org/#version=soljson-v0.5.1+commit.c8a2cb62.js&optimize=false&gist=
// We will be using Solidity version 0.5.3
pragma solidity 0.5.1; // WTF, got "Unknown key 'details' with 0.5.3 after some time!?"
// Importing OpenZeppelin's ERC-721 Implementation
import "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/token/ERC721/ERC721Full.sol";
// Importing OpenZeppelin's SafeMath Implementation
import "https://github.com/OpenZeppelin/openzeppelin-solidity/contracts/math/SafeMath.sol";
contract ExplodingCryptoKittiesToken is ERC721Full {
using SafeMath for uint256;
// This struct will be used to represent one ExplodingCryptoKitty
struct ExplodingCryptoKitty {
address prevKittehOwner; // a previous owner
// uint256 selfdestruct_blocks_remaining; // Bastet has plans to automate herself out of a job
// uint8 level_of_agony;
// string last_words;
// uint8 num_lives;
}
// Bastet is the Oracle. Bastet...leader of exploding kittehs.
address bastet;
// List of existing ExplodingCryptoKitties
ExplodingCryptoKitty[] public ExplodingCryptoKitties;
// Initializing an ERC-721 Token named 'ExplodingCryptoKitties' with a symbol 'XCK'
constructor() ERC721Full("ExplodingCryptoKitties", "XCK") public {
bastet = msg.sender;
// Bastet likes kittehs
createExplodingKitty(bastet);
createExplodingKitty(bastet);
}
function createExplodingKitty(address newOwner) internal returns(uint)
{
ExplodingCryptoKitty memory newExplodingCryptoKitty = ExplodingCryptoKitty({
prevKittehOwner: newOwner
});
uint xckId = ExplodingCryptoKitties.push(newExplodingCryptoKitty).sub(1);
_mint(newOwner, xckId);
return xckId;
}
/// Bastet likes to check in occasionally to see WHO HAS DEH KITTEHS. No burning today.
function bastetChecksOwners() public {
require(bastet == msg.sender);
uint numKittehs = ExplodingCryptoKitties.length;
uint kitteh = 0;
while(kitteh < numKittehs)
{
if(ExplodingCryptoKitties[kitteh].prevKittehOwner != address(0))
{
ExplodingCryptoKitties[kitteh].prevKittehOwner = ownerOf(kitteh);
}
kitteh = kitteh + 1;
}
}
/// Attempts to transfer a kitteh you own, fails if no kittehs to transfer!
function txKitteh(address to_whom) public {
uint numKittehs = ExplodingCryptoKitties.length;
uint kitteh = 0;
while(kitteh < numKittehs)
{
// if kitteh still alive
if(ExplodingCryptoKitties[kitteh].prevKittehOwner != address(0))
{
// if you own it
if(ownerOf(kitteh) == msg.sender)
{
safeTransferFrom(msg.sender, to_whom, kitteh);
return;
}
}
kitteh = kitteh + 1;
}
require(false, "You have no kittehs!");
}
/// Whenever Bastet the Oracle determines it's time for Judgement Day: explode the kitteh if it's owner is the same (i.e. hasn't transferred the token recently).
/// BONUS: Mint 10x the Kittehs to previous owner of token if they did transfer it in time!
function bastetOracle() public {
require(bastet == msg.sender);
uint numKittehs = ExplodingCryptoKitties.length;
uint kitteh = 0;
while(kitteh < numKittehs)
{
address prevKittehOwner = ExplodingCryptoKitties[kitteh].prevKittehOwner;
if(prevKittehOwner != address(0))
{
address kittehOwner = ownerOf(kitteh);
// if previous and current owner are the same since Bastet last checked...BURN IT
if(prevKittehOwner == kittehOwner)
{
// Oh noes! Kitteh explode!
_burn(kitteh);
// No more prevKittehOwner...
ExplodingCryptoKitties[kitteh].prevKittehOwner = address(0);
}
else
{
// // else give 10X DAH KITTEHS to previous owner as reward
if(prevKittehOwner != address(0))
{
uint i;
while(i < 10)
{
createExplodingKitty(prevKittehOwner);
i = i + 1;
}
}
}
}
kitteh = kitteh + 1;
}
// Bastet likes kittehs...give one to the Oracle herself.
createExplodingKitty(bastet);
// Bastet...always...watching.
bastetChecksOwners();
}
// Fallback function
function() external payable {
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment