|
| 1 | +import 'dart:io'; |
| 2 | + |
| 3 | +import 'package:blockchain_utils/bip/bip/conf/core/coin_conf.dart'; |
| 4 | +import 'package:blockchain_utils/helper/extensions/extensions.dart'; |
| 5 | +import 'package:blockchain_utils/service/service.dart'; |
| 6 | +import 'package:blockchain_utils/utils/string/string.dart'; |
| 7 | +import 'package:cosmos_sdk/cosmos_sdk.dart'; |
| 8 | +import 'package:http/http.dart' as http; |
| 9 | + |
| 10 | +class ChainRegistryHTTPProvider implements ChainRegistryServiceProvider { |
| 11 | + ChainRegistryHTTPProvider( |
| 12 | + {this.url = CCRConst.chainRegisteryUri, |
| 13 | + http.Client? client, |
| 14 | + this.defaultRequestTimeout = const Duration(seconds: 30)}) |
| 15 | + : client = client ?? http.Client(); |
| 16 | + |
| 17 | + final String url; |
| 18 | + final http.Client client; |
| 19 | + final Duration defaultRequestTimeout; |
| 20 | + |
| 21 | + @override |
| 22 | + Future<BaseServiceResponse<T>> doRequest<T>( |
| 23 | + ChainRegistryRequestRequestDetails params, |
| 24 | + {Duration? timeout}) async { |
| 25 | + final uri = params.toUri(url); |
| 26 | + final response = await client.get(uri, |
| 27 | + headers: {...params.headers}).timeout(timeout ?? defaultRequestTimeout); |
| 28 | + return params.parseResponse(response.bodyBytes, response.statusCode); |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +void main() async { |
| 33 | + const List<ChainType> chains = [ChainType.mainnet, ChainType.testnet]; |
| 34 | + |
| 35 | + Map<String, dynamic> chainInfos = { |
| 36 | + ChainType.mainnet.name: [], |
| 37 | + ChainType.testnet.name: [] |
| 38 | + }; |
| 39 | + |
| 40 | + String getUrl(ChainType type) { |
| 41 | + switch (type) { |
| 42 | + case ChainType.testnet: |
| 43 | + return CCRConst.cosmosTestnetDirectoryUri; |
| 44 | + case ChainType.mainnet: |
| 45 | + return CCRConst.cosmosDirectoryUri; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + for (final chain in chains) { |
| 50 | + final provider = |
| 51 | + ChainRegistryProvider(ChainRegistryHTTPProvider(url: getUrl(chain))); |
| 52 | + final chains = |
| 53 | + await provider.request(ChainRegistryRequestCosmosDirectoryChains()); |
| 54 | + print("chains done!"); |
| 55 | + final List<Map<String, dynamic>> infos = []; |
| 56 | + for (final i in chains) { |
| 57 | + final data = await provider |
| 58 | + .request(ChainRegistryRequestCosmosDirectoryChain(chainName: i.name)); |
| 59 | + final natvieAsset = data.assets?.firstWhere((e) => e.denom == i.denom); |
| 60 | + if (natvieAsset == null) { |
| 61 | + print("native not found ${i.chainId}"); |
| 62 | + continue; |
| 63 | + } |
| 64 | + List<String> accountPageUrls = []; |
| 65 | + List<String> txPageUrls = []; |
| 66 | + for (final e in i.explorers) { |
| 67 | + if (e.accountPage != null) { |
| 68 | + accountPageUrls.add(e.accountPage! |
| 69 | + .replaceFirst("\${accountAddress}", "#address") |
| 70 | + .replaceFirst(r"{$accountAddress}", "#address")); |
| 71 | + } |
| 72 | + if (e.txPage != null) { |
| 73 | + txPageUrls.add(e.txPage! |
| 74 | + .replaceFirst("\${txHash}", "#txid") |
| 75 | + .replaceFirst("#txId", "#txid")); |
| 76 | + } |
| 77 | + } |
| 78 | + final Map<String, dynamic> chainData = { |
| 79 | + "name": i.name, |
| 80 | + "chain_name": i.prettyName ?? i.chainName, |
| 81 | + "chain_id": i.chainId, |
| 82 | + "bech32_prefix": i.bech32Prefix, |
| 83 | + "type": chain.name, |
| 84 | + "native": { |
| 85 | + "name": natvieAsset.name, |
| 86 | + "denom": natvieAsset.denom, |
| 87 | + "decimals": natvieAsset.decimals, |
| 88 | + "symbol": natvieAsset.symbol, |
| 89 | + if (natvieAsset.logoURIs?.png != null) |
| 90 | + "png_logo": natvieAsset.logoURIs?.png, |
| 91 | + if (natvieAsset.logoURIs?.svg != null) |
| 92 | + "svg_logo": natvieAsset.logoURIs?.svg, |
| 93 | + if (natvieAsset.coingeckoId != null) |
| 94 | + "coingecko_id": natvieAsset.coingeckoId |
| 95 | + }, |
| 96 | + "best_apis": i.bestApis.toJson(), |
| 97 | + "explorers": { |
| 98 | + "account_page": |
| 99 | + accountPageUrls.firstWhereNullable((e) => !e.contains(r"${")), |
| 100 | + "tx_page": txPageUrls.firstWhereNullable((e) => !e.contains(r"${")) |
| 101 | + }, |
| 102 | + "slip44": i.slip44, |
| 103 | + "path": i.path |
| 104 | + }; |
| 105 | + |
| 106 | + if (data.keyAlgos.isNotEmpty) { |
| 107 | + chainData["algs"] = data.keyAlgos; |
| 108 | + } |
| 109 | + List<Map<String, dynamic>> fees = []; |
| 110 | + for (final i in data.fees.feeTokens) { |
| 111 | + final asset = data.assets?.firstWhere((e) => e.denom == i.denom); |
| 112 | + assert(asset != null); |
| 113 | + if (asset == null) continue; |
| 114 | + final f = { |
| 115 | + "name": asset.name, |
| 116 | + "denom": asset.denom, |
| 117 | + "decimals": asset.decimals, |
| 118 | + "symbol": asset.symbol, |
| 119 | + if (asset.logoURIs?.png != null) "png_logo": asset.logoURIs?.png, |
| 120 | + if (asset.logoURIs?.svg != null) "svg_logo": asset.logoURIs?.svg, |
| 121 | + if (asset.coingeckoId != null) "coingecko_id": asset.coingeckoId |
| 122 | + }..removeWhere((k, v) => v == null); |
| 123 | + final fee = {...f, ...i.toJson()}; |
| 124 | + fees.add(fee); |
| 125 | + } |
| 126 | + if (fees.isEmpty) { |
| 127 | + print("noo fee ${data.chainId}"); |
| 128 | + continue; |
| 129 | + } |
| 130 | + chainData["fees"] = fees; |
| 131 | + infos.add(chainData); |
| 132 | + print("done ${data.chainId}"); |
| 133 | + } |
| 134 | + chainInfos[chain.name] = infos; |
| 135 | + } |
| 136 | + final file = File('./assets/chains.json'); |
| 137 | + await file.writeAsString(StringUtils.fromJson(chainInfos)); |
| 138 | +} |
0 commit comments