<!DOCTYPE article PUBLIC "-//NLM//DTD JATS (Z39.96) Journal Archiving and Interchange DTD v1.0 20120330//EN" "JATS-archivearticle1.dtd">
<article xmlns:xlink="http://www.w3.org/1999/xlink">
  <front>
    <journal-meta>
      <journal-title-group>
        <journal-title>DLT</journal-title>
      </journal-title-group>
    </journal-meta>
    <article-meta>
      <title-group>
        <article-title>A Netting Protocol for Liquidity-saving Automated Market Makers</article-title>
      </title-group>
      <contrib-group>
        <contrib contrib-type="author">
          <string-name>Margherita Renieri</string-name>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Letterio Galletta</string-name>
          <xref ref-type="aff" rid="aff1">1</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>Alberto Lluch Lafuente</string-name>
          <xref ref-type="aff" rid="aff2">2</xref>
        </contrib>
        <contrib contrib-type="author">
          <string-name>James Hsin-yu Chiang</string-name>
          <xref ref-type="aff" rid="aff0">0</xref>
        </contrib>
        <aff id="aff0">
          <label>0</label>
          <institution>Aarhus University</institution>
          ,
          <country country="DK">Denmark</country>
        </aff>
        <aff id="aff1">
          <label>1</label>
          <institution>IMT School for Advanced Studies Lucca</institution>
          ,
          <country country="IT">Italy</country>
        </aff>
        <aff id="aff2">
          <label>2</label>
          <institution>Technical University of Denmark</institution>
          ,
          <country country="DK">Denmark</country>
        </aff>
      </contrib-group>
      <pub-date>
        <year>2024</year>
      </pub-date>
      <volume>6</volume>
      <fpage>14</fpage>
      <lpage>15</lpage>
      <abstract>
        <p>Automated Market Makers are one of the most used Decentralized Finance services. They allow users to exchange crypto-assets without a third party. Current protocols have strong constraints related to the liquidity level that users' balances must satisfy for each transaction. In this paper, we propose a liquidity-saving mechanism that aims at reducing the required amount of liquidity in an AMM service. We provide an operational semantics of such a mechanism that precisely characterizes the interactions between users and AMMs and the conditions when the liquidity-saving mechanism is triggered. Our mechanism collects the proposed transactions in a finite queue, providing a global perspective of all users' actions. Starting from the queue, it finds a feasible transaction sequence that satisfies the users' balances. Finally, it performs these transactions on the blockchain atomically, reaching a state where all liquidity constraints are met. By doing so, the mechanism allows for novel liquidity saving behavior for multi-party exchange and multi-AMM arbitrage with less upfront liquidity as usually required.</p>
      </abstract>
      <kwd-group>
        <kwd>eol&gt;Formal methods</kwd>
        <kwd>Operational semantics</kwd>
        <kwd>Liquidity-Saving Mechanism</kwd>
        <kwd>Netting</kwd>
        <kwd>AMMs</kwd>
      </kwd-group>
    </article-meta>
  </front>
  <body>
    <sec id="sec-1">
      <title>1. Introduction</title>
      <p>In Decentralized Finance (DeFi) systems, Automated Market Makers (AMMs) are decentralized
exchanges (DEXs) that provide users with liquidity using diferent reserves, enabling them to trade
diferent crypto-assets, i.e., tokens. Users participating in these token exchanges pay fixed trading and
gas fees charged by the Ethereum network. Similarly to traditional financial systems, users need to
provide an upfront amount of crypto-assets as collateral to operate with such decentralized financial
services. Liquidity is essential for AMMs to work properly, otherwise, it is possible to incur scenarios
that hinder their economic performance and limit user involvement.</p>
      <p>
        AMMs rely on users who act as liquidity providers by depositing a certain amount of tokens into AMM
smart contracts that can be used for trades. AMM protocols implement liquidity-saving mechanisms
(LSM) that aim at reducing liquidity costs by minimizing the required liquidity for transaction settlement.
DeFi protocols have introduced various approaches to achieve this goal, e.g., flash loans [
        <xref ref-type="bibr" rid="ref1 ref2">1, 2</xref>
        ] and flash
swaps [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ]. These mechanisms allow users to temporarily loan tokens with no collateral but require
them to pay back their debt within a single transaction. One of their limitations is that they do not
easily permit scenarios with more users involved or trades requiring more than one transaction to be
completed.
      </p>
      <p>
        In this paper, we propose the design of an LSM for AMMs that ofers the advantages of flash loans
and flash swaps but overcomes their limitations. In particular, our mechanism permits users to operate
on the AMM without upfront balance by aggregating multiple actions over a specific time frame. Our
aggregation mechanism is inspired by netting [
        <xref ref-type="bibr" rid="ref4 ref5 ref6">4, 5, 6</xref>
        ], an LSM used in traditional finance: netting creates
payment instruction queues among banks, ofsets the value of multiple payments, and finds the most
convenient payment combination to eliminate stunting periods. We provide here a formalization of our
mechanism in terms of a labelled transition system (LTS) that precisely characterizes the interactions
between users and AMMs and the conditions when the netting mechanism is triggered.
      </p>
      <p>Intuitively, our mechanism works as follows. First, we equip AMMs with a finite queue to store
trading transactions (swaps) proposed by users. When a user performs a transaction that would result
in a balance overdraft, the transaction is stored in the queue. Otherwise, if the transaction has the efect
of setting all balances of users having pending transactions in the queue to positive, the transaction
queue is executed atomically, resulting in a liquidity-saving sequence of swaps. If the queue reaches its
maximum capacity where some users’ balances are still negative, a netting algorithm selects a subset of
the stored transactions that meet the liquidity constraints for each user, if any, and atomically performs
them. In such a way, users capitalize on market ineficiencies, facilitating the trading of assets more
eficiently and cost-efectively. We discuss the scenarios where our proposed netting-based AMM
and traditional AMM difer in the performed swap transactions. In particular, we show examples of
multi-party trading operations that are not executed in standard AMMs due to lack of liquidity but are
settled by ours.</p>
      <p>
        In summary, our main contributions are:
