| 1 | import 'dart:async'; |
| 2 | import 'dart:convert'; |
| 3 | import 'dart:io'; |
| 4 | import 'package:http/http.dart' as http; // very_insecure_http_do_not_use |
| 5 | import 'package:cake_wallet/.secrets.g.dart' as secrets; |
| 6 | import './print_verbose_dummy.dart'; |
| 7 | |
| 8 | // 1. Configuration |
| 9 | const _fiatApiClearNetAuthority = 'fiat-api.cakewallet.com'; |
| 10 | const _fiatApiPath = '/v2/rates'; |
| 11 | const _apiKey = secrets.fiatApiKey; |
| 12 | |
| 13 | // 2. Define Lists |
| 14 | const List<String> cryptoCurrencies = [ |
| 15 | 'btc', |
| 16 | 'ltc', |
| 17 | 'xmr', |
| 18 | 'bch', |
| 19 | 'doge', |
| 20 | 'eth', |
| 21 | 'pol', |
| 22 | 'sol', |
| 23 | 'xno', |
| 24 | 'trx', |
| 25 | 'dcr', |
| 26 | 'zano', |
| 27 | 'wow', |
| 28 | 'arb', |
| 29 | 'usdt', |
| 30 | 'pepe', |
| 31 | 'zec', |
| 32 | 'bnb', |
| 33 | 'xrp', |
| 34 | 'ada', |
| 35 | 'avax', |
| 36 | 'shib', |
| 37 | 'ton', |
| 38 | 'dot', |
| 39 | 'link', |
| 40 | 'uni', |
| 41 | 'near', |
| 42 | 'atom', |
| 43 | 'xlm', |
| 44 | 'stx', |
| 45 | 'kas', |
| 46 | 'dai', |
| 47 | ]; |
| 48 | |
| 49 | const List<String> fiatCurrencies = [ |
| 50 | 'usd', |
| 51 | 'eur', |
| 52 | 'aud', |
| 53 | 'gbp', |
| 54 | 'jpy', |
| 55 | 'cad', |
| 56 | 'chf', |
| 57 | 'cny', |
| 58 | 'inr', |
| 59 | 'brl', |
| 60 | 'zar', |
| 61 | 'mxn', |
| 62 | 'krw', |
| 63 | 'hkd', |
| 64 | 'sgd', |
| 65 | 'nzd', |
| 66 | 'sek', |
| 67 | 'try' |
| 68 | ]; |
| 69 | |
| 70 | void main() { |
| 71 | // --- A. Setup the Output File --- |
| 72 | final logFile = File('fiat-check-output.txt'); |
| 73 | // Write a header to start fresh (overwrite old file) |
| 74 | logFile.writeAsStringSync('--- Starting Verified Price Check at ${DateTime.now()} ---\n'); |
| 75 | |
| 76 | // --- B. Run App in a Zone to Capture Prints --- |
| 77 | runZoned( |
| 78 | () async { |
| 79 | print('--- Starting Verified Price Check ---'); |
| 80 | |
| 81 | final Map<String, List<String>> workingPairs = {}; |
| 82 | final Map<String, List<String>> failedPairs = {}; |
| 83 | final client = http.Client(); |
| 84 | |
| 85 | try { |
| 86 | for (final crypto in cryptoCurrencies) { |
| 87 | workingPairs[crypto] = []; |
| 88 | failedPairs[crypto] = []; |
| 89 | |
| 90 | for (final fiat in fiatCurrencies) { |
| 91 | // Clean ticker logic |
| 92 | String cleanCrypto = crypto.split(".").first; |
| 93 | |
| 94 | final Map<String, String> queryParams = { |
| 95 | 'interval_count': '1', |
| 96 | 'base': cleanCrypto, |
| 97 | 'quote': fiat, |
| 98 | }; |
| 99 | |
| 100 | final uri = Uri.https(_fiatApiClearNetAuthority, _fiatApiPath, queryParams); |
| 101 | bool isSuccess = false; |
| 102 | String logPrefix = "❌"; |
| 103 | String logMessage = ""; |
| 104 | |
| 105 | try { |
| 106 | final response = await client.get(uri, headers: {"x-api-key": _apiKey}); |
| 107 | |
| 108 | if (response.statusCode == 200) { |
| 109 | final data = jsonDecode(response.body) as Map<String, dynamic>; |
| 110 | final Map<String, dynamic> results = data['results'] as Map<String, dynamic>? ?? {}; |
| 111 | final Map<String, dynamic> errors = data['errors'] as Map<String, dynamic>? ?? {}; |
| 112 | |
| 113 | if (results.isNotEmpty && errors.isEmpty) { |
| 114 | isSuccess = true; |
| 115 | logPrefix = "✅"; |
| 116 | final price = results.values.first; |
| 117 | logMessage = "${cleanCrypto.toUpperCase()}/${fiat.toUpperCase()} = $price"; |
| 118 | } else { |
| 119 | isSuccess = false; |
| 120 | logPrefix = "❌"; |
| 121 | logMessage = |
| 122 | "${cleanCrypto.toUpperCase()}/${fiat.toUpperCase()} returned empty results or error."; |
| 123 | } |
| 124 | } else { |
| 125 | logMessage = |
| 126 | "${cleanCrypto.toUpperCase()}/${fiat.toUpperCase()} HTTP ${response.statusCode}"; |
| 127 | } |
| 128 | } catch (e) { |
| 129 | logMessage = "${cleanCrypto.toUpperCase()}/${fiat.toUpperCase()} Error: $e"; |
| 130 | } |
| 131 | |
| 132 | // Print immediate status (Captured by Zone) |
| 133 | print('$logPrefix $logMessage'); |
| 134 | |
| 135 | // Aggregate |
| 136 | if (isSuccess) { |
| 137 | workingPairs[crypto]!.add(fiat); |
| 138 | } else { |
| 139 | failedPairs[crypto]!.add(fiat); |
| 140 | } |
| 141 | |
| 142 | // 50ms delay to prevent rate limiting |
| 143 | await Future.delayed(Duration(milliseconds: 50)); |
| 144 | } |
| 145 | } |
| 146 | } finally { |
| 147 | client.close(); |
| 148 | } |
| 149 | |
| 150 | // --- FINAL SUMMARY --- |
| 151 | print('\n\n=== SUMMARY ===\n'); |
| 152 | |
| 153 | // Print Successful |
| 154 | workingPairs.forEach((crypto, fiats) { |
| 155 | if (fiats.isNotEmpty) { |
| 156 | print('✅ ${crypto.toUpperCase()}: ${fiats.join(", ")}'); |
| 157 | } |
| 158 | }); |
| 159 | |
| 160 | print('\n--------------------------------------------------\n'); |
| 161 | |
| 162 | // Print Failed |
| 163 | failedPairs.forEach((crypto, fiats) { |
| 164 | if (fiats.isNotEmpty) { |
| 165 | print('❌ ${crypto.toUpperCase()}: ${fiats.join(", ")}'); |
| 166 | } |
| 167 | }); |
| 168 | |
| 169 | print('\n=== DONE ==='); |
| 170 | }, |
| 171 | |
| 172 | // --- C. The Interceptor --- |
| 173 | zoneSpecification: ZoneSpecification( |
| 174 | print: (self, parent, zone, line) { |
| 175 | // 1. Print to Standard Console |
| 176 | parent.print(zone, line); |
| 177 | |
| 178 | // 2. Append to Text File |
| 179 | // We use Sync to ensure data isn't lost if the script crashes |
| 180 | logFile.writeAsStringSync('$line\n', mode: FileMode.append); |
| 181 | }, |
| 182 | ), |
| 183 | ); |
| 184 | } |