From 5bf678d41ac56952cd008945f6e32ebe428c4f9a Mon Sep 17 00:00:00 2001 From: julian Date: Mon, 10 Apr 2023 10:02:19 -0600 Subject: [PATCH] Balance class clean up --- lib/models/balance.dart | 17 ++--- lib/models/token_balance.dart | 62 ------------------- .../exchange_view/choose_from_stack_view.dart | 2 +- .../desktop_expanding_wallet_card.dart | 2 +- .../coins/epiccash/epiccash_wallet.dart | 1 - .../coins/ethereum/ethereum_wallet.dart | 9 +-- lib/services/coins/firo/firo_wallet.dart | 2 - lib/services/coins/monero/monero_wallet.dart | 1 - .../coins/wownero/wownero_wallet.dart | 1 - .../ethereum/cached_eth_token_balance.dart | 5 +- .../ethereum/ethereum_token_service.dart | 9 ++- .../mixins/coin_control_interface.dart | 1 - lib/services/mixins/eth_token_cache.dart | 11 ++-- lib/services/mixins/wallet_cache.dart | 6 +- lib/widgets/eth_wallet_radio.dart | 2 +- ...ture.dart => wallet_info_row_balance.dart} | 0 .../wallet_info_row/wallet_info_row.dart | 2 +- .../wallet_info_row_balance_future_test.dart | 2 +- .../wallet_info_row/wallet_info_row_test.dart | 2 +- 19 files changed, 28 insertions(+), 109 deletions(-) delete mode 100644 lib/models/token_balance.dart rename lib/widgets/wallet_info_row/sub_widgets/{wallet_info_row_balance_future.dart => wallet_info_row_balance.dart} (100%) diff --git a/lib/models/balance.dart b/lib/models/balance.dart index da03b3dae..63fbe9ab7 100644 --- a/lib/models/balance.dart +++ b/lib/models/balance.dart @@ -1,17 +1,14 @@ import 'dart:convert'; import 'package:stackwallet/utilities/amount/amount.dart'; -import 'package:stackwallet/utilities/enums/coin_enum.dart'; class Balance { - final Coin coin; final Amount total; final Amount spendable; final Amount blockedTotal; final Amount pendingSpendable; Balance({ - required this.coin, required this.total, required this.spendable, required this.blockedTotal, @@ -25,42 +22,40 @@ class Balance { "pendingSpendable": pendingSpendable.toJsonString(), }); - // need to fall back to parsing from in due to cached balances being previously + // need to fall back to parsing from int due to cached balances being previously // stored as int values instead of Amounts - factory Balance.fromJson(String json, Coin coin) { + factory Balance.fromJson(String json, int deprecatedValue) { final decoded = jsonDecode(json); return Balance( - coin: coin, total: decoded["total"] is String ? Amount.fromSerializedJsonString(decoded["total"] as String) : Amount( rawValue: BigInt.from(decoded["total"] as int), - fractionDigits: coin.decimals, + fractionDigits: deprecatedValue, ), spendable: decoded["spendable"] is String ? Amount.fromSerializedJsonString(decoded["spendable"] as String) : Amount( rawValue: BigInt.from(decoded["spendable"] as int), - fractionDigits: coin.decimals, + fractionDigits: deprecatedValue, ), blockedTotal: decoded["blockedTotal"] is String ? Amount.fromSerializedJsonString(decoded["blockedTotal"] as String) : Amount( rawValue: BigInt.from(decoded["blockedTotal"] as int), - fractionDigits: coin.decimals, + fractionDigits: deprecatedValue, ), pendingSpendable: decoded["pendingSpendable"] is String ? Amount.fromSerializedJsonString( decoded["pendingSpendable"] as String) : Amount( rawValue: BigInt.from(decoded["pendingSpendable"] as int), - fractionDigits: coin.decimals, + fractionDigits: deprecatedValue, ), ); } Map toMap() => { - "coin": coin, "total": total, "spendable": spendable, "blockedTotal": blockedTotal, diff --git a/lib/models/token_balance.dart b/lib/models/token_balance.dart deleted file mode 100644 index f2606f04d..000000000 --- a/lib/models/token_balance.dart +++ /dev/null @@ -1,62 +0,0 @@ -import 'dart:convert'; - -import 'package:stackwallet/models/balance.dart'; -import 'package:stackwallet/utilities/amount/amount.dart'; -import 'package:stackwallet/utilities/enums/coin_enum.dart'; - -class TokenBalance extends Balance { - TokenBalance({ - required this.contractAddress, - required super.total, - required super.spendable, - required super.blockedTotal, - required super.pendingSpendable, - super.coin = Coin.ethereum, - }); - - final String contractAddress; - - @override - String toJsonIgnoreCoin() => jsonEncode({ - "contractAddress": contractAddress, - "total": total.toJsonString(), - "spendable": spendable.toJsonString(), - "blockedTotal": blockedTotal.toJsonString(), - "pendingSpendable": pendingSpendable.toJsonString(), - }); - - factory TokenBalance.fromJson( - String json, - int fractionDigits, - ) { - final decoded = jsonDecode(json); - return TokenBalance( - contractAddress: decoded["contractAddress"] as String, - total: decoded["total"] is String - ? Amount.fromSerializedJsonString(decoded["total"] as String) - : Amount( - rawValue: BigInt.from(decoded["total"] as int), - fractionDigits: fractionDigits, - ), - spendable: decoded["spendable"] is String - ? Amount.fromSerializedJsonString(decoded["spendable"] as String) - : Amount( - rawValue: BigInt.from(decoded["spendable"] as int), - fractionDigits: fractionDigits, - ), - blockedTotal: decoded["blockedTotal"] is String - ? Amount.fromSerializedJsonString(decoded["blockedTotal"] as String) - : Amount( - rawValue: BigInt.from(decoded["blockedTotal"] as int), - fractionDigits: fractionDigits, - ), - pendingSpendable: decoded["pendingSpendable"] is String - ? Amount.fromSerializedJsonString( - decoded["pendingSpendable"] as String) - : Amount( - rawValue: BigInt.from(decoded["pendingSpendable"] as int), - fractionDigits: fractionDigits, - ), - ); - } -} diff --git a/lib/pages/exchange_view/choose_from_stack_view.dart b/lib/pages/exchange_view/choose_from_stack_view.dart index 505554a7c..bbfe91880 100644 --- a/lib/pages/exchange_view/choose_from_stack_view.dart +++ b/lib/pages/exchange_view/choose_from_stack_view.dart @@ -8,7 +8,7 @@ import 'package:stackwallet/utilities/theme/stack_colors.dart'; import 'package:stackwallet/widgets/background.dart'; import 'package:stackwallet/widgets/custom_buttons/app_bar_icon_button.dart'; import 'package:stackwallet/widgets/rounded_white_container.dart'; -import 'package:stackwallet/widgets/wallet_info_row/sub_widgets/wallet_info_row_balance_future.dart'; +import 'package:stackwallet/widgets/wallet_info_row/sub_widgets/wallet_info_row_balance.dart'; import 'package:stackwallet/widgets/wallet_info_row/sub_widgets/wallet_info_row_coin_icon.dart'; class ChooseFromStackView extends ConsumerStatefulWidget { diff --git a/lib/pages_desktop_specific/my_stack_view/dialogs/desktop_expanding_wallet_card.dart b/lib/pages_desktop_specific/my_stack_view/dialogs/desktop_expanding_wallet_card.dart index 3d40d7e9e..21493a31d 100644 --- a/lib/pages_desktop_specific/my_stack_view/dialogs/desktop_expanding_wallet_card.dart +++ b/lib/pages_desktop_specific/my_stack_view/dialogs/desktop_expanding_wallet_card.dart @@ -11,7 +11,7 @@ import 'package:stackwallet/widgets/animated_widgets/rotate_icon.dart'; import 'package:stackwallet/widgets/expandable.dart'; import 'package:stackwallet/widgets/rounded_white_container.dart'; import 'package:stackwallet/widgets/wallet_card.dart'; -import 'package:stackwallet/widgets/wallet_info_row/sub_widgets/wallet_info_row_balance_future.dart'; +import 'package:stackwallet/widgets/wallet_info_row/sub_widgets/wallet_info_row_balance.dart'; import 'package:stackwallet/widgets/wallet_info_row/sub_widgets/wallet_info_row_coin_icon.dart'; import 'package:tuple/tuple.dart'; diff --git a/lib/services/coins/epiccash/epiccash_wallet.dart b/lib/services/coins/epiccash/epiccash_wallet.dart index cbbbc76c8..c15a5bee3 100644 --- a/lib/services/coins/epiccash/epiccash_wallet.dart +++ b/lib/services/coins/epiccash/epiccash_wallet.dart @@ -1978,7 +1978,6 @@ class EpicCashWallet extends CoinServiceAPI (jsonBalances['amount_awaiting_finalization'] as double).toString(); _balance = Balance( - coin: coin, total: Amount.fromDecimal( Decimal.parse(total) + Decimal.parse(awaiting), fractionDigits: coin.decimals, diff --git a/lib/services/coins/ethereum/ethereum_wallet.dart b/lib/services/coins/ethereum/ethereum_wallet.dart index b5fafeff5..380f4dc04 100644 --- a/lib/services/coins/ethereum/ethereum_wallet.dart +++ b/lib/services/coins/ethereum/ethereum_wallet.dart @@ -10,7 +10,6 @@ import 'package:stackwallet/models/balance.dart'; import 'package:stackwallet/models/isar/models/isar_models.dart'; import 'package:stackwallet/models/node_model.dart'; import 'package:stackwallet/models/paymint/fee_object_model.dart'; -import 'package:stackwallet/models/token_balance.dart'; import 'package:stackwallet/services/coins/coin_service.dart'; import 'package:stackwallet/services/ethereum/ethereum_api.dart'; import 'package:stackwallet/services/event_bus/events/global/node_connection_status_changed_event.dart'; @@ -78,14 +77,13 @@ class EthereumWallet extends CoinServiceAPI with WalletCache, WalletDB { ); } - TokenBalance getCachedTokenBalance(EthContract contract) { + Balance getCachedTokenBalance(EthContract contract) { final jsonString = DB.instance.get( boxName: _walletId, key: TokenCacheKeys.tokenBalance(contract.address), ) as String?; if (jsonString == null) { - return TokenBalance( - contractAddress: contract.address, + return Balance( total: Amount( rawValue: BigInt.zero, fractionDigits: contract.decimals, @@ -104,7 +102,7 @@ class EthereumWallet extends CoinServiceAPI with WalletCache, WalletDB { ), ); } - return TokenBalance.fromJson( + return Balance.fromJson( jsonString, contract.decimals, ); @@ -222,7 +220,6 @@ class EthereumWallet extends CoinServiceAPI with WalletCache, WalletDB { web3.Web3Client client = getEthClient(); web3.EtherAmount ethBalance = await client.getBalance(_credentials.address); _balance = Balance( - coin: coin, total: Amount( rawValue: ethBalance.getInWei, fractionDigits: coin.decimals, diff --git a/lib/services/coins/firo/firo_wallet.dart b/lib/services/coins/firo/firo_wallet.dart index 22cdde933..38d1fb172 100644 --- a/lib/services/coins/firo/firo_wallet.dart +++ b/lib/services/coins/firo/firo_wallet.dart @@ -2515,7 +2515,6 @@ class FiroWallet extends CoinServiceAPI } _balancePrivate = Balance( - coin: coin, total: Amount( rawValue: BigInt.from(intLelantusBalance + unconfirmedLelantusBalance), @@ -3803,7 +3802,6 @@ class FiroWallet extends CoinServiceAPI // finally update public balance _balance = Balance( - coin: coin, total: satoshiBalanceTotal, spendable: satoshiBalanceSpendable, blockedTotal: satoshiBalanceBlocked, diff --git a/lib/services/coins/monero/monero_wallet.dart b/lib/services/coins/monero/monero_wallet.dart index 87b803255..29debe753 100644 --- a/lib/services/coins/monero/monero_wallet.dart +++ b/lib/services/coins/monero/monero_wallet.dart @@ -747,7 +747,6 @@ class MoneroWallet extends CoinServiceAPI with WalletCache, WalletDB { final total = await _totalBalance; final available = await _availableBalance; _balance = Balance( - coin: coin, total: total, spendable: available, blockedTotal: Amount( diff --git a/lib/services/coins/wownero/wownero_wallet.dart b/lib/services/coins/wownero/wownero_wallet.dart index 84f806be2..4e3591d8c 100644 --- a/lib/services/coins/wownero/wownero_wallet.dart +++ b/lib/services/coins/wownero/wownero_wallet.dart @@ -774,7 +774,6 @@ class WowneroWallet extends CoinServiceAPI with WalletCache, WalletDB { final total = await _totalBalance; final available = await _availableBalance; _balance = Balance( - coin: coin, total: total, spendable: available, blockedTotal: Amount( diff --git a/lib/services/ethereum/cached_eth_token_balance.dart b/lib/services/ethereum/cached_eth_token_balance.dart index f36cf2ea3..d477d0460 100644 --- a/lib/services/ethereum/cached_eth_token_balance.dart +++ b/lib/services/ethereum/cached_eth_token_balance.dart @@ -1,5 +1,5 @@ +import 'package:stackwallet/models/balance.dart'; import 'package:stackwallet/models/isar/models/ethereum/eth_contract.dart'; -import 'package:stackwallet/models/token_balance.dart'; import 'package:stackwallet/services/ethereum/ethereum_api.dart'; import 'package:stackwallet/services/mixins/eth_token_cache.dart'; import 'package:stackwallet/utilities/amount/amount.dart'; @@ -21,8 +21,7 @@ class CachedEthTokenBalance with EthTokenCache { if (response.value != null) { await updateCachedBalance( - TokenBalance( - contractAddress: token.address, + Balance( total: response.value!, spendable: response.value!, blockedTotal: Amount( diff --git a/lib/services/ethereum/ethereum_token_service.dart b/lib/services/ethereum/ethereum_token_service.dart index 90edb1904..f56561e8d 100644 --- a/lib/services/ethereum/ethereum_token_service.dart +++ b/lib/services/ethereum/ethereum_token_service.dart @@ -8,10 +8,10 @@ import 'package:isar/isar.dart'; import 'package:stackwallet/db/isar/main_db.dart'; import 'package:stackwallet/dto/ethereum/eth_token_tx_dto.dart'; import 'package:stackwallet/dto/ethereum/eth_token_tx_extra_dto.dart'; +import 'package:stackwallet/models/balance.dart'; import 'package:stackwallet/models/isar/models/isar_models.dart'; import 'package:stackwallet/models/node_model.dart'; import 'package:stackwallet/models/paymint/fee_object_model.dart'; -import 'package:stackwallet/models/token_balance.dart'; import 'package:stackwallet/services/coins/ethereum/ethereum_wallet.dart'; import 'package:stackwallet/services/ethereum/ethereum_api.dart'; import 'package:stackwallet/services/event_bus/events/global/updated_in_background_event.dart'; @@ -60,8 +60,8 @@ class EthTokenWallet extends ChangeNotifier with EthTokenCache { EthContract get tokenContract => _tokenContract; EthContract _tokenContract; - TokenBalance get balance => _balance ??= getCachedBalance(); - TokenBalance? _balance; + Balance get balance => _balance ??= getCachedBalance(); + Balance? _balance; Coin get coin => Coin.ethereum; @@ -413,8 +413,7 @@ class EthTokenWallet extends ChangeNotifier with EthTokenCache { String _balance = balanceRequest.first.toString(); - final newBalance = TokenBalance( - contractAddress: tokenContract.address, + final newBalance = Balance( total: Amount.fromDecimal( Decimal.parse(_balance), fractionDigits: tokenContract.decimals, diff --git a/lib/services/mixins/coin_control_interface.dart b/lib/services/mixins/coin_control_interface.dart index e9af99161..d3e6079a0 100644 --- a/lib/services/mixins/coin_control_interface.dart +++ b/lib/services/mixins/coin_control_interface.dart @@ -76,7 +76,6 @@ mixin CoinControlInterface { } final balance = Balance( - coin: _coin, total: satoshiBalanceTotal, spendable: satoshiBalanceSpendable, blockedTotal: satoshiBalanceBlocked, diff --git a/lib/services/mixins/eth_token_cache.dart b/lib/services/mixins/eth_token_cache.dart index ccaa293d0..b1fdf6bb3 100644 --- a/lib/services/mixins/eth_token_cache.dart +++ b/lib/services/mixins/eth_token_cache.dart @@ -1,6 +1,6 @@ import 'package:stackwallet/db/hive/db.dart'; +import 'package:stackwallet/models/balance.dart'; import 'package:stackwallet/models/isar/models/ethereum/eth_contract.dart'; -import 'package:stackwallet/models/token_balance.dart'; import 'package:stackwallet/utilities/amount/amount.dart'; abstract class TokenCacheKeys { @@ -19,14 +19,13 @@ mixin EthTokenCache { } // token balance cache - TokenBalance getCachedBalance() { + Balance getCachedBalance() { final jsonString = DB.instance.get( boxName: _walletId, key: TokenCacheKeys.tokenBalance(_token.address), ) as String?; if (jsonString == null) { - return TokenBalance( - contractAddress: _token.address, + return Balance( total: Amount( rawValue: BigInt.zero, fractionDigits: _token.decimals, @@ -45,13 +44,13 @@ mixin EthTokenCache { ), ); } - return TokenBalance.fromJson( + return Balance.fromJson( jsonString, _token.decimals, ); } - Future updateCachedBalance(TokenBalance balance) async { + Future updateCachedBalance(Balance balance) async { await DB.instance.put( boxName: _walletId, key: TokenCacheKeys.tokenBalance(_token.address), diff --git a/lib/services/mixins/wallet_cache.dart b/lib/services/mixins/wallet_cache.dart index 49f53381a..435e28717 100644 --- a/lib/services/mixins/wallet_cache.dart +++ b/lib/services/mixins/wallet_cache.dart @@ -70,7 +70,6 @@ mixin WalletCache { ) as String?; if (jsonString == null) { return Balance( - coin: _coin, total: Amount(rawValue: BigInt.zero, fractionDigits: _coin.decimals), spendable: Amount(rawValue: BigInt.zero, fractionDigits: _coin.decimals), @@ -80,7 +79,7 @@ mixin WalletCache { Amount(rawValue: BigInt.zero, fractionDigits: _coin.decimals), ); } - return Balance.fromJson(jsonString, _coin); + return Balance.fromJson(jsonString, _coin.decimals); } Future updateCachedBalance(Balance balance) async { @@ -99,7 +98,6 @@ mixin WalletCache { ) as String?; if (jsonString == null) { return Balance( - coin: _coin, total: Amount(rawValue: BigInt.zero, fractionDigits: _coin.decimals), spendable: Amount(rawValue: BigInt.zero, fractionDigits: _coin.decimals), @@ -109,7 +107,7 @@ mixin WalletCache { Amount(rawValue: BigInt.zero, fractionDigits: _coin.decimals), ); } - return Balance.fromJson(jsonString, _coin); + return Balance.fromJson(jsonString, _coin.decimals); } Future updateCachedBalanceSecondary(Balance balance) async { diff --git a/lib/widgets/eth_wallet_radio.dart b/lib/widgets/eth_wallet_radio.dart index c20a4cbb9..e5a0c2bb5 100644 --- a/lib/widgets/eth_wallet_radio.dart +++ b/lib/widgets/eth_wallet_radio.dart @@ -3,7 +3,7 @@ import 'package:flutter_riverpod/flutter_riverpod.dart'; import 'package:stackwallet/providers/providers.dart'; import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; -import 'package:stackwallet/widgets/wallet_info_row/sub_widgets/wallet_info_row_balance_future.dart'; +import 'package:stackwallet/widgets/wallet_info_row/sub_widgets/wallet_info_row_balance.dart'; import 'package:stackwallet/widgets/wallet_info_row/sub_widgets/wallet_info_row_coin_icon.dart'; class EthWalletRadio extends ConsumerStatefulWidget { diff --git a/lib/widgets/wallet_info_row/sub_widgets/wallet_info_row_balance_future.dart b/lib/widgets/wallet_info_row/sub_widgets/wallet_info_row_balance.dart similarity index 100% rename from lib/widgets/wallet_info_row/sub_widgets/wallet_info_row_balance_future.dart rename to lib/widgets/wallet_info_row/sub_widgets/wallet_info_row_balance.dart diff --git a/lib/widgets/wallet_info_row/wallet_info_row.dart b/lib/widgets/wallet_info_row/wallet_info_row.dart index 26879aa7e..c31e8e19f 100644 --- a/lib/widgets/wallet_info_row/wallet_info_row.dart +++ b/lib/widgets/wallet_info_row/wallet_info_row.dart @@ -8,7 +8,7 @@ import 'package:stackwallet/utilities/text_styles.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; import 'package:stackwallet/utilities/util.dart'; import 'package:stackwallet/widgets/custom_buttons/blue_text_button.dart'; -import 'package:stackwallet/widgets/wallet_info_row/sub_widgets/wallet_info_row_balance_future.dart'; +import 'package:stackwallet/widgets/wallet_info_row/sub_widgets/wallet_info_row_balance.dart'; import 'package:stackwallet/widgets/wallet_info_row/sub_widgets/wallet_info_row_coin_icon.dart'; class WalletInfoRow extends ConsumerWidget { diff --git a/test/widget_tests/wallet_info_row/sub_widgets/wallet_info_row_balance_future_test.dart b/test/widget_tests/wallet_info_row/sub_widgets/wallet_info_row_balance_future_test.dart index 943a02e99..70dfbfdc4 100644 --- a/test/widget_tests/wallet_info_row/sub_widgets/wallet_info_row_balance_future_test.dart +++ b/test/widget_tests/wallet_info_row/sub_widgets/wallet_info_row_balance_future_test.dart @@ -15,7 +15,7 @@ import 'package:stackwallet/utilities/amount/amount.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/theme/light_colors.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; -import 'package:stackwallet/widgets/wallet_info_row/sub_widgets/wallet_info_row_balance_future.dart'; +import 'package:stackwallet/widgets/wallet_info_row/sub_widgets/wallet_info_row_balance.dart'; import 'wallet_info_row_balance_future_test.mocks.dart'; diff --git a/test/widget_tests/wallet_info_row/wallet_info_row_test.dart b/test/widget_tests/wallet_info_row/wallet_info_row_test.dart index 4308ac273..a9318e11a 100644 --- a/test/widget_tests/wallet_info_row/wallet_info_row_test.dart +++ b/test/widget_tests/wallet_info_row/wallet_info_row_test.dart @@ -15,7 +15,7 @@ import 'package:stackwallet/utilities/amount/amount.dart'; import 'package:stackwallet/utilities/enums/coin_enum.dart'; import 'package:stackwallet/utilities/theme/light_colors.dart'; import 'package:stackwallet/utilities/theme/stack_colors.dart'; -import 'package:stackwallet/widgets/wallet_info_row/sub_widgets/wallet_info_row_balance_future.dart'; +import 'package:stackwallet/widgets/wallet_info_row/sub_widgets/wallet_info_row_balance.dart'; import 'package:stackwallet/widgets/wallet_info_row/wallet_info_row.dart'; import 'wallet_info_row_test.mocks.dart';