Automatic market-makers (AMMs) are one of the major innovations which decentralized finance has brought. They use the maths function to price assets when exchanging two or more tokens. First, Uniswap brought markets created by
Since the inception of AMMs, there have been several notable improvements in AMMs. One such improvement is Curve V2 which introduced CurveCrypto Invariant and can be used to create liquidity for tokens that are not necessarily pegged to each other i.e. their price changes frequently w.r.t. each other.
Limitation of AMMs in Leverage Trading
All these innovations work perfectly fine when it comes to token swaps but it is difficult to apply the same to derivatives, such as perpetual futures. As derivative trading involves leverage, the position value will be bounded by the pool size, and also the liquidity providers might suffer from high impermanent loss.
To overcome this problem, Perpetual protocol introduced the concept of Virtual AMMs or vAMMs. As the “virtual” part of vAMM implies, there is no real asset pool stored inside the vAMM itself. Instead, the real asset is stored in a smart contract vault that manages all of the collateral backing the vAMM; and the vAMM is just used for pricing the perps. In fact, that is how the Mark Price is determined. The perpetual protocol uses
Hubble vAMM
At Hubble, we are using CurveCrypto Invariant in our vAMM. As described in Curve V2 whitepaper, it is way more efficient than
Here is an example of how it works under the hood.
- Before starting any trade, we add virtual liquidity to the pool and set the initial rate. Let say we have a vUSDT/vETH pool with 1,000,000 vUSDT and 1000 vETH initially, setting the initial vETH price 1000 vUSDT.
- Alice adds 1000 USDT to the smart contract vault as margin and wants to long 5 ETH i.e. 5x leverage. The protocol calculates the amount of vUSDT required to buy 5 vETH using CurveCrypto invariant and adds the same to the vAMM pool and removes the 5 vETH from it. The final state of the pool after the transaction is 995 vETH and 1,005,008.997 vUSDT.
- Another trader Bob also adds a 1000 USDT margin and wants to short 5 ETH i.e. 5x leverage. The protocol calculates the amount of vUSDT required to sell 5 vETH using CurveCrypto invariant and removes the same from the vAMM pool and adds the 5 vETH to it. The final state of the pool after the transaction is 1000 vETH and 1,000,000 vUSDT.
Unique Properties of Hubble vAMM
Repegging
In contrast to Uniswap V3, Curve V2 uses an internal oracle to concentrate liquidity and the repegging algorithm takes care of the price update. This makes our vAMM ‘smart’ enough to concentrate liquidity around the current price by itself.
Since Curve V2 is not path independent, there is a need for Makers in the system to counter the profit/loss due to that. Makers add virtual liquidity to the pool on leverage and earn a part of the trading fee. The good part is, makers don’t have to worry about the price range in which they have to provide liquidity to deepen the liquidity around the current price. The repegging algorithm deepens liquidity around current price automatically and hence makers earn fee regardless of the price change. During highly volatile market, makers don’t have to worry about getting out of range and lose out on pool fee.
Maths behind Hubble vAMM
The main advantage of using this vAMM is that the repegging algorithm takes care of liquidity concentration around the current price.
Repegging Algorithm
The repegging profit/loss is quantified by constant-product invariant at the equilibrium point. For a pool with
where D is the total value of the pool in the terms of the base token (0th token) when the pool is in equilibrium and
We track
After every operation, we multiply a variable xcp_profit
by virtual_price/old_virtual_price
, starting with virtual_price-1
to fall lower than half of xcp_profit-1
.
Internally, we have a price oracle given by an exponential moving average applied in N-dimensional price space. Suppose that the last reported price is
CurveCrypto Invariant
This invariant is encouraged by the StableSwap invariant. For a pool with
where

Figure 1: Comparison of AMM invariants: constant-product (dashed line), stable swap (blue), and curve crypto invariant (orange)
Figure 1: Comparison of AMM invariants: constant-product (dashed line), stable swap (blue), and curve crypto invariant (orange) Image source: https://curve.fi/files/crypto-pools-paper.pdf
Check out this notebook to know more about the effects of varying
Swapping Tokens (newton_y)
Suppose a trader wants to swap
Substituting
Using
Finally,
Check out the implementation of the above algorithm in the newton_y(Ann,gamma,x,D,i)
function of Curve’s math contract.
Similarly, newton_D(ANN, gamma, x)
is used to calculate