diff --git a/lib/main.dart b/lib/main.dart index 2cf7e5911..18da983f6 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -35,6 +35,7 @@ import 'package:cake_wallet/src/domain/common/wallet_type.dart'; import 'package:cake_wallet/src/domain/services/wallet_service.dart'; import 'package:cake_wallet/generated/i18n.dart'; import 'package:cake_wallet/src/domain/common/language.dart'; +import 'package:cake_wallet/src/domain/common/setup_locator.dart'; void main() async { WidgetsFlutterBinding.ensureInitialized(); @@ -65,6 +66,8 @@ void main() async { await Hive.openBox(Trade.boxName, encryptionKey: tradesBoxKey); final walletInfoSource = await Hive.openBox(WalletInfo.boxName); + setupLocator(trades); + final sharedPreferences = await SharedPreferences.getInstance(); final walletService = WalletService(); final walletListService = WalletListService( diff --git a/lib/src/domain/bitcoin/bitcoin_amount_format.dart b/lib/src/domain/bitcoin/bitcoin_amount_format.dart new file mode 100644 index 000000000..c816dedc2 --- /dev/null +++ b/lib/src/domain/bitcoin/bitcoin_amount_format.dart @@ -0,0 +1,6 @@ +import 'package:cake_wallet/src/domain/common/crypto_amount_format.dart'; + +const bitcoinAmountDivider = 100000000; + +double bitcoinAmountToDouble({int amount}) => + cryptoAmountToDouble(amount: amount, divider: bitcoinAmountDivider); \ No newline at end of file diff --git a/lib/src/domain/bitcoin_cash/bitcoin_cash_amount_format.dart b/lib/src/domain/bitcoin_cash/bitcoin_cash_amount_format.dart new file mode 100644 index 000000000..bbb0192c8 --- /dev/null +++ b/lib/src/domain/bitcoin_cash/bitcoin_cash_amount_format.dart @@ -0,0 +1,6 @@ +import 'package:cake_wallet/src/domain/common/crypto_amount_format.dart'; + +const bitcoinCashAmountDivider = 100000000; + +double bitcoinCashAmountToDouble({int amount}) => + cryptoAmountToDouble(amount: amount, divider: bitcoinCashAmountDivider); \ No newline at end of file diff --git a/lib/src/domain/common/crypto_amount_format.dart b/lib/src/domain/common/crypto_amount_format.dart new file mode 100644 index 000000000..649ac45f5 --- /dev/null +++ b/lib/src/domain/common/crypto_amount_format.dart @@ -0,0 +1 @@ +double cryptoAmountToDouble({num amount, num divider}) => amount / divider; \ No newline at end of file diff --git a/lib/src/domain/common/setup_locator.dart b/lib/src/domain/common/setup_locator.dart new file mode 100644 index 000000000..f9c991596 --- /dev/null +++ b/lib/src/domain/common/setup_locator.dart @@ -0,0 +1,7 @@ +import 'package:get_it/get_it.dart'; + +GetIt locator = GetIt.instance; + +void setupLocator(T instance) { + locator.registerSingleton(instance); +} \ No newline at end of file diff --git a/lib/src/domain/dash/dash_amount_format.dart b/lib/src/domain/dash/dash_amount_format.dart new file mode 100644 index 000000000..3f1632cd6 --- /dev/null +++ b/lib/src/domain/dash/dash_amount_format.dart @@ -0,0 +1,6 @@ +import 'package:cake_wallet/src/domain/common/crypto_amount_format.dart'; + +const dashAmountDivider = 100000000; + +double dashAmountToDouble({int amount}) => + cryptoAmountToDouble(amount: amount, divider: dashAmountDivider); \ No newline at end of file diff --git a/lib/src/domain/ethereum/ethereum_amount_format.dart b/lib/src/domain/ethereum/ethereum_amount_format.dart new file mode 100644 index 000000000..644153a22 --- /dev/null +++ b/lib/src/domain/ethereum/ethereum_amount_format.dart @@ -0,0 +1,6 @@ +import 'package:cake_wallet/src/domain/common/crypto_amount_format.dart'; + +const ethereumAmountDivider = 1000000000000000000; + +double ethereumAmountToDouble({num amount}) => + cryptoAmountToDouble(amount: amount, divider: ethereumAmountDivider); \ No newline at end of file diff --git a/lib/src/domain/exchange/morphtoken/morphtoken_exchange_provider.dart b/lib/src/domain/exchange/morphtoken/morphtoken_exchange_provider.dart index 55df4d26e..e90b924d7 100644 --- a/lib/src/domain/exchange/morphtoken/morphtoken_exchange_provider.dart +++ b/lib/src/domain/exchange/morphtoken/morphtoken_exchange_provider.dart @@ -14,6 +14,12 @@ import 'package:cake_wallet/src/domain/exchange/morphtoken/morphtoken_request.da import 'package:cake_wallet/src/domain/exchange/exchange_provider_description.dart'; import 'package:cake_wallet/src/domain/exchange/trade_not_created_exeption.dart'; import 'package:cake_wallet/src/domain/monero/monero_amount_format.dart'; +import 'package:cake_wallet/src/domain/bitcoin/bitcoin_amount_format.dart'; +import 'package:cake_wallet/src/domain/bitcoin_cash/bitcoin_cash_amount_format.dart'; +import 'package:cake_wallet/src/domain/dash/dash_amount_format.dart'; +import 'package:cake_wallet/src/domain/ethereum/ethereum_amount_format.dart'; +import 'package:cake_wallet/src/domain/litecoin/litecoin_amount_format.dart'; +import 'package:cake_wallet/src/domain/common/setup_locator.dart'; class MorphTokenExchangeProvider extends ExchangeProvider { MorphTokenExchangeProvider() @@ -55,7 +61,7 @@ class MorphTokenExchangeProvider extends ExchangeProvider { ExchangePair(from: CryptoCurrency.btc, to: CryptoCurrency.xmr) ]); - final trades = Hive.box(Trade.boxName); + final trades = locator.get>(); static const apiUri = 'https://api.morphtoken.com'; static const _morphURISuffix = '/morph'; @@ -72,8 +78,6 @@ class MorphTokenExchangeProvider extends ExchangeProvider { @override Future fetchLimits({CryptoCurrency from, CryptoCurrency to}) async { - const ethereumAmountDivider = 1000000000000000000; - const defaultAmountDivider = 100000000; final url = apiUri + _limitsURISuffix; final headers = {'Content-type': 'application/json'}; final body = @@ -107,13 +111,25 @@ class MorphTokenExchangeProvider extends ExchangeProvider { minFormatted = moneroAmountToDouble(amount: min); maxFormatted = moneroAmountToDouble(amount: max); break; - case CryptoCurrency.eth: - minFormatted = min/ethereumAmountDivider; - maxFormatted = ethMax/ethereumAmountDivider; + case CryptoCurrency.btc: + minFormatted = bitcoinAmountToDouble(amount: min); + maxFormatted = bitcoinAmountToDouble(amount: max); break; - default: - minFormatted = min/defaultAmountDivider; - maxFormatted = max/defaultAmountDivider; + case CryptoCurrency.bch: + minFormatted = bitcoinCashAmountToDouble(amount: min); + maxFormatted = bitcoinCashAmountToDouble(amount: max); + break; + case CryptoCurrency.dash: + minFormatted = dashAmountToDouble(amount: min); + maxFormatted = dashAmountToDouble(amount: max); + break; + case CryptoCurrency.eth: + minFormatted = ethereumAmountToDouble(amount: min); + maxFormatted = ethereumAmountToDouble(amount: ethMax); + break; + case CryptoCurrency.ltc: + minFormatted = litecoinAmountToDouble(amount: min); + maxFormatted = litecoinAmountToDouble(amount: max); break; } @@ -133,7 +149,8 @@ class MorphTokenExchangeProvider extends ExchangeProvider { "asset": _request.to.toString(), "weight": weight, "address": _request.address - }] + }], + "tag": "cakewallet" }; final response = await post(url, @@ -190,7 +207,7 @@ class MorphTokenExchangeProvider extends ExchangeProvider { final state = TradeState.deserialize(raw: status.toLowerCase()); String amount = ""; - for (final trade in trades.values) { + for (final trade in trades.values.toList()) { if (trade.id == id) { amount = trade.amount; break; diff --git a/lib/src/domain/litecoin/litecoin_amount_format.dart b/lib/src/domain/litecoin/litecoin_amount_format.dart new file mode 100644 index 000000000..f030f5e34 --- /dev/null +++ b/lib/src/domain/litecoin/litecoin_amount_format.dart @@ -0,0 +1,6 @@ +import 'package:cake_wallet/src/domain/common/crypto_amount_format.dart'; + +const litecoinAmountDivider = 100000000; + +double litecoinAmountToDouble({int amount}) => + cryptoAmountToDouble(amount: amount, divider: litecoinAmountDivider); \ No newline at end of file diff --git a/lib/src/domain/monero/monero_amount_format.dart b/lib/src/domain/monero/monero_amount_format.dart index 3eb54d880..7dfb7142b 100644 --- a/lib/src/domain/monero/monero_amount_format.dart +++ b/lib/src/domain/monero/monero_amount_format.dart @@ -1,4 +1,5 @@ import 'package:intl/intl.dart'; +import 'package:cake_wallet/src/domain/common/crypto_amount_format.dart'; const moneroAmountLength = 12; const moneroAmountDivider = 1000000000000; @@ -7,6 +8,6 @@ final moneroAmountFormat = NumberFormat() ..minimumFractionDigits = 1; String moneroAmountToString({int amount}) => - moneroAmountFormat.format(amount / moneroAmountDivider); + moneroAmountFormat.format(cryptoAmountToDouble(amount: amount, divider: moneroAmountDivider)); -double moneroAmountToDouble({int amount}) => amount / moneroAmountDivider; +double moneroAmountToDouble({int amount}) => cryptoAmountToDouble(amount: amount, divider: moneroAmountDivider); diff --git a/pubspec.lock b/pubspec.lock index d4adb3dbb..2c3b118d1 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -301,6 +301,13 @@ packages: url: "https://pub.dartlang.org" source: hosted version: "0.1.19" + get_it: + dependency: "direct main" + description: + name: get_it + url: "https://pub.dartlang.org" + source: hosted + version: "3.1.0" glob: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index e20521171..002f95960 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -46,6 +46,7 @@ dependencies: hive: ^1.2.0 hive_flutter: ^0.2.1 package_info: ^0.4.0+13 + get_it: ^3.1.0 # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons.