Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion assets/translations/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
"rewardClaiming": "Rewards claim in progress",
"noKmdAddress": "No KMD address found",
"dex": "DEX",
"asset": "Assets",
"asset": "Asset",
"assets": "Assets",
"price": "Price",
"volume": "Volume",
"history": "History",
Expand Down
42 changes: 39 additions & 3 deletions lib/bloc/coins_bloc/coins_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,10 @@ class CoinsBloc extends Bloc<CoinsEvent, CoinsState> {
transformer: droppable(),
);
on<CoinsWalletCoinUpdated>(_onWalletCoinUpdated, transformer: sequential());
on<CoinsPubkeysRequested>(
_onCoinsPubkeysRequested,
transformer: concurrent(),
);
}

final KomodoDefiSdk _kdfSdk;
Expand Down Expand Up @@ -70,6 +74,38 @@ class CoinsBloc extends Bloc<CoinsEvent, CoinsState> {
await super.close();
}

Future<void> _onCoinsPubkeysRequested(
CoinsPubkeysRequested event,
Emitter<CoinsState> emit,
) async {
try {
// Get current coin
final coin = state.coins[event.coinId];
if (coin == null) return;

// Get pubkeys from the SDK through the repo
final asset = _kdfSdk.assets.assetsFromTicker(event.coinId).single;
final pubkeys = await _kdfSdk.pubkeys.getPubkeys(asset);

// Update state with new pubkeys
emit(
state.copyWith(
pubkeys: {
...state.pubkeys,
event.coinId: pubkeys,
},
),
);
} catch (e, s) {
log(
'Failed to get pubkeys for ${event.coinId}: $e',
isError: true,
path: 'coins_bloc => _onCoinsPubkeysRequested',
trace: s,
).ignore();
}
}