• We provide an LSM that settles AMM trades through a netting algorithm and allows us to
implement liquidity-optimized AMM. We illustrate examples that allow users to perform
multiparty exchanges and multi-AMM arbitrage without the upfront liquidity that would be required
in traditional AMMs.
• We formalize our mechanism by adopting the operational semantics of the interactions between
users and AMMs proposed by Bartoletti et al. [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ]. We adapt their semantics by removing the
liquidity constraints on users’ balances when performing swap actions and by appending swap
transactions in a queue until a liquidity-balanced state is reached.
• We provide a lightweight netting algorithm that can run on the blockchain as part of a smart
contract to select a subset of swaps that can actually be performed without violating any liquidity
constraints.
      </p>
      <p>In the rest of the paper we proceed as follows. We provide a high-level overview of our approach in
Section 2. Section 3 presents the operational semantics of our LSM mechanism, together with relevant
scenarios comparing it with standard AMMs. Section 4 discusses related literature, and Section 5
concludes and discusses some future work.</p>
    </sec>
    <sec id="sec-2">
      <title>2. Overview of the approach</title>
      <p>
        In current DeFi protocols, such as Uniswap [
        <xref ref-type="bibr" rid="ref3">3</xref>
        ], a user interacts with the AMM reserve through single
actions. She can deposit, redeem, or swap crypto-assets in change of others in the AMM. However,
she can perform the transaction only if she has the needed capital upfront to cover it. For instance,
consider the example of Figure 1 where there are two users A and B that interact with two AMMs
performing a sequence of swap actions 1234. Initially, user A owns 4 tokens of type  2, no tokens
of types  0 and  1 (denoted with the notation A[0 :  0, 0 :  1, 4 :  2] in the figure); whereas user B has
4 tokens of type  0, no tokens of types  1 and  2. The AMMs have 12 tokens type of  0,  1 and  1,  2,
respectively (denoted with {12 :  0, 12 :  1} and {12 :  1, 12 :  2} in the figure). The main idea is that
the sequence of transactions would allow A and B to exchange their tokens at 1-to-1 rate, via the two
AMMs. In standard protocols, some transactions are rejected because users’ balances cannot cover
their fulfillment: A cannot perform 1, namely swapping 6 tokens for at least 4 tokens of type  0, in
symbols 1 = A : swap(6 :  1, 4 :  0), and B cannot perform 2, in symbols 2 = B : swap(6 :  1, 4 :  2).
Our LSM overcomes the previous scenario. It allows users to instantaneously perform transactions
whenever they lead to a state where the overall balance is positive. Otherwise, these transactions are
delayed and stored in a queue. Once the queue is full, the protocol executes a netting procedure to
discard the transactions that make balances negative. Thus, the protocol reaches a state where no
liquidity constraints are violated, and transactions can be safely performed. The underlying idea of
our mechanism is to accept a momentary deficit in users’ balances as long as they may be covered
by subsequent transactions, for instance, a swap in a diferent direction made by the same user or an
update to the token reserve. Back to the example of Figure 1, our approach allows the sequence of the
actions 1234 (for each action in the figure, we report how it afects the user balances and AMM
reserves) to be executed since the 4 yields a state where all balances are positive. It is not relevant if
there are intermediate states where user balances are temporarily negative (colored in the figure). In
this way, our LSM permits a multi-party trade between A and B, even when they do not have suficient
funds. In contrast, traditional AMMs reject all four swap actions due to lack of liquidity.
      </p>
    </sec>
    <sec id="sec-3">
      <title>3. Liquidity-saving mechanism</title>
      <p>This section formalizes our LSM, illustrates our netting algorithm, and shows how our approach difers
from the standard one through some examples.</p>
      <sec id="sec-3-1">
        <title>3.1. Formal model of Automated Market Makers</title>
        <p>
          We formalize the interaction between users and AMMs as a labelled transition system (LTS). Our
semantics is based on those of Bartoletti et al. [
          <xref ref-type="bibr" rid="ref7">7</xref>
          ]. Intuitively, LTS states represent the system configurations
that store each user’s token supplies and the AMMs. Transitions represent transactions performed by
users, which may result in an update of token supplies. For the time being, we focus only on swap
actions and neglect redeem and deposit. We also assume that swap actions require no fees and that
there are no transaction fees.
        </p>
        <sec id="sec-3-1-1">
          <title>3.1.1. AMM basic definitions</title>
          <p>We assume a set T of atomic token types (ranged over by ,  ′) representing native cryptocurrencies
and application-specific tokens. In general, we denote with , ′ real numbers (R), and we write  :  to
denote  atomic tokens of type  ( ∈ T).</p>
          <p>We also assume a set of users A (ranged over by A, A′). The wallet of a user A is denoted by A[ A],
where the partial map  A ∈ T ⇀ R represents A token balances, i.e.,  A( ) denotes the amount of 
owned by A.</p>
          <p>We model the AMM as a reserve of 0 :  0 and 1 :  1 where  0 ̸=  1, written as an unordered pair
{0 :  0, 1 :  1}.</p>
          <p>The states Γ, Γ′ of the protocol are finite non-empty compositions of wallets and AMM. Formally,
