Hello everyone and welcome to the CavalRe! ๐ค
In this article, I present a primer on discrete calculus with the intention of helping us analyze DeFi protocols running on blockchains, which are inherently discrete in nature.
Blockchain as a State Machineโ
The Ethereum yellow paper describes the Ethereum blockchain as a state machine with the state of the world denoted by . Given a transaction , the state is updated to a new state by the Ethereum state transition function denoted by
It is important to note the inherent discreteness of this process. There is no state between and A transaction takes you from some initial state to some final state with nothing in between and repeats indefinitely with every new transaction.
This discreteness has implications for how we should analyze blockchain protocols such as automated market makers (AMMs) so we will enhance our toolbox for dealing with this discreteness in the remainder of this article.
Node Functionsโ
Every blockchain needs to start with some initial state created by some kind of genesis event. Therefore, it makes sense to restrict to be a natural number with the initial state denoted A node function is simply a function
where denotes real numbers. Let denote a special function whose value is given by
Any node function can be expressed as a linear combination of these functions, i.e.
where is the value of at . The basis functions can be multiplied
which extends to the product of arbitrary node functions intuitively as
The product of two node functions is commutative, i.e.
Edge Functionsโ
The natural numbers can be thought of as a simple directed graph. In the context of blockchain, the state would then reside on the node whereas the transaction would reside on the directed edge between states and as illustrated below.
Letting denote the set of edges between natural numbers when thinking of as a directed graph, define functions by
We can then define a general discrete edge function as a linear combination
where is the value of on the edge
Multiplication of an edge function by a node function on the left can be defined in terms of basis functions via
In other words, the product is zero unless the node coincides with the beginning of the edge. Similarly, multiplication of an edge function by a node function on the right can be defined in terms of basis functions via
In other words, the product is zero unless the node coincides with the end of the edge. In general we have
and
so that the product of node functions and edge functions is noncommutative, i.e.
Discrete Differentialsโ
There is a special "unit" node function
that satisfies and Similarly, there is a special "graph" edge function
With the graph edge function, we can define the differential of a node function via
where denotes the commutator, i.e.
with
It follows from the properties of the commutator that
which gives rise to the discrete product rule
Although the product of node functions on the left-hand side is commutative, the products on the right-hand side are noncommutative so the order they are written matters. We could have also written the discrete product rule as
This means that when dealing with discrete node and edge functions, as we must with blockchain protocols, we have a degree of freedom in how we decompose terms in the discrete product rule.
Let us introduce a new general notation
where and we have
When the product is commutative, as it is with node functions we have
and it is straightforward to show that the general expression for the discrete product rule is given by
For numerical implementation, we'll need to expand the above into components and we get:
and
where
so that the discrete product rule may be expressed in components as
The value of has no impact on the sum. It merely impacts the way the discrete product rule decomposes into the two terms. As we will see in subsequent articles, this degree of freedom stemming from the inherent discreteness of a blockchain has important implications for AMM design.
Closer look ๐ง: Note on Symmetries
One kind of symmetry was already noted above, namely
There is another related kind of symmetry given by
and
In other words, if the direction of time is reversed so that and are swapped, then we can simply replace with in the discrete product rule. This means that:
- The discrete product rule for is equivalent to the discrete product rule for with time reversed;
- The discrete product rule for is equivalent to the discrete product rule for with time reversed; and
- The discrete product rule for is the same whether forward in time or time reversed.
We will see this again when looking at self financing in DeFi protocols.