diff --git a/lib/buy/buy_provider.dart b/lib/buy/buy_provider.dart index adf0f7fc2..0700a8641 100644 --- a/lib/buy/buy_provider.dart +++ b/lib/buy/buy_provider.dart @@ -1,6 +1,7 @@ import 'package:cake_wallet/buy/buy_amount.dart'; import 'package:cake_wallet/buy/order.dart'; import 'package:cw_core/wallet_base.dart'; +import 'package:cw_core/wallet_type.dart'; import 'package:flutter/material.dart'; abstract class BuyProvider { @@ -32,4 +33,6 @@ abstract class BuyProvider { Future findOrderById(String id) => throw UnimplementedError(); Future calculateAmount(String amount, String sourceCurrency) => throw UnimplementedError(); + + List getSupportedWalletTypes(bool isBuy) => throw UnimplementedError(); } diff --git a/lib/buy/dfx/dfx_buy_provider.dart b/lib/buy/dfx/dfx_buy_provider.dart index 189300c2d..a4549cbbe 100644 --- a/lib/buy/dfx/dfx_buy_provider.dart +++ b/lib/buy/dfx/dfx_buy_provider.dart @@ -73,6 +73,23 @@ class DFXBuyProvider extends BuyProvider { } } + @override + List getSupportedWalletTypes(bool isBuy) { + if (isBuy) { + return [ + WalletType.bitcoin, + WalletType.ethereum, + WalletType.polygon, + WalletType.monero, + ]; + } + return [ + WalletType.bitcoin, + WalletType.ethereum, + WalletType.polygon, + ]; + } + String get walletAddress => wallet.walletAddresses.primaryAddress ?? wallet.walletAddresses.address; diff --git a/lib/buy/meld/meld_provider.dart b/lib/buy/meld/meld_provider.dart index 8024777e5..c1b0782da 100644 --- a/lib/buy/meld/meld_provider.dart +++ b/lib/buy/meld/meld_provider.dart @@ -45,6 +45,31 @@ class MeldProvider extends BuyProvider { @override String get darkIcon => lightIcon; + @override + List getSupportedWalletTypes(bool isBuy) { + if (isBuy) { + return [ + WalletType.bitcoin, + WalletType.ethereum, + WalletType.polygon, + WalletType.monero, + WalletType.litecoin, + WalletType.bitcoinCash, + WalletType.nano, + WalletType.banano, + WalletType.solana, + ]; + } + return [ + WalletType.bitcoin, + WalletType.ethereum, + WalletType.polygon, + WalletType.solana, + WalletType.litecoin, + WalletType.bitcoinCash, + ]; + } + String get currencyCode => walletTypeToCryptoCurrency(wallet.type).title.toLowerCase(); static String convertTheme(ThemeBase theme) { diff --git a/lib/buy/moonpay/moonpay_provider.dart b/lib/buy/moonpay/moonpay_provider.dart index d04b2504d..fcb0750f3 100644 --- a/lib/buy/moonpay/moonpay_provider.dart +++ b/lib/buy/moonpay/moonpay_provider.dart @@ -7,14 +7,11 @@ import 'package:cake_wallet/buy/buy_provider.dart'; import 'package:cake_wallet/buy/buy_provider_description.dart'; import 'package:cake_wallet/buy/order.dart'; import 'package:cake_wallet/exchange/trade_state.dart'; -import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/palette.dart'; import 'package:cake_wallet/routes.dart'; -import 'package:cake_wallet/src/widgets/alert_with_one_action.dart'; import 'package:cake_wallet/store/settings_store.dart'; import 'package:cake_wallet/themes/theme_base.dart'; import 'package:cake_wallet/utils/device_info.dart'; -import 'package:crypto/crypto.dart'; import 'package:cw_core/crypto_currency.dart'; import 'package:cw_core/wallet_base.dart'; import 'package:cw_core/wallet_type.dart'; @@ -57,6 +54,28 @@ class MoonPayProvider extends BuyProvider { @override String get darkIcon => 'assets/images/moonpay_dark.png'; + @override + List getSupportedWalletTypes(bool isBuy) { + if (isBuy) { + return [ + WalletType.bitcoin, + WalletType.bitcoinCash, + WalletType.litecoin, + WalletType.polygon, + WalletType.ethereum, + WalletType.litecoin, + WalletType.solana, + WalletType.nano, + ]; + } + return [ + WalletType.bitcoin, + WalletType.ethereum, + WalletType.polygon, + WalletType.solana, + ]; + } + static String themeToMoonPayTheme(ThemeBase theme) { switch (theme.type) { case ThemeType.bright: diff --git a/lib/buy/onramper/onramper_buy_provider.dart b/lib/buy/onramper/onramper_buy_provider.dart index 35058ac7a..cc442aeed 100644 --- a/lib/buy/onramper/onramper_buy_provider.dart +++ b/lib/buy/onramper/onramper_buy_provider.dart @@ -7,6 +7,7 @@ import 'package:cake_wallet/themes/extensions/cake_text_theme.dart'; import 'package:cake_wallet/utils/device_info.dart'; import 'package:cw_core/crypto_currency.dart'; import 'package:cw_core/wallet_base.dart'; +import 'package:cw_core/wallet_type.dart'; import 'package:flutter/material.dart'; import 'package:url_launcher/url_launcher.dart'; @@ -34,6 +35,29 @@ class OnRamperBuyProvider extends BuyProvider { @override String get darkIcon => 'assets/images/onramper_dark.png'; + @override + List getSupportedWalletTypes(bool isBuy) { + if (isBuy) { + return [ + WalletType.bitcoin, + WalletType.ethereum, + WalletType.polygon, + WalletType.monero, + WalletType.litecoin, + WalletType.bitcoinCash, + WalletType.nano, + WalletType.banano, + WalletType.solana, + ]; + } + return [ + WalletType.bitcoin, + WalletType.ethereum, + WalletType.polygon, + WalletType.solana, + ]; + } + String get _apiKey => secrets.onramperApiKey; String get _normalizeCryptoCurrency { diff --git a/lib/buy/robinhood/robinhood_buy_provider.dart b/lib/buy/robinhood/robinhood_buy_provider.dart index 12195a9fd..9d4f70e22 100644 --- a/lib/buy/robinhood/robinhood_buy_provider.dart +++ b/lib/buy/robinhood/robinhood_buy_provider.dart @@ -33,6 +33,23 @@ class RobinhoodBuyProvider extends BuyProvider { @override String get darkIcon => 'assets/images/robinhood_dark.png'; + @override + List getSupportedWalletTypes(bool isBuy) { + if (isBuy) { + return [ + WalletType.bitcoin, + WalletType.ethereum, + WalletType.polygon, + WalletType.litecoin, + WalletType.bitcoinCash, + WalletType.solana, + ]; + } + return [ + WalletType.solana, + ]; + } + String get _applicationId => secrets.robinhoodApplicationId; String get _apiSecret => secrets.exchangeHelperApiKey; diff --git a/lib/buy/wyre/wyre_buy_provider.dart b/lib/buy/wyre/wyre_buy_provider.dart index 016bcf06b..3174cc2a5 100644 --- a/lib/buy/wyre/wyre_buy_provider.dart +++ b/lib/buy/wyre/wyre_buy_provider.dart @@ -45,6 +45,11 @@ class WyreBuyProvider extends BuyProvider { @override String get darkIcon => 'assets/images/robinhood_dark.png'; + @override + List getSupportedWalletTypes(bool isBuy) { + return []; + } + String get trackUrl => isTestEnvironment ? _trackTestUrl : _trackProductUrl; String baseApiUrl; diff --git a/lib/entities/provider_types.dart b/lib/entities/provider_types.dart index 1416edcfb..3c4d60bbb 100644 --- a/lib/entities/provider_types.dart +++ b/lib/entities/provider_types.dart @@ -54,86 +54,27 @@ extension ProviderTypeName on ProviderType { class ProvidersHelper { static List getAvailableBuyProviderTypes(WalletType walletType) { - switch (walletType) { - case WalletType.nano: - case WalletType.banano: - return [ - ProviderType.askEachTime, - ProviderType.onramper, - ProviderType.meld, - ]; - case WalletType.monero: - return [ProviderType.askEachTime, ProviderType.onramper, ProviderType.dfx]; - case WalletType.bitcoin: - case WalletType.polygon: - case WalletType.ethereum: - return [ - ProviderType.askEachTime, - ProviderType.onramper, - ProviderType.dfx, - ProviderType.robinhood, - ProviderType.moonpay, - ProviderType.meld, - ]; - case WalletType.litecoin: - case WalletType.bitcoinCash: - return [ - ProviderType.askEachTime, - ProviderType.onramper, - ProviderType.robinhood, - ProviderType.moonpay, - ProviderType.meld, - ]; - case WalletType.solana: - return [ - ProviderType.askEachTime, - ProviderType.onramper, - ProviderType.robinhood, - ProviderType.meld, - ]; - case WalletType.none: - case WalletType.haven: - return []; + final providers = []; + for (final providerType in ProviderType.values) { + final dynamic p = getProviderTypeByType(providerType); + final supportedWalletTypes = p.getSupportedWalletTypes(true) as List; + if (supportedWalletTypes.contains(walletType)) { + providers.add(providerType); + } } + return providers; } static List getAvailableSellProviderTypes(WalletType walletType) { - switch (walletType) { - case WalletType.bitcoin: - case WalletType.ethereum: - case WalletType.polygon: - return [ - ProviderType.askEachTime, - ProviderType.onramper, - ProviderType.moonpay, - ProviderType.dfx, - ProviderType.meld, - ]; - case WalletType.litecoin: - case WalletType.bitcoinCash: - return [ - ProviderType.askEachTime, - ProviderType.moonpay, - ProviderType.meld, - ]; - case WalletType.solana: - return [ - ProviderType.askEachTime, - ProviderType.onramper, - ProviderType.robinhood, - ProviderType.moonpay, - ProviderType.meld, - ]; - case WalletType.nano: - return [ - ProviderType.meld, - ]; - case WalletType.banano: - case WalletType.monero: - case WalletType.none: - case WalletType.haven: - return []; + final providers = []; + for (final providerType in ProviderType.values) { + final dynamic p = getProviderTypeByType(providerType); + final supportedWalletTypes = p.getSupportedWalletTypes(false) as List; + if (supportedWalletTypes.contains(walletType)) { + providers.add(providerType); + } } + return providers; } static BuyProvider? getProviderByType(ProviderType type) { @@ -152,4 +93,21 @@ class ProvidersHelper { return null; } } + + static Type? getProviderTypeByType(ProviderType type) { + switch (type) { + case ProviderType.robinhood: + return RobinhoodBuyProvider; + case ProviderType.dfx: + return DFXBuyProvider; + case ProviderType.onramper: + return OnRamperBuyProvider; + case ProviderType.moonpay: + return MoonPayProvider; + case ProviderType.meld: + return MeldProvider; + case ProviderType.askEachTime: + return null; + } + } }