CryptoKitties: Smart Contract Breakdown | by Nazar Ilamanov | Mar, 2022

Explaining its functionality by grouping lines of code

  • What is CryptoKitties?
  • Code structure (core, breeding, auction)
  • Timeline of CryptoKitties
  • My opinion on the code
  • Blockchain-based, play-to-earn game
  • People collect and trade kitties
  • Can breed two kitties to get a new kitty
  • Earn real ETH by selling your kitties or renting them out for breeding
  • Breeding works based on “genes”. Child kitty gets a mix of parents’ genes
  • Kitties do not have a gender
  • CK makes money by charging a cut in its marketplace (auctioneer fee) and minting new kitties
  • To enter the game you need to purchase some kitties

Core contract

  • KittyAccessControl: creates 3 roles: CEO, CFO, COO, and restricts access from some functions to these roles. CEO can reassign roles, change points to sibling contracts. CFO can withdraw. COO can mint new kitties.
  • KittyBase: data structure of kitties; stores all kitties and ownership info; transferring of ownership.
  • KittyOwnership: implementation of ERC-721 interface. I explained the implementation of ERC-721 in my BAYC smart contract breakdown. Check it out if you’re interested.
  • KittyBreeding: will be explained in the “Breeding” section.
  • KittyAuction: will be explained in the “Auction” section.
  • KittyMinting: only 50K kitties can be minted — 5K are promo kitties, the rest are regular gen0 kitties. (Difference: promo can be transferred to a specific address at the mint time, regular gen0 can only be auctioned). Any genes can be specified during minting.
  • KittyCore: ties everything together, adds payments/withdrawals, and handles upgradeability – covered later in the article.
  • First, there are a bunch of helper functions like isReadyToBreed, isSiringPermitted, isValidMatingPairetc.
  • Then there are 2 functions that actually do the breeding. breedWith starts the breeding process and giveBirth ends it. giveBirth call will succeed only after the gestation period is complete.

Midwives and auto-birth

Super-secret genetic combination algorithm

  • ClockAuctionBase: keeps track of existing auctions and a function to bid
  • ClockAuction: Just a wrapper on top of ClockAuctionBase
  • SiringClockAuction and SaleClockAuction: auctions for renting out your kittie for breeding and for selling your kittie, respectively. These need to be separate because the actions taken after a successful bid are quite different for each case.
  • SaleClockAuction does not add much except tracking the last 5 prices of auctions used to set the optimal auction starting price for the newly minted kitties.
  • SiringClockAuction is yet another wrapper on ClockAuction contract, except it transfers the rented kittie back to the owner instead of the bidder (bidder keeps the offspring).
blue: ETH balance (scale is on the left). black: USD balance (scale is on the right)
  • first and foremost, the art does not live on-chain. The genes are kept on-chain but ideally, the images of kitties would also be generated on-chain (like Art Blocks).
  • CK did not even put the links to the artwork on-chain. They could have used the getMetadata function to return the links to immutable images. Instead, they just return “Hello World” 🤷‍♂️. The mapping from tokenIds to images happens on the frontend. So if CK were to shut down their website, you would only be left with a meaningless 256-bit number.
  • Upgradeability is not ideal. The core contract can be updated via setting a new address in the KittyCore contract. In that case, an event will be emitted and it’s up to the clients to listen to it and switch to the new contract. The old one will be forever paused.
  • Good separation of concerns: Breeding and auction are separate from the core contract to minimize bugs. You can plug in updated versions of the sibling contracts without disrupting the core.
  • Clean code and a solid understanding of Solidity. Lots of useful comments throughout the contract. Guarding against reentrancy attack and maximizing gas efficiency:

Leave a Comment