Skip to content
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
27 changes: 18 additions & 9 deletions packages/komodo_coins/lib/src/asset_filter.dart
Original file line number Diff line number Diff line change
@@ -1,15 +1,24 @@
import 'package:equatable/equatable.dart';
import 'package:komodo_defi_types/komodo_defi_type_utils.dart';
import 'package:komodo_defi_types/komodo_defi_types.dart';

/// Strategy interface for filtering assets based on coin configuration.
abstract class AssetFilterStrategy {
abstract class AssetFilterStrategy extends Equatable {
const AssetFilterStrategy(this.name);

/// A unique name for the strategy used for comparison and caching.
final String name;

/// Returns `true` if the asset should be included.
bool shouldInclude(Asset asset, JsonMap coinConfig);

@override
List<Object?> get props => [name];
}

/// Default strategy that includes all assets.
class NoAssetFilterStrategy implements AssetFilterStrategy {
const NoAssetFilterStrategy();
class NoAssetFilterStrategy extends AssetFilterStrategy {
const NoAssetFilterStrategy() : super('none');

@override
bool shouldInclude(Asset asset, JsonMap coinConfig) => true;
Expand All @@ -21,8 +30,8 @@ class NoAssetFilterStrategy implements AssetFilterStrategy {
/// activate on Trezor.
/// ERC20, Arbitrum, and MATIC explicitly do not support Trezor via KDF
/// at this time, so they are also excluded.
class TrezorAssetFilterStrategy implements AssetFilterStrategy {
const TrezorAssetFilterStrategy();
class TrezorAssetFilterStrategy extends AssetFilterStrategy {
const TrezorAssetFilterStrategy() : super('trezor');

@override
bool shouldInclude(Asset asset, JsonMap coinConfig) {
Expand All @@ -37,8 +46,8 @@ class TrezorAssetFilterStrategy implements AssetFilterStrategy {
}

/// Filters out assets that are not UTXO-based chains.
class UtxoAssetFilterStrategy implements AssetFilterStrategy {
const UtxoAssetFilterStrategy();
class UtxoAssetFilterStrategy extends AssetFilterStrategy {
const UtxoAssetFilterStrategy() : super('utxo');

@override
bool shouldInclude(Asset asset, JsonMap coinConfig) {
Expand All @@ -51,8 +60,8 @@ class UtxoAssetFilterStrategy implements AssetFilterStrategy {
/// This includes various EVM-compatible chains like Ethereum, Binance, etc.
/// This strategy is necessary for external wallets like Metamask or
/// WalletConnect.
class EvmAssetFilterStrategy implements AssetFilterStrategy {
const EvmAssetFilterStrategy();
class EvmAssetFilterStrategy extends AssetFilterStrategy {
const EvmAssetFilterStrategy() : super('evm');

@override
bool shouldInclude(Asset asset, JsonMap coinConfig) =>
Expand Down
13 changes: 10 additions & 3 deletions packages/komodo_coins/lib/src/komodo_coins_base.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class KomodoCoins {
}

Map<AssetId, Asset>? _assets;
final Map<String, Map<AssetId, Asset>> _filterCache = {};

@mustCallSuper
Future<void> init() async {
Expand Down Expand Up @@ -81,9 +82,10 @@ class KomodoCoins {
coinData,
knownIds: platformIds,
).map(
(id) => id.isChildAsset
? AssetId.parse(coinData, knownIds: platformIds)
: id,
(id) =>
id.isChildAsset
? AssetId.parse(coinData, knownIds: platformIds)
: id,
);

// Create Asset instance for each valid AssetId
Expand Down Expand Up @@ -124,13 +126,18 @@ class KomodoCoins {
if (!isInitialized) {
throw StateError('Assets have not been initialized. Call init() first.');
}
final cacheKey = strategy.name;
final cached = _filterCache[cacheKey];
if (cached != null) return cached;

final result = <AssetId, Asset>{};
for (final entry in _assets!.entries) {
final config = entry.value.protocol.config;
if (strategy.shouldInclude(entry.value, config)) {
result[entry.key] = entry.value;
}
}
_filterCache[cacheKey] = result;
return result;
}

Expand Down
3 changes: 3 additions & 0 deletions packages/komodo_defi_sdk/lib/src/assets/asset_manager.dart
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ class AssetManager implements IAssetProvider {
late final AssetIdMap _orderedCoins;
StreamSubscription<KdfUser?>? _authSubscription;
bool _isDisposed = false;
AssetFilterStrategy? _currentFilterStrategy;

/// NB: This cannot be used during initialization. This is a workaround
/// to publicly expose the activation manager's activation methods.
Expand Down Expand Up @@ -95,9 +96,11 @@ class AssetManager implements IAssetProvider {
}

void _refreshCoins(AssetFilterStrategy strategy) {
if (_currentFilterStrategy?.name == strategy.name) return;
_orderedCoins
..clear()
..addAll(_coins.filteredAssets(strategy));
_currentFilterStrategy = strategy;
}

/// Applies a new [strategy] for filtering available assets.
Expand Down
Loading