From 06ca66b0612b123d1f22b85a140aa8bfb811300c Mon Sep 17 00:00:00 2001 From: tommytrg Date: Thu, 14 Dec 2023 17:24:01 +0100 Subject: [PATCH] refactor --- example/explorer_example.dart | 12 ++-- lib/src/network/explorer/explorer_client.dart | 70 +++++++++++-------- 2 files changed, 47 insertions(+), 35 deletions(-) diff --git a/example/explorer_example.dart b/example/explorer_example.dart index fa82dfa..cde6da9 100644 --- a/example/explorer_example.dart +++ b/example/explorer_example.dart @@ -1,16 +1,18 @@ import 'package:witnet/explorer.dart'; -String explorerUrl = 'witnet.network'; +String explorerUrl = 'witscan.xyz'; ExplorerClient explorer = ExplorerClient(url: explorerUrl, mode: ExplorerMode.production); void main() async { try { - Status status = await explorer.status(); - PrioritiesEstimate priority = await explorer.valueTransferPriority(); - print("Status: ${status.databaseMessage}"); - print("Priority ${priority.vttHigh.priority}"); + dynamic status = await explorer.address(tab: "value_transfers", value: "wit1zl7ty0lwr7atp5fu34azkgewhtfx2fl4wv69cw", page: 1, pageSize: 1000 ); + print("Status: ${status}"); } on ExplorerException catch (e) { print(e.message); } } + +// https://witscan.xyz/api/address/value-transfers?address=wit1zl7ty0lwr7atp5fu34azkgewhtfx2fl4wv69cw&page=1&page_size=1 +// https://witscan.xyz/api/address/value-transfers?address=wit1zl7ty0lwr7atp5fu34azkgewhtfx2fl4wv69cw&page=1&page_size=100 + diff --git a/lib/src/network/explorer/explorer_client.dart b/lib/src/network/explorer/explorer_client.dart index 6d011c3..c13c574 100644 --- a/lib/src/network/explorer/explorer_client.dart +++ b/lib/src/network/explorer/explorer_client.dart @@ -103,44 +103,54 @@ class ExplorerClient { } Future> getUtxoInfo({required String address}) async { - Uri urlEndpoint = api('utxos', {'address': address}); - - // Await the http get response, then decode the json-formatted response. - var response = await client.get(urlEndpoint); - List _utxos = response[address]['utxos'] as List; - List utxos = []; - _utxos.forEach((element) { - utxos.add(Utxo.fromJson(element)); - }); + try { + Uri urlEndpoint = api('utxos', {'address': address}); + + // Await the http get response, then decode the json-formatted response. + var response = await client.get(urlEndpoint); + List _utxos = response[address]['utxos'] as List; + List utxos = []; + _utxos.forEach((element) { + utxos.add(Utxo.fromJson(element)); + }); - return utxos; + return utxos; + } on ExplorerException catch (e) { + throw ExplorerException( + code: e.code, message: '{"getUtxoInfo": "${e.message}"}'); + } } Future>> getMultiUtxoInfo( {required List addresses}) async { - int addressLimit = 10; - Map> addressMap = {}; + try { + int addressLimit = 10; + Map> addressMap = {}; - List urlCalls = []; + List urlCalls = []; - for (int i = 0; i < addresses.length; i += addressLimit) { - int end = (i + addressLimit < addresses.length) - ? i + addressLimit - : addresses.length; - urlCalls - .add(api('utxos', {'address': addresses.sublist(i, end).join(',')})); - } - // Await the http get response, then decode the json-formatted response. - for (int i = 0; i < urlCalls.length; i++) { - var response = await client.get(urlCalls[i]); - response.forEach((key, value) { - List _utxos = - List.from(value['utxos'].map((ut) => Utxo.fromJson(ut))); - addressMap[key] = _utxos; - }); - } + for (int i = 0; i < addresses.length; i += addressLimit) { + int end = (i + addressLimit < addresses.length) + ? i + addressLimit + : addresses.length; + urlCalls.add( + api('utxos', {'address': addresses.sublist(i, end).join(',')})); + } + // Await the http get response, then decode the json-formatted response. + for (int i = 0; i < urlCalls.length; i++) { + var response = await client.get(urlCalls[i]); + response.forEach((key, value) { + List _utxos = + List.from(value['utxos'].map((ut) => Utxo.fromJson(ut))); + addressMap[key] = _utxos; + }); + } - return addressMap; + return addressMap; + } on ExplorerException catch (e) { + throw ExplorerException( + code: e.code, message: '{"getMultiUtxoInfo": "${e.message}"}'); + } } Future sendVTTransaction(VTTransaction transaction,