Block-based Funding Payment On Perp v2

田少谷 Shao
Perpetual Protocol
Published in
7 min readDec 24, 2021

--

Plus a recap of funding designs!

Outline

0. Intro
1. Funding Rate & Payment
2. Periodic Funding
3. Block-based Funding
4. Funding Payments for Makers
5. Conclusion

0. Intro

Invented by Bitmex, the perpetual contract is one of the most popular innovations in the crypto space. By introducing the funding mechanism, which can be thought of as a regular settlement schedule, futures have no expiry and traders can thus worry less about the time factor and focus solely on the price speculation.

As a perpetual contract project, whose name is Perpetual, it’s only right that we talk about how funding is designed in Perp v2, Curie. Compared to our hourly funding in Perp v1, we have a new funding mechanism: block-based funding, which settles funding payments on each trade.

Also, as we introduce a new role maker to the system, things are quite different now: we not only have funding payments for takers, but also for makers!

In this article, we’d like to recap the basics of funding and introduce the Block-based Funding of Perp v2. Hopefully, it can serve as the stepping stone for readers to dive deeper into one of our many Perp v2 designs!

Let’s get started in the company of relaxing piano music!

1. Funding Rate & Payment

Though formulas of funding calculations can vary across exchanges*, the basic forms are as follows:

Premium = Mark Price - Index PriceFunding Rate = Premium / Index Price = (Mark - Index) / Index
  • Mark price is the current price on the perpetual contract market, which is also referred to as Mid price on Uniswap
  • Index price is the reference price usually from other exchanges; price oracles such as Chainlink are included
  • Premium is the difference between Mark and Index

Funding rate is essentially a measurement of how deviated Mark price is from Index price.

As for the funding payment:

Position Value = Position Size * Index PriceFunding Payment = Position Value * Funding Rate
= Position Size * Index * Premium / Index
= Position Size * Premium

Since Premium is the difference between Mark price and Index price, funding payment suggests the corresponding payment for certain position size.

Let’s look at an example!

Say Alice opens up 1 ETH long position, whose current price on the perpetual contract market is 4200, while the index price is merely 4000.

Funding rate is thus (4200 — 4000) / 4000 = 5%, and Alice has to pay 4000 * 0.05 = 1 * (4200 - 4000) = 200 as the funding payment… to whom?

  • When funding rate > 0, longs pay shorts
  • When funding rate < 0, shorts pay longs

As Alice goes long on ETH, she pays 200$ to owners of short positions.

Note that funding payment isn’t necessarily a payment, but can be a receipt as well. It’s common usage. In the above case, those who go short on ETH can receive Alice’s funding payment!

*For the comparison of different funding calculations: https://medium.com/derivadex/funding-rates-under-the-hood-352e6be83ab

2. Periodic Funding

The example in the previous section clearly misses something: how often is the funding payment incurred?

What the formula Funding Rate = Premium / Index Price describes is the funding rate for one day.

From Bitmex’s original design, whose fundings are calculated every 8 hours, we have to further extend the formula as:

Funding Rate = (Premium / Index Price) / Funding Interval

Since there are 3 funding routines per day on Bitmex, the funding interval is 3. If it’s an hourly funding mechanism, such as FTX, the funding Interval should then be 24.

This design, however, has some room for gaming the system. An hourly funding mechanism means that fundings are only charged/paid out on the hours. Thus, if traders always close their positions 1 minute before the hour/settlement, they’ll never have to pay for fundings!

Or, traders can be unwilling to open new positions before the hour to avoid immediately paying out for funding.

Whether this is a flaw or not, a periodic funding mechanism introduces an interesting arbitraging opportunity that requires traders to pay attention to the timing when opening or updating a position.

3. Block-based Funding

Now, what if funding rates are calculated on each new trade? This is Deribit’s design!

Fundings are settled whenever a trade happens, no matter long or short. The formula is pretty much the same, with only Funding Interval slightly modified:

