This guide offers a plain-english introduction to the most important concepts you'll want understand in order to begin building within the Chia ecosystem. Unlike guides that focus on getting started plotting or farming, our focus here is on building a workable mental model of the system and understanding the lifecycle of a smart coin (in Chia, all the coins are smart!). We assume that you are familiar with basic blockchain and programming concepts, but not necessarily LISP - the language on which Chialisp is based.
Visit chialisp.com for a deeper dive into Chia fundamentals: docs | video tutorials
In most blockchain systems (like Ethereum), transactions are first class objects. There is a single smart contract that maintains an internal representation of who owns the token objects that the contract has been given permission to manipulate. The state of this contract is updated not when a transaction is sent to the contract, but when the transaction is processed by the contract (minted into a block). This means that the order that transactions are processed matters very much and opens the system to tampering by miners who might manipulate the order in which transactions are mined.
In Chia, coins are the first class object. This means that all the rules and functionality for a coin exist inside the coin itself. Coins can be spent in many ways, e.g. sending a coin from Alice to Bob, donating to a crowd-funding campaign, or buying an NFT. Each of these actions consumes existing coin(s), ending in creation of a new coin with updated state. Thus, the ledger is resistant to transaction order fidgeting, and the overall system is more "decentralized" because each coin is running on rules intrinsic to itself.
Spending a coin requires solving that coin's puzzle, which is also equivalent to successfully running a coin's program. Running a coin's program successfully will always result in destruction of that coin and creation of one or more new coins. If the puzzle isn't solved successfully, the coin isn't spent. Anyone can attempt to solve any puzzle, so it's up to the coin's creator to secure the intended behavior of the coin.
In Chia, coin spends are atomic: they have either happened or they have not (i.e. we avoid any re-entrancy bugs). While the operation of each coin is stateless, coins can carry states that are traceable down through their descendents. They can contain arbitrary fixed data blobs that travel with the coin forever, increment a tally with each spend, or refuse to be spent unless some other requirements have been met. The possibilities are endless.
The process of solving a puzzle involves sending the puzzle arguments (which are specified ahead of time within the puzzle) that allow the program to come to a successful conclusion. Those arguments can be public keys, constants, or even other puzzles themselves. Thus, puzzles can be nested infinitely, allowing each iteration of a coin to attach and change the rules about how that coin might be spent in its next iteration (but never reversing how it was spent previously). The spending of a coin equates to consenting to adopting the rules of the next generation of the coin.
Note that once solved, a puzzle's solution is stored transparently -- albeit as bytecode -- on the blockchain. This means that arguments passed in may eventually become public knowledge.
The most common coin program is the "standard puzzle," which says "I will only respond to a spend attempt that is signed by a specific public key, and I will then follow any other instructions given to me." Because the standard puzzle is the one used by all newly farmed Chia tokens (XCH) and governs the perpetual transfer of all XCH, it includes a variety of security and extensibility features that make it a wonderful puzzle to study, but not actually a very simple puzzle to begin with for building basic understanding. (We'll explore a simple [and insecure] custom token in the next guide.)
Coins follow the same format:
- ParentID - Information about the coin that created this coin.
- Puzzle Hash - The hash of the puzzle (program) that governs this coin.
- Amount - How many mojos this coin represents (or, for custom tokens, how many of that token's denomination). Mojos are to Chia as Satoshis are to Bitcoin.
CoinID - The hash of these three pieces of information together.
Anyone can attempt to solve a puzzle. By requiring arguments that are unknown to outsiders, we can restrict who is able to interact with the coin.
...Ok. But the coins in my wallet are mine, right?
Yes! Coins and tokens that must be signed by your pubkey (and only your pubkey) as an input to their puzzle solution can only be spent by you... Just like in Bitcoin, your coins are as secure as your private key.
- Coins are things which exists on the Chia blockchain, analogous to a UTXO in Bitcoin.
- Chia Asset Tokens (CATs) are coins which represent an underlying asset, sometimes colloquially just called tokens. CATs are fungible coins similar to ERC-20 in Ethereum.
Some Chia documentation refers to Coloured Coins, which have been deprecated in deference to the new CAT standard. For more details, see this Glossary.
In a process familiar to most blockchains, a spend bundle is broadcast to the network and a Chia farmer will eventually evaluate it:
- Does a coin with the Coin ID exist?
- Does the puzzle reveal match the puzzle hash inside the coin?
- Does the puzzle (program) run successfully, given the arguments (solution) passed to it during runtime?
- What conditions are returned by this spend?
- Do the conditions pass?
Instead of writing contracts about data, ask yourself, "What rules govern this coin?"
- This coin can only be spent by _____.
- This coin can be increased in value by anyone, as long as _____.
- As long as the coin always _____, let the owner add whatever other rules they want going forward.
- This coin must exist for ______ days before it can be spent.
- All these rules can be layered.
Next Topic > Getting Started with Development on Chia