How does cryptokitties.co work?
Let’s look under the hood of the Ethereum based cryptocurrency cat craze and see how it works!
FYI: This post is aimed at those without much knowledge of Ethereum or Genetic Algorithms. I will be looking into the actual smart contract code in a later blog post though, so stick around! Here’s my website.
Cryptokitties is a blockchain game. Players collect and breed CryptoKitties, which are all unique, thanks to a digital genome that defines how they look.
The whole thing is powered by the Ethereum blockchain, a cryptocurrency kind of like Bitcoin, except transactions may contain executable code. This means that you can create a Distributed Application (DApp) that makes use of the decentralised structure of a cryptocurrency, and the usefulness of a computer application. You can use complex logic within a transaction.
So what? Well, if you don’t quite see the potential of this combination straight off the bat, this post wont convince you. Think: completely transparent and autonomous transactions with possibilities far beyond simply sending money from one person to another. I’ll leave it to the commenters to provide further insightful examples.
Basic explanation: Genetic Algorithm
Let’s start with the kitties themselves. CryptoKitties use what’s called a Genetic Algorithm (GA) to create the Kitties. A GA is not actually too complex. It’s really similar to how real biological genetics work, where genes — your DNA — store information about how the rest of your body works. In people there are genes for eye colour, hair colour, straight or curly hair, skin colour, or how tall you are. In CryptoKitties there are genes for background colour, stripes, fur patterns or facial expressions.
In your body, these genes consist of proteins that are all connected in a specific way. In a GA, we just use numbers to represent them.
In biology, animals can reproduce by mating. For those of you not familiar with the process, it involves taking genes from two parents and mixing them together. The sperm and the egg each contribute half the total amount of DNA which makes a child that has some traits belonging to the father, and some traits belonging to the mother, but never being exactly the same as either.
In a GA, we call this crossover. It works exactly the same too. You copy one half of the father’s genome, and the other half of the mother’s genome into a new, and unique child genome. How exactly you split up the genes can differ each time you crossover, meaning that even two siblings will have unique genomes. Just like in real life.
CryptoKitties works just like this. After mating (or “Siring” as they call it in-game) the new child genome is used to generate a new kitty.
Running a GA on Ethereum
This is where things get interesting. A Genetic Algorithm is a useful tool for solving some types of problems, (and creating cute kitties) but how do we represent genomes and do crossover with a cryptocurrency?
The key here is understanding that representing a genome is as simple as storing a few numbers. Doing crossover then, is really a simple piece of code that takes two numbers (parent genomes) as input and produces a single number as output (child genome).
On the Etherum blockchain, we can store data (like a genome), and perform calculations with that data using Smart Contracts.
WTF is a Smart Contract?
In Bitcoin, a transaction can be sent from one person to another. There is only one type of “account” in bitcoin. These would be called External Accounts.
In Ethereum, there are two types of accounts: External Accounts and Contract Accounts.
A Contract Account is a special account. Like an External Account, it has an address, so you can make transactions with it. Unlike External Accounts, it isn’t necessarily owned by anybody, and it contains some code. Every time a transaction is made with it, the code is run (like running a program on your computer).
I’m purposefully leaving out how mining works. For now, don’t worry about it. Briefly, a miner runs the code as part of verifying the transaction.
Contract Accounts are also referred to as a Smart Contracts. There are lots of smart contracts being calculated all the time as transactions are verified on the blockchain.
CryptoKitties Smart Contracts
Because the blockchain is publicly available, the main smart contract that runs the whole CryptoKitties system is also readable here: https://etherscan.io/address/0x06012c8cf97bead5deae237070f9587f8e7a266d#code
There’s a lot to digest there…but thankfully it’s all very well commented. The main contract “KittyCore” explains the various aspects:
- KittyBase: This is where we define the most fundamental code shared throughout the core functionality. This includes our main data storage, constants and data types, plus internal functions for managing these items.
The “data storage” referenced here means how the Genome of a kitty can be stored in the contract.
— KittyOwnership: This provides the methods required for basic non-fungible token transactions, following the draft ERC-721 spec (https://github.com/ethereum/EIPs/issues/721).
Funigible [Adjective]: Able to be substituted for something of equal value or utility; interchangeable, exchangeable, replaceable.
So non-fungible means non-interchangeable(?). This refers to the parts of an asset, rather than the asset itself. Example, rice is fungible, because you can substitute one grain of rice in a sack for any other grain in the world, and the value of the sack as a whole will remain the same. This doesn’t hold true for real estate or land for example. Any acre of land in a property cannot be swapped for any other acre in the world without changing the price of the property as a whole.
In this application, imaging owning a bunch of kitties worth $100 as a whole. They are all unique and therefore cannot be swapped for other kitties without changing the value of the group of kitties.
(I welcome feedback on this…I’m not entirely sure of my analogy)
— KittyBreeding: This file contains the methods necessary to breed cats together, including keeping track of siring offers, and relies on an external genetic combination contract.
This refers to the smart contracts that do the crossover as we discussed earlier.
— KittyAuctions: Here we have the public methods for auctioning or bidding on cats or siring services. The actual auction functionality is handled in two sibling contracts (one for sales and one for siring), while auction creation and bidding is mostly mediated through this facet of the core contract.
This let’s you sell kitties and… basically pimp them out to breed with other kitties. A core feature, clearly.
— KittyMinting: This final facet contains the functionality we use for creating new gen0 cats. We can make up to 5000 “promo” cats that can be given away (especially important when the community is new), and all others can only be created and then immediately put up for auction via an algorithmically determined starting price. Regardless of how they are created, there is a hard limit of 50k gen0 cats. After that, it’s all up to the community to breed, breed, breed!
This part is actually quite interesting. The functionality here is very similar to how many ICOs (initial coin offerings) work. Often, the creator of the coin will create a bunch of these coins (or in this case, kitties) to be given away for free. This way, you can kickstart the community by giving people something that will have value (however intangible) and scarcity at a later date.
I haven’t covered much of the technical aspects of the system. That’s something for a later post, once I figure it out myself. However, I do hope you have found this even slightly useful.
I welcome any feedback on the content, or suggestions for future posts!
Lastly..there have been way too many kitties mentioned throughout this post (and on the internet in general as of late…). I hope this serves to bring balance to the force.