Funding Rate = (Premium / Index Price) * Δtime / 1 day

Since it’s calculated on every trade, Δtime is the time difference between two trades. Usually, Δtime is measured in seconds, and 1 day is 60 * 60 * 24 = 86400 seconds.

Thus, Δtime / 1 day here expresses the same idea as the Funding Interval in the Periodic Funding equation: normalise the time difference/period to 1 day.

If we adopt this design in the blockchain’s venue, on Ethereum specifically, the time should be measured with block.timestamp, and fundings are settled for each new timestamp/new block.

This is the rationale behind the Block-based Funding Payment of Perp v2.

Upon each new trade, a new funding rate is recorded for the time since the last trade.

With this design, the above-mentioned arbitraging opportunity on exchanges with a periodic funding mechanism is gone, and users need no to consider the timing when taking any action on a position.

Sounds ideal, right? For those familiar with coding, an immediate question that might come to mind is: if there are numerous funding rates that last for different time spans, how should we calculate the funding payment efficiently? Cannot be loops, as we’re on a blockchain and the computation resource is scarce!

Update on April 14th, 2022
The following explanation were updated with the missing element Index Price.

For example, if we have a list of funding rates and index prices with their corresponding time as follows

Time, Funding Rate, Index Price1. (1000, 0.5%, i0)
2. (1030, 0.2%, i1)
3. (1045, 0.4%, i2)
4. (1100, 0.3%, i3)

Say a taker opens a position of size S at the time 1000. The funding payment should be

S * ( (1030 - 1000) * 0.2% * i1) // funding payment for (1000, 1030)
+ (1045 - 1030) * 0.4% * i2) // funding payment for (1030, 1045)
+ (1100 - 1045) * 0.3% * i3))// funding payment for (1045, 1100)

A small reminder: the reason for multiplying S with Index Price is to get Position Value, asFunding Payment = Position Value * Funding Rate.

Even though the calculation seems to involve loops, a common technique in writing smart contracts is to replace loops with cumulative values.

All we need are two cumulative values. The first one is a global one that records the cumulative global funding growth, and the second one is a personal cumulative funding growth that is updated on every action on the position.

This is exactly how funding payment is implemented on Perp v1, and we will get into more details in future articles!

But an advanced question for those familiar with Perp v2 is: a taker’s funding payment is easy. How do we calculate a maker’s funding payment, given that the position size of a maker can change all the time if the liquidity provided is active?

4. Funding Payments for Makers

So makers are a new role in Perp v2 that did not exist in Perp v1, and they have funding payments as well. Why? For the same reasons as on order book exchanges, makers are the liquidity providers (LP), which makes them the counter-party of takers!

For example, if there is only one taker and one maker in a market, a taker going long on 1 position size means that the maker is taking 1 short position, as the maker provides the liquidity for the taker to go long.

The only difference is that makers on AMMs are passive. Once they decide the range and place their liquidity, they have no choice over when to provide liquidity or at what price, unlike makers on order book exchanges.

Makers’ position sizes on AMMs are essentially how many position sizes they are forced to take against takers.

Thus, the biggest difference between the implementation of funding payments of takers and makers is that the taker’s position size is only changed on every action on the position. Each time a taker modifies the position, we can settle previous funding payments and update the position size for the funding payments to come.

Maker’s position size, however, not only changes on adding or removing liquidity, aka updates on the order. As long as their liquidity is in range/active, which means the liquidity is currently utilised by takers, a maker’s position size changes constantly according to the amount of position size taken by takers.

Obviously, we cannot apply the same algorithm of takes’ funding payment on makers.

However, this is not an easy topic that can be covered in one section. We will have a dedicated article that talks about the implementation of makers’ funding payments!

5. Conclusion

That’s it for our first series of funding! This article is a recap of different kinds of funding designs and a brief introduction of the Block-based Funding of Perp v2. As mentioned above, we will have another one diving into the implementation details, especially about makers.

Stay tuned and until the next one!

--

--