Medici: Dialectic’s Yield Farmer
A look at Dialectic's Yield Farming operation and how we automated it.
When we started Dialectic, we almost immediately looked into and decided to take advantage of stable incomes by way of DeFi yield farming programs. As the fund has continued to grow, so has the portfolio with which we farm. Early on this was manageable for a single person. However, it quickly became unscalable, so we needed a technical solution that would allow us to automate most of the mundane tasks that come with yield farming, while also allowing for continued growth and further innovation on what we now call our "automated market-neutral stable income fund".
Our initial requirement for the proof of concept was to reduce the 3-4 transactions harvesting would take into a single transaction. This would mean that the members of our team responsible for yield farming would gain a significant amount of time waiting for pending transactions, etc.
We accomplished this by building an abstraction for every strategy that we implement as a smart contract and having our team interact with those rather than with the protocol itself directly. Given that we were already using the Gnosis Safe to store our funds, we decided that using Modules would probably be the best way forward. Gnosis Safe Modules essentially allow you to add extra functionality to the Safe itself by simply deploying a smart contract containing some logic that is executed on behalf of the Safe. At this point, we are probably one of the largest deployers of Safe Modules.
The proof of concept was simple but proved successful. For every strategy we were using, I wrote a smart contract that implemented a simple interface with the following functions:
- deposit: to deposit a given amount
- withdraw: to withdraw a given amount
- harvest: to harvest and sell the rewards into a preferred asset
- harvestableAmount: to read how much is currently harvestable in a preferred asset
Each of these Modules then gets added to the Safe and can be called by any owner of the Safe. The beauty of this system is that none of the assets ever leave the custody of the Safe. Beyond the initial headache of codifying all of our strategies, we saw an instant productivity boost even with our simple PoC. It was decided that this project is critical to Dialectic and therefore will be extended.
As of this point, the harvesting still needed to be done manually by one of our team members, although we wanted to automate this. Additionally, all tracking of harvests was done manually, which is not optimal. So now our next goals are clear, automation of harvesting and reporting.
The tracking was relatively simple: our abstractions emit events whenever capital is deployed or withdrawn, as well as whenever a harvest occurs along with some data about the harvest. We simply watch for log emissions and store them in our database for future analysis. In this way, we are able to automatically calculate how much was spent on gas and what slippage we experienced, as well as other one-off queries.
The automatic harvesting was a more complex issue that needed to be solved, and one important aspect was security. We wanted to ensure that the private key used to harvest was limited in what it could do, e.g. a bot should only be able to harvest and nothing else. We did this relatively easily by allowing Safes to whitelist certain addresses to call the harvest function.
Next, we determined when harvests should occur. For this we developed a rule based system allowing us to define harvests for every strategy. This ranges from rules such as how many blocks have elapsed to what percentage the gas cost is of a given harvest. Our DeFi team is able to decide the rules within a special frontend we built for Medici.
One of the things we quickly determined throughout this process is, not only do we want harvesting to be possible, but for certain strategies we also want to be able to compound. This means harvesting, taking the resulting capital and redeploying it into the original strategy. Compounding is not implemented in every strategy, as we do not always need it, but our team is able to control whether to harvest or compound through our frontend.
Medici is currently deployed on Arbitrum, Moonriver, Fantom, Polygon and Ethereum. In its lifetime it has done more than 300 harvests, freeing up hours of time for our team and providing the best possible harvests due to its ability to harvest any time of the day whenever prices are optimal.
Obviously Medici has been a huge success, but we are nowhere near done. There are a bunch of improvements we want to undertake in order to ensure that the stable income fund can be 100% automated.
So what are some of the improvements we are looking at? First, we currently encode which exchanges to use for selling harvests within the smart contract. This means when a new DEX pops up we would need to redeploy the strategies smart contract, which is not optimal. So this component should be moved off-chain. With the help of Weiroll, a scripting language developed by Nick Johnson, Matthew Di Ferrante and I, we will be able to achieve this and simply pass a Weiroll script to the harvest function.
Secondly, we want to build a system that can automatically rebalance the fund based on target APY and risk levels. Assume we have USDC in Aave. However, the APY on USDT is now higher. The off-chain component should be able to detect this and move the funds from one strategy into the next while taking into account the gas cost for switching along with various other variables.
Additionally, we are looking at deploying Medici on chains beyond those that are EVM compatible. This is a far larger undertaking and will take quite some time to be achieved. But with Solana, Cosmos and other non-EVM chains becoming more popular for DeFi, it is only logical to invest time there.
We are considering other organizational aspects that would benefit the developer community. For example, we are looking at open sourcing some of the components of Medici, as we believe many other teams can benefit from them and build other useful tools. We are also thinking about how we can deploy Medici strategies openly for other people to be able to profit, while allowing Dialectic to keep the alpha we have on those strategies that are profitable.
If you are interested in working on something like this or want to learn more, feel free to email me at email@example.com. We are always looking for talented people thinking about the next generation of capital deployment.
Subscribe to our newsletter