Skip to content

Commit

Permalink
feat: ✨ Implementation of GraphQL method: chainUnspentOutputs
Browse files Browse the repository at this point in the history
  • Loading branch information
redDwarf03 committed Jan 15, 2025
1 parent 4f51487 commit 7ba0faf
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 1 deletion.
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,
);
});
}

0 comments on commit 7ba0faf

Please sign in to comment.