Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ✨ Implementation of GraphQL method: chainUnspentOutputs #177

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
# 7.2.0
- Implementation of GraphQL method: `chainUnspentOutputs`

# 7.1.0
- Integrate Transaction Version 4 features

Expand Down
3 changes: 3 additions & 0 deletions lib/src/model/transaction.dart
Original file line number Diff line number Diff line change
Expand Up @@ -704,4 +704,7 @@ class Transaction with _$Transaction {

static const String kContractQueryAllFields =
' data { code, contract { bytecode, manifest { abi { functions, state } } } } ';

static const String kUnspentOutputQueryFieldsWithoutState =
' amount, from, timestamp, tokenAddress, tokenId, type';
}
64 changes: 64 additions & 0 deletions lib/src/services/api_service.dart
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import 'package:archethic_lib_dart/src/model/transaction.dart';
import 'package:archethic_lib_dart/src/model/transaction_fee.dart';
import 'package:archethic_lib_dart/src/model/transaction_input.dart';
import 'package:archethic_lib_dart/src/model/transaction_status.dart';
import 'package:archethic_lib_dart/src/model/unspent_outputs.dart';
import 'package:archethic_lib_dart/src/services/graph_ql_client_logger.dart';
import 'package:archethic_lib_dart/src/utils/collection_utils.dart';
import 'package:archethic_lib_dart/src/utils/crypto.dart';
Expand Down Expand Up @@ -985,4 +986,67 @@ class ApiService with JsonRPCUtil {
exception.toString(),
);
}

/// Query the network to retrieve the unspent output of a chain (address should be the genesis address of the chain)
Future<Map<String, List<UnspentOutputs>>> chainUnspentOutputs(
List<String> genesisAddresses, {
String request = Transaction.kUnspentOutputQueryFieldsWithoutState,
int limit = 0,
// pagingOffset should be a Sha256Hash
String pagingOffset = '',
}) async {
if (genesisAddresses.isEmpty) {
return {};
}

final fragment = 'fragment fields on UnspentOutput { $request }';
final body = StringBuffer()..write('query { ');
for (final genesisAddress in genesisAddresses) {
body.write(
' _$genesisAddress: chainUnspentOutputs(address:"$genesisAddress" ',
);
if (limit > 0) {
body.write(
' limit:$limit ',
);
}
if (pagingOffset.isNotEmpty) {
body.write(
' pagingOffset:$pagingOffset ',
);
}
body.write(
' ) { ...fields } ',
);
}
body.write(' } $fragment');

final result = await _client
.withLogger(
'chainUnspentOutputs',
)
.query(
QueryOptions(
document: gql(body.toString()),
parserFn: (json) {
final unspentOutputs = json.mapValues(
(unspentOutputs) => (unspentOutputs as List<dynamic>)
.map(
(unspentOutput) => UnspentOutputs.fromJson(
unspentOutput as Map<String, dynamic>,
),
)
.toList(),
keysToIgnore: _responseKeysToIgnore,
);
return removeAliasPrefix<List<UnspentOutputs>>(
unspentOutputs,
) ??
{};
},
),
);
manageLinkException(result);
return result.parsedData ?? {};
}
}
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ name: archethic_lib_dart
description: Archethic dart library for Flutter for Node and Browser. This library aims to provide a easy way to create Archethic transaction and to send them over the network
homepage: https://github.com/archethic-foundation/libdart

version: 7.1.0
version: 7.2.0

environment:
sdk: ">=3.5.3 <4.0.0"
Expand Down
23 changes: 23 additions & 0 deletions test/api_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -611,4 +611,27 @@ void main() {
});
},
);

test('chainUnspentOutputs', () async {
final chainUnspentOutputsList =
await ApiService('https://testnet.archethic.net').chainUnspentOutputs([
'00000bc0eba2dbce4455b46f5e51afaaba6eb4c7fba1d4e2e6dabd55dc70f9a04d6f',
'00003eeab3636e4017e961285f6f51c62db39a5ceb52dc5f1f8741b0b3b63ba00fd5',
]);

expect(
chainUnspentOutputsList.keys.first.length,
greaterThan(0),
);

expect(
chainUnspentOutputsList.keys.last.length,
greaterThan(0),
);

expect(
chainUnspentOutputsList.length,
2,
);
});
}
Loading