they are defined as follows:
Γ = [A1[ A1 ]| · · · |</p>
          <p>A[ A ] | {0 :  0, 1 :  1} | · · · | {  :  ,  :  }]
(1)
({ ,  ′} ̸= {  ,  ′ }).</p>
          <p>The labels of LTS are swap transactions that are terms of the form
and for the sake of simplicity of our formalization, we assume the following conditions: for all  ̸=  ()
each user has a single wallet (A ̸= A ); () distinct AMMs cannot hold exactly the same token types</p>
          <p>
            A : swap( :  0, * :  1)
meaning that user A transfers  :  0 to the AMM {0 :  0, 1 :  1} specifying that she wants to
receive back at least * units of token  1 in return. The amount of token * must meet the constraint
0 &lt; * ≤  where  =  · (, 0, 1) (see below for ) to preserve the constant-product swap rate
of the reserve. In our mechanism, * represents a minimum acceptable threshold for the amount of
token a user expects to receive from an exchange with an AMM reserve. A transaction will be rejected
if the swap rate does not ensure the user receives at least this amount. Users submit their actions
without knowing whether they will be executed at the resulting swap rate. In our formalization, we
adopt the Constant Product Market Maker (CPMM) which is one of the most popular forms of DEX that
considers a trade valid if the value of the reserve before and after (with an additional amount for fees)
is constant or simply the same. More precisely, we consider the constant product swap rate that is the
most commonly implemented in Uniswap [
            <xref ref-type="bibr" rid="ref8">8</xref>
            ], SushiSwap [9], and Curve [10] protocols. We use the
swap rate function in all instances throughout this paper. In particular, when a user swaps a token  0
with an AMM {0 :  0, 1 :  1}, the actual amount  of  1 is calculated using the following formula:
(, 0, 1) =
          </p>
          <p>1
(0 + )
(2)
(3)
(4)
(5)
This rate ensures that the evolution and the update of an AMM from {0 :  0, 1 :  1} to {0 +  :
 0, 1 −  :  1} preserve the ratio between the reserve maintaining a constant level of tokens:
1
0 · 1 = (0 + ) · (1 −  · 0 +  ) = 0 · 1
The formula above is derived by substituting the definition of (, 0, 1) from the equation (3)
in place of , then, by performing some algebraic simplification to obtain again 0 · 1. The actions
that result in overdrafts on users’ balances are not immediately executed but are stored in a queue Λ.
Formally, a queue is a term obtained by the following grammar:</p>
          <p>Λ := ∅ | Λ ∘ 
where ∅ denotes an empty queue with no pending actions, and Λ ∘  a queue Λ where its head is
the action . In the semantic rules below, we use Λ ∘  to represent when an action  is added to
the queue Λ. Moreover, we write |Λ| to indicate the size of the queue Λ. For instance, ∅ is an empty
queue with no pending action; while the term ∅ ∘ 1 denotes a queue with a single action 1 that is
equal to A : swap(6 :  1, 4 :  0); and the term ∅ ∘ 1 ∘ 2 denotes a queue with two actions: the tail is
1 = A : swap(6 :  1, 4 :  0) and the head is 2 that corresponds to B : swap(4 :  2, 6 :  1). The LTS
states are configurations defined as a 3-tuple ⟨Γ, Γ′, Λ⟩ where the first component is a state Γ of the form
(1), the second one Γ′ is the last simulated state (see the next subsection), and the third one is a swap
action queue Λ of the form (5) of size ℓ. Given a state Γ we define ⟨Γ, Γ, ∅⟩ as an initial configuration .</p>
        </sec>
        <sec id="sec-3-1-2">
          <title>3.1.2. AMM semantics</title>
          <p>submits a transaction , and are defined by the inference rules below.</p>
          <p>Transitions ⟨Γ, Γ′, Λ→⟩− ⟨</p>
          <p>Γ′′, Γ′′′, Λ′⟩ from a configuration to another one are triggered when a user</p>
          <p>It is convenient to introduce some notation for the semantics. We say that a state Γ is green when
the token supply of each user is non-negative, formally:</p>
          <p>∀ A ∈ A ∩ Γ,  ∈ T, s.t.  A( ) ≥ 0
we have three possible scenarios.
apply the following rule:
otherwise, namely if
where we denote with A ∩ Γ the set of users occurring in the configuration Γ. While we say it is red
∃ A ∈ A ∩ Γ,  ∈ T, s.t.  A( ) &lt; 0</p>
          <p>
            Moreover, we denote with Γ =⇒ Γ′ a simulation of the transaction  in the state Γ using the semantic
rules of [
            <xref ref-type="bibr" rid="ref7">7</xref>
            ] without constraints on users’ balances. This is why we want to maximize the number of
settled actions performed by users. Diferently, we maintain checks to impose non-negativity constraints
on the AMM reserve. This means that some user balances could be negative in the resulting state Γ′
but not AMM reserves. Given a sequence of transactions Λ, we denote with Γ =Λ⇒ Γ′ the simulation of
the actions in Λ. Also, we assume that when Λ is the empty sequence  , the simulation results in an
unchanged state, i.e., Γ =⇒ Γ. When a transition is triggered by an action  in a configuration ⟨Γ, Γ′, Λ⟩,
The first scenario arises when the simulation of  in Γ′ reaches a green state Γ′′. In this case, we
Γ′ =⇒ Γ′′
          </p>
          <p>Γ′′green

⟨Γ, Γ′, Λ⟩→− ⟨ Γ′′, Γ′′, ∅⟩</p>
          <p>Cover
The rule Cover performs all the pending actions in Λ (if any), and the resulting configuration consists
of the green state, Γ′′ (in the first two components), and the emptied queue.</p>
          <p>The second scenario happens when the simulation of  in Γ′ reaches a red state Γ′′, and the length
