Up until today I was very worried that I had bitten off more than I could chew with the current project I'm working on.

Wow, it sure would be nice if I was able to add DeFi farms and an algorithmically pegged stable-coin to this project. But of course that's impossible because it's way too much work.

So yesterday I was doing more research on AMM, trying to gauge how difficult it will be to get Liquidity Pools up and running. Yesterday I realized that it's going to be surprisingly easy.

#### At first, this function was a little intimidating.

But honestly the hardest math class I ever did was physics: electricity & magnetism. This math is a walk in the park comparatively to the nightmare that is integrals and magnetic fields.

https://medium.com/bollinger-investment-group/constant-function-market-makers-defis-zero-to-one-innovation-968f77022159

Where R_α and R_β are reserves of each asset and γ is the transaction fee. Trading any amount of either asset must change the reserves in such a way that, when the fee is zero, the product R_α

R_β remains equal to the constant k. This is often simplified in the form of xy=k, where x and y are the reserves of each asset. In practice, because Uniswap charges a 0.3% trading fee that is added to reserves, each trade actually increases k.

### That's it.

This is the entire problem that needs understanding and solving. The main issue is that no matter where I looked I wasn't getting a straight answer. Gamma (Y) is the trading fee? How is the trading fee measured? In a percentage? Does a 0.1% trading fee register as 0.001 or 1.001? Or is the fee how many coins were lost from the buyer?

#### No mention of these key details anywhere.

when the fee is zero, the product R_αR_β remains equal to the constant k.

This is clearly the key to solving the problem. So I had to figure it out myself with a paper and pencil like I was back in middle school. Although some of my best planning is done in this manner.

### Okay so...

How do we calculate the constant? That's another thing all these tutorials seem to completely fail at explaining. On a certain level it's really annoying and frustrating that people try to run around teaching others in a manner that assumes the student has already learned the content. How ironic. Like no, I'm trying to learn this, don't assume I know what the fuck you're talking about. Jesus Christ.

So after playing around with the number by hand and reading info on this topic I realized that the main equation:

can be simplified to the x*y=k equation. Much easier. However, we only know one of the variables, which is how much money is being pumped into one side of the pool. We want to know how much money will pop out of the other side, but we still don't have the constant k.

### This video kind of explains it.

In order to find k we simply pretend like a trade is not occurring. This creates TWO `x*y=k`

functions. That's what none of these tutorials seem to point out. Seems like a pretty blatant case of teacher assuming student should already know.

#### When no trade occurs, there is no Delta.

That means in order to find the constant k all we have to do is multiply how many assets we have in the vault together. Take the CUB-BUSD LP for example. There are 718,652 CUB in that pool right now, and 2102 BNB. Therefore, the constant for the CUB/BNB pool is 1,510,606,504. Now that we have the constant, we can solve the equation pretty easily.

Say someone dumped 5000 CUB into the pool at these levels? How much BNB would they get in return? Assuming zero fee, we know that 5000 CUB is entering the pool and x amount of BNB will be removed.

( 718,652 + 5000 ) ( x ) = 1,510,606,504

### Turns out, AMM math devolves into some of the most basic algebra possible.

718,652 + 5000 = 723,652 1,510,606,504 / 723,652 = 2,087.47644448

So after everything is said and done, if we pump 5000 CUB into one side of the equation, there's only going to be 2,087.47644448 BNB left in the pool afterwards. Seeing as we started with 2102 BNB in the pool, this implies that that we will get 14.52355 BNB with the 5000 CUB at these levels.

#### Not counting the fee.

And this is the part I don't truly fully understand yet.

How does the fee come into play?

Gamma (Y) is the fee, but it actually doesn't need to be part of the equation. To me it makes the most logical sense to tax the user AFTER the math has been done and the trade has been made. Referencing the example above I think it makes the most logical sense to charge a fee to the 14.52355 BNB that we pulled out of the pool, not the 5000 CUB that was initially pumped into it.

