How HMX utilizes Pyth’s Low-Latency Price Feed to create the best trading experience for users

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

Dear Dragons,

A robust and reliable price feed is vital for pool-based Perp DEXs, such as HMX, to function. In this article, we will discuss some of the challenges we faced when building HMX and how we solved them with our novel approach utilizing Pyth’s low-latency price feeds to create the best trading experience for our users.

The Challenges :🤔

In designing our price feed architecture, the key considerations are: transparency, reliability / accuracy, latency, and gas cost. In the case of HMX, the issue of gas efficiency becomes crucial because of our cross margin feature. Let us explain why…

For an isolated position, only one price data (the trading market), needs to be updated each time the position owner wants to execute a trade (open / close / adjust position size.) The contract would take the price to calculate all the required stats about the position such as its health, etc. However, with a cross-margin account, users can have multiple positions opened under one subaccount. So to accurately calculate the health of the subaccount, we are required to update all the relevant prices in the sub-account before any transaction can be executed.

Similarly, when a user tries to interact with the liquidity pool — e.g., adding / removing liquidity, we are required to update prices of all the supported trading markets for accurate calculation and arbitrage prevention. We can quickly see from above that the number of price updates required will scale linearly with the number of markets we support. So as HMX grows and lists more markets overtime, the gas cost of performing transactions on the platform would grow correspondingly. This is not a good UX.

Our Solutions 💡

We came up with a novel method to optimize and compress the Pyth’s calldata to achieve greater gas efficiency to support many price feeds in one single transaction.

Two key pieces of information we need are: the price and its published time (to check for staleness.) However, price data usually takes ~128 bits and time data takes 64 bits. Given the gas model of Arbitrum, this will become a big issue if more price feeds are added to the protocol.

To reduce the gas usage, we devised a method to pack the price information as efficiently as possible. We ask ourselves: what if we can somehow encode price info into a simpler data format? After some research, we realize that the solution already exists — Uniswap V3 TickMath. Instead of having to write the price of each asset, we can encode the price information in a tick, represented by an integer according to the formula below

For example,

In the example above, we can encode the prices with 75,000 and 75,001 respectively. Now, instead of storing the full price data which takes up a lot of space, we can just pass the tick number to represent different prices. This technique allows us to fit around 10 prices into a single word (256 bit) as a tick only requires 24-bits to store. This method represents a 10x reduction in gas cost!

Note: since each tick is approximately 0.01% away from one another, the system provides a sufficiently accurate price information. Any slight deviation in execution price is offset by HMX’s lowest trading fee among peers.

The same approach applies to the published time data. Instead of feeding the raw published time, we can feed the minPublishedTime and the difference between each price to the contract. The contract then stores the minPublishedTime and its respective difference. This allows us to fit the publishTimeDiff into a uint24 format. Again, this technique reduces the gas usage by ~10x.

All of the above implementation is made possible by Pyth’s cross chain price feed technology which helps provide transparent and reliable data. Our team utilizes Pyth’s VAAs and logs them on-chain as a checksum. This way, our community members can run the VAAs in a fork block and verify that the price fed is accurate and matches with Pyth’s published data.

Summary

Through our novel use of TickMath and Pyth’s cross-chain price feeds technology, we are able to implement the cross-margin and multi-collateral features in a cost efficient way, reducing gas consumption by 10x. Pyth infrastructure also allows us to feed the price reliably with a wide selection of different asset classes available.

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.