In addition to a new consensus algorithm, with Chia we’ve also built a new on-chain programming environment called Chialisp which is just as powerful as Solidity while also being far more auditable, and scalable. It also fosters much better software development practices. As an example of the power of on-chain programming in Chia, and how it differs from Solidity, in this post I’ll give an overview of how it enables truly decentralized exchange with significant defenses against miner/farmer extracted value (MEV). I will also explain the possibilities of automated market makers (AMMs) in Chia.
Chia works in the coin set model (similar to Bitcoin’s UTXO model): The only persistent data is the current set of unspent coins, which have sizes and rules about how they can be spent. When a coin is spent it can make new coins using its value but the original coin is gone forever. All persistent data has to be kept in coins. This is much better for scaling because the data which full nodes have to keep track of is simpler and smaller than the objects which the EVM has to store. Validating a block requires running all of its transactions but the only output is a list of coins which are spent and destroyed. This allows us to have an effective transaction rate greater than Ethereum’s while still allowing a full node to be easily run on an average desktop. At first it looks like this makes it much harder to develop smart coins in Chia, but the flattening of data types comes with some great benefits. Everything interoperates with everything else, and pieces of functionality can be reused and composed on top of each other.
A fundamental tool for doing commerce in Chia is coloured coins. A coloured coin represents a tokenization of some kind, with coin sizes whose melt value is extremely small but represent something else at a high ratio. (The default divisibility of a coloured coin is a billion. There’s room for this because a Chia is a trillion Mojo, which is the smallest indivisible unit, comparable to a Satoshi in Bitcoin). Superficially this sounds like Omni protocol (a colouration format on Bitcoin), but the colouration of coins can be validated in a very lightweight manner. In fact it is so lightweight that even smart coins can do it, unlike in Omni protocol which requires running a full node to find out which coloured coins aren’t counterfeit. This is done while preserving the ability of coloured coins to have just as smart of functionality as non-coloured coins. You could even have a coloured coin implemented in a coloured coin.
One of the new pieces of functionality which our coloured coin implementation enables is that it allows ‘offers’. For example, if you want to trade some chia for some of a coloured coin, you can make an unfinished transaction which burns some chia and prints some of the coloured coin. This will be an invalid transaction by itself but you can take your offer and send it to an exchange, or email it to a friend, or post it on reddit. Whoever receives it will be able to accept the offer by making their own partial transaction which prints a counterbalancing amount of chia and burns a counterbalancing amount of coloured coin and combining with yours to make a valid transaction which will go through on the blockchain. This is valid because in Chia everything in a block happens simultaneously so the funds ‘move’ in one step from one input to the other output without there being any moment where too many coins exist. In fact it’s possible for a third party to do bookmaking of multiple offers of a variety of different assets and as long as the amounts add up, everything will go through.
To understand miner extracted value, how it’s a problem, and what can be done about it, let’s go through an example. Let’s say the current ‘true’ price of chia (which it’s trading at in high volumes elsewhere) is $1000, and there’s one offer to sell one chia for anywhere between $900 and $1200 and one offer to buy one chia for between $800 and $1100. This can be done through offers on chain if there’s a reasonably trustworthy stablecoin. In this case a completely honest/altruistic exchange would match the buyer and seller at a price of $1000 while a maximally greedy/dishonest one would buy one chia for $900 and sell it for $1100 and pocket the $200 difference. If miners/farmers are the ones doing bookmaking they have no reputation to maintain and should be assumed to be maximally greedy. In blockchains this is called ‘miner extracted value’ while in finance it’s called ‘pocketing the spread’.
An important note about prices: There is no one true fair price of a thing. There are prices paid for things, and ideally when someone buys a thing there isn’t a cheaper price they could have bought an identical thing for and when someone sells a thing there isn’t a greater price they could have sold it for. There’s always some noise and slop in the system and even in theoretically ideal circumstances markets become very efficient but not so efficient that whoever’s putting the market together doesn’t have a way of staying in business doing so. Figuring out what prices ‘should’ be, or at least approximating that reasonably, is the whole job of a marketplace. Designating someone to report a ‘true’ price of a thing doesn’t fix the problem. Even if that person is ‘honest’ no one individual knows all the information in the world or has all the insights into how to guess future prices. That individual’s weaknesses will be a direct opening into market participants getting exploited.
There is a real role in markets for large bookmakers which, unlike miners/farmers, have a reputation to maintain and are trusted to not pocket the spread and set prices in a way which is reasonably representative of the total set of offers. With Chia offers it’s possible for a bookmaker to aggregate several offers together and introduce them to the blockchain as a single unit so a farmer can’t piecemeal remove some of them to their own advantage. An exchange which does this is trusted a little but the worst thing they could do is pocket the spread. A more traditional trusted blockchain exchange can simply pocket everyone’s money or treat deposits as a no interest loan and invest them in high risk investments on the back end and keep the returns for themselves.
Another way of reducing MEV which works synergistically with the above is to have price oracles. (In computer science, things which report difficult to find or compute information are called ‘oracles’.) A price oracle reports a current exchange rate. To understand how this works we first need to explain singletons.
A singleton is an identity which is embodied by exactly one coin at any given time, and where it’s possible for coins to validate that they’re interacting with the current representative of that identity. It’s similar to, but simpler than, coloured coins. An NFT is barely more than a raw singleton. Singletons are also the foundation of rate limited wallets, distributed identities, pool protocol, and price oracles.
You can read the technical details of how singletons are implemented here. This functionality has already been built and used for our standard pooling protocol. You can review the source of how we’ve implemented a prototype of coloured coins that I originally introduced here. We will update this once we update our coloured coin implementation details on chialisp.com.
As ridiculous as it sounds, a price oracle which has no security on it whatsoever other than that it only reports one price per block provides significant resistance to MEV. Offers can be made to support a range of possible prices but only use the one provided by the designated oracle. Because all transactions in a block in Chia happen simultaneously, this means a malicious miner can make the price too low or too high but not both. In the example given above they could either buy one chia for $900 and sell it elsewhere for $1000 making $100 or buy one chia elsewhere and sell it for $1100 also making $100 but both are better than the example with no price oracle where they could pull off both and make $200 without even having to make an external transaction.
Ideally in a healthy market you want offers to buy and sell posted at all times. In order for someone to successfully turn a profit actively trading at all times, they need access to orders, liquidity of their own to trade, and expertise in trading. Or maybe they don’t need expertise in trading and one weird trick will do. Automated market makers (AMMs), which are on-chain programs which follow a defined trading strategy have been very successful. Generally they work by having deposits of two different currencies, in our example they could be chia and a coloured coin. The AMM then accepts deposits and withdrawals with the rules that (a) If someone makes a deposit they’re given claims to the funds the AMM holds in proportion to their contribution and (b) The AMM assumes that the value of the amounts of the two currencies it’s holding are equal to get an exchange rate. AMMs in practice have worked well despite the simple trading strategy because they have a lot of order flow and due to their accessibility. It’s easy for currency holders to provide them liquidity while having confidence that they won’t get ripped off.
Implementing an AMM in Chia
It is straightforward to implement AMMs in Chia, and will be part of future development. The deposits are held by a pair of singletons (in the case of pairing chia with a coloured coin, one coloured and one not) which spend in unison and work together. Unlike in Solidity, instead of the singletons remembering all deposits it’s far more elegant and powerful for the pairing to have its own internal use coloured coin. That way the AMM itself only needs to remember its total deposits and the user deposits can be self-custodied and even support smart functionality like rate limited wallets and ridiculous things like pairing with other AMMs. When a user wants to make a deposit with the AMM, they make an offer to trade some of their own funds for some of the AMM’s internal use token and aggregate that with the AMM accepting the offer. The only logic the AMM has to do is be told by the solution what the new amounts of chia, paired coloured coin, and its internal use coloured coin are and decide whether it’s okay with that. Using the AMM for a currency exchange is the same but the offer is for the tokens being paired without using the internal coin.
In practice offers are likely to be a bit more complicated. The AMM should act as a price oracle and offers should use that. Also, different transactions using the AMM will all conflict so it’s best if there’s an exchange which aggregates them all and introduces the one big transaction to the blockchain. It’s even possible to make a hybrid ‘mostly’ automatic market maker which requires a specified key to sign off on all transactions but follows predefined business logic about how deposits move. That gets the benefits of a lightly trusted exchange which isn’t able to exit scam with everyone’s money, but can still help reduce the spread. This further allows others to provide the liquidity which that exchange uses while still having confidence that their funds are being held properly.
The combination of all of the above will lead to chia being a far more secure and liquid marketplace for tokens than has ever existed before. There’s a long road ahead to build it but it will be well worth it.