# What is self financing anyway?

Hello everyone and welcome to the CavalRe! 🤠

In this article, we examine the concept of self financing. What it is and why it is important in the design and analysis of DeFi protocols.

So... what is self financing anyway?

Self financing is a lot like a conservation law of physics. Just as energy cannot be created or destroyed and can only be transformed, a transaction is self financing if no value is created or destroyed during the transaction and value is only transferred.

In the quant literature, e.g. the derivation of the Black-Scholes equation, self financing is such a natural and obvious constraint it is often mentioned in passing (if mentioned at all) by noting that the change in value of a portfolio consisting of $\alpha_i$ units of security $i$ with price $P_i$ is given by

$dV = \sum_{i=1}^n \alpha_i d P_i.$

To distill this a bit, step back and write the value of the portfolio as

$V = \sum_{i=1}^n \alpha_i P_i.$

Taking the differential of this and applying the product rule gives

$dV = \sum_{i=1}^n (d \alpha_i) P_i + \sum_{i=1}^n \alpha_i dP_i.$

We see the change in value of the portfolio can be decomposed into a change that is due to trading activity, i.e.

$dV_{\text{trading}} = \sum_{i=1}^n (d \alpha_i) P_i,$

and a change that is due to market movements, i.e.

$dV_{\text{market}} = \sum_{i=1}^n \alpha_i dP_i.$

Comparing this with the previous expression, we see that another way of saying a portfolio is self financing is to say that the change in value due to trading activity is zero, i.e.

$\sum_{i=1}^n (d \alpha_i) P_i = 0.$

Although requiring transactions to be self financing might seem intuitively obvious (because it is!), the unfortunate fact is that most DeFi protocols out there today are not self financing and this has implications.

## Self Financing in DeFi​

Self financing in DeFi is essentially the same an in TradFi except, as mentioned in our previous article, when analyzing DeFi protocols, the inherent discreteness of the blockchain becomes important. We need to use analogous tools from discrete calculus to define what self financing means in the context of discrete blockchains.

The quantities and prices of tokens in a portfolio can be expressed as node functions

$\alpha_i = \sum_{t\in\N} \alpha_{i,t} \mathbf{e}^t\quad\text{and}\quad P_i = \sum_{t\in\N} P_{i,t} \mathbf{e}^t$

where the components $\alpha_{i,t}, P_{i,t}$ represent the number and price, respectively, of token $i$ in the state $t\in\N.$ The total value of a portfolio is then given by

$V = \sum_{i=1}^n \alpha_i P_i\implies V_t = \sum_{i=1}^n \alpha_{i,t} P_{i,t}.$

The change in value can then be expressed using the discrete product rule as

$(\Delta V)_{t,t+1} = \sum_{i=1}^n (\Delta\alpha_i)_{t,t+1} (E_{k_i} P_i)_{t,t+1} + \sum_{i=1}^n (E_{1-k_i} \alpha_i)_{t,t+1} (\Delta P_i)_{t,t+1},$

recalling that

$(\Delta f)_{t,t+1} = f_{t+1} - f_t$

and

$(E_k f)_{t,t+1} = (1-k) f_t + k f_{t+1}.$

In complete analogy to the continuum TradFi case, the discrete change in value can be decomposed into

$(\Delta V_\text{trading})_{t,t+1} = \sum_{i=1}^n (\Delta\alpha_i)_{t,t+1} (E_{k_i} P_i)_{t,t+1},$

which is the change in value due to trading activity and

$(\Delta V_\text{market})_{t,t+1} = \sum_{i=1}^n (E_{1-k_i} \alpha_i)_{t,t+1} (\Delta P_i)_{t,t+1},$

which is the change in value due to market movements.

We can now state that a portfolio is self financing on a blockchain if the change in value due to trading activity is zero, i.e. if

$\sum_{i=1}^n (\Delta\alpha_i)_{t,t+1} (E_{k_i} P_i)_{t,t+1} = 0$

or, equivalently,

$(\Delta V)_{t,t+1} = \sum_{i=1}^n (E_{1-k_i} \alpha_i)_{t,t+1} (\Delta P_i)_{t,t+1}.$

## Effective Prices​

Examining the expression for the change in value due to trading activity, we can interpret the term

$(E_{k_i} P_i)_{t,t+1} = (1-k_i) (P_i)_t + k_i (P_i)_{t+1}$

as the effective price used for determining the value paid for / received from exchanging $(\Delta\alpha_i)_{t,t+1}$ tokens. This means that

$(E_0 P_i)_{t,t+1} = (P_i)_t$

corresponds to transactions occurring at the initial (stale) price determined in the previous block with no price slippage. Similarly,

$(E_1 P_i)_{t,t+1} = (P_i)_{t,t+1}$

corresponds to transactions occurring at the terminal price only known in the next block, which depends on the size of the transaction and includes maximum price slippage. Finally,

$(E_{1/2} P_i)_{t,t+1} = 1/2 (P_i)_t + 1/2 (P_i)_{t+1}$

corresponds to transactions occurring at the exact midpoint between the initial and terminal prices.

So, what started as a purely mathematical consequence of the inherent discreteness of blockchain provides an intuitive financial interpretation of self financing with implications for the design, analysis and implementation of DeFi protocols.