How To Study DeFi Protocols Without Losing Your Mind
By: Natan Baredes
Tags: Smart Contracts, Decentralized Finance, Blockchain, Ethereum, Finance

After going through the general parameters, I search where each user’s information is stored. Every DeFi product we’ve mentioned has individual accounts information stored as structs that can be accessed via a public mapping in the contract. Find those mappings and you will be able to understand which data needs the protocol from each specific user to work.

The Market struct that stores a dYdX’s market main variables (solo contract)

Don't trust, verify

Reading a protocol’s documentation is also a good way to start. If protocols want developers to interact with their smart contracts and build products on top of them, they need to have good documentation. Unfortunately, this was not the standard a year ago and much of the protocols’ documentation was hard to understand or was outdated.

Efforts like MakerDAO developers guide or Aave documentation have really raised the standards of documentation. However, many aspects of the protocols are usually not well explained or not explained at all. Also, you can find discrepancies on how the documentation says that a protocol works and how it actually works.

MakerDAO has included flowcharts that explain MCD smart contracts interactions

Usually, you will not find a detailed explanation about how interest rates are calculated and how the oracles contracts work. Some of this info is spread in articles released by the protocol’s team itself or by users asking questions, which takes me to the next tip.

Chat with the community

This may seem quite trivial, but talking to people can save you hours of research. Most of these products don’t have big teams, which can explain why documentation may be incomplete or flawed.

Founders are usually available in the Discord/Telegram chats of the protocols and are generally willing to answer technical and economical questions about their products. Also, there are a lot of enthusiasts that can help you too. That is the cool thing about open source financial protocols, anyone can become an expert on them.

Follow a path

Once you understand the where the global variables and user data is stored I try to follow a specific action of the protocol. A single action usually triggers function calls in more than one contract. So, it is unlikely that you will succeed in understanding a DeFi protocol if you read a whole contract and don't "jump" between contracts.

Aave Docs include an overview of each action's path

I like to start with the action that allows a user to deposit funds in the protocol and follow all the function calls that the transaction triggers. Also, I try to pay attention to how these actions change the global parameters of the protocol. In dYdX and Compound, when a user deposits tokens to their account, the global interest indexes are updated.

Code

I have learned a lot by coding scripts that query data from the Ethereum mainnet. Data from the Ethereum blockchain does not come in “friendly” formats and you will learn a lot about the protocol you are studying while querying it. For example, there is a lot of handling different solidity data types that will allow you to understand them better when studying the contracts. You can use Web3 or Ethers libraries to connect with the Ethereum blockchain via Infura nodes.

You can also learn a lot from using tools that others built to get data from your protocols. Many protocols have APIs that let you query account balances, interest rates and trades in real time. Running simple queries and getting to know what the protocol team thought important to store in a database is a good way to start.

Associate

It is always a good practice to write down how things work. If you can explain the contracts to yourself while writing them down, you have probably gained a decent understanding of how they work. Also, you can go back to your annotations when studying other protocols and you will find the differences and similarities.

Once you have read a few protocols you will start to associate architectures and good practices, and your speed will increase. For example, Aave uses a lot of concepts from the Compound protocol. Both protocols issue interest bearing tokens (aTokens / cTokens). However, the way in which they accrue interest is different. With cTokens, the cToken / underlyingToken exchange rate changes as the interest accrues. With aTokens, the user wallet’s aToken balance increases as they accrue interest.

Also, some projects share resources, like MakerDAO oracles. dYdX uses MakerDAO oracles as their ETH/USD price feed.

Be specific only when you need to

Unless you are auditing the code, you do not need to understand every library that the protocol uses. Most contracts use standard math libraries like SafeMath from OpenZeppelin to handle operations. In general, those libraries’ functions are quite verbose and you can understand by its context what operations they are being used for without reading the whole library.

It is not rocket science

Understanding smart contracts requires time and dedication. However, it is not an impossible task. The protocols I mentioned do not make complicated calculations and all their code is published and has been audited by many people.

You will find lots of people willing to help you along the way and as time passes you will get better. Once you understand that each transaction is deterministic and master the tooling to decode them, you will know all the functions that make the protocol work.

I hope that I provided useful guidance in this article.

At ZBL Capital we are a team of engineers and traders developing trading algorithms in Decentralized Finance. We are always looking for interesting minds to join our team, so if you want to know more about DeFi or what we build at ZBL, please email info@zblcapital.com or DM us on Twitter @zblcapital.