This is a submission for the Build Better on Stellar: Smart Contract Challenge : Build a dApp
What We Built
Hermes is a decentralized perpetual exchange built on the Stellar blockchain, designed to offer traders the ability to trade with up to 100x leverage. See it in action in our demo The platform draws heavy inspiration from Jupiter on Solana—not by copying its code, as Jupiter is not open-sourced, but rather by emulating its system architecture and trading mechanics.
Our perpetual exchange operates on an LP-to-trader model, leveraging liquidity pools and oracles to offer up to 100x leverage. This structure ensures zero price impact, zero slippage, zero spread, and deep liquidity. Oracles are crucial for maintaining stable market operations during liquidations, minimizing the risk of position bankruptcy, and protecting the LP pool from potential losses.
Users can open and close positions in a single step, without the need for additional accounts or aditional actions, streamlining the trading process. Additionally, we've incorporated smart wallet functionality to further simplify user interactions by allowing users to sign transactions in a single click and with the use of biometric data. Currently, our platform has no fees and allows only one open position per user, though we plan to introduce fees and support multiple positions in the future.
When a user deposits into the LP pool, they receive the corresponding amount of SLP tokens in exchange. These SLP tokens can later be redeemed to withdraw the available assets from the pool. To determine just how many SLP tokens a user receives after a certain deposit, the following formula is used:
new_slp = (deposit_value * total_slp) / total pool value.
The amount of new SLP is dependent on the value of the deposit, total amount of SLP in circulation and the total value of the pool. When a user decides to redeem their SLP in, the amount that can be withdrawn is calculated as follows:
(turned_in_slp / total slp) * total_poolvalue = amount_to_withdraw
The pool tries to maintain a target ratio between XLM and Orbit USD, this is done when users deposit or withdraw assets from the pool. Currently, we maintain a target ratio of 50/50 between XLM and Orbit USD, but this may be subject to change. At present, this means that any deposit into the pool must contain equal amounts of XLM and Orbit USD if the pool is balanced. If the pool becomes unbalanced, for example, due to price fluctuations, the ratio of the deposit should ideally aim to restore the 50/50 balance in the pool. The same principle applies to withdrawals.
One of the reasons to reconsider the 50/50 balance of the pool would be to obtain a favorable utility rate of the assets. If there is too much unused XLM in the pool, this would leave liquidity providers greatly exposed to the risk of drops in the price of XLM. However, the liquidity rates should also never be too high, because the pool should still be able to handle large withdrawal orders.
Example Trades
Long Trade Scenarios
A trader puts down 100 XLM Collateral, with XLM being at $0.10, and opens a 5x Leverage Long. 500 XLM (USD$50) is borrowed from the pool for this position.
If the price goes up 10% to $0.11, the trader gains $5 USD worth of XLM or 50 XLM when closing the position and 450 XLM (USD$50) is returned to the pool.
- The pool hasn't experienced a loss in USD value, but there has been a slight decrease in the amount of XLM. The user receive more USD, as 150 XLM would be ~USD$16.5.
If the price goes down 10% to $0.09, the trader loses $5 USD worth of the original XLM or 55.55 XLM when closing the position and ~555.55 XLM (USD$50) is returned to the pool.
- The pool hasn't experienced a loss in USD value, but there has been a slight increase in the amount of XLM. The user receive less USD, as 45.45 XLM would be ~USD$4.
Short Trade Scenarios
A trader puts down 10 oUSD Collateral, with XLM being at $0.10, and opens a 5x Leverage Short. 50 oUSD (500 XLM) is borrowed from the pool for this position.
If the price goes down 10% to $0.09, the trader gains $5 USD worth of oUSD when closing the position and 45 oUSD (500 XLM) is returned to the pool.
- The pool has lost oUSD but since holds an equivalent amount of XLM. The user would receive 10 oUSD.
If the price goes up 10% to $0.11, the trader loses $5 USD worth of oUSD when closing the position and 55 oUSD (500 XLM) is returned to the pool.
- The pool has gained oUSD but since holds an equivalent amount of XLM. The user would receive 5 oUSD.
My Code
Journey
Developing Hermes has been a challenging journey, driven by our work on OrbitCDP—a protocol that enables the creation of decentralized stablecoins for all currencies. Together, Hermes and OrbitCDP will bring Forex trading to Stellar, tapping into a market that trades $7 trillion daily.
The learning curve was steep, especially in mastering fixed-point math for precise calculations within smart contracts. This knowledge was crucial in implementing complex financial models for leverage trading, including position management and liquidation mechanisms.
We're particularly proud of two aspects:
- Our mathematical implementation: The insights gained from fixed-point math enabled us to create efficient models for leverage trading, forming the backbone of our system.
- Smart wallet integration: We incorporated contracts developed by kalepail, enhancing security and ease of use.
While our focus was on smart contracts and math models, we also developed a good looking user-friendly frontend for interacting with the platform.
The Stellar community's support was invaluable, we used multiple libraries written by users from the ecosystem and the support in the discord has been very good. Looking ahead, we plan to expand Hermes to support more trading pairs and implement support for multiple positions. We're also planning on adding a comprehensive fee structure, including fees for opening and closing positions, liquidation fees, and time-based fees for holding positions. These fees will help grow the pool size so liquidity providers get adequately rewarded. Other planned features include advanced trading options like take-profit/stop-loss orders and a utilization rate management system to minimize risk for liquidity providers.
This journey has significantly expanded our expertise in blockchain-based financial systems. We're excited to continue growing with the Stellar ecosystem and push the boundaries of what's possible in decentralized leverage trading.
Our team:
- Notishansingh
- Joostmbakker
- robeart
- brent_tenboom
Top comments (2)
Thanks for sharing 🔥
This is very insightful thanks! thumbs up to you