24
24
load_dotenv ()
25
25
26
26
27
+ async def demo_simple_prices (api : TokenAPI ) -> None :
28
+ """Demonstrate simple price queries."""
29
+ print ("\n 📊 Simple Price Queries" )
30
+ print ("-" * 25 )
31
+
32
+ print ("🔍 Fetching current prices..." )
33
+
34
+ # Get ETH price
35
+ eth_price = await api .price .get (Currency .ETH )
36
+ if eth_price :
37
+ print (f"💎 ETH: ${ eth_price :.2f} " )
38
+ else :
39
+ print ("❌ ETH price unavailable" )
40
+
41
+ # Get SOL price
42
+ sol_price = await api .price .get (Currency .SOL )
43
+ if sol_price :
44
+ print (f"☀️ SOL: ${ sol_price :.2f} " )
45
+ else :
46
+ print ("❌ SOL price unavailable" )
47
+
48
+
49
+ async def demo_detailed_analysis (api : TokenAPI ) -> None :
50
+ """Demonstrate detailed price analysis with statistics."""
51
+ print ("\n 📈 Detailed Price Analysis" )
52
+ print ("-" * 27 )
53
+
54
+ # ETH with detailed stats
55
+ print ("🔍 Analyzing ETH price data..." )
56
+ eth_stats = await api .price .get (Currency .ETH , include_stats = True )
57
+
58
+ if eth_stats :
59
+ print ("💎 ETH Detailed Analysis:" )
60
+ print (f" 💰 Price: ${ eth_stats ['price' ]:.2f} " )
61
+ print (f" 📊 Confidence: { eth_stats ['confidence' ]:.0%} " )
62
+ print (f" 📈 Trades analyzed: { eth_stats ['trades_analyzed' ]} " )
63
+ print (f" 📉 Volatility: ${ eth_stats ['std_deviation' ]:.2f} " )
64
+ print (f" 📋 Range: ${ eth_stats ['min_price' ]:.2f} - ${ eth_stats ['max_price' ]:.2f} " )
65
+
66
+ # Confidence interpretation
67
+ conf = eth_stats ["confidence" ]
68
+ if conf >= 0.8 :
69
+ print (" 🟢 High confidence - excellent data quality" )
70
+ elif conf >= 0.5 :
71
+ print (" 🟡 Medium confidence - good data quality" )
72
+ else :
73
+ print (" 🟠 Low confidence - limited data available" )
74
+ else :
75
+ print ("❌ ETH detailed analysis unavailable" )
76
+
77
+ # SOL with detailed stats
78
+ print ("\n 🔍 Analyzing SOL price data..." )
79
+ sol_stats = await api .price .get (Currency .SOL , include_stats = True )
80
+
81
+ if sol_stats :
82
+ print ("☀️ SOL Detailed Analysis:" )
83
+ print (f" 💰 Price: ${ sol_stats ['price' ]:.2f} " )
84
+ print (f" 📊 Confidence: { sol_stats ['confidence' ]:.0%} " )
85
+ print (f" 📈 Trades analyzed: { sol_stats ['trades_analyzed' ]} " )
86
+ print (f" 📉 Volatility: ${ sol_stats ['std_deviation' ]:.2f} " )
87
+ print (f" 📋 Range: ${ sol_stats ['min_price' ]:.2f} - ${ sol_stats ['max_price' ]:.2f} " )
88
+
89
+ # Confidence interpretation
90
+ conf = sol_stats ["confidence" ]
91
+ if conf >= 0.8 :
92
+ print (" 🟢 High confidence - excellent data quality" )
93
+ elif conf >= 0.5 :
94
+ print (" 🟡 Medium confidence - good data quality" )
95
+ else :
96
+ print (" 🟠 Low confidence - limited data available" )
97
+ else :
98
+ print ("❌ SOL detailed analysis unavailable" )
99
+
100
+
101
+ async def demo_caching_performance (api : TokenAPI ) -> None :
102
+ """Demonstrate smart caching performance."""
103
+ print ("\n ⚡ Smart Caching Demo" )
104
+ print ("-" * 20 )
105
+
106
+ # First call (fetches from DEX data)
107
+ print ("🌐 First call (fetching from DEX)..." )
108
+ start = time .time ()
109
+ price1 = await api .price .get (Currency .ETH )
110
+ time1 = time .time () - start
111
+
112
+ # Second call (uses cache)
113
+ print ("⚡ Second call (using cache)..." )
114
+ start = time .time ()
115
+ price2 = await api .price .get (Currency .ETH )
116
+ time2 = time .time () - start
117
+
118
+ if price1 and price2 :
119
+ print ("📊 Results:" )
120
+ print (f" 🌐 API call: ${ price1 :.2f} - { time1 :.2f} s" )
121
+ print (f" ⚡ Cached: ${ price2 :.2f} - { time2 :.3f} s" )
122
+ if time1 > time2 :
123
+ print (f" 🚀 Speedup: { time1 / time2 :.0f} x faster!" )
124
+
125
+
126
+ async def demo_supported_currencies (api : TokenAPI ) -> None :
127
+ """Demonstrate supported currencies and error handling."""
128
+ print ("\n 🗂️ Supported Currencies" )
129
+ print ("-" * 21 )
130
+
131
+ supported = await api .price .get_supported_currencies ()
132
+ print ("✅ Currently supported:" )
133
+ for currency in supported :
134
+ print (f" • CURRENCY.{ currency } " )
135
+
136
+ print ("\n 🛡️ Error Handling" )
137
+ print ("-" * 17 )
138
+
139
+ # Demo enum-only interface - no string acceptance
140
+ print ("✅ API accepts only Currency enums - no backward compatibility" )
141
+ print (" Example: Currency.ETH, Currency.SOL, Currency.POL" )
142
+
143
+ # Check supported currencies
144
+ print (f"🪙 Currently supported: { ', ' .join ([c .value for c in await api .price .get_supported_currencies ()])} " )
145
+
146
+
147
+ def print_demo_summary () -> None :
148
+ """Print the demo summary and usage examples."""
149
+ print ("\n 🎉 Unified Price API Demo Complete!" )
150
+ print ("\n 💡 Key Features Demonstrated:" )
151
+ print (" • Simple Currency.SYMBOL interface" )
152
+ print (" • Multi-blockchain support (Ethereum + Solana)" )
153
+ print (" • Smart caching with volatility-based TTL" )
154
+ print (" • Detailed statistical analysis" )
155
+ print (" • Automatic outlier filtering" )
156
+ print (" • Progressive retry with adaptive sampling" )
157
+ print (" • Robust error handling" )
158
+
159
+ print ("\n 📝 Example usage patterns:" )
160
+ print (" price = await api.price.get(Currency.ETH)" )
161
+ print (" stats = await api.price.get(Currency.SOL, include_stats=True)" )
162
+ print (" supported = await api.price.get_supported_currencies()" )
163
+
164
+
27
165
async def main ():
28
166
"""Demonstrate Unified Price API functionality."""
29
167
print ("🌟 Unified Price API Demo" )
@@ -40,137 +178,18 @@ async def main():
40
178
api = TokenAPI (api_key = api_key )
41
179
42
180
try :
43
- # ===== Simple Price Queries =====
44
- print ( " \n 📊 Simple Price Queries" )
45
- print ( "-" * 25 )
46
-
47
- print ( "🔍 Fetching current prices..." )
181
+ await demo_simple_prices ( api )
182
+ await demo_detailed_analysis ( api )
183
+ await demo_caching_performance ( api )
184
+ await demo_supported_currencies ( api )
185
+ print_demo_summary ( )
48
186
49
- # Get ETH price
50
- eth_price = await api .price .get (Currency .ETH )
51
- if eth_price :
52
- print (f"💎 ETH: ${ eth_price :.2f} " )
53
- else :
54
- print ("❌ ETH price unavailable" )
55
-
56
- # Get SOL price
57
- sol_price = await api .price .get (Currency .SOL )
58
- if sol_price :
59
- print (f"☀️ SOL: ${ sol_price :.2f} " )
60
- else :
61
- print ("❌ SOL price unavailable" )
62
-
63
- # ===== Price with Statistics =====
64
- print ("\n 📈 Detailed Price Analysis" )
65
- print ("-" * 27 )
66
-
67
- # ETH with detailed stats
68
- print ("🔍 Analyzing ETH price data..." )
69
- eth_stats = await api .price .get (Currency .ETH , include_stats = True )
70
-
71
- if eth_stats :
72
- print ("💎 ETH Detailed Analysis:" )
73
- print (f" 💰 Price: ${ eth_stats ['price' ]:.2f} " )
74
- print (f" 📊 Confidence: { eth_stats ['confidence' ]:.0%} " )
75
- print (f" 📈 Trades analyzed: { eth_stats ['trades_analyzed' ]} " )
76
- print (f" 📉 Volatility: ${ eth_stats ['std_deviation' ]:.2f} " )
77
- print (f" 📋 Range: ${ eth_stats ['min_price' ]:.2f} - ${ eth_stats ['max_price' ]:.2f} " )
78
-
79
- # Confidence interpretation
80
- conf = eth_stats ["confidence" ]
81
- if conf >= 0.8 :
82
- print (" 🟢 High confidence - excellent data quality" )
83
- elif conf >= 0.5 :
84
- print (" 🟡 Medium confidence - good data quality" )
85
- else :
86
- print (" 🟠 Low confidence - limited data available" )
87
- else :
88
- print ("❌ ETH detailed analysis unavailable" )
89
-
90
- # SOL with detailed stats
91
- print ("\n 🔍 Analyzing SOL price data..." )
92
- sol_stats = await api .price .get (Currency .SOL , include_stats = True )
93
-
94
- if sol_stats :
95
- print ("☀️ SOL Detailed Analysis:" )
96
- print (f" 💰 Price: ${ sol_stats ['price' ]:.2f} " )
97
- print (f" 📊 Confidence: { sol_stats ['confidence' ]:.0%} " )
98
- print (f" 📈 Trades analyzed: { sol_stats ['trades_analyzed' ]} " )
99
- print (f" 📉 Volatility: ${ sol_stats ['std_deviation' ]:.2f} " )
100
- print (f" 📋 Range: ${ sol_stats ['min_price' ]:.2f} - ${ sol_stats ['max_price' ]:.2f} " )
101
-
102
- # Confidence interpretation
103
- conf = sol_stats ["confidence" ]
104
- if conf >= 0.8 :
105
- print (" 🟢 High confidence - excellent data quality" )
106
- elif conf >= 0.5 :
107
- print (" 🟡 Medium confidence - good data quality" )
108
- else :
109
- print (" 🟠 Low confidence - limited data available" )
110
- else :
111
- print ("❌ SOL detailed analysis unavailable" )
112
-
113
- # ===== Cache Performance Demo =====
114
- print ("\n ⚡ Smart Caching Demo" )
115
- print ("-" * 20 )
116
-
117
- # First call (fetches from DEX data)
118
- print ("🌐 First call (fetching from DEX)..." )
119
- start = time .time ()
120
- price1 = await api .price .get (Currency .ETH )
121
- time1 = time .time () - start
122
-
123
- # Second call (uses cache)
124
- print ("⚡ Second call (using cache)..." )
125
- start = time .time ()
126
- price2 = await api .price .get (Currency .ETH )
127
- time2 = time .time () - start
128
-
129
- if price1 and price2 :
130
- print ("📊 Results:" )
131
- print (f" 🌐 API call: ${ price1 :.2f} - { time1 :.2f} s" )
132
- print (f" ⚡ Cached: ${ price2 :.2f} - { time2 :.3f} s" )
133
- if time1 > time2 :
134
- print (f" 🚀 Speedup: { time1 / time2 :.0f} x faster!" )
135
-
136
- # ===== Supported Currencies =====
137
- print ("\n 🗂️ Supported Currencies" )
138
- print ("-" * 21 )
139
-
140
- supported = await api .price .get_supported_currencies ()
141
- print ("✅ Currently supported:" )
142
- for currency in supported :
143
- print (f" • CURRENCY.{ currency } " )
144
-
145
- # ===== Error Handling Demo =====
146
- print ("\n 🛡️ Error Handling" )
147
- print ("-" * 17 )
148
-
149
- # Demo enum-only interface - no string acceptance
150
- print ("✅ API accepts only Currency enums - no backward compatibility" )
151
- print (" Example: Currency.ETH, Currency.SOL, Currency.POL" )
152
-
153
- # Check supported currencies
154
- print (f"🪙 Currently supported: { ', ' .join ([c .value for c in await api .price .get_supported_currencies ()])} " )
155
-
156
- print ("\n 🎉 Unified Price API Demo Complete!" )
157
- print ("\n 💡 Key Features Demonstrated:" )
158
- print (" • Simple Currency.SYMBOL interface" )
159
- print (" • Multi-blockchain support (Ethereum + Solana)" )
160
- print (" • Smart caching with volatility-based TTL" )
161
- print (" • Detailed statistical analysis" )
162
- print (" • Automatic outlier filtering" )
163
- print (" • Progressive retry with adaptive sampling" )
164
- print (" • Robust error handling" )
165
-
166
- print ("\n 📝 Example usage patterns:" )
167
- print (" price = await api.price.get(Currency.ETH)" )
168
- print (" stats = await api.price.get(Currency.SOL, include_stats=True)" )
169
- print (" supported = await api.price.get_supported_currencies()" )
170
-
171
- except Exception as e :
187
+ except (ConnectionError , TimeoutError , ValueError ) as e :
172
188
print (f"❌ Demo failed with error: { e } " )
173
189
traceback .print_exc ()
190
+ except Exception as e : # noqa: BLE001
191
+ print (f"❌ Demo failed with unexpected error: { e } " )
192
+ traceback .print_exc ()
174
193
175
194
176
195
if __name__ == "__main__" :
0 commit comments