From 5538c51dec98dd346c2d730e67bd9b5288bf9c33 Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Wed, 3 Apr 2024 10:32:54 -0700 Subject: [PATCH 1/2] fixup --- lib/buy/buy_provider.dart | 3 + lib/buy/dfx/dfx_buy_provider.dart | 18 ++++ lib/buy/moonpay/moonpay_provider.dart | 25 +++++- lib/buy/onramper/onramper_buy_provider.dart | 24 ++++++ lib/buy/robinhood/robinhood_buy_provider.dart | 17 ++++ lib/buy/wyre/wyre_buy_provider.dart | 5 ++ lib/entities/provider_types.dart | 83 ++++++++----------- 7 files changed, 122 insertions(+), 53 deletions(-) diff --git a/lib/buy/buy_provider.dart b/lib/buy/buy_provider.dart index 4e4c113f4..910eacf8e 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 { @@ -30,4 +31,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 8e2d58d11..68e21210f 100644 --- a/lib/buy/dfx/dfx_buy_provider.dart +++ b/lib/buy/dfx/dfx_buy_provider.dart @@ -70,6 +70,24 @@ class DFXBuyProvider extends BuyProvider { } } + @override + List getSupportedWalletTypes(bool isBuy) { + if (isBuy) { + return [ + WalletType.bitcoin, + WalletType.ethereum, + WalletType.polygon, + WalletType.monero, + WalletType.nano, + ]; + } + return [ + WalletType.bitcoin, + WalletType.ethereum, + WalletType.polygon, + ]; + } + String get walletAddress => wallet.walletAddresses.primaryAddress ?? wallet.walletAddresses.address; diff --git a/lib/buy/moonpay/moonpay_provider.dart b/lib/buy/moonpay/moonpay_provider.dart index 52a4f6187..cea3e4b43 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'; @@ -54,6 +51,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 3819f074d..525777922 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'; @@ -31,6 +32,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 7610e51f3..4740c103c 100644 --- a/lib/buy/robinhood/robinhood_buy_provider.dart +++ b/lib/buy/robinhood/robinhood_buy_provider.dart @@ -30,6 +30,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 4dd091c33..adeee0ab2 100644 --- a/lib/buy/wyre/wyre_buy_provider.dart +++ b/lib/buy/wyre/wyre_buy_provider.dart @@ -42,6 +42,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 701781cc2..dda9a87cd 100644 --- a/lib/entities/provider_types.dart +++ b/lib/entities/provider_types.dart @@ -48,61 +48,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]; - 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, - ]; - case WalletType.litecoin: - case WalletType.bitcoinCash: - return [ProviderType.askEachTime, ProviderType.onramper, ProviderType.robinhood, ProviderType.moonpay]; - case WalletType.solana: - return [ProviderType.askEachTime, ProviderType.onramper, ProviderType.robinhood]; - 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, - ]; - case WalletType.litecoin: - case WalletType.bitcoinCash: - return [ProviderType.askEachTime, ProviderType.moonpay]; - case WalletType.solana: - return [ - ProviderType.askEachTime, - ProviderType.onramper, - ProviderType.robinhood, - ProviderType.moonpay, - ]; - case WalletType.monero: - case WalletType.nano: - case WalletType.banano: - 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) { @@ -119,4 +85,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; + } + } } From 8f33124e004f1afd0989945dbddb8001387ce33f Mon Sep 17 00:00:00 2001 From: Matthew Fosse Date: Thu, 4 Apr 2024 09:09:35 -0700 Subject: [PATCH 2/2] minor fixes --- lib/buy/dfx/dfx_buy_provider.dart | 1 - 1 file changed, 1 deletion(-) diff --git a/lib/buy/dfx/dfx_buy_provider.dart b/lib/buy/dfx/dfx_buy_provider.dart index 68e21210f..f48d4dab6 100644 --- a/lib/buy/dfx/dfx_buy_provider.dart +++ b/lib/buy/dfx/dfx_buy_provider.dart @@ -78,7 +78,6 @@ class DFXBuyProvider extends BuyProvider { WalletType.ethereum, WalletType.polygon, WalletType.monero, - WalletType.nano, ]; } return [