getWalExchangePackageId method

Future<String> getWalExchangePackageId()

Resolve the WAL exchange package ID from the first exchange object.

Fetches the exchange object's type from chain and extracts the package address. The result is cached for subsequent calls.

Mirrors the TS SDK's approach: parseStructTag(exchange.type).address.

Throws StateError if no exchange IDs are configured.

Implementation

Future<String> getWalExchangePackageId() async {
  if (_walExchangePackageId != null) return _walExchangePackageId!;

  final exchangeIds = packageConfig.exchangeIds;
  if (exchangeIds == null || exchangeIds.isEmpty) {
    throw StateError(
      'No exchange IDs configured. WAL exchange is not available '
      'for this network configuration.',
    );
  }

  // Fetch the first exchange object to get its type.
  final obj = await suiClient.getObject(
    exchangeIds.first,
    options: SuiObjectDataOptions(showType: true),
  );

  final objectType = obj.data?.type;
  if (objectType == null) {
    throw StateError(
      'Could not resolve exchange object type for ${exchangeIds.first}',
    );
  }

  // Parse the struct type to extract the package address.
  // Type format: "0x<pkg>::wal_exchange::Exchange"
  final parts = objectType.split('::');
  if (parts.length < 2) {
    throw StateError('Unexpected exchange object type format: $objectType');
  }

  _walExchangePackageId = parts[0];
  return _walExchangePackageId!;
}