Welcome
Hello and welcome to a series of articles that will hopefully provide some exploration of the fundamental mathematics by modeling DeFi behaviors.
I’m kickflip. I’m a mechanical engineer with a research background in optimization of mechanical systems by way of mathematical modeling and control systems, and with a professional background in Defence engineering. Lately, I have been writing scripts to model how certain DeFi principles work, and I’d like to share my findings with you.
Compounding, manual vs. auto
It seems intuitive that manually compounding would outperform auto-compounders. Auto-compounders charge fees and those fees typically outweigh the gas costs of manually compounding (at least on the Fantom network). But is the effort worth it?
I set out to compare the pair, and in doing so, opted to model a compounding LP mathematically from first principles. It sounds crazy, but deriving the equations is as simple as critically thinking about decentralized finance and offers a great opportunity to better understand how things work.
In this installment, I’ll attempt to walk readers through my thought process and derivations for the basic LP model I made in MATLAB. We’ll follow the steps in DeFi, and use them to generate our equations. The current build-state includes an LP with yield from swap fees and emissions. I’ll develop it further in the future but this is where I wanted to start. Please note that this article may become quite technical, so feel free to follow along with a pen and write the equations yourself to help build your understanding. Alternatively, reach out to the community to help you understand bits and pieces.
First Step: Initial capital
Our capital (Cap) is represented by the total value of the pool (PV) divided by the total quantity of LP tokens (LPTtot), multiplied by our quantity of LP tokens (LPT). We can also account for how many emission tokens (ET) we have and the emission token value (ETV).
Cap = (PV/LPTtot*LPT) +(ET*ETV)
To normalize the start-state, we can say we own all the liquidity and have 1 LP token of value 1, and 0 emission tokens of value 1. Therefore, our capital is 1.
For now, we’ll ignore the underlying LP assets. I will address those in a later installment. Price fluctuations are irrelevant because we’re comparing compounding strategies. We want to look at LP token yield, emissions, and fees.
We will also start with 0 accrued fees.
We have now established our start-state.
Second Step: Yield on yield
Now we can introduce an iteration or time step. Let’s jump forward an hour. If we start with the manual compounding scenario, we have presumably earned yield on the LP token (swap fees), and yield from emissions. Therefore, the value of the pool (represented by our LP token) goes up, and the quantity of our emission tokens goes up. Our capital goes up with it. Cool!
But by how much does the LP token value go up? And how many emission tokens do we earn?
If we ignore one, we can model the other, and vice versa, before putting them together. We aim for 10% from swap fees and a whopping 100% from emissions. This isn’t unreasonable to expect from fresh plays on the network.
If we focus on swap fees, 1 unit of LP value accrues 0.1 value in one year. With some simple algebra, we can figure out how much it grows in one hour.
On the other hand, emissions aren’t distributed based on LP value; they’re distributed based on LP token quantity. Remember how we normalized everything to 1? For 1 LP token (value 1) we should earn 1 whole emission token in one year (value 1). That equates to 100% APR. Again, we can work out the quantity of emission tokens earned in one hour. These considerations are important because if we later introduce price feeds and varying yields, the mathematical relationships need to represent real-world relationships.
Final Step: Let’s compound
In our first step, we gained yield in the LP token, and yield from our fraction of an emission token! If we start with manual compounding, we can iterate 23 more steps before anything needs to happen. On step 24 (24h), we compound.
When we compound, the value of the emission tokens that we have accrued is converted into LP tokens. The ratio between the quantity and value of LP tokens that we have still applied to the ratio of the LP tokens we receive from the value of the emissions. That’s how minting LP tokens works. In this time step, we reset our emissions to zero, increase the LP value, and overwrite our quantity of LP tokens.
We just compounded!
In the case of auto-compounding, we can set the compounder to trigger every hour instead of every 24. The difference is that before we convert the value of the emissions into LP tokens, we take 4.5% of the value (in the case of Reaper Farm), and accrue it separately (tracking fees). The rest of the value of the emissions can be converted into LP tokens in the same manner as before. That’s it.
A real face for modeling DeFi
In my case, from a modeling perspective, the processes are scripted in MATLAB, and the only parameters I made variable are the fees and number of steps before compounding. The comparison is 0% fees, 24 steps (manual) vs. 4.5% fees, 1 step (automatic).
The last piece is to tell the process to stop. Again, in my case, I set the model to continue to iterate until capital reaches 2 (double our capital). From here, we can compare anything we want. Smaller or larger fees, more or less frequent compounding. Even no compounding. Recall that our capital considers emissions so we can just collect emissions indefinitely.
Additional considerations
As always, mathematical modeling is only as accurate as the design. We’ve ignored gas and price fluctuations. We’ve assumed that auto-compounding will take place every hour and that manual compounding will occur daily. But we can also make the assessment that the result might be insensitive to or entirely independent of these parameters, so we can worry about them later (if at all).
Hopefully, our model is correct. Always think critically about the feasibility of results, and use them to inform your assessments. Don’t take them as gospel. That said, let’s take a look:
Gains
Manual compounding achieves 2x faster than Reaper Farm would. That’s great. But the difference is 10.5 days. Personally, I’d rather wait an extra 10.5 days and not have to do anything for the first 200+. But what about the fees? Well, I built the model to accrue fees:
For 1 unit capital, an auto-compounder like Reaper Farm can double it for the cost of 0.0471 capital. That is, if you put $100 in and wait long enough, an auto-compounder will double it for the cost of a coffee. Seems worth it to me.
Thanks
If you’ve made it this far, thank you. I hope it made sense, and if not, I hope you seek the discussions that will help you grow and learn. Feel free to reach out and let me know if this piece was useful (more so if you think I made any mistakes!). I intend to continue modeling and provide more content. In a future installment, I hope to model a randomly generated price feed for our three tokens (two tokens in the LP and the emission token) to see how price movement affects these results. I’d also like to hear from you if there are any concepts you’d like to explore. I can’t promise that I’ll be able to model everything, but I will try.
Peace out,
kickflip
If you want to discuss this article you can join the conversation on Discord! Be sure to follow kickflip on Twitter!