|
| 1 | +# Understanding the Stellar Price API |
| 2 | + |
| 3 | +This document explains the technical details of how the Stellar Price API works and the methods used to calculate token prices on the Stellar network. |
| 4 | + |
| 5 | +## Overview |
| 6 | + |
| 7 | +The Stellar Price API fetches and calculates the price of a specific Stellar token in both XLM (native Stellar currency) and USD. It uses two primary sources of price data: |
| 8 | + |
| 9 | +1. Recent trades on the Stellar DEX (Decentralized Exchange) |
| 10 | +2. Liquidity pool reserves (AMM - Automated Market Maker) |
| 11 | + |
| 12 | +## Technical Details |
| 13 | + |
| 14 | +### Price Data Sources |
| 15 | + |
| 16 | +#### 1. Stellar DEX Trade Data |
| 17 | + |
| 18 | +The API queries the Stellar Horizon server to get the most recent trade for the token pair (e.g., OVRL/XLM). This provides the most recent market price at which the token was traded: |
| 19 | + |
| 20 | +```python |
| 21 | +trades = server.trades().for_asset_pair( |
| 22 | + base=Asset(TOKEN_CODE, TOKEN_ISSUER), |
| 23 | + counter=Asset.native() |
| 24 | +).limit(1).order("desc").call() |
| 25 | +``` |
| 26 | + |
| 27 | +The price is extracted from the trade record using the price ratio (`n/d`). |
| 28 | + |
| 29 | +#### 2. Liquidity Pool Data (AMM) |
| 30 | + |
| 31 | +Liquidity pools on Stellar provide another source of pricing data. The price of the token in a pool is determined by the ratio of reserves: |
| 32 | + |
| 33 | +```python |
| 34 | +pool_response = server.liquidity_pools().for_reserves([token_asset, native_asset]).call() |
| 35 | +``` |
| 36 | + |
| 37 | +The AMM price is calculated as `XLM_reserves / token_reserves`. |
| 38 | + |
| 39 | +### Price Selection Logic |
| 40 | + |
| 41 | +The API uses the following logic to determine the final price: |
| 42 | +- If both trade and AMM prices are available, it uses the AMM price (generally more stable) |
| 43 | +- If only one price source is available, it uses that |
| 44 | +- If no price data is available, it returns zero |
| 45 | + |
| 46 | +### XLM to USD Conversion |
| 47 | + |
| 48 | +To convert the XLM price to USD, the API fetches the current XLM/USD price from the Kraken exchange API: |
| 49 | + |
| 50 | +```python |
| 51 | +response = requests.get("https://api.kraken.com/0/public/Ticker?pair=XLMUSD", timeout=5) |
| 52 | +``` |
| 53 | + |
| 54 | +Then it multiplies the token's XLM price by the XLM/USD exchange rate. |
| 55 | + |
| 56 | +## Stellar Concepts Used |
| 57 | + |
| 58 | +### Assets |
| 59 | + |
| 60 | +On Stellar, assets are represented by: |
| 61 | +- Asset code (e.g., "OVRL") |
| 62 | +- Asset issuer (the public key of the account that issued the asset) |
| 63 | + |
| 64 | +Native XLM is a special case that doesn't require an issuer. |
| 65 | + |
| 66 | +### Liquidity Pools |
| 67 | + |
| 68 | +Stellar's liquidity pools allow users to provide liquidity for pairs of assets. The API uses these pools to determine the market price based on the ratio of assets in the pool. |
| 69 | + |
| 70 | +### DEX (Decentralized Exchange) |
| 71 | + |
| 72 | +Stellar has a built-in decentralized exchange where users can place orders to buy or sell assets. The API queries recent trades from this exchange. |
| 73 | + |
| 74 | +## Further Reading |
| 75 | + |
| 76 | +- [Stellar Developer Documentation](https://developers.stellar.org/docs) |
| 77 | +- [Stellar SDK for Python](https://stellar-sdk.readthedocs.io/) |
| 78 | +- [Understanding Liquidity Pools on Stellar](https://developers.stellar.org/docs/learn/encyclopedia/sdex/liquidity-on-stellar-sdex-liquidity-pools) |
0 commit comments