|
12 | 12 | tags=["onchain"],
|
13 | 13 | )
|
14 | 14 |
|
15 |
| - |
16 |
| -@app.get( |
17 |
| - "/{pair:path}", |
18 |
| - responses={ |
19 |
| - 200: {"description": "Successfully retrieved aggregated on-chain data"}, |
20 |
| - }, |
21 |
| - tags=["onchain"], |
22 |
| -) |
23 |
| -async def get_aggregated_onchain_data( |
24 |
| - pair: str = Path(..., description="Trading pair (e.g., btc/usd)"), |
25 |
| - network: str = Query("mainnet", description="Network name"), |
26 |
| - aggregation: str = Query("median", description="Aggregation method"), |
27 |
| - client: PragmaApiClient = Depends(get_api_client), |
28 |
| -): |
29 |
| - """Retrieve aggregated on-chain data for a specific pair and network.""" |
30 |
| - try: |
31 |
| - pair = pair.upper() |
32 |
| - data = await client.get_onchain_data_aggregated(pair, network, aggregation) |
33 |
| - |
34 |
| - base = pair.split("/")[0].lower() |
35 |
| - |
36 |
| - if not data or "error" in data: |
37 |
| - return { |
38 |
| - "image": f"/assets/currencies/{base}.svg", |
39 |
| - "type": "Crypto", |
40 |
| - "ticker": pair, |
41 |
| - "lastUpdated": "Error fetching data", |
42 |
| - "price": 0, |
43 |
| - "sources": 0, |
44 |
| - "variations": { |
45 |
| - "past1h": 0, |
46 |
| - "past24h": 0, |
47 |
| - "past7d": 0, |
48 |
| - }, |
49 |
| - "chart": "", |
50 |
| - "ema": "N/A", |
51 |
| - "macd": "N/A", |
52 |
| - "error": data.get("error") if data else "Failed to fetch data", |
53 |
| - "isUnsupported": False, |
54 |
| - } |
55 |
| - |
56 |
| - # Format successful response in the same structure |
57 |
| - return { |
58 |
| - "image": f"/assets/currencies/{base}.svg", |
59 |
| - "type": "Crypto", |
60 |
| - "ticker": pair, |
61 |
| - "lastUpdated": data.get("last_updated_timestamp", 0), |
62 |
| - "price": float(int(data.get("price", "0x0"), 16)) |
63 |
| - / (10 ** data.get("decimals", 8)), # Convert hex price to decimal |
64 |
| - "sources": data.get("nb_sources_aggregated", 0), |
65 |
| - "variations": { |
66 |
| - "past1h": data.get("variations", {}).get("1h", 0), |
67 |
| - "past24h": data.get("variations", {}).get("1d", 0), |
68 |
| - "past7d": data.get("variations", {}).get("1w", 0), |
69 |
| - }, |
70 |
| - "chart": "", |
71 |
| - "ema": "N/A", |
72 |
| - "macd": "N/A", |
73 |
| - "error": None, |
74 |
| - "isUnsupported": False, |
75 |
| - } |
76 |
| - |
77 |
| - except Exception as e: |
78 |
| - base = pair.split("/")[0].lower() |
79 |
| - return { |
80 |
| - "image": f"/assets/currencies/{base}.svg", |
81 |
| - "type": "Crypto", |
82 |
| - "ticker": pair, |
83 |
| - "lastUpdated": "Error fetching data", |
84 |
| - "price": 0, |
85 |
| - "sources": 0, |
86 |
| - "variations": { |
87 |
| - "past1h": 0, |
88 |
| - "past24h": 0, |
89 |
| - "past7d": 0, |
90 |
| - }, |
91 |
| - "chart": "", |
92 |
| - "ema": "N/A", |
93 |
| - "macd": "N/A", |
94 |
| - "error": str(e), |
95 |
| - "isUnsupported": False, |
96 |
| - } |
97 |
| - |
98 |
| - |
99 | 15 | @app.get(
|
100 | 16 | "/checkpoints",
|
101 | 17 | responses={
|
@@ -226,3 +142,86 @@ async def get_publishers(
|
226 | 142 | formatted_publishers.append(formatted_publisher)
|
227 | 143 |
|
228 | 144 | return formatted_publishers
|
| 145 | + |
| 146 | +@app.get( |
| 147 | + "/{pair:path}", |
| 148 | + responses={ |
| 149 | + 200: {"description": "Successfully retrieved aggregated on-chain data"}, |
| 150 | + }, |
| 151 | + tags=["onchain"], |
| 152 | +) |
| 153 | +async def get_aggregated_onchain_data( |
| 154 | + pair: str = Path(..., description="Trading pair (e.g., btc/usd)"), |
| 155 | + network: str = Query("mainnet", description="Network name"), |
| 156 | + aggregation: str = Query("median", description="Aggregation method"), |
| 157 | + client: PragmaApiClient = Depends(get_api_client), |
| 158 | +): |
| 159 | + """Retrieve aggregated on-chain data for a specific pair and network.""" |
| 160 | + try: |
| 161 | + pair = pair.upper() |
| 162 | + data = await client.get_onchain_data_aggregated(pair, network, aggregation) |
| 163 | + |
| 164 | + base = pair.split("/")[0].lower() |
| 165 | + |
| 166 | + if not data or "error" in data: |
| 167 | + return { |
| 168 | + "image": f"/assets/currencies/{base}.svg", |
| 169 | + "type": "Crypto", |
| 170 | + "ticker": pair, |
| 171 | + "lastUpdated": "Error fetching data", |
| 172 | + "price": 0, |
| 173 | + "sources": 0, |
| 174 | + "variations": { |
| 175 | + "past1h": 0, |
| 176 | + "past24h": 0, |
| 177 | + "past7d": 0, |
| 178 | + }, |
| 179 | + "chart": "", |
| 180 | + "ema": "N/A", |
| 181 | + "macd": "N/A", |
| 182 | + "error": data.get("error") if data else "Failed to fetch data", |
| 183 | + "isUnsupported": False, |
| 184 | + } |
| 185 | + |
| 186 | + # Format successful response in the same structure |
| 187 | + return { |
| 188 | + "image": f"/assets/currencies/{base}.svg", |
| 189 | + "type": "Crypto", |
| 190 | + "ticker": pair, |
| 191 | + "lastUpdated": data.get("last_updated_timestamp", 0), |
| 192 | + "price": float(int(data.get("price", "0x0"), 16)) |
| 193 | + / (10 ** data.get("decimals", 8)), # Convert hex price to decimal |
| 194 | + "sources": data.get("nb_sources_aggregated", 0), |
| 195 | + "variations": { |
| 196 | + "past1h": data.get("variations", {}).get("1h", 0), |
| 197 | + "past24h": data.get("variations", {}).get("1d", 0), |
| 198 | + "past7d": data.get("variations", {}).get("1w", 0), |
| 199 | + }, |
| 200 | + "chart": "", |
| 201 | + "ema": "N/A", |
| 202 | + "macd": "N/A", |
| 203 | + "error": None, |
| 204 | + "isUnsupported": False, |
| 205 | + } |
| 206 | + |
| 207 | + except Exception as e: |
| 208 | + base = pair.split("/")[0].lower() |
| 209 | + return { |
| 210 | + "image": f"/assets/currencies/{base}.svg", |
| 211 | + "type": "Crypto", |
| 212 | + "ticker": pair, |
| 213 | + "lastUpdated": "Error fetching data", |
| 214 | + "price": 0, |
| 215 | + "sources": 0, |
| 216 | + "variations": { |
| 217 | + "past1h": 0, |
| 218 | + "past24h": 0, |
| 219 | + "past7d": 0, |
| 220 | + }, |
| 221 | + "chart": "", |
| 222 | + "ema": "N/A", |
| 223 | + "macd": "N/A", |
| 224 | + "error": str(e), |
| 225 | + "isUnsupported": False, |
| 226 | + } |
| 227 | + |
0 commit comments