How To Study DeFi Protocols Without Losing Your Mind

Natan
ZBL Capital
Published in
7 min readJul 21, 2020

DeFi products can be quite simple to use from their frontend (although there is a lot of room for improvement from a UI/UX perspective). However, if you want to understand how these products actually work you will need to dig deep into the smart contracts that rule them.

To understand entirely how a complex protocol works is not an easy task and it will be more difficult in the future as complexity increases. In this article I will provide some tips that I’ve learned through studying some of DeFi’s most popular protocols such as dYdX, Maker, Compound and Aave.

Use it

The first tip is not actually to use it but to understand what the financial primitive behind the protocol is. For example, Compound is a money market, so you will have to have a basic understanding of how money markets work (spoiler: it is not a new concept).

The second tip is to use it. For two reasons. The first reason is that maybe you did not quite understand the financial primitive behind the protocol. When interacting with the real product you will get a lot of information that it is hard to gather by just reading the contracts.

The mint event is emitted when a user supplies a token to the Compound protocol and mints a CToken

I encourage you to try every transaction that can be done with Metamask (or any other wallet) in the protocol’s mainnet contracts and use one of our favorite weapons, Etherscan. Then check every event the transaction triggered. It is a good practice in protocol design to trigger events for every important action a user can do in the protocol. Some events are easier to read than others, because their name and their variables can be read on Etherscan. In others, you will need to get the signature (topic 0) through hashing the function and the input variables types. (here is a cool tutorial to learn how to do so).

Decoding a transaction using ethtx.info

Also, there are some great transaction decoding websites. ethtx.info decodes the execution trace of the transaction, so you can see all the functions that were called in that transaction.

Start big

It may seem trivial to start from general concepts and then dig into more specific contracts, but when you get into the contracts you can get anxious to understand where the main functionalities are coded.

I learned that starting by learning the global variables that govern the protocol is probably the best way to go. For example, the general risk parameters of the protocol are usually stored in the main contract of the protocol and can be accessed via getters. In dYdX solo contracts there are getters to query data from every market.

Querying the dYdX ETH market info trough Etherscan

If you do not have a technical background (like me) this empirical knowledge will help you with things like with how many decimals a certain price or interest rate is stored in the smart contract.

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.

--

--