of Λ is less than ℓ (max queue size, a parameter of our mechanism). In this case, we apply the following
rule:
Γ′ =⇒ Γ′′
Γ′′red
|Λ| &lt; ℓ</p>
          <p>Λ′ = Λ ∘ 

⟨Γ, Γ′, Λ⟩→− ⟨ Γ, Γ′′, Λ′⟩</p>
          <p>Overdraft
The rule Overdraft enqueues  in Λ, thus, the resulting configuration consists of the current state Γ,
the red state Γ′, and Λ′ that is Λ extended with the incoming action .</p>
          <p>The last scenario occurs when the simulation of  leads to a red state Γ′′, and the length of Λ equals
ℓ. In this case, we use the netting procedure (denoted by the function  in the following rule) to
perform the settlement from the state Γ and the actions of Λ plus .</p>
          <p>The netting procedure identifies and returns a (possibly empty) sequence of feasible actions Λ′. Such
sequence is a subset of the input sequence Λ. The obtained subsequence Λ′ is executed to obtain the
resulting state Γ* . All the other actions of Λ that are not selected by the procedure are discarded.</p>
          <p>The resulting configuration is a 3-tuple with the state
Γ* (for the first two components) and the
empty queue for the last one (the queued actions were carried out or discarded). Formally, we apply the
following rule:
Γ′ =⇒ Γ′′ Γ′′red
|Λ| = ℓ</p>
          <p>Λ′ = net (Γ, Λ ∘ ) Γ =Λ⇒′ Γ*

⟨Γ, Γ′, Λ⟩→− ⟨ Γ* , Γ* , ∅⟩</p>
          <p>Netting
It is worth remarking that the mechanism provided in this section is agnostic with respect to the netting
procedure that we invoke in a black-box manner. For example, a simple procedure could be to discard
all enqueued transactions. The next section introduces a more meaningful procedure. Also, we remark
that when we apply the above rule, the queue Λ contains a prefix of actions leading to a red state that
is not balanced by other actions in the queue. Otherwise, the rule Cover would be applicable. The rule
invokes the netting procedure to execute a subset of actions that allows the AMM to progress.</p>
          <p>Finally, note that our configurations and semantic rules could be written without Γ′ (the 2nd
component of the configuration recording the simulated/speculative final state) and replacing the premise
Γ′ =⇒ Γ′′ with Γ =Λ=∘⇒ Γ′′. This approach requires re-computing the final state every time a new action
is performed. These re-computations are not eficient in an implementation where it is more convenient
to store the intermediate state. We decided to follow this approach to make our formalization coherent
with the implementation.</p>
        </sec>
      </sec>
      <sec id="sec-3-2">
        <title>3.2. Netting problem</title>
        <p>Here, we provide an algorithm for the netting procedure  invoked in Netting rule above. The
netting problem is defined as follows: Given as inputs a state Γ, a queue Λ, find a subset Λ′ of Λ, whose
actions lead to a green state Γ* from Γ. Recall that the liquidity constraints required to each user are
satisfied in a green state. Ideally, netting aims at finding an optimal solution, maximizing a specific
parameter. For example, one may want to maximize the size of Λ′, the number of users afected, or the
amount of tokens involved. We formalize the netting as an optimization problem that, given as inputs a
state Γ of the LTS and a queue Λ, maximizes the size of Λ′, a queue, whose actions bring to a valid state
Γ* that meet the liquidity constraints:</p>
        <p>
          max |Λ′|
subject to
Γ →−Λ′ Γ*
Γ* green
(6)
(7)
(8)
Note that the liquidity constraints (8) are satisfied if the final state Γ* is a green (there is no overdraft
in users’ wallets). The problem in financial literature is known as bank payment clearance, which is
NP-hard [
          <xref ref-type="bibr" rid="ref4">4, 11</xref>
          ], so we adopt a heuristic algorithm to implement the netting procedure that avoids
enumerations and tries to maximize the number of transactions performed.
        </p>
        <p>Since we need an algorithm that can run on a smart contract (thus incurring afordable gas expenses),
we adopt a heuristic approach that sacrifices optimality for eficiency, and we propose Algorithm 1 that
runs in polynomial time (quadratic in the size of the queue, at worst).</p>
        <p>Algorithm 1 Heuristic netting procedure implementation
Input: Λ = [1, 2, . . . , ], Γ
Output: Λ
Initialization: Λ ← Λ, Γ0 ← Γ
1: Γ0 =Λ=⇒ Γ
6:
2: while Γ red ∧ Λ ̸= ∅ do
3: select   s.t.  ∈ Λ where  A( ) &lt; 0
4:
5:
Λ ← Λ − 
Γ ← Γ− 1
Γ ===⇒ Γ+1 ==+=⇒2 Γ+2 · · ·
+1</p>
        <p>=⇒ Γ
7: Γ ← Γ
8: end while
9: return Λ
◁ Starting simulation
◁ Final state has overdraft
◁ Select action that overdraft</p>
        <p>◁ Update actions queue
◁ Update last green state
◁ Update the variable Γ</p>
        <p>Intuitively, Algorithm 1 simulates the swap actions ignoring the liquidity constraints. It starts by
initializing Λ with Λ, the initial queue of pending actions, and Γ0 with Γ, the initial state.</p>
        <p>It then starts a loop (line 2), where it simulates action execution until either the final state Γ is not
