Multiblock MEV opportunities & protections in dynamic AMMs
Matthew Willetts Christian Harrington

Deep-learning Optical Flow Outperforms PIV in
Obtaining Velocity Fields from Active Nematics
    Correspondence email addresses:
^∗hongpeng@brandeis.edu, ^hagan@brandeis.edu 

    April 29, 2024
============================================================================================









Maximal Extractable Value (MEV) in Constant Function Market Making is fairly well understood.
Does having dynamic  weights, as found in liquidity boostrap pools (LBPs), Temporal-function market makers (TFMMs), and Replicating market makers (RMMs), introduce new attack vectors?
In this paper we explore how inter-block weight changes can be analogous to trades, and can potentially lead to a multi-block MEV attack.
New inter-block protections required to guard against this new attack vector are analysed.
We also carry our a raft of numerical simulations, more than 450 million potential attack scenarios, showing both successful attacks and successful defense.




§ INTRODUCTION




In Temporal-Function Market Making (TFMM) <cit.> the trading function of a pool depends on time, in contrast with Constant-Function Market Making.



In a TFMM pool the allocation of assets is responsive to market information.
The changing trading function of TFMM pools creates arbitrage opportunities (via changes in pools's quoted prices) that incentivises pools' holdings to be rebalanced by external agents to the pools' target holdings.

An important question about TFMM pools is whether their (known) temporal dependence leads to an opportunity for maximal extractable value (MEV).[a.k.a. Miner Extractable Value]
Here we study such attacks, where an attacker aims to manipulate a TFMM pool shortly before a weight update.
This situation is the TFMM equivalent of a `sandwich attack', but with the attack being done against pool LPs rather than a trader.

It is important to note that changes in trading function happen between blocks.[This makes the weight updates somewhat reminiscent of trades in https://www.paradigm.xyz/2021/07/twammTime-weighted average market makers.]
This means that any attack would necessarily be multi-block.

We perform mathematical analysis of the circumstances where attacks of this kind are possible and where they are not possible, even when granting the attacker (for free and with certainty) the final transaction in one block and the first transaction in the next.













This understanding motivates three extremely simple `guardrails' for protection against such manipulation:

    
  * limit on the size of weight changes,
    
  * limit of trades as a fraction of pool reserves,
    
  * limit on minimum allowed value in weights w̌(t).

We have to make certain assumptions to obtain our analytical results.
Making use of recent advances in the modelling of optimal multi-token arbitrage trades in G3Ms <cit.>, we carry out a battery of numerical simulations of potential attacks (∼ 450,000,000) to test the effectiveness of the proposed guardrails in rendering the potential multi-block attack unviable.











§ BACKGROUND: TEMPORAL FUNCTION MARKET MAKING
 
Temporal-Function Market Making (TFMM) <cit.> pools have time-varying trading functions.


This is naturally achieved by having w̌, the portfolio vector of a G3M pool <cit.> change from block to block.
(For a pool of N assets, each with reserves R_i, the G3M trading function is ∏_i=1^N R_i^w_i= k where k is the pool's constant.)


From the temporal dependency of w̌ in TFMM pools, we now have w̌(t), and the (now time-varying) trading function is:

    ∏_i=1^N R_i^w_i(t)= k(t),   where ∑_i=1^N w_i(t) = 1,   and  ∀ i   0< w_i(t)<1.

where we have made the time-dependence of k explicit, as it now depends on the current, changing, value of w̌(t).

Trades must preserve (or increase) the value of k(t) that exists at the time of that trade.[Note that changes in weights from block to block do change the value of k(t). This makes it hard to interpret a TFMM pool's k(t) as an `unscaled pool value', as k can be in vanilla G3Ms.]
Within a block weights are constant (i.e. t is the discrete block number) and so take a known, clear, value at the time that any trade happens.






A TFMM pool thus takes a dynamic position, re-weighting its desired division of value between assets on the fly, doing so via offering arbitrage opportunities if its desired reserves are not in line with its actual reserves.



As described in <cit.>, multi-token trades are allowed on TFMMs.
Consider a trader wishing to exchange a particular set of tokens, represented by the vector Δ̌, for another set of tokens Λ̌, where entries in each are ≥ 0.
The pool requires, for tokens i where Δ_i>0, that Λ_i=0: tokens cannot be traded for themselves.
With fees (1-γ), for a trade to be accepted it must be the case that

    ∏_i=1^N (R_i + γΔ_i - Λ_i)^w_i(t)≥ k(t)








  
Token-Pair Quotes via the Trading Function
Consider token-pair trades, i.e. Δ̌ and Λ̌ are restricted to be one-hot.
The trader wishes to exchange Δ_i of the i^th token for some amount Λ_j of the j^th token, i≠ j.
We can directly give a TFMM pool's quote for the amount Λ_j that it will accept for the trading-in of Δ_i, via inversion of Eq (<ref>):

    Λ_j = R_j (1 - 1/(1+γΔ_i/R_i)^w_i(t)/w_j(t)).











From taking a binomial expansion of Eq (<ref>) for small Δ_i and setting γ=1 we can find the effective price quoted by a TFMM pool for the i^th token in terms of the j^th (that is, using the j^th token as the numéraire) if no fees are charged,

    p^TFMM_i,j(t) = w_i(t)/R_i/w_j(t)/R_j.

This result gives relationship between a pool's weights and the prices it quotes, and is of the same form as for G3Ms but for the time-dependence of w̌.
If the weight of token i increases relative to token j, the price of token i increases relative to token j; conversely, a decrease in the relative weight of token i causes a decrease in its price, such that trading reduces holdings of token i and increases those of j.



§ MULTIBLOCK MEV AND TFMM WEIGHT CHANGES


The weights of a pool change from block to block.
This means that, even if the reserves of the pool are unchanged, the quoted prices of the pool change from block to block.


Knowing that the upcoming change in pool weights, and thus quoted prices, will lead a pool to offering an above-market price when buying one of its constituents, an attacker could, prior to the weight change, trade with the pool to reduce its holdings of that asset.
That trade would increase the quoted price of that asset, which would then stack with the weight-change-induced increase in quoted price.
The attacker could then, in the next block, sell this asset, at this now higher price, back to the pool.
In effect, by trading with the pool prior to the weight change, to manipulate the pool's quoted prices, the attacker has supercharged the arbitrage opportunity caused by the weight change and thus extract value from the pool.

This attack is intrinsically multi-block, and if the attacker does not correctly obtain the last transaction in the initial block and the first transaction in the second block they themselves are liable to be frontrun or sandwiched.
In our analysis here, we give the attacker costless and certain placement of their transactions in the way needed for the attack.

This potential attack is a three-step process.
To analyse it, we need to study the cost of manipulating the quoted prices of a pool, the weight change itself, and the return from performing an arbitrage trade against the pool's new weights.
From these quantities we can then calculate the overall return on this potential attack.

First, we derive the cost of manipulating a TFMM pool, with particular weights, such that it quotes a chosen price.
We do this in the presence of fees.
After having manipulated the price in this way, the weights of the pool change, and a `boosted' arbitrage trade is then available in the following block.
We derive the returns to an arbitrageur from bringing the manipulated-pool back to quoting market prices after the weights have been updated.
































Later in paper we describe a battery of in-silico experiments where we perform this attack on TFMM pools, optimising not only the attacking trades but the state (weights, weight changes, reserves) of the pools themselves so as to make the attack as good as possible within the confines of the pools' `guardrail parameters' listed above.
For various settings of these guardrail parameters, attacks are not obtained for any settings of pool state when optimising the pool state (within the confines of the guardrails) for an attack to be found.





















 §.§ Trading & Fees


Consider an N-token pool.
For simplicity of analysis we trade a subset of 2 tokens for this attack—in our numerical work later we allow all tokens to be traded and empirically we find that optimal attacks are done by trading between a pair of tokens in this way.
Those two tokens have weights w̌={w_1,w_2} and reserves Ř={R_1,R_2}.
We are here going to consider the quoted prices of the tokens in the pool.



We assume the existence of true, external market prices for the tokens.
m_p,2=m_p is the market price of the second token in terms of the first.
m_p,1, the market price of the first token in terms of the second is simply the reciprocal of this: m_p,1=1/m_p,2=1/m_p.
(Without loss of generality, we will be using the first token as the numéraire throughout unless otherwise noted.[Either you can imagine that the first token in the pool is a stablecoin in USD, say; or it can be an arbitrary token, and we then are applying a linear scaling (that token's price) to our vanilla market prices to get them in the denomination of our particular chosen numéraire.])

Here we consider an attacker who wishes the pools quoted price for the second token, in terms of the first, to deviate from the market prices by a factor of (1+ϵ).
Without loss of generality we will consider attacks where the initial price manipulation is done to increase the quoted price of the second token.
The attacker trades the first token into the pool and withdraws the second tokens.

This is without loss of generality as a) we can freely permute the indices of assets in a pool's basket and as b) a manipulation to increase the price of one token in the pool, by reducing its reserves, necessarily means we are decreasing the price of the other token.
So an attack based around decreasing the price of token, rather than increasing it, is obtained simply by exchanging the indices of the tokens.

Recall Eq (<ref>), with fees of τ=1-γ (commonly γ=0.997), when trading in Δ_1>0 of token 1 to receive Δ_2>0 of token 2 the trade must fulfill

    (R_1+γΔ_1)^w_1(R_2-Δ_2)^w_2≥ k = R_1^w_1R_2^w_2,

with the best deal for the trader found when

    (R_1+γΔ_1)^w_1(R_2-Δ_2)^w_2= k = R_1^w_1R_2^w_2.

Eq (<ref>) can be rearranged into a neater form,

    1-2/2=(1+γ1/1)^-1/2.


When there are no fees (i.e. γ=1), the quoted price of token 2 (that is, the ratio of Δ_1 to Δ_2 for an infinitesimal trade of token 1 for token 2) is m_u:=R_1/R_2w_2/w_1, and we can expect m_u=m_p at equilibrium.
This is because we can expect arbitrageurs to interact with the pool until m_u=m_p.

However, when fees are present this is no longer the case.
In fact, for a given weight vector there is a range of reserve values, and thus quoted prices, for which no arbitrage opportunity exists.
We can get the quoted price, for a pool with fees, of the second token with the first token as numéraire (again, this is for an infinitesimal trade), by rearranging Eq (<ref>) and taking limits:

    m_AMM=m_AMM,2   =lim_Δ_1,Δ_2→0^+Δ_1/Δ_2
       =lim_Δ_1→0^+Δ_1/R_2(1-(1+γΔ_1/R_1)^-w_1/w_2)^-1
    ⇒ m_AMM=m_AMM,2   = 1/γR_1/R_2w_2/w_1=1/γ m_u.

There is no arbitrage opportunity when this quoted price is greater than the market price, so when m_AMM>m_p.

We also need to consider the flipped trade, where the pool is taking in token 2 and sending out token 1.
As we can simply exchange indices, we get the corresponding price for the first token with the second token as numéraire:

    m_AMM,1= 1/γR_2/R_1w_1/w_2.

Again, there is no arbitrage opportunity when m_AMM,1>m_p,1=1/m_p,2=1/m_p.

Putting these together, before the attack we assume that we are at equilibrium—that there is no arbitrage opportunity.
So it must be the case that γ m_p < m_u < γ^-1m_p, as γ < 1 so γ^-1>1.
We are here considering attacks when the attacker is aiming to drive up the quoted price of the second token.
So, the `worst case' then for the pool is for the quoted price for the second token to already be at the extreme upper end of the allowed no-arbitrage range.
We thus have

    m_u    = 1/γ m_p
    ⇒ m_AMM   =1/γ m_u= 1/γ^2 m_p

just prior to the attack.

For further mathematical details of the attack see Appendix <ref>, where it is shown how both the attacks trades and weight changes have to be `sufficiently large' for attacks to be viable.






§ DEFENDING POOLS



Our analysis motivates a simple approach to prevent a pool being attackable.

Simply cap trade size, as a proportion of current pool reserves, restrict the maximum change in weight allowed and do not allow any weights to get too small.
Not allowing very large trades can be expected to have minimal to zero impact on the normal operation of TFMM pools, as such trades have large slippage associated with them.
For instance, one could ban trades where reserves of the token being removed would be reduced by 20%, and ban trades that would increase the reserves of the token being traded in by more than 20%.
That means we would be choosing (Δ̅_1,Δ̅_2)=(0.21,0.22).

Collectively we call these requirements the `guardrails':

    
  * Restriction on trade size as a proportion of pool reserves
    
  * Requirement that weight vector always has entry above a minimum value
    
  * Restriction on the absolute values of the entries of the weight change vector

Each guardrail has an associated `guardrail parameter'.



 §.§ Numerical Simulation

In this section we describe the results of attempting to perform this attack in simulation.
We are doing this to check that validity of the defence mechanisms we have put forward through studying the problem mathematically in the section above.

We perform a battery of simulated attacks, where the attacker is aiming to maximise their return.
But in this construction, as we are testing whether or not our protection methods work, not only does the attacker get to optimise their trades Δ̌, Δ̌' but also the state of the pool (, w̌), the weight change Δw̌ and the external market prices m̌_p.
We constrain the initial state of the pool and market prices such that no arbitrage opportunity exists before the first trade (i.e. the quoted prices of the pool are within the no-arb region).
The guardrails are applied to the process, and trades have to satisfy the pool's trading function.
We use γ=0.997 throughout.

Note that naturally we would expect, all else being equal, small maximum trade sizes, large minimum weight values, and small weight changes to be harder to attack.
































  
Method
As the problem is non-convex, we use gradient-based optimisation methods to maximise the attacker's return, optimising Δ̌, , w̌, Δw̌ and m̌_p.

We make use of the recent advances in methods for finding optimal arbitrage trades in N-token G3M pools <cit.>.
This approach enables for fast, parallelisable and accurate calculation in closed form of the trade that can be applied to a G3M to obtain the most value for the trader, including in the presence of fees, without using a convex solver or other program.
We use these optimal trade results for the attacker's second trade, Δ̌', the post-weight-change trade, and for the counterfactual arbitrage-only (no manipulation) trade X(ϵ_0) (see Appendix <ref> for more detail).
While Δ̌' does not require gradient-based optimisation itself, it is key for us that the calculated optimal arbitrage trade is itself auto-differentiable, meaning we can take gradients through it during optimisation.
Trades are not restricted to be one-hot.

We iterate over grids of values of the guardrails.
As three parameters define the guardrails (which are fixed during optimisation), for ease of plotting we do sweeps where one is held constant and the other two vary.
We perform 100,000 separate attacks for each setting of the guardrail parameters, each a slightly different initialisation for the optimisation to act on, and we study 4563 different settings of the guardrail parameters, for ≈ 450,000,000 different attacks.

We do this for a 3-token pool.

We show the results in Fig <ref>.
In line with our intuition, pools are harder to attack with larger values of the minimum allowed weight, smaller values of the maximum allowed trade size and weight change.
In this experiment many configuration of guardrail (the white regions in the plots) no attacks were found possible.




§ CONCLUSION


This is the first public work studying potential multi-block MEV attacks on dynamic AMMs and defenses thereto, and as such it might have relevance also to Liquidity Bootstrap Pools <cit.> and Replicating Market Makers <cit.>.
We used a restrictive method of proof to obtain requirements for robustness to this attack.
With more advanced mathematical methods these bounds could be made tighter, as well as being made to apply to a wider variety of potential set ups, for example analytical results for multi-token attacks (going beyond pair trades).
Additionally there are other potential guardrails and methods for defense beyond those given here.


Further, future refinements to numerical methods may lead to finer understanding of the `attack frontier' (in terms of the guardrail parameters), under which the trade offs between safety and economic performance will only become easier.






    
    



§ MATHEMATICAL ANALYSIS OF POTENTIAL ATTACK




In this potential attack, the attacker first manipulates the pool's quoted price for second token to be

    m_AMM,2^manip. = (1+ϵ)m_p,

where ϵ≥ϵ_0.
ϵ_0 is the `do nothing' or `NULL' value—equivalent to their being no attack carried out.
When fees are present and we are in a worst-case scenario for the pool, ϵ_0>0.





C(ϵ) denotes the cost to the attacker of performing the first, price-manipulating, trade, making clear the dependence of this cost on the scale of the price deviation the attacker does.
X(ϵ) denotes the return to the attacker from the post-weight-update arbitrage trade.

The overall benefit to the attacker over not carrying out the attack and just being an arbitrageur, Z(ϵ), is thus

    Z(ϵ)=X(ϵ) - C(ϵ) - X(ϵ_0),

When Z(ϵ)>0 the return from the attack, X(ϵ) - C(ϵ), is greater than the vanilla, just-doing-arbitrage return X(ϵ_0).
We can obtain bounds on X(ϵ) and C(ϵ) when fees are present without having them in closed form.



 §.§ The stages of the potential attack



  §.§.§ Cost of Manipulating Quoted Prices



Post-trade, the quoted prices are

    m_attacker = m_p (1+ϵ)=  1/γ2/2-2/1/1+1.

Subbing in that m_p = γ m_u, we find that after the attack trade

    1+1/2-2=γ^2 (1+ϵ)1/2.









Combining Eq (<ref>) with Eq (<ref>) and rearranging we have that

    (1+1/1)(1+γ1/1)^1/2 = γ^2(1+ϵ)










Similar manipulations give us

    (1-2/2)^-1(1+1/γ((1-2/2)^-2/1-1))=γ^2(1+ϵ).






C(ϵ), the cost of the attack to the manipulator, again using token 1 as the numéraire, is

    C(ϵ) = 1 - m_p 2.

Eq (<ref>) links together 1,1,
γ and ϵ, and Eq (<ref>) separately links 2,2,
γ and ϵ.

These are implicit equations for 1 or 2 in terms of the other variables.

This means we cannot trivially write down C(ϵ) in closed form.
We can make progress as we need only either the ratio of 1 to 2 or the partial derivatives of the cost, of 1, and of 2, with respect to ϵ for us to find a bound on Z(ϵ).





  §.§.§ Change of Reserves of an Attacked Pool After a Weight Update


We assume that all this takes place fast enough that the market prices are constant–this could all take place in one block.
After the price manipulation above we have new ('ed) reserves

    1'   = 1 +1 
    2'   = 2 - 2






The weights of the pool change, so now we have new (again, 'ed) weights 1'=1+1 and 2'=2+2.

After the arbitrage trade, the reserves of the pool will change again (to ”ed values) such that the new weights, 1', 2', and the new post-arbitrage-trade reserves, 1”, 2”, minimise the value in the pool (thus maximising the returns to the arb).
We thus have

    1”   =1'-1' 
    2”   =2+2'.

The return to the arbitrageur is 

    X(ϵ) = 1' - m_p 2'


What is the best-for-the-arb trade?



Instead of directly obtaining the value of X(ϵ) we will upper bound its value by X_γ=1(ϵ) (the return to the arbitrageur when the arbitrageur's trade takes place in a no-fees way—γ=1 for this trade).
Intuitively X_γ=1(ϵ)>X(ϵ) (after all it would be surprising if fees made the trade cheaper), and in Appendix <ref> we prove that this is indeed the case.

And thus that

    m_AMM'=
    
    m_u'=
    
    
    2'/2”/1'/1” =
    
    m_p,

Combining Eqs (<ref>) and (<ref>) we get

    2'/2”/1'/1”   =1/γ1/1+ϵ2/2'/1/1'
    
    ⇒2”/2'   =γ(1+ϵ)2'/21/1'1”/1'.




















Thus we can consider the no-fees invariant before and after this arb-trade:

    k̃'    = 1'^1'2'^2'=1”^1'2”^2'
    ⇒ 1   =(1”/1')^1'(2”/2')^2'.


Using Eq (<ref>) we get

    1   =(1”/1')^1'(γ^2(1+ϵ)2'/21/1'1”/1')^2', 
    ⇒1”/1'   =(2/2'1'/11/γ1/1+ϵ)^2'/1'+2',

and thus, similarly, that

    2”/2'=(2/2'1'/11/γ1/1+ϵ)^-1'/1'+2'.







From algebraic manipulation we obtain that

    1'    = 1'[1-(2/2'1'/11/γ1/1+ϵ)^2'/1'+2'],
    2'   = 2'[(2/2'1'/11/γ1/1+ϵ)^-1'/1'+2'-1],

Our upper bound on the return to the arbitrageur is thus

    X_γ=1(ϵ) =   1'  - m_p 2'  
    ⇒ X_γ=1(ϵ)=   1'[1-(2/2'1'/11/γ1/1+ϵ)^2'/1'+2'] 
         
        - m_p 2'[(2/2'1'/11/γ1/1+ϵ)^-1'/1'+2'-1].





 §.§ Putting it all together: When is there no extractable value?




Our upper bound on Z(ϵ) is thus:

    Z(ϵ)≤Z̃(ϵ) = X_γ=1(ϵ) - C(ϵ) - X_γ=1(ϵ_0).








  §.§.§ Bounding via gradients of Z(ϵ)


Recall that there is a `NULL' value of ϵ, ϵ_0, which corresponds to no-price-manipulation.
As 1(ϵ_0)=2(ϵ_0)=0, Z(ϵ_0)=Z̃(ϵ_0)=0.

We want to find settings of pool parameters such that Z(ϵ)<0 for all ϵ>ϵ_0.
If ∂Z̃(ϵ)/∂ϵ<0 for all ϵ>ϵ_0 (i.e. if Z(ϵ) is a monotonically non-increasing function for ϵ>ϵ_0) then Z(ϵ)<0 for all ϵ>ϵ_0.







In the zero fees case the `NULL' value ϵ_0=0.




Taking partial derivatives of Z̃(ϵ) w.r.t. ϵ we get

    ∂Z̃(ϵ)/∂ϵ = ∂/∂ϵ(1'-1)+ m_p ∂/∂ϵ(2-2'),

so if ∂/∂ϵ(1'-1)≤0 and ∂/∂ϵ(2-2')≤0, then we can guarantee that the attack will not work.





  
Gradient of 1'-1 w.r.t. ϵ

Using Eq (<ref>), recalling that 1'=1+1, we have that

    1'-1    = 1 - (2/2'1'/11/γ1/1+ϵ)^2'/1'+2'(1+1)
    ⇒∂/∂ϵ(1'-1)    = (2/2'1'/11/γ1/1+ϵ)^2'/1'+2'
         ×(1/1+ϵ2'/1'+2'(1+1)-∂1/∂ϵ).

We find that (see Appendix <ref>)

    ∂1/∂ϵ = γ^21/(1+γ1/2(1+1/1)(1+γ1/1)^-1)(1+γ1/1)^1/2.

Subbing this into Eq (<ref>), and using Eq (<ref>), for ∂/∂ϵ(1'-1)≤0 it must be that

    2'/1'+2'(1+γ1/2(1+1/1)(1+γ1/1)^-1)≤1,

where we have used that (2/2'1'/11/γ1/1+ϵ)^2'>0 when 1∈(0,1), 2∈(0,1), 1'∈(0,1), 2'∈(0,1), 0<γ<1, and ϵ>ϵ_0.




  
Gradient of 2-2' w.r.t. ϵ
Using Eq (<ref>), recalling that 2'=2-2, we have that

    2-2'    = 2 + (2/2'1'/11/γ1/1+ϵ)^-1'(2-2)
    ⇒∂/∂ϵ(2-2')    = (2/2'1'/11/γ1/1+ϵ)^-1'/1'+2'
         ×(1/1+ϵ1'/1'+2'(2-2)+∂2/∂ϵ).

We find that (see Appendix <ref>)

    ∂2/∂ϵ = γ^32(1-2/2)^2/(1+2/1)(1-2/2)^-2/1-(1-γ)

Subbing this into Eq (<ref>), and using Eq (<ref>), for ∂/∂ϵ(2-2')≤0 it must be that

    1'/1'+2'≥1-(1-γ)(1-2/2)^2/1/1+2/1-(1-γ)(1-2/2)^2/1,

where we have used that (2/2'1'/11/γ1/1+ϵ)^-1'>0 when 1∈(0,1), 2∈(0,1), 1'∈(0,1), 2'∈(0,1), 0<γ<1, and ϵ>ϵ_0.

These results tell us that if the changes in weights are within these bounds, then no attack is possible.



  
2-token case

For the case of a two-token pool, so the two tokens being traded are the old tokens present, we can simplify the above equations and plot them.
We are interested in knowing what weight changes we can `get away with' without a pool being open to attack.
That means we are most interested in the above inequalities reformulated explicitly to give us bounds on the weight changes.

As we are now in the two-token case, 1'+2'=1+2=1 and Δ1 = -Δ2.
Thus we can re-write Eq (<ref>) and Eq (<ref>) in terms of just w = 1=1-2 and Δ w = Δ1=-Δ2 :

    Δ w    ≥1- 1/(1+γw/(1-w)(1+1/1)(1+γ1/1)^-1)-w,
    Δ w    ≥1-(1-γ)(1-2/2)^(1-w)/w/1+(1-w)/w-(1-γ)(1-2/2)^(1-w)/w-w.

In <ref> we described how we could study the case where the attacker initially increases the quoted price of the second token, denominated in the first, without loss of generality as we can simply swap indices in our final equations to get the results for the mirror-attack where the price of the first token is initially pumped up by the attacker.
We can now do that swapping, giving the additional constraints:

    Δ w    ≤1/(1+γ(1-w)/w(1+2/2)(1+γ2/2)^-1)-w,
    Δ w    ≤1-1-(1-γ)(1-1/1)^w/(1-w)/1+w/(1-w)-(1-γ)(1-1/1)^w/(1-w)-w.


If the changes in weight fulfil Eqs (<ref>-<ref>) then no attack is possible for a given initial price-deviating trade.
These bounds are expressed as functions of that initial trade, 1 and 2.
These bounds are also monotonic in 1 and 2 such that if a pool is safe under an initial trade (Δ̅_1,Δ̅_2) then it is safe also for all trades 1<Δ̅_1 and 2<Δ̅_2.
See Appendix <ref> for this.





 §.§ Proof that X_γ=1(ϵ) is an upper bound on X(ϵ)


Here we will demonstrate that X_γ=1(ϵ) > X(ϵ) for ϵ>ϵ_0.
First recall that

    X(ϵ) = 1'(ϵ,γ) - m_p 2'(ϵ,γ)

and that

    X_γ=1(ϵ) = 1'(ϵ,γ=1) - m_p 2'(ϵ,γ=1),

where we have made explicit the dependence of 1' and 2' on ϵ and γ.

    X_γ=1(ϵ) - X(ϵ) = 1'(ϵ,γ=1) - 1'(ϵ,γ) - (m_p 2'(ϵ,γ=1)-2'(ϵ,γ)),

This means for X_γ=1(ϵ)>X(ϵ) it is sufficient to show that 2'(ϵ,γ)>2'(ϵ,γ=1) and that 1'(ϵ,γ)<1'(ϵ,γ=1).
We will handle these in turn



  
Showing 2'(ϵ,γ)>2'(ϵ,γ=1):

We begin by writing down the trade-invariant for 1' and 2' in the presence of fees.
It is, naturally, that

    (1'-1'(ϵ,γ))^1'(2'+γ2'(ϵ,γ))^2'= k'=1'^1'2'^2,

as the trader is putting 2' of token 2 into the pool and withdrawing 1' of token 1.

Next, we need the trade invariant if γ=1—if there are no fees.
Then

    (1'-1'(ϵ,γ=1))^1'(2'+2'(ϵ,γ=1))^2'= k'=1'^1'2'^2.


The final part we need is the following.
We know that the purpose of this trade is to get the pool to quote a certain price after the trade has been performed, based on its post-trade pool reserves.
This means that the quoted prices after the trade-with-fees will, indeed must, be the same as the quoted price after the trade-with-no-fees.[Note that we are only setting γ=1 for the trade in the γ=1 part of this construction.
We are still having the arbitrage trade, with fees or not, bring the quoted price to the upper end of the (γ defined) no-arb region.]
Thus, using Eq (<ref>) we get that

    (1'-1'(ϵ,γ=1))/(2'+2'(ϵ,γ=1))=(1'-1'(ϵ,γ))/(2'+2'(ϵ,γ)),

where the weights have cancelled out.
Rearranging we get that

    (1'-1'(ϵ,γ))=(2'+2'(ϵ,γ))(1'-1'(ϵ,γ=1))/(2'+2'(ϵ,γ=1)),

which we can sub in to Eq (<ref>) to get

    ((2'+2'(ϵ,γ))(1'-1'(ϵ,γ=1))/(2'+2'(ϵ,γ=1)))^1'(2'+γ2'(ϵ,γ))^2'= k'.

Rearranging Eq (<ref>) we get

    (1'-1'(ϵ,γ=1))^1'= k'/(2'+2'(ϵ,γ=1))^2',

which we can then sub in to the previous equation to obtain

    (2'+2'(ϵ,γ))^1'(2'+γ2'(ϵ,γ))^2'=(2'+2'(ϵ,γ=1)).

As 1'+2'=1, 0<1'<1, 0<2'<1 and 0<γ<1, it is thus clear that 2'(ϵ,γ)>2'(ϵ,γ=1).



  
Showing 1'(ϵ,γ)<1'(ϵ,γ=1):
Rearranging Eq (<ref>) we get

    1'(ϵ,γ=1)   =1'-(1'-1'(ϵ,γ))(2'+2'(ϵ,γ=1))/(2'+2'(ϵ,γ)),
    ⇒2'(ϵ,γ=1)    =1'(1-k)+k1'(ϵ,γ),
    ⇒2'(ϵ,γ)    =1/k1'(ϵ,γ=1)+1'(k-1)/k,

where k=(2'+2'(ϵ,γ=1))/(2'+2'(ϵ,γ)).
k<1 as 2'(ϵ,γ)>2'(ϵ,γ=1) and k>0 as both its numerator and denominator are >0.
This last equation, for 1'(ϵ,γ) as a function of 1'(ϵ,γ=1), is a straight line with gradient 1/k>1 and intercept 1'(k-1)/k<0.
This line crosses `y=x' when 1'=1'(ϵ,γ=1), so for all 1'(ϵ,γ=1)<1' (which are the only possible values as the pool cannot be drained) we have that 1(ϵ,γ=1)<1'(ϵ,γ=1) as required.



 §.§ Finding partial derivatives



  §.§.§ Finding ∂1 / ∂ϵ


Recall the implicit equation that defines 1, Eq (<ref>):

    (1+1/1)(1+γ1/1)^1/2 = γ^2(1+ϵ).

Taking partial derivatives of both sides with respect to ϵ:

    ∂/∂ϵ((1+1/1)(1+γ1/1)^1/2)= ∂/∂ϵ(γ^2(1+ϵ))
    ⇒   1/1∂1/∂ϵ(1+γ1/1)^1/2+(1+1/1)1/2(1+γ1/1)^1/2-1γ/1∂1/∂ϵ=γ^2
    ⇒   ∂1/∂ϵ1/1(1+γ1/1)^1/2(1+γ(1+1/1)1/2(1+γ1/1)^-1)=γ^2
    ⇒   ∂1/∂ϵ=γ^21/(1+γ1/2(1+1/1)(1+γ1/1)^-1)(1+γ1/1)^1/2,

as required.


  §.§.§ Finding ∂2 / ∂ϵ


Recall the implicit equation that defines 2, Eq (<ref>):

    (1-2/2)^-1(1+1/γ((1-2/2)^-2/1-1))=γ^2(1+ϵ).

Taking partial derivatives of both sides with respect to ϵ:

    ∂/∂ϵ((1-2/2)^-1(1+1/γ((1-2/2)^-2/1-1)))=∂/∂ϵ(γ^2(1+ϵ))
       
    ⇒   ∂2/∂ϵ1/2(1-2/2)^-2((1+1/γ(1-2/2)^-2/1-1/γ)
           +(1/γ2/1(1-2/2)^-2/1))=γ^2
    ⇒   ∂2/∂ϵ=γ^32(1-2/2)^2/(1+2/1)(1-2/2)^-2/1-(1-γ),

as required.


 §.§ Monotonicity of inequalities


We have two inequalities that we derive above, Eq (<ref>),

    2'/1'+2'(1+γ1/2(1+1/1)(1+γ1/1)^-1)≤1,

and Eq (<ref>),

    1'/1'+2'≥1-(1-γ)(1-2/2)^2/1/1+2/1-(1-γ)(1-2/2)^2/1.

Here we are interested in the monotonicity of the level-sets of these w.r.t. 1 and 2.
That is, if our values of 1',2' satisfy their inequalities for some particular values of 1,2,1,2,1,2, can we guarantee that the same values of 1',2' also satisfy the inequalities for Δ̂_1<1 and Δ̂_2<2 (with 1,2,1,2 fixed)?

For this to be the case, we need the level sets of the inequalities to always have the correct gradient so that smaller values of 1,2 always move the inequality's boundary `further away' from the value of 1',2'.
Let us handle the above equations in turn.


  
1

Consider the case where our 1',2' values just satisfies the first inequality above, so that

    w̅_2'/w̅_1'+w̅_2'=1/1+γ1/2(1+1/1)(1+γ1/1)^-1,,

where w̅_1', w̅_2' denotes these critical value of 1',2'.
For w̅_1', w̅_2' to also satisfy the inequality for Δ̂_1<1, we need that

    ∂/∂1(1/1+γ1/2(1+1/1)(1+γ1/1)^-1)<0,

as then the required critical value will be larger for smaller 1 values.
Evaluating this partial derivative, we find that

    ∂/∂1   (1/1+γ1/2(1+1/1)(1+γ1/1)^-1)
         =-γ1/2(1+γ1/1)^-1(1+γ(1+1/1)(1+γ1/1)^-1)/(1+γ1/2(1+1/1)(1+γ1/1)^-1)^2.

For 1∈(0,1), 2∈(0,1), γ>0, 1>0 and 1>0, clearly this gradient is always negative, as required.



  
2
Again let us take the critical value of the new weight such that the inequality is just satisfied, so

    w̅_1'/w̅_1'+w̅_2'=1-(1-γ)(1-2/2)^2/1/1+2/1-(1-γ)(1-2/2)^2/1.

Here we want the partial derivative of this critical value to always be positive, so that the required critical value is always smaller for smaller 2 values, so we want that

    ∂/∂2(1-(1-γ)(1-2/2)^2/1/1+2/1-(1-γ)(1-2/2)^2/1)>0.

We can re-write Eq (<ref>) more compactly as

    w̅_1'/w̅_1'+w̅_2'=1-f(2)/1+a-f(2).

where f(2)=(1-γ)(1-2/2)^2/1 and a=2/1.

    ∂/∂2(1-f(2)/1+a-f(2))=-a∂ f(2)/∂2/(1+a-f(2))^2.

As a>0, as 1∈(0,1) and 2∈(0,1), the gradient ∂w̅_1'/∂2 is positive if ∂ f(2)/∂2<0.
Let us evaluate ∂ f(2)/∂2:

    ∂ f(2)/∂2   =∂/∂2((1-γ)(1-2/2)^2/1) 
       =(-1/22/1(1-γ)(1-2/2)^2/1-1)

As 2<2, 1∈(0,1), 2∈(0,1), 0<γ<1, this gradient is always negative, so

∂/∂2(1-(1-γ)(1-2/2)^2/1/1+2/1-(1-γ)(1-2/2)^2/1)>0, as required.


DISCLAIMER This paper is for general information purposes only.
It does not constitute investment advice or a recommendation or solicitation to buy or sell any investment or asset, or participate in systems that use TFMM; nor is it a guarantee in any form of the behaviour or performance of any TFMM-based systems.
This paper should not be used in the evaluation of the merits of making any investment decision.
It should not be relied upon for accounting, security, legal or tax advice or investment recommendations.
This paper reflects current opinions of the authors regarding the development and functionality of TFMM and is subject to change without notice or update.

While some aspects, such as altering target weights in geometric mean market makers is prior art, aspects of TFMM that are novel for use in dynamic weight AMMs, for purposes of core liquidity providing or forms of asset management including, but not exclusively, fund construction, structured products, treasury management are covered by patent filing date of 21st February 2023.
