From Banking Theory to Blockchain Reality: Creating a Decentralized Bank with Solidity (part 1)

Numa
7 min readOct 20, 2023

--

Important change: I’ve switched my testing framework from Hardhat to Foundry.

Introduction

The money market is one of the most significant global economic engines. At both a micro and macroeconomic level, the decisions to sacrifice present consumption for future consumption (and vice versa) and the costs and benefits of this decision influence each of the aspects of our lives as social beings: from the decision to go out with friends instead of studying for a final exam to, and here the focus of this post, the costs and benefits of using money below or above your budgetary capacity. In this case, we will focus on the microeconomic aspect of this area and talk about the possibilities generated through the existence of a money market for users of an economy and how this project works. At the same time, I will accompany this series of posts with reinforcements of economic theory and will code the project using two different protocols: Compound and AAVE. I will explain the infrastructure differences between both, and finally, I will test the contracts using Foundry.

For reasons of order, we will divide this content into two parts:

  • part 1: economical explanation of banks and their role + differences between traditional bank roles and web3 protocols + explain the logic of our Smart Contract
  • part 2: step-by-step guide on how to code this Smart Contract using both AAVE and Compound + testing in Foundry

Let´s start with the content of our first part

What is a Bank, and what does it do?

We will start this post with a bit of economic theory. As an Economist passionate about the decentralized nature of things, I believe that knowing the theoretical background of banks and their function worldwide is essential to understanding their role and rethinking how these institutions function and, if possible, offer improvements to their operation.

A simple way we can use to define a bank is

“a bank is an institution whose current operations consist of granting loans and receiving deposits from the public”

This is the definition that regulators usually rely on to define whether a financial intermediary has to be subject to pre-existing regulations for this type of entity.

Every word that makes up this definition is essential:

  • The word “current” is vital because there are many companies that occasionally lend money to their customers or borrow from their suppliers
  • Highlighting that the institution must receive deposits AND offer loans is important because that is what is expected of commercial banks. They finance a large part of their loans through user deposits. This significant potential single point of failure represents the primary justification for the need for banking regulation.
  • The term “public” emphasizes that these services are provided to the general public. Here, I want to pause and highlight something important to traditional finance. Access to a secure and efficient payment system can be considered a public good in a globalized world. Since commercial banks offer this service, we are talking about a public good that is privately provided. Since users generally do not have the tools to evaluate the security and integrity of financial institutions, the state enters this industry to ensure that the public good is provided most conveniently for citizens (or, at least, that is what the theory indicates. Non-compliance with this is part of the corruption of the institutions and not of the potentially altruistic motives for which this system was designed).

Banks also have a fundamental role in the allocation of capital in the economy, given that the better the financial system functions, the better the life cycle of household consumption occurs, and the more efficiently capital can be allocated for use in the business sector. At this point, we can delve into some points about how the financial sector improves capital allocation and contrast how they currently work in Web3 to understand the differences present:

  1. Offer liquidity and payment services
    Money arises in response to the frictions in transactions and their costs. The fact of exchanging goods in the form of barter ends up generating numerous problems that would be solved if there were a common good for all with a relatively constant value, standardized and divisible enough so that each of the goods and services in an economy can establish a Precise exchange ratio (in case I haven’t been explicit enough, I’m talking about money). Historically, banks have played two critical parts in money management: exchanging money (between different currencies provided by various institutions) and providing payment services.
    In the decentralized world, the roles are different: the security and speed of the payment service are provided by the blockchain (see fat protocol thesis), and DEXes facilitate the exchange. So, the role of money market protocols is limited to designing incentives to attract deposits and provide loans (as we will see later, this work represents a great challenge, given that it is difficult to know the incentives of economic agents in a world where anonymity is the rule and not the exception.).
  2. Transforming assets
    Asset transformation can be seen from three points of view: quality transformation, convenience of denomination, and maturity transformation.
  • Quality transformation
    This occurs when bank deposits offer better risk-return characteristics than direct investments. An example is when there are indivisible investments, small investors cannot diversify their portfolios.
  • Convenience of denomination
    When we talk about this, we refer to banks' ability to collect small deposits as intermediaries and then invest that income in large loans. That is, the bank chooses the denomination of its products (deposits and loans) in a way that is convenient for its clients.
  • Maturity Transformation
    This function can be seen when banks take short-maturity securities offered to depositors and transform them into long-maturity securities desired by borrowers. This function is used at the cost of the banking entity’s liquidity, which does not represent risks unless expectations of a bank run are generated.