red or the queue Λ becomes empty, namely, until there are still actions to be processed and overdrafts
in the system. During each iteration, we select from Λ the first action  that makes the balance of
some account A become negative, i.e., the execution of  leads Γ to red state (line 3). Then, the
algorithm removes  from Λ (line 4) and updates the simulation state Γ (line 5) by reverting . We
achieve that by simply considering the previous state Γ− 1 that is the last green state. After we recover
to the last green state, the simulation is run again but from Γ using the actions following  until all
balances are non-negative. As a result of this last simulation, we obtain the green state Γ (line 6).
Finally, Γ is updated with the final state Γ (line 7) and the loop starts again.</p>
        <p>These steps are iterated until we obtain a green state or empty Λ. When this happens, the algorithm
returns Λ. In the following section, we provide an example of execution of Algorithm 1.</p>
      </sec>
      <sec id="sec-3-3">
        <title>3.3. Liquidity-saving behavior</title>
        <p>Our mechanism enables liquidity-saving behavior that is not possible in ordinary AMMs. An example is
the simultaneous change of tokens through AMMs that we presented in Section 2. Another interesting
behavior enabled by our mechanism is the ability to perform arbitrage on multiple AMMs simultaneously
and with no liquidity.</p>
        <sec id="sec-3-3-1">
          <title>3.3.1. Liquidity-saving arbitrage Example</title>
          <p>Assume to have three AMMs with the following token reserves</p>
          <p>{8 :  0, 18 :  1}, {8 :  1, 18 :  2}, {8 :  2, 18 :  0}
If we assume, a 1-to-1 exchange rate, we could see three arbitrage opportunities, only available to users
with suficient funds. Now suppose that a user A has no tokens but wants to perform the following
actions 123:</p>
          <p>A : swap(4 :  0, 6 :  1), A : swap(4 :  1, 6 :  2), A : swap(4 :  2, 6 :  0)
When considering AMMs without our mechanism, all the actions are discarded because A does not have
an upfront balance. Figure 2 shows a flow diagram of the actions performed adopting our mechanism.
Now 12 are enqueued because they cause an overdraft that is then covered by 3. The execution of 3
results in the following green state:</p>
          <p>
            [A[2 :  0, 2 :  1, 2 :  2]|{12 :  0, 12 :  1}|{12 :  1, 12 :  2}|{12 :  2, 12 :  0}]
The above scenario has similarities with the traditional finance scenario known as gridlock [
            <xref ref-type="bibr" rid="ref6">6</xref>
            ] where
banks cannot settle their payments individually due to their insuficient liquidity. Through netting,
each bank submits its payment instructions to designated queues, performing multilateral settlement
exclusively for the net obligations. Back to our example, user A overcomes the stuck state with possible
no evolution of the system of traditional AMMs protocol where individual swap incurs in an overdraft,
enqueuing her actions and atomically perform them when reaching a positive balance.
          </p>
          <p>The approach we developed may settle transactions on the blockchain diferently than the ones used
by standard AMM protocols: netting can select transactions that the standard approach discards and
vice versa. This diference may afect users’ rewards and lead them to apply new/diferent market
strategies. The following scenario exemplifies a case in which the netting of transactions could prevent
swaps that would otherwise be executed.</p>
        </sec>
        <sec id="sec-3-3-2">
          <title>3.3.2. Two Users scenario Example</title>
          <p>Consider a scenario where there are two users A and B and two AMMs with the following wallets and
reserves:
[A[0 :  0, 0 :  1, 4 :  2]|B[4 :  0, 6 :  1, 0 :  2]|{12 :  0, 12 :  1}|{18 :  1, 8 :  2}]</p>
          <p>User A
[0 :  0, 0 :  1, 0 :  2]
{8 :  0, 18 :  1}
{8 :  1, 18 :  2}</p>
          <p>Assume that user A wants to perform two actions. The first one, 1 = A : swap(6 :  1, 4 :  0), on the
ifrst AMM, {12 :  0, 12 :  1}, and the second one, 2 = A : swap(4 :  2, 6 :  1) on the second AMM,
{18 :  1, 8 :  2}. User B wants to perform an action 3 = B : swap(6 :  1, 4 :  0) on the first AMM.</p>
          <p>When considering an AMM without our mechanism, 1 and 2 are discarded because A does not
have enough balance, whereas 3 is performed reaching the configuration:</p>
          <p>[A[0 :  0, 0 :  1, 4 :  2]|B[8 :  0, 0 :  1, 0 :  2]|{8 :  0, 18 :  1}|{18 :  1, 8 :  2}]
Diferently with our mechanism 1 is enqueued, and when A performs 2 on the second AMM, both
actions are settled because 2 covers the overdraft on A’s wallet (see Figure 3). While 3 is not executed
on the first AMM because the execution of the previous actions changes the ratio in the reserve, and
hence the swap rate. Once 12 are performed, the ratio of the first AMM is updated and 3 is discarded
(as the red and crossed box denotes in Figure 3) because user B can swap 6 tokens of  1 with 2 tokens
of  0 that is a diferent swap amount compared to what she wants to perform ( 6 tokens of  1 with 4
tokens of  0).</p>
          <p>This example highlights that the standard mechanism and ours generally settle diferent transactions,
so they are incomparable. However, the behavior is not uncommon from what happens in ordinary
AMMs where a user (in this case B) would typically decide to trade at a swap rate based on his local
view or speculation on the state of AMMs, which can of course change if transactions of other users (in
this case A) are executed.</p>
        </sec>
        <sec id="sec-3-3-3">
          <title>3.3.3. Netting scenario Example</title>
          <p>Consider a scenario where there is user A and three AMMs with the following wallet and reserves:
