Print money like an AMM
A comprehensive explanation on mechanics, mathematics and the working of an automated market maker in Web 3.0, through examples and calculations.
Assume It is a time when there is nothing like currency at the moment, everything is running on a barter system.
It all starts with you inheriting a brick of gold and a huge piece of land where there is sand and nothing else, a desert probably.
You also got some contacts of incredible people, one who cultivates wheat and wants rice and vice versa.
You know one thing for sure the inherited land is almost of no use, you can't build anything and you can't even grow something in the sand.
I know you are smart enough to build and even grow something in the sand but for the sake of explanation assume it.
You got a genius idea to harness the supply and demand of wheat and rice to grow your capital i.e the gold brick that you got.
Here is the genius plan:
You barter your gold brick to get an equal value of wheat and rice from your network. You got 20,000 kg of wheat and 10,000 kg of rice, thus 2kg wheat = 1 kg rice.
Remember you got a network of people who wanted wheat and rice in exchange for crops that they cultivated?
You call them up and tell them that you got supplies if anyone wants to exchange them. You collect fees in wheat or rice whenever someone exchanges one crop for another, for example, if I want 100kg of wheat, you will ask for 50kg rice + fees (in kg of rice) for 100kg of rice.
You can now add those fees to your system/reserve, thus increasing the liquidity for wheat as well as rice.
Your capital is gaining value as trade happens. Your assets are appreciating, traders can trade easily an easy win-win.
You might think, "Not a bad idea, this will generate returns on my investment but where is the part where I sell a thing with no value to make money?"
Patience!
The aha..!! moment is just waiting for you.
Some of your contacts are big whales, they usually trade in huge quantities. Currently, you cannot facilitate that kind of huge trade due to the limited amount of your reserve.
Whales got big money, if you can attract them to trade you can get huge value out of them.
You have an idea to attract them as well. You run an advertisement campaign that promotes the idea that people can gain additional rewards if they supply their wheat and rice to you.
You have no issues giving a fair share of the generated exchange fees to the people supplying the liquidity i.e. wheat and rice.
The next big part, introducing the valueless component i.e. golden sand.
The next thing you do is, color the sand gold such that the sand is unique to you (nobody can create such golden colored sand), the value of that sand is almost nothing as earlier.
Actually, the value of that sand is equal to the value of color and labor for coloring the sand, it is called seigniorage in financial terms but let’s keep that for another post.
Whenever someone supplies you liquidity, you give them an equivalent share of that golden sand. That share of golden sand is a token that represents the value of liquidity that someone provided. He/She can redeem his/her provided liquidity with that share of golden sand.
We initially talked about how the generated fees increase the quantity of the assets in our reserve as the trade happens. The received token amount of sand remains the same unless someone decides to supply more assets.
On the other hand, the value in reserve keeps on rising thus people who supplied their assets get appreciation on their supplied assets.
This is our scenario right now:
Your reserve is rising - you attract a huge number of traders - big whales - massive rewards as exchange fees - a greater incentive for people to supply liquidity.
A win-win-win
Now you just need one simple model that ensures the sell pressure of the golden sand remains low to make sure that your reserve doesn't decrease in value suddenly.
Sell pressure is a public sentiment that forces people to cash out their assets i.e. exchanging golden sand for their provided assets or selling golden sand in the open market.
You managed to get a room with a huge number of lockers in it.
You again launch an advertising campaign urging people to lock their share of golden sand to receive extra rewards and the ability to unlock their golden sand at any time they wish to.
People attracted by the startling rewards, lock their golden sand in your lockers. This will decrease the circulating supply of golden sand in the open market thus the value of golden sand increases and selling tendencies decreases.
This is called "staking" in decentralized finance.
The scenario just got more rewarding to you; people want to supply assets to get rewards thus there is demand for golden sand. The price of golden sand will increase, the supply of golden sand decreased, and the price increases even more.
What if you create more golden sand and sell it in the open market?
Profits! profits!! profits!!! But
What happens to the people who supplied their assets in the reserve?
They get diluted, and their share of golden sand is not worth the assets they provided anymore. You pulled off a scam with the people who supplied assets to your reserve.
This was how an AMM operates (not the scam part if you are lucky enough), explained in a most simplistic form.
Here are some questions that you might ask yourself:
What if someone trades your whole reserve? (e.g. all the wheat you have in exchange for rice)
What if you are on the supply side of this scenario? (you supply assets to a reserve to get rewards)
How can you make sure that you are not scammed/dumped?
What if someone trades your whole reserve?
(e.g. all the rice you have in exchange for wheat)
What happens when there is a huge demand for rice? everyone will come to you and exchange rice for wheat, soon you will be left with 100% asset in wheat i.e. 40,000 kg of wheat and 0 kg of rice.
The increased supply and low demand for wheat will affect the overall value of wheat. The wheat will no longer be equal to 0.5kg of rice, it would go lower.
You are screwed.
What could you have done to mitigate this kind of loss resulting from the absurd demand and supply?
Let's understand what caused the problem in the first place.
You operated your trades without altering your price regardless of the demand and supply of wheat and rice. you were unknowingly operating with a constant sum function.
X (value of asset 1) + Y (value of asset 2) = k (constant)
This function enables our exchange price to be fixed based on the quantity of the assets in the reserve. When I traded 100kg wheat for the rice, the price was 2kg wheat = 1 kg rice and I'll receive 50kg of rice at that price.
Your reserve will look like 20,100kg of wheat and 9,950kg of rice,
You had 20,000 kg of wheat and 10,000 kg of rice before the trade; the value of 20,100kg of wheat and 9,950kg of rice is equal to the value of 20,000 kg of wheat and 10,000 kg of rice, thus the equation is held true.
The solution is to develop a price model that takes demand and supply into consideration.
Was that a conclusion or do we have anything like that?
Sadly It is a conclusion actually but there is a constant product function that takes the demand and supply into consideration. My upcoming post is exactly on this topic explaining everything about constant product function.
What if you are on the supply side of this scenario?
(you supply assets to a reserve to get rewards)
You will gain rewards that you are entitled to, meanwhile, there are risks as well:
A risk that the reserve/liquidity pool owner creates more golden sand and sells it in the open market. This will be hugely profitable for the pool owner but can be catastrophic to you.
A risk of impermanent loss: Impermanent loss is an opportunity cost that may occur while providing liquidity, due to the price shift of one asset of the liquidity pool, versus holding the assets. (Will be explained in an upcoming post)
When there is the same scenario in Web 3.0 there is smart contract risk as well; a risk that someone might exploit a bug to wipe out the pool of underlying assets.
We had assumed a barter system but in reality, DeFi does have a stable currency. There is a risk that supplied assets may lose their value over the time it can be better to have assets in stable currency in some market conditions.
How can you make sure that you are not scammed/dumped?
Always think a step ahead, someone gives. you a reward for doing something ask yourself:
What is the value of my action?
There is no free lunch….seems familiar right?
Find where the rewards are generated from and how sustainable is that source
Understand how the liquidity pool holder makes money, and what actions of that pool holder can jeopardize your assets.
Researching deep before investing helps a lot to avoid scams, rug pulls, bank runs, etc.
If you have more questions, do let me know in the comment section. I will try my best to get them resolved ASAP.