Skip to content

Commit 688ece6

Browse files
committed
add documentation for Stellar Price API and usage instructions
1 parent 026c9f1 commit 688ece6

File tree

2 files changed

+128
-0
lines changed

2 files changed

+128
-0
lines changed

LEARN.md

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
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)

README.md

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
# Stellar Price API
2+
3+
A lightweight API that provides real-time price data for a Stellar token in both XLM and USD.
4+
5+
## Features
6+
7+
- Fetches live token prices from the Stellar DEX (Decentralized Exchange)
8+
- Calculates price using both recent trades and liquidity pool data
9+
- Converts token price to USD using current XLM/USD exchange rates
10+
- Provides a simple REST API endpoint to access the pricing data
11+
12+
## Installation
13+
14+
1. Clone the repository
15+
2. Install the required dependencies:
16+
```
17+
pip install -r requirements.txt
18+
```
19+
20+
## Configuration
21+
22+
Create a `.env` file in the project root with the following variables:
23+
24+
```
25+
TOKEN_CODE=OVRL
26+
TOKEN_ISSUER=GBZH36ATUXJZKFRMQTAAW42MWNM34SOA4N6E7DQ62V3G5NVITC3QOVRL
27+
```
28+
29+
Replace OVRL and the issuer address with your own token details if needed.
30+
31+
## Usage
32+
33+
Start the server:
34+
```python main.py```
35+
36+
The API will be available at `http://localhost:5000/`
37+
38+
### API Endpoints
39+
40+
- `GET /`: Returns the current token price in both XLM and USD
41+
42+
Example Response:
43+
```json
44+
{
45+
"xlm": "0.0123456",
46+
"usd": "0.0012345"
47+
}
48+
```
49+
### License
50+
MIT

0 commit comments

Comments
 (0)