[A[0 :  0, 0 :  1, 4 :  2]|{12 :  0, 12 :  1}|{18 :  1, 8 :  2}|{12 :  2, 12 :  0}]
Assume user A wants to execute three actions sequentially: 1, 2, and 3. With 1 user A swaps 6 tokens
of type  2 for 4 tokens of type  0 on the third AMM, in symbols 1 = A : swap(6 :  2, 4 :  0). With 2 user
A swaps 6 tokens of type  1 for 4 tokens of type  0 on the first AMM, namely 2 = A : swap(6 :  1, 4 :  0).
Finally, with 3 user A exchanges 4 tokens of type  2 for 6 tokens of type  1 on the second AMM:
3 = A : swap(4 :  2, 6 :  1).</p>
          <p>User A
[0 :  0, 0 :  1, 4 :  2]</p>
          <p>AMM1</p>
          <p>AMM2</p>
          <p>AMM3</p>
          <p>Upon executing the three actions illustrated in Figure 4, the system achieves a red state where the
user’s wallet fails to meet liquidity constraints. Consequently, our mechanism triggers the Netting rule
and runs the Algorithm 1. During this process, 1 is identified as the first action leading to an overdraft
in A’s wallet (see line 3 of Algorithm 1), and is thus removed from the queue. After this adjustment, the
simulation is run again, resulting in a green state, achieved through the execution of 2 followed by 3.
It is worth noting that without 1, 3 covers the overdraft caused by 2, thus, the reached state is green.
This example illustrates how our netting mechanism manages transactions while considering liquidity
constraints: our mechanism enables the execution of two actions that subsequently cover the balance
overdraft, which would typically not be feasible using a standard semantics to execute the transactions.</p>
        </sec>
      </sec>
    </sec>
    <sec id="sec-4">
      <title>4. Related work</title>
      <p>The widely spread interest in distributed ledger technology has fostered the development of optimized
trading protocols. This section illustrates the most relevant proposals concerning netting mechanism,
optimal routing problems, and intent-centric protocols, and compares them with our work.</p>
      <sec id="sec-4-1">
        <title>4.1. Netting mechanism</title>
        <p>
          Several papers have developed decentralized inter-bank payment systems where the role of the payment
instructions operator is implemented through a public ledger-based protocol, and the netting mechanism
through smart contracts. As a first example of this line of work, Jasper-Ubin Project [ 12] investigates the
possibility of achieving near-instant cross-border payments using blockchain. The project removes the
single point of failure and obtains an immediate settlement without transaction reconciliation but does
not give a decentralized multilateral netting. Naganuma et al. [13] provide a secure netting protocol
using the Hyperledger Fabric channel and its access control mechanism. Their secure settlement protocol
does not require a specific central server and keeps part of the payment transaction information secret.
Similarly, Wang et al. [14] introduce a blockchain-based netting solution that relies on a central party
and preserves the total amount of liquidity, revealing only the net amount of each bank. Cao et al. [
          <xref ref-type="bibr" rid="ref6">6</xref>
          ]
propose a non-interactive zero-knowledge proof mechanism to post payment instructions on a public
ledger. More precisely, each bank locally computes the netting results and submits its proposal to a
coordinating smart contract. However, this approach is not robust against cheating users, who can
post-invalid partial netting proposals. The proposals above consider blockchain-related technologies to
develop standard financial services, whereas our work introduces a standard financial mechanism into
DeFi protocols. To the best of our knowledge, ours is the first attempt to apply LSM used by the works
above [
          <xref ref-type="bibr" rid="ref6">13, 14, 6</xref>
          ] to DeFi services.
        </p>
      </sec>
      <sec id="sec-4-2">
        <title>4.2. Optimal routing problems</title>
        <p>Another relevant research field investigates how to execute several trades of diferent crypto-assets
on networks of multiple CFMMs. These approaches are known as routing problems on Decentralized
Exchanges. Angeris et al. [15] and Diamandis et al. [16] pointed out that the optimal routing problem
can be formulated as an eficiently solvable optimization problem. Solving such a problem means
determining the most eficient path for a trade, i.e., a sequence of crypto-assets exchanges in a network
of CFMMs that realizes a given trade, to maximize the user’s utility and minimize its costs.</p>
        <p>Danos et al. [17] introduce arbitrage scenarios within exchange networks and develop an efective
global routing system. In detail, they explore how optimal routing strategies are employed to capitalize
on price diferentials across multiple exchanges, enhancing opportunities for profitable arbitrage. Similar
to those approaches, our mechanism aims at finding a solution to maximizing a specific parameter.
However, the main diference between ours and the above-mentioned papers [ 15, 16, 17] is the objective
function to maximize. As illustrated in Section 3, our objective function consists of maximizing the
number of actions in the queue, i.e., the number of transactions to settle. On the other hand, those
proposals formulate the optimization problem in terms of the largest possible user’s utility, and their
routing problem tries to detect the most convenient and profitable route for executing trades across
AMMs of the same and diferent token types belonging to the network.</p>
      </sec>
      <sec id="sec-4-3">
        <title>4.3. Intent-centric protocols</title>
        <p>A significant area of research in Ethereum ecosystem focuses on addressing the aggregated token swap
problem, i.e., determining a sequence of swaps on AMMs that realizes a given trade by improving
the liquidity of users. Various DeFi applications, such as UniswapX [18], Uniswap V4 [19], and CoW
Protocol [20], tackle this challenge by adopting intent-centric protocols. These protocols shift the
focus from transaction execution to defining users’ desired outcomes, creating competitive routing
marketplaces, introducing gas-free cross-chain swaps, and incorporating batch auctions to discover
profitable prices.</p>
        <p>UniswapX [18] implements intent-centric swaps, combining on-chain and of-chain liquidity for a