Either way the fee doesn't really factor into the equation. We either take the fee out BEFORE we do the math, in which case less than 5000 CUB would get plugged into the equation, or we take the fee AFTER we do the math and tax the output rather than the input.

### Output vs Input fee

Now, I can understand why Uniswap and Pancakeswap tax the buyer BEFORE the trade is made, but I do not agree with this decision. If we tax the output then the fee becomes a dependent variable of slippage. Meaning: whale traders who create a lot of slippage would pay less of a trading fee, because the fee is charged after the transaction rather than before (meaning the funds they lost from slippage will not be taxed).

Personally, my project will be doing exactly that. I would much rather tax the output because that just makes way more logical sense to me than taxing the input. My code will flow better and the process will be easier to explain and document. This trumps a more consistent trading fee in my view.

In addition, I will NOT be adding the trading fee back into the pool for farmers to yield. This is redundant logic that I will not employ. It is truly insane how fast everything is moving at this point. Uniswap created the 0.3% fee to give incentive for yield farmers to stake their money. Why else would yield farmers stake their money?

It becomes obvious that this 0.3% is a joke. DeFi has shown us the true meaning of yield farming. We don't need to yield farm 0.3% in order to incentivize liquidity. What we need are more huge yield farms like what we are seeing with CUB and all the other DeFi coins out there.

We can still farm 200% on some of these pools. Sure, that might not be sustainable in the long term, but it's a helluva lot more than 0.3% even if these APRs crash into the mountain. Therefore, the trading fee becomes completely irrelevant in terms of incentivizing the farmers. The farmers are already incentivized by token yields x100 times more than trading fees.

Therefore, rather than pump the fee back into the pool, I'll be burning it just like proof-of-burn would imply. Ether that or the trading fees will go into a vault that the network can vote on where the money should be allocated (one of those options being to send it to @null and burn it once and for all).

### When does constant k change?

When a pool has more liquidity k will be higher. This should be obvious from the equation: we multiply the total number of coins on both sides together to get the k value. That means when someone enters or exits the LP pool this k value will change drastically.

In practice, because Uniswap charges a 0.3% trading fee that is added to reserves, each trade actually increases k.

So my pools will not increase k when trading occurs, because no additional funds are being pumped in. Rather the funds will likely be sent to a vault that no one controls so we can decide what to do later. This makes sense because there's going to be 4 tradable assets: mana, fire, lightning, ice. That means all four assets will be in the vault, and we can set up the main buy & burn to liquidate the fire/lightning/ice to buy and burn the governance token mana.

Another option here is that we liquidate mana/fire/lightning and pump ice should the value of our stable coin break the peg to the downside. There are many possibilities with a proof-of-burn vault controlled only by governance votes. In fact, we could even fund development from the vault should we have consensus to do so.

#### Conclusion

These are very exciting times. We are in the middle of a raging mega bull market and everyone is complaining about Elon Musk tweets and Vitalik giving money to charity. The shortsightedness of the cryptosphere is at all all time high right now, and we are at the foothills of a Goliath run.

I am trying to build a network whose tokenomics will be pretty much superior in every way compared to every other project I've seen. Never before has anyone attempted to create an AMM DeFi farm with a full-fledged futures market and an algorithmic stable coin. I've been doing research and posting on these topics for over three years, and I'm just now realizing I can prototype it all on Hive extremely quickly.

In addition, no one has ever considered burning exchange fees. Classic case of devs forgetting to adapt to the new climate. When Uniswap was born, the ONLY REASON to stake LP tokens was to farm that 0.3% trading fee. Now with DeFi farming the 0.3% trading fee has become completely irrelevant. We can lower the fee and send it to @null to avoid wash-trading. This will be a totally unique system.

This dive into AMM has shown me that these things are possible. The math is easy. It's the Solidity toolkits of ETH and BSC that are difficult. Pretty sure we can build these things on Hive much quicker than the Solidity guys could ever dream. Heading back to work: For the Swarm.

Posted Using LeoFinance ^{Beta}

Return from AMM Deep Dive to edicted's Web3 Blog