Future<void> _onCoinsStarted(
CoinsStarted event,
Emitter<CoinsState> emit,
Expand Down Expand Up @@ -122,7 +158,7 @@ class CoinsBloc extends Bloc<CoinsEvent, CoinsState> {
Emitter<CoinsState> emit,
) async {
final coin = event.coin;
final walletCoins = Map<String, Coin>.from(state.walletCoins);
final walletCoins = Map<String, Coin>.of(state.walletCoins);

if (coin.isActivating || coin.isActive || coin.isSuspended) {
await _kdfSdk.addActivatedCoins([coin.abbr]);
Expand Down Expand Up @@ -257,7 +293,7 @@ class CoinsBloc extends Bloc<CoinsEvent, CoinsState> {
return;
}

final coins = Map<String, Coin>.from(state.coins);
final coins = Map<String, Coin>.of(state.coins);
for (final entry in state.coins.entries) {
final coin = entry.value;
final CexPrice? usdPrice = prices[abbr2Ticker(coin.abbr)];
Expand Down Expand Up @@ -314,7 +350,7 @@ class CoinsBloc extends Bloc<CoinsEvent, CoinsState> {
case WalletType.iguana:
case WalletType.hdwallet:
coin.reset();
final newWalletCoins = Map<String, Coin>.from(state.walletCoins);
final newWalletCoins = Map<String, Coin>.of(state.walletCoins);
newWalletCoins.remove(coin.abbr.toUpperCase());
emit(state.copyWith(walletCoins: newWalletCoins));
log('${coin.name} has been removed', path: 'coins_bloc => _onLogout')
Expand Down
10 changes: 10 additions & 0 deletions lib/bloc/coins_bloc/coins_event.dart
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,13 @@ final class CoinsWalletCoinUpdated extends CoinsEvent {
@override
List<Object> get props => [coin];
}

// TODO! Refactor to remove this so that the pubkeys are loaded with the coins
class CoinsPubkeysRequested extends CoinsEvent {
const CoinsPubkeysRequested(this.coinId);

final String coinId;

@override
List<Object> get props => [coinId];
}
33 changes: 20 additions & 13 deletions lib/bloc/coins_bloc/coins_state.dart
Original file line number Diff line number Diff line change
@@ -1,46 +1,53 @@
part of 'coins_bloc.dart';

final class CoinsState extends Equatable {
class CoinsState extends Equatable {
const CoinsState({
required this.coins,
required this.walletCoins,
required this.loginActivationFinished,
required this.pubkeys,
});

factory CoinsState.initial() => const CoinsState(
coins: {},
walletCoins: {},
loginActivationFinished: false,
pubkeys: {},
);

final Map<String, Coin> coins;
final Map<String, Coin> walletCoins;
final bool loginActivationFinished;
final Map<String, AssetPubkeys> pubkeys;

double? getUsdPriceByAmount(String amount, String coinAbbr) {
final Coin? coin = coins[coinAbbr];
final double? parsedAmount = double.tryParse(amount);
final double? usdPrice = coin?.usdPrice?.price;

if (coin == null || usdPrice == null || parsedAmount == null) {
return null;
}
return parsedAmount * usdPrice;
}
@override
List<Object> get props =>
[coins, walletCoins, loginActivationFinished, pubkeys];

CoinsState copyWith({
Map<String, Coin>? coins,
Map<String, Coin>? walletCoins,
bool? loginActivationFinished,
Map<String, AssetPubkeys>? pubkeys,
}) {
return CoinsState(
coins: coins ?? this.coins,
walletCoins: walletCoins ?? this.walletCoins,
loginActivationFinished:
loginActivationFinished ?? this.loginActivationFinished,
pubkeys: pubkeys ?? this.pubkeys,
);
}

@override
List<Object> get props => [coins, walletCoins, loginActivationFinished];
// TODO! Migrate to SDK
double? getUsdPriceByAmount(String amount, String coinAbbr) {
final Coin? coin = coins[coinAbbr];
final double? parsedAmount = double.tryParse(amount);
final double? usdPrice = coin?.usdPrice?.price;

if (coin == null || usdPrice == null || parsedAmount == null) {
return null;
}
return parsedAmount * usdPrice;
}
}
2 changes: 1 addition & 1 deletion lib/bloc/withdraw_form/withdraw_form_bloc.dart
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ class WithdrawFormBloc extends Bloc<WithdrawFormEvent, WithdrawFormState> {
// If enabling custom fees, set a default fee or reuse from `_getDefaultFee()`
emit(
state.copyWith(
isCustomFeeEnabled: event.isEnabled,
isCustomFee: event.isEnabled,
customFee: event.isEnabled ? () => _getDefaultFee() : () => null,
customFeeError: () => null,
),
Expand Down
7 changes: 1 addition & 6 deletions lib/bloc/withdraw_form/withdraw_form_state.dart
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ class WithdrawFormState extends Equatable {
final PubkeyInfo? selectedSourceAddress;
final bool isMaxAmount;
final bool isCustomFee;
final bool isCustomFeeEnabled;
final FeeInfo? customFee;
final String? memo;
final bool isIbcTransfer;
Expand Down Expand Up @@ -61,7 +60,6 @@ class WithdrawFormState extends Equatable {
this.selectedSourceAddress,
this.isMaxAmount = false,
this.isCustomFee = false,
this.isCustomFeeEnabled = false,
this.customFee,
this.memo,
this.isIbcTransfer = false,
Expand Down Expand Up @@ -89,7 +87,6 @@ class WithdrawFormState extends Equatable {
ValueGetter<PubkeyInfo?>? selectedSourceAddress,
bool? isMaxAmount,
bool? isCustomFee,
bool? isCustomFeeEnabled,
ValueGetter<FeeInfo?>? customFee,
ValueGetter<String?>? memo,
bool? isIbcTransfer,
Expand Down Expand Up @@ -118,7 +115,6 @@ class WithdrawFormState extends Equatable {
: this.selectedSourceAddress,
isMaxAmount: isMaxAmount ?? this.isMaxAmount,
isCustomFee: isCustomFee ?? this.isCustomFee,
isCustomFeeEnabled: isCustomFeeEnabled ?? this.isCustomFeeEnabled,
customFee: customFee != null ? customFee() : this.customFee,
memo: memo != null ? memo() : this.memo,
isIbcTransfer: isIbcTransfer ?? this.isIbcTransfer,
Expand Down Expand Up @@ -147,7 +143,7 @@ class WithdrawFormState extends Equatable {
asset: asset.id.id,
toAddress: recipientAddress,
amount: isMaxAmount ? null : Decimal.parse(amount),
fee: isCustomFeeEnabled ? customFee : null,
fee: isCustomFee ? customFee : null,
from: selectedSourceAddress?.derivationPath != null
? WithdrawalSource.hdDerivationPath(
selectedSourceAddress!.derivationPath!,
Expand Down Expand Up @@ -178,7 +174,6 @@ class WithdrawFormState extends Equatable {
selectedSourceAddress,
isMaxAmount,
isCustomFee,
isCustomFeeEnabled,
customFee,
memo,
isIbcTransfer,
Expand Down
1 change: 1 addition & 0 deletions lib/generated/codegen_loader.g.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ abstract class LocaleKeys {
static const noKmdAddress = 'noKmdAddress';
static const dex = 'dex';
static const asset = 'asset';
static const assets = 'assets';
static const price = 'price';
static const volume = 'volume';
static const history = 'history';
Expand Down
74 changes: 60 additions & 14 deletions lib/shared/widgets/coin_balance.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,74 @@
import 'package:app_theme/app_theme.dart';
import 'package:flutter/material.dart';
import 'package:web_dex/model/coin.dart';
import 'package:web_dex/shared/utils/utils.dart';
import 'package:web_dex/shared/widgets/auto_scroll_text.dart';
import 'package:web_dex/shared/widgets/coin_fiat_balance.dart';

// TODO! Integrate this widget directly to the SDK and make it subscribe to
// the balance changes of the coin.
class CoinBalance extends StatelessWidget {
const CoinBalance({required this.coin});
const CoinBalance({
super.key,
required this.coin,
this.isVertical = false,
});

final Coin coin;
final bool isVertical;

@override
Widget build(BuildContext context) {
return Column(
crossAxisAlignment: CrossAxisAlignment.end,
children: [
Text(
doubleToString(coin.balance),
style: const TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
final baseFont = Theme.of(context).textTheme.bodySmall;
final balanceStyle = baseFont?.copyWith(
fontWeight: FontWeight.w500,
);

final children = [
Row(
mainAxisSize: MainAxisSize.min,
children: [
Flexible(
child: AutoScrollText(
key: Key('coin-balance-asset-${coin.abbr.toLowerCase()}'),
text: doubleToString(coin.balance),
style: balanceStyle,
textAlign: TextAlign.right,
),
),
Text(
' ${Coin.normalizeAbbr(coin.abbr)}',
style: balanceStyle,
),
],
),
ConstrainedBox(
constraints: const BoxConstraints(
maxWidth: 100,
),
const SizedBox(height: 2),
CoinFiatBalance(
coin,
style: TextStyle(color: theme.custom.increaseColor),
child: Row(
// mainAxisSize: MainAxisSize.min,
children: [
Text('(', style: balanceStyle),
CoinFiatBalance(
coin,
isAutoScrollEnabled: true,
),
Text(')', style: balanceStyle),
],
),
],
);
),
];

return isVertical
? Column(
mainAxisSize: MainAxisSize.min,
crossAxisAlignment: CrossAxisAlignment.start,
children: children,
)
: Row(
mainAxisSize: MainAxisSize.min,
mainAxisAlignment: MainAxisAlignment.start,
children: children,
);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ class CoinsTableItem<T> extends StatelessWidget {
subtitleText: subtitleText,
),
const SizedBox(width: 8),
if (coin.isActive) CoinBalance(coin: coin),
if (coin.isActive) CoinBalance(coin: coin, isVertical: true),
],
),
);
Expand Down
Loading