Breaking the Barrier: Efficient On-Chain Computation of HLP’s PnL for Instant Liquidity Provision (ILP)

Taoree
HMX.org
Published in
4 min readJun 29, 2023

HMX’s liquidity pool, HLP, acts as the primary counterparty to all traders on the platform. As a result, when traders win, HLP incurs losses. When traders lose, HLP gains profit. With this dynamic, it is crucial to accurately track the real-time PnL of all active positions to enable liquidity providers to enter or exit the pool fairly.

Calculating the global PnL for the HLP pool is relatively straightforward if the platform offers a 0% price impact on trades, as the pool can easily keep track of the weighted average position prices and sizes, which can then be used to calculate the pool’s PnL.

However, the method above is not applicable to HLP, as all trades would incur a virtual price impact to reduce directional exposure risks for liquidity providers. The virtual price impact results in each position having a different close price, which, on the surface, may seem impossible to be computed on-chain due to the high computational cost. It would require looping through all positions, calculating the close price of each position, and determining the PnL. Another approach to solving this problem is to move the computation off-chain and implement an epoch system that only allows liquidity providers to enter or exit the pool within a specific time window. However, we believe that this design would create a bad UX and hinder the adoption of our protocol.

Our Solution 💡

We have developed a novel feature: Instant Liquidity Provision (ILP). This innovation enables us to efficiently calculate HLP’s countertrade PnL without the need for any loops, instead relying on the tracking of just three variables. Let’s delve into how this feature works.

The delta (PnL) of each position can be calculated using the following formula:

Where:

  • size is a position size.
  • fillPrice is an oracle price with a virtual price impact.
  • avgEntryPrice is the average entry price of the position.

Next let’s examine how fillPrice is calculated:

Where:

  • price is an oracle price.
  • skew is the difference between long open interest and short open interest.
  • maxSkew is a constant of maximum allowed skew.

With these two foundational equations, the PnL of HLP can be calculated as follows:

To simplify the equation above and find a common factor, we can distribute the sigma into the equation. This allows us to express the HLP’s PnL in a simpler form:

This implies that the smart contract only needs to keep track three variables in order to efficiently calculate the HLP’s PnL on-chain.

The Test 🎯

During the stress test conducted on the equation above, we implemented the core functions of HLP in Python and executed over 1,000,000 random trades. By comparing the PnL calculated from the expensive approach to our equation, we found that the differences were insignificant and negligible in size.

Conclusion

By introducing the ILP, HLP’s depositors can still enter and exit the pool instantly, eliminating any need for locking periods, while still receiving benefits from the virtual price impact feature which safeguards them from directional exposure in the extreme bullish or bearish conditions.

Official HMX Links 🐉:

WebsiteDocs | TwitterTelegramTelegram AnnouncementMediumDiscordZealy

--

--

Taoree
HMX.org

Co-founder at HMX, a decentralized perpetual exchange. Trade Crypto, FX, Equity, and Commodity with up to 1,000x leverage on the Arbitrum Network.