Cover photo

Decoding DeFi Perpetuals: From Basics to Advanced

These days I’m into auditing DeFi perpetuals, and that requires me understanding how the overall system works if I’m to find unique, business logic bugs. One of the best ways to learn something is to talk about it, share and teach one’s knowledge. I plan to share what I understand from perpetuals, from basics to advanced. This is the first part of a series, expect more to come.


In order to understand perpetuals, it might be the best to go back to futures contracts briefly, as perpetuals are pretty much just futures without an expiration date. So, what are futures?

Futures are an agreement between two parties about a trade that will take place in the future (hence the name) on a prearranged price. The best way to understand this concept is with an example:

Alice wants to buy Bitcoin. Bitcoin, say, trades at 50k USD at the moment. She believes that the price of Bitcoin will go way up, so she makes an agreement with a DeFi protocol that exactly after a month, she will pay 50k USD for 1 Bitcoin. And after a month, Bitcoin reaches 55k. Since they've made an agreement, the market has to sell that 1 Bitcoin position to Alice at the agreed price instead of its real price that's 55k. So, Alice profits 5k.

Of course, it could've gone the other way too. If the price of 1 Bitcoin tanked to 40k, Alice would have to pay the agreed price to the market. She would lose 10k.

Note that this is a seriously simplified version of futures trading and smooths over lots of details, but the core idea is there.

From futures to perpetuals

Now, perpetuals are like futures, but they don’t have an expiration date. You bet that the price of Bitcoin will go up, and if it goes up, you keep making profit. And if it goes down, you are at risk of being liquidated. But there are some gotchas.

Quick fill: Liquidation. This is a financial concept that means the money you put upfront (that is called the collateral) will be taken from you if your position becomes unprofitable. You longed (bet that the price will go up) Bitcoin but it tanked (price went down), you were wrong, the protocol will close your position once losses approach your collateral, preventing losses beyond what you deposited.

New concept: Leverage

Perpetuals are generally paired with this concept called leverage. Leverage is how much you want to multiply your position, without having to add extra collateral. Again, it’s better with an example.

Let’s say Bitcoin trades at 50k right now. You put 1k usd with 10x leverage saying that the price will go up.

Bitcoin price goes to 55k.

We’ll get a profit, but what’s the profit formula might be? Profit (or loss) on futures/perps is calculated as:

PnL = Position Size × (Exit Price − Entry Price)

But how does Position Size calculated then?

Position Size = Collateral × Leverage / Entry Price

Let’s first calculate without leverage.

  • Entry price = 50,000

  • Exit price = 55,000

  • Collateral = 1,000 USD

  • Leverage = none (1x)

Let’s do the math:

Position Size= 1000 * 1 / 50.000 = 0.02

PnL= 0.02 * (55.000-50.000=5000)= 100

100 bucks profit. Not bad.

Now let’s think with 10x leverage. Since now Position Size is 0.2 instead of 0.02 we’ll get 1000 usd instead of 100.

You see, in order to get the profit that would require us to use 10.000 usd, we get that profit with only 1000 usd collateral. That’s leverage.

But what if the price tanked?

Simply, in general the protocol will liquidate our position before getting to that point, but that’s a topic for another day.