Balance class clean up

This commit is contained in:
julian 2023-04-10 10:02:19 -06:00
parent 2b0ad1c7eb
commit 5615fbaeca
19 changed files with 28 additions and 109 deletions

View file

@ -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<String, dynamic> toMap() => {
"coin": coin,
"total": total,
"spendable": spendable,
"blockedTotal": blockedTotal,

View file

@ -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,
),
);
}
}

View file

@ -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 {

View file

@ -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';

View file

@ -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,

View file

@ -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<dynamic>(
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,

View file

@ -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,

View file

@ -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(

View file

@ -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(

View file

@ -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(

View file

@ -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,

View file

@ -76,7 +76,6 @@ mixin CoinControlInterface {
}
final balance = Balance(
coin: _coin,
total: satoshiBalanceTotal,
spendable: satoshiBalanceSpendable,
blockedTotal: satoshiBalanceBlocked,

View file

@ -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<dynamic>(
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<void> updateCachedBalance(TokenBalance balance) async {
Future<void> updateCachedBalance(Balance balance) async {
await DB.instance.put<dynamic>(
boxName: _walletId,
key: TokenCacheKeys.tokenBalance(_token.address),

View file

@ -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<void> 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<void> updateCachedBalanceSecondary(Balance balance) async {

View file

@ -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 {

View file

@ -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 {

View file

@ -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';

View file

@ -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';