Some examples of this in the blockchain ecosystem and future research look promising. In this, we can name cases where it is possible to use Receipt Tokens as collateral, NFTs as collateral, or fragment NFT property.

  1. Managing Risks
    Typically, three sources of risk that affect banks are defined: credit risk, interest rate risk, and liquidity risk. These risks correspond to different lines on the banks’ balance sheets. It is also worth mentioning the risks of off-balance sheet operations. To avoid extending the post, I will not discuss this topic since I believe this section deserves special dedication, but we will not use our time in this aspect for now. Regarding risk management, today, the only sure guarantee in blockchain is the collateral the user deposits. Without an incentive to keep a wallet for a long time and with the existing possibility at 0 costs of a “clean slate”, I think it becomes difficult to create a user profile that can contemplate the possibility of continuity of payment of the users. Consequently, managing the previously mentioned risks is very difficult to estimate, so the only certainty that can be offered is the collateral.
  2. Processing information and monitoring borrowers
    Banks are responsible for collecting data on loan applicants to improve the calculation of the values provided for these applicants. Collecting and processing user data reduces the problem of imperfect information between parties. It allows values to be calculated based on long-term relationships (and uncertain ones, given that at the end of the day, even if it is exact, it is still an estimate). For this reason, bank loans and values in financial markets are different: bonds reflect the value of market information, while loans, as we already mentioned, seek to establish a long-term relationship, considering the user’s profile to calculate the maximum value of that relationship. The same thing I said in the previous point (managing risks) would apply in this case. Since there are no incentives for using a particular address, we cannot create this long-term relationship since the link is maintained only with collateral in the protocol.

Getting into the particular project

We have already seen differences between traditional money markets and those found on web3. Now, we will begin to describe our project. We will seek to create a decentralized bank using the existing money market protocols, explaining the step by step of each of the decisions that will be made.
First, let’s define our smart contract: It will receive deposits in DAI and deliver loans in USDC. The contract will act as an intermediary between both parties (user and money market protocol), redirecting the funds to the corresponding party. If we make our contract work this way, we will respect the definition of bank that we mentioned at the beginning of this post.
The funds (directed towards the protocol as a deposit or payment of debt or towards users as a withdrawal of deposits or loans received) will always go through our smart contract to have an internal count and not depend on the protocol’s balances. While this is a step back in the path of decentralization, if we rely on the balance functions of AAVE or Compound (which we will use), there may be discrepancies in the sense that a user could interact with our bank or could do so directly with the money market (we cannot assume that the user will interact with us in every case). On the other hand, we will use stablecoins because running a bank with non-stable tokens could generate incentives for financially irresponsible users to leverage the financial health of other users: since it is a smart contract that manages all the users’ funds, there could be some who take advantage of the liquidity of the contract to take riskier positions, given that the LTV is calculated on the total value of the assets stored in the contract and not on the individual position (we could later apply the division of the LTV between users, but it will escape of this series of posts).

Summary

This has been a brief theoretical introduction to the world of banks, briefly describing the characteristics of these institutions and explaining, in some way, why they are fundamental in our current world. Also, I gave some opinions on how these characteristics change when we consider the blockchain ecosystem. Finally, I made a brief high-level description that will govern the reasoning of everything we will describe in the second part of this post.

--

--