TheMayor


Chia App Building Series

What is a constant product automated market maker?

Watch the above video to get a great explanation.

Basic Idea

The basic idea is we want to a way to describe the value of each item and we'll do that with math. The formula is the following.

X * Y = K

I know what you're thinking. You're thinking "I didn't know there was going to be math!" I'll walk you through it step by step. It'll be ok, I promise.

X and Y are the quantity of the tokens/coins we want to trade. From now on lets think of it it as Total(XCH) * Total(DBX) = Constant.

What is Constant? It's the magic number we want to keep the same. Here's an example.

See how it works

Lets pretend that we start a brand new AMM (That's an Automated Market Maker). This AMM deals in Apples (X) and Potatoes (Y). Using the example in the video above, we start this AMM out with 50,000 of each Apples and Potatoes.

Apples(X) * Potatoes(Y) = Constant (K)
or
50000*50000=2500000000

If a new farmer comes along and wants to trade 7000 potatoes in for some Apples, how many apples do we give the farmer?

Potatoes

We know how many Potatoes the AMM now has [57000], and what the constant should be [2500000000]. We use these numbers to determine how many apples should remain in the AMM.

(k)2500000000 / (Y)57000 = (X)43859

There should be 43859 apples left in the AMM, but there are currently 50000. So we just subtract the two numbers to find out how much to give to the farmer.

50000 - 43859 = 6140

The farmer would get 6140 Apples for their 7000 potatoes.

Here's where Chia shines.

In the video above, the "trade" had a magical lamp with a genie inside that ran the AMM. You had to trust the genie and it's master that they'd keep the lamp safe and the genie or the master wouldn't eat all the potatoes and apples. The risk of most AMMs is when you give your coins/tokens to other people. The AMM we're going to build won't do that. You keep all of your tokens until your trade gets claimed.

How is that done?

Chia offer files.

Offer files are a way for you to create a swap directly on the blockchain. You can offer your XCH for HOA/DBX/SBX or any other token/NFT you can think of. You sign 1/2 the transaction and publish a file to the internet. Anyone on the internet can see that file, load it in their wallet and say I agree to this and they sign the 2nd half. Then both of you simultaneously trade for the assets you wanted. The offer files cannot be adjusted or tampered with. People can't make you spend more than you have, they can't drain your wallet because you accidently clicked a wrong link.

It just works. In my opinion, this is the biggest thing in crypto. So much risk has been removed because of offers.

The other great thing about offers is you can set a time limit. This offer only good for the next 30 minutes.

Are centralized AMMs bad?

No. But there's risk. Chia had 2 AMMs. TibetSwap and HashGreen. HashGreen was drained by an exploit (The genie ate the apples). That's why I want to build an application that anyone can use to provide their own liquidity to the chain.

The best Total Value Locked (TVL) a chain can have is 0.

So what exactly will we be doing?

The App will be designed as follows. We will use dexie as the host for our offer files. We will gather information from the APIs of TibetSwap and dexie to find out what we need to build an offer that is correctly priced. We will use Expiring Offer files to provide that liquidity. We will use the Liquidity Incentive Program from dexie to earn a bit of DBX/HOA and will will add our own fee that we collect ourselves for our AMM.

Lets start building!


Help support me by donating below.

XCH Address: xch1xtn62vckj2dmpdlttewfgpsz6zluw8jpj57v308whcu5ty86xhlq3a0h0e

XCH Amount