Skip to content

Commit

Permalink
refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
Tommytrg committed Dec 14, 2023
1 parent 87d60e6 commit 06ca66b
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 35 deletions.
12 changes: 7 additions & 5 deletions example/explorer_example.dart
Original file line number Diff line number Diff line change
@@ -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

70 changes: 40 additions & 30 deletions lib/src/network/explorer/explorer_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -103,44 +103,54 @@ class ExplorerClient {
}

Future<List<Utxo>> 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<dynamic> _utxos = response[address]['utxos'] as List<dynamic>;
List<Utxo> 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<dynamic> _utxos = response[address]['utxos'] as List<dynamic>;
List<Utxo> 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<Map<String, List<Utxo>>> getMultiUtxoInfo(
{required List<String> addresses}) async {
int addressLimit = 10;
Map<String, List<Utxo>> addressMap = {};
try {
int addressLimit = 10;
Map<String, List<Utxo>> addressMap = {};

List<Uri> urlCalls = [];
List<Uri> 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<Utxo> _utxos =
List<Utxo>.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<Utxo> _utxos =
List<Utxo>.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<dynamic> sendVTTransaction(VTTransaction transaction,
Expand Down

0 comments on commit 06ca66b

Please sign in to comment.