Trading

Example

Let's walk through an example trade.

How Perpetual Protocol Works

Step 1 Alice sends 100 USDC to the Clearing House on Perpetual Protocol and specifies to use that amount as the margin to open a 2x leveraged long position.

The primary function of the Clearing House is to record the position ownership and its related information, including initial margin, leverage, and direction (long or short).

Step 2~3 Upon receiving the 100 USDC, the Clearing House deposits the funds into the Vault. After that, Perpetual Protocol updates the asset price in our vAMM according to the margin amount, position direction (long or short), and the amount of leverage.

In contrast to the applications that utilize automated market makers (AMMs) to facilitate token swaps like Uniswap and Balancer, Perpetual Protocol only uses the constant-product curve in our AMMs for price discovery so that we can handle leverage and shorting. This also means there is no risk of impermanent loss for liquidity providers.

As you can see from the diagram above, the deposited tokens from traders aren't stored inside our AMMs, whereas on Uniswap, the deposited tokens from traders are indeed stored inside their AMMs. Due to this difference, we name our AMMs "Virtual AMMs" because there is no actual token swapping involved in our AMMs.

For those who don't know what a constant-product curve is or how Uniswap works, please see:

Back to our trader, Alice. Assume we have 100 ETH/USDC and 10,000 USDC in our vAMM as its initial state.

Action (Chronological)

ETH/USDC (x)

USDC (y)

*Calculation

Initial State

100

10,000

As mentioned before, the amount of ETH/USDC and USDC set in a vAMM aren't real tokens - they're just two numbers credited in a vAMM, and this vAMM uses them as the variables in the constant-product curve formula to calculate the price of the derivative on the platform.

Step 4~5 If Alice uses 100 USDC as the margin to open a 2x leveraged long position, which means that the amount of USDC in our vAMMs will become 10,200 (10,000+100*2), the amount of ETH/USDC will become 98.04 (100*10,000/10,200), which is calculated by the constant-product curve, and the position Alices opens is 1.96 ETH (100-98.04).

Action (Chronological)

ETH/USDC (x)

USDC (y)

*Calculation

Initial State

100

10,000

Alice gets 1.96 long position

98.04*

10,200

100*10,000/10,200

Step 6~ Following Alice, Bob also uses 100 USDC to open a long position with 2x leverage. His position size will be 1.88 ETH (98.04-96.15) as calculated by the vAMM.

Action (Chronological)

ETH/USDC (x)

USDC (y)

*Calculation

Initial State

100

10,000

Alice gets 1.96 long position

98.04*

10,200

100*10,000/10,200

Bob gets 1.89 long position

96.15*

10,400

98.04*10,200/10,400

After Bob gets his long position, Alice decides to close her position and realizes a profit of 7.84 USDC (10400 - 96.15*10,400/(96.15+1.96)- 200)

Action (Chronological)

ETH/USDC (x)

USDC (y)

*Calculation

Initial State

100

10,000

Alice gets 1.96 long position

98.04*

10,200

100*10,000/10,200

Bob gets 1.89 long position

96.15*

10,400

98.04*10,200/10,400

Alice closes her position

98.115

10192.16*

96.15*10,400/98.115

Seeing Alice makes a profit, Bob wants to close his position too, only to find out that he lost -7.84 USDC (10192.16-98.115*10192.16 / (98.115+1.885) - 200) after closing his position.

Action (Chronological)

ETH/USDC (x)

USDC (y)

*Calculation

Initial State

100

10,000

Alice gets 1.96 long position

98.04*

10,200

100*10,000/10,200

Bob gets 1.89 long position

96.15*

10,400

98.04*10,200/10,400

Alice closes her position

98.11

10,192.16*

96.15*10,400/98.115

Bob closes his position

100

10,000*

98.115*10,192.16/100

So what can learn from the example above?

Unique Properties of Perpetual Protocol's vAMM

  • One trader's gain equals another trader's loss. As you can see Alice's gain equals Bob's loss, which is similar to traditional peer-to-peer futures trading.

  • No liquidity providers are involved because we don't store tokens inside our vAMMs.

  • The vault always has enough collateral to pay back every trader because one trader's gain will cancel out another trader's loss.

Trading on xDai Chain

All trading activities occur on the xDai Chain to save gas fees and improve trading speed.

Learn why we chose xDai here.

Here is the updated architecture:

The architecture of Perpetual Protocol with Ethereum as Layer 1 and xDai as Layer 2

As you can probably tell, all of the components related to trading activities such as Clearing House, Insurance Fund, etc., are all on xDai now.

Despite this major change, only one additional step is needed for traders to start trading compared to the above example, which assumes the trading activities happening on the main Ethereum network:

  1. You deposit USDC to the Deposit Proxy (using MetaMask), which is a smart contract that passes your deposit to the Root Bridge Contract, crediting you the same amount of xUSDC to your address on xDai.

When you want to open a position from our trading platform, the only thing you need to do is to sign a transaction with your wallet (using MetaMask); the signature will be submitted to the Meta Transaction Relayer (no gas is involved in this step, since it's a meta transaction). The position is opened on xDai, and the trading platform is updated.

One thing we want to emphasize is that, despite using xDai to launch on Ethereum mainnet, with the above architecture, it’s possible to migrate the whole system to other scaling solutions (L2 and other chains alike) without much hassle. One can imagine the Perpetual DAO voting to pause trading activities on xDai, snapshot the state (traders’ active positions, balances, etc.), and proceed to ‘paste’ every trader’s state onto another L2/chain and resume trading activities.

You can check this Medium post to know the rationale behind the move to xDai and the pros and cons of this decision.