Swaps

Pearl provides a platform to trade tokenized RWAs and other premium digital assets with low slippage and minimal fees.

Slippage is the difference between the current market price of an asset and the price at which the actual trade (transaction) is executed. Slippage becomes an issue when the user receives a significantly smaller amount (higher price paid) of the desired token in a trade.

Through the protocol's ability to incentivize deep liquidity, which we can further optimize through concentrated liquidity, our goal is to create the most efficient trading environment in DeFi for tokenized RWAs.

Pearl uses the Uniswap v3 swap engine to calculate and execute trades. Following is a breakdown of all the technical details.

Number Concepts & Equations

Q notation:

Q notation specifies the parameters of the binary fixed point number format, which allows variables to remain integers, but function similarly to floating point numbers. Variables that must be as precise as possible in V2 are represented with a maximum of 256 bits and account both for overflow and potential rounding issues. By using Q notation, the protocol can ensure that granular decimal precision is not lost.

sqrtX96 means that the variable has been multiplied by 2^96 to guarantee 96 bits of precision during the computation. Multiplying a variable by 2^96 is equivalent to shifting its bits to the left by 96 bits.

You can convert from Q notation to the ā€œactual valueā€ by dividing by 2^k where k is the value after the X. For example, you can convert sqrtPriceX96 to sqrtPrice by dividing by 2^96.

How do I calculate the current exchange rate?

The exchange rate is calculated using sqrtPriceX96. This is found inside the slot0 struct where most of the commonly accessed values are stored

price=(sqrtPriceX96296)2orprice=1.0001tickprice= (\frac {sqrtPriceX96} {2^{96}}) ^2 \quad or\quad price = 1.0001^{tick}

Example: sqrtPriceX96 = 2018382873588440326581633304624437

USDC/WETH=(2018382873588440326581633304624437296)2{USDC/WETH} = (\frac {2018382873588440326581633304624437} {2^{96}})^2
USDC/WETH=649004842.70137{USDC/WETH} = 649004842.70137

If we adjust for decimals we get:

adjUSDC/WETH=649004842.70137/1018āˆ’6adj {USDC/WETH} = 649004842.70137 / {10^{18-6}}

The quoting convention for how the tokens are presented is determined by the addresses of the contracts, the contract with the lower address (by numeric representation) is the one that is quoted in terms of the other.

If you want to see the price in the more typical quoting convention for WETH we can do that as follows:

price=adjWETH/USDC=1012649004842.701371012ā€‹=āˆ¼1540.82price = adj {WETH/USDC} = \frac {10^{12}} { 649004842.701371012ā€‹} = āˆ¼1540.82

What Is a Tick?

Ticks are boundaries creating separations along price space. Ticks function as boundaries for liquidity positions. When a position is created, the provider must choose the lower and upper tick that will represent the range of their liquidity position.

By slicing the price range [0,āˆž] into numerous granular ticks, trading on V2 is highly similar to trading on order book exchanges, with only some differences:

  • Trades that happen between ticks still follows the pricing function of the AMM, while the equation has to be updated once the price crosses the tick.

  • Orders can be executed with any price within the price range, instead of being fulfilled at the same one price on order book exchanges.

With the tick design, V2 possesses most of the merits of both AMM and an order book exchange.

How do tick and tick spacing relate to sqrtPrice?

Ticks are used in V2 to determine the liquidity that is in-range. V2 pools are made up of ticks ranging from -887272 to 887272, which functionally equate to a token price between 0 and infinity, respectively. This conversion from tick to price directly equates to 2^-128 to 2^128, which are functionally 0 to infinity.

Ticks vs Tick-Spacing

  • Ticks: Units of measurement that are used to define specific price ranges

  • Tick-spacing: The distance between two ticks, as defined by the fee tier

Not every tick can be initialized. Instead, each pool is initialized with a tick-spacing that determines the space between each tick. Tick-spacings are also important to determine where liquidity can be placed or removed. If there is an initialized tick at tick 202910, then liquidity at most can change as early as the first value given by the tick-spacing, which is 202910 + 10 for the 5 bps pool

Tick Spacing
Fee (%)
Fee (bps)

1

0.01

1

10

0.05

5

60

0.3

30

200

1

100

Using the table above, we can determine the tick-spacing of the pool directly from the fee-tier. We show both the percentage and the bps format for these values, as used interchangeably by practitioners, but may be confusing for new users.

Swap Equations

Concentrated Liduidity

Given virtual reserves x and y, the liquidity L is simply:

xāˆ—y=L2x*y=L^2

Real Reserves

Virtual reserves are tracked by tracking the liquidity and tick bounds of each position. Real reserves is only some part of the virtual reserves because real reserves is limited by maximum x minimum price(y maximum price) and y minimum price(x maximum price).

Price

price=(sqrtPriceX96296)2price=1.0001tick{price = (\frac {sqrtPriceX96} {2^{96}}) ^2} \atop {price = 1.0001^{tick}}

TWAP

priceavg=1.0001tickavgprice_{avg} = 1.0001^{tick_{avg}}

Last updated