resolveNames method

Future<void> resolveNames({
  1. required Future<String> resolvePackage(
    1. String package
    ),
  2. required Future<String> resolveType(
    1. String package
    ),
})

Resolve Move Registry (@org/app) names in this transaction's moveCall commands — both the package name and any type arguments — using the given resolver callbacks. Rewrites the commands in place. No-op if no MVR names are present. Mirrors the TypeScript SDK's NamedPackagesPlugin.

Implementation

Future<void> resolveNames({
  required Future<String> Function(String package) resolvePackage,
  required Future<String> Function(String type) resolveType,
}) async {
  final packageFields = <Map>[];
  final typeFields = <(Map, String)>[];
  final typeArguments = <List>[];
  for (final command in _blockData.commands) {
    final call = command['MoveCall'];
    if (call is Map) {
      packageFields.add(call);
      typeArguments.add(call['typeArguments'] as List);
    }
    final intent = command[r'$Intent'];
    if (intent is Map &&
        const {
          'CoinWithBalance',
          'AllowanceBalance',
        }.contains(intent['name'])) {
      final data = intent['data'] as Map;
      typeFields.add((data, 'type'));
      if (data['appType'] != null) typeFields.add((data, 'appType'));
    }
  }
  for (final input in _blockData.inputs) {
    final type = input['FundsWithdrawal']?['typeArg'];
    if (type is Map) typeFields.add((type, 'Balance'));
  }
  final packages = <String, Future<String>>{};
  final types = <String, Future<String>>{};
  for (final call in packageFields) {
    final value = call['package'] as String;
    if (value.contains('@')) {
      call['package'] = await packages.putIfAbsent(
        value,
        () => resolvePackage(value),
      );
    }
  }
  for (final arguments in typeArguments) {
    for (var i = 0; i < arguments.length; i++) {
      final value = arguments[i] as String;
      if (value.contains('@')) {
        arguments[i] = await types.putIfAbsent(
          value,
          () => resolveType(value),
        );
      }
    }
  }
  for (final (map, key) in typeFields) {
    final value = map[key] as String;
    if (value.contains('@')) {
      map[key] = await types.putIfAbsent(value, () => resolveType(value));
    }
  }
}