competitive trading marketplace. Uniswap V4 [19] enhances liquidity with hooks, enabling gas-free
cross-chain swaps and ofering flexibility through customizable features. CoW Protocol [ 20] utilizes
batch auctions for eficient price discovery, and CoW Swap, its decentralized exchange interface,
introduces CoW Hooks, allowing custom actions before and after trades.</p>
        <p>While these protocols aim to optimize trading across multiple AMMs, our approach stands out
by introducing a Liquidity-Saving Mechanism designed to maximize users’ swap actions without a
formalized analysis of users’ intent.</p>
      </sec>
    </sec>
    <sec id="sec-5">
      <title>5. Conclusion</title>
      <p>
        We have proposed liquidity-optimized AMMs, providing an LSM that settles AMM swap actions through
a netting algorithm at the application level. We precisely characterized our mechanism by extending
the operational semantics proposed by Bartoletti et al. [
        <xref ref-type="bibr" rid="ref7">7</xref>
        ]. We removed the liquidity constraints on
participants’ balances when performing swap actions and introduced a queue of actions. When the
queue size reaches a fixed bound, and the liquidity constraints are unsatisfied, a greedy algorithm is
triggered to compute a sequence of actions with no liquidity violation. Our mechanism enables novel
scenarios, allowing users to perform exchanges and arbitrages without the required upfront funds.
      </p>
      <p>In future work, we plan to extend our mechanism to deal with the deposit and redeem actions and
take into account price trends, enabling us to consider realistic DeFi protocols. Moreover, we want
to introduce the formalization of fixed transaction costs (such as gas costs) in the LTS. Another line
of investigation is to understand if knowing the internals of the netting mechanism encourages users
to misbehave and take advantage of it. Additionally, we want to extend our mechanism to take into
account also other properties, such as, for example, fairness when selecting the actions to remove.
Furthermore, we would like to study the impact of the queue length (a key parameter in our mechanism)
on the eficiency of our LSM. We also plan to investigate the impact of validators at the consensus
layer on our approach and whether they could be the ones promoting transaction orders that result in
near-to-optimal queue orderings, hence taking care of the optimal netting problem.</p>
      <p>Another line of research involves considering our mechanism work in diferent AMM models, such
as Concentrated Liquidity which has been recently added to Uniswap v3 [21]. Furthermore, our
formalization is designed to be implemented as a smart contract on the blockchain. It encompasses
the management of various aspects such as user subscriptions, netting procedures with corresponding
incentive mechanisms, queue management, and striking a balance between the mechanism’s cost and
the incentives it ofers. An additional research direction involves exploring of-chain management of the
actions, either through a trusted third party or in a decentralized manner. This approach aims to ensure
the proper behavior of the queue, mitigating potential control issues by validators and simplifying
the analysis of incentivized actions for each validator. Finally, we plan to re-model our mechanism,
considering users’ intents and trying to maximize their utility related to their desired outcome. In this
way, we could better analyze user objectives and strategies.</p>
    </sec>
    <sec id="sec-6">
      <title>Acknowledgments</title>
      <p>This work was partially supported by project SERICS (PE00000014) under the MUR National Recovery
and Resilience Plan funded by the European Union - NextGenerationEU.
[9] A. Person, Sushi protocol documentation, https://docs.sushi.com/pdf/whitepaper.pdf, 2020.
Accessed: 2024-03-12.
[10] M. Egorov, Stableswap-eficient mechanism for stablecoin liquidity, Retrieved Feb 24 (2019) 2021.
[11] Y. M. Shafransky, A. A. Doudkin, An optimization algorithm for the clearing of interbank payments,</p>
      <p>European Journal of Operational Research 171 (2006) 743–749.
[12] B. of Canada, M. A. of Singapore, Enabling cross-border high value transfer using distributed
ledger technologies, https://www.mas.gov.sg/-/media/Jasper-Ubin-Design-Paper.pdf?la=en&amp;
hash=EF5857437C4857373A9287CD86F56D0E7C46E7FF, 2018. Accessed: 2023-07-11.
[13] K. Naganuma, M. Yoshino, H. Sato, N. Yamada, T. Suzuki, N. Kunihiro, Decentralized netting
protocol over consortium blockchain, in: International Symposium on Information Theory and Its
Applications,ISITA 2018, IEEE, 2018, pp. 174–177. doi:10.23919/ISITA.2018.8664259.
[14] X. Wang, X. Xu, L. Feagan, S. Huang, L. Jiao, W. Zhao, Inter-bank payment system on enterprise
blockchain platform, in: 11th IEEE International Conference on Cloud Computing, CLOUD 2018,
IEEE Computer Society, 2018, pp. 614–621. doi:10.1109/CLOUD.2018.00085.
[15] G. Angeris, A. Evans, T. Chitra, S. Boyd, Optimal routing for constant function market makers, in:</p>
      <p>Proceedings of the 23rd ACM Conference on Economics and Computation, 2022, pp. 115–128.
[16] T. Diamandis, M. Resnick, T. Chitra, G. Angeris, An eficient algorithm for optimal routing through
constant function market makers, arXiv preprint arXiv:2302.04938 (2023).
[17] V. Danos, H. E. Khalloufi, J. Prat, Global order routing on exchange networks, in: Financial
Cryptography and Data Security. FC 2021 International Workshops: CoDecFin, DeFi, VOTING,
and WTSC, Springer, 2021, pp. 207–226.
[18] UniswapX whitepaper, Uniswapx, https://uniswap.org/whitepaper-uniswapx.pdf, 2023. Accessed:
2024-01-21.
[19] Uniswap V4 core whitepaper, Uniswap v4, https://github.com/Uniswap/v4-core/blob/main/docs/
whitepaper-v4.pdf, 2023. Accessed: 2024-01-21.
[20] C. developers, Cow protocol, https://docs.cow.fi/, 2021. Accessed: 2023-10-10.
[21] A. Hayden, Z. Noah, S. Moody, K. River, R. Dan, Uniswap v3, https://uniswap.org/whitepaper-v3.
pdf, 2021. Accessed: 2024-04-23.</p>
    </sec>
  </body>
  <back>
    <ref-list>
      <ref id="ref1">
        <mixed-citation>
          [1]
          <string-name>
            <given-names>Aave</given-names>
            <surname>Documentation</surname>
          </string-name>
          , Flash loans, https://docs.aave.com/faq/flash-loans,
          <year>2021</year>
          . Accessed:
          <fpage>2023</fpage>
          -11- 23.
        </mixed-citation>
      </ref>
      <ref id="ref2">
        <mixed-citation>
          [2]
          <string-name>
            <given-names>K.</given-names>
            <surname>Qin</surname>
          </string-name>
          ,
          <string-name>
            <given-names>L.</given-names>
            <surname>Zhou</surname>
          </string-name>
          ,
          <string-name>
            <given-names>B.</given-names>
            <surname>Livshits</surname>
          </string-name>
          ,
          <string-name>
            <given-names>A.</given-names>
            <surname>Gervais</surname>
          </string-name>
          ,
          <article-title>Attacking the defi ecosystem with flash loans for fun and profit</article-title>
          ,
          <source>in: International conference on financial cryptography and data security</source>
          , Springer,
          <year>2021</year>
          , pp.
          <fpage>3</fpage>
          -
          <lpage>32</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref3">
        <mixed-citation>
          [3]
          <string-name>
            <given-names>Uniswap</given-names>
            <surname>V2 Guide</surname>
          </string-name>
          , Flash swaps, https://docs.uniswap.org/contracts/v2/guides/ smart-contract
          <article-title>-integration/using-flash-</article-title>
          <string-name>
            <surname>swaps</surname>
          </string-name>
          ,
          <year>2023</year>
          . Accessed:
          <fpage>2023</fpage>
          -11-23.
        </mixed-citation>
      </ref>
      <ref id="ref4">
        <mixed-citation>
          [4]
          <string-name>
            <surname>M. M. Güntzer</surname>
            ,
            <given-names>D.</given-names>
          </string-name>
          <string-name>
            <surname>Jungnickel</surname>
            ,
            <given-names>M.</given-names>
          </string-name>
          <string-name>
            <surname>Leclerc</surname>
          </string-name>
          ,
          <article-title>Eficient algorithms for the clearing of interbank payments</article-title>
          ,
          <source>European Journal of Operational Research</source>
          <volume>106</volume>
          (
          <year>1998</year>
          )
          <fpage>212</fpage>
          -
          <lpage>219</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref5">
        <mixed-citation>
          [5]
          <string-name>
            <given-names>M.</given-names>
            <surname>Bech</surname>
          </string-name>
          ,
          <string-name>
            <given-names>K.</given-names>
            <surname>Soramaki</surname>
          </string-name>
          ,
          <article-title>Gridlock resolution in payment systems</article-title>
          ,
          <source>Danmarks Nationalbank Monetary Review</source>
          (
          <year>2001</year>
          )
          <fpage>67</fpage>
          -
          <lpage>79</lpage>
          . doi:
          <volume>10</volume>
          .2139/ssrn.274290.
        </mixed-citation>
      </ref>
      <ref id="ref6">
        <mixed-citation>
          [6]
          <string-name>
            <given-names>S.</given-names>
            <surname>Cao</surname>
          </string-name>
          ,
          <string-name>
            <given-names>Y.</given-names>
            <surname>Yuan</surname>
          </string-name>
          ,
          <string-name>
            <surname>A. De Caro</surname>
            ,
            <given-names>K.</given-names>
          </string-name>
          <string-name>
            <surname>Nandakumar</surname>
            ,
            <given-names>K.</given-names>
          </string-name>
          <string-name>
            <surname>Elkhiyaoui</surname>
            ,
            <given-names>Y.</given-names>
          </string-name>
          <string-name>
            <surname>Hu</surname>
          </string-name>
          ,
          <article-title>Decentralized privacy-preserving netting protocol on blockchain for payment systems</article-title>
          ,
          <source>in: Financial Cryptography and Data Security: 24th International Conference, FC 2020</source>
          , Springer,
          <year>2020</year>
          , pp.
          <fpage>137</fpage>
          -
          <lpage>155</lpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref7">
        <mixed-citation>
          [7]
          <string-name>
            <given-names>M.</given-names>
            <surname>Bartoletti</surname>
          </string-name>
          ,
          <string-name>
            <surname>J. H</surname>
          </string-name>
          .
          <article-title>-y.</article-title>
          <string-name>
            <surname>Chiang</surname>
            ,
            <given-names>A.</given-names>
          </string-name>
          <string-name>
            <surname>Lluch-Lafuente</surname>
          </string-name>
          ,
          <article-title>A theory of automated market makers in defi</article-title>
          ,
          <source>Logical Methods in Computer Science</source>
          <volume>18</volume>
          (
          <year>2022</year>
          )
          <fpage>12</fpage>
          .
        </mixed-citation>
      </ref>
      <ref id="ref8">
        <mixed-citation>
          [8]
          <string-name>
            <given-names>H.</given-names>
            <surname>Adams</surname>
          </string-name>
          , Uniswap v3 core, https://uniswap.org/whitepaper-v3.pdf,
          <year>2021</year>
          . Accessed:
          <fpage>2023</fpage>
          -07-09.
        </mixed-citation>
      </ref>
    </ref-list>
  </back>
</article>