Skip to content

Commit 70e42e8

Browse files
committed
refactor(spl): align DefaultSplTokens usage w/ DefaultTokens
1 parent 14c506c commit 70e42e8

File tree

6 files changed

+85
-189
lines changed

6 files changed

+85
-189
lines changed

lib/pages/token_view/sol_token_view.dart

Lines changed: 0 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -13,18 +13,13 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
1313
import 'package:flutter_svg/svg.dart';
1414
import 'package:tuple/tuple.dart';
1515

16-
import '../../models/isar/models/isar_models.dart';
17-
import '../../providers/db/main_db_provider.dart';
18-
import '../../providers/providers.dart';
1916
import '../../services/event_bus/events/global/wallet_sync_status_changed_event.dart';
2017
import '../../themes/stack_colors.dart';
2118
import '../../utilities/assets.dart';
2219
import '../../utilities/constants.dart';
23-
import '../../utilities/default_spl_tokens.dart';
2420
import '../../utilities/text_styles.dart';
2521
import '../../wallets/isar/providers/solana/current_sol_token_wallet_provider.dart';
2622
import '../../wallets/isar/providers/solana/solana_wallet_provider.dart';
27-
import '../../wallets/wallet/impl/sub_wallets/solana_token_wallet.dart';
2823
import '../../widgets/background.dart';
2924
import '../../widgets/custom_buttons/app_bar_icon_button.dart';
3025
import '../../widgets/custom_buttons/blue_text_button.dart';
@@ -65,75 +60,9 @@ class _SolTokenViewState extends ConsumerState<SolTokenView> {
6560
? WalletSyncStatus.syncing
6661
: WalletSyncStatus.synced;
6762

68-
// Initialize the Solana token wallet provider with mock data.
69-
//
70-
// This sets up the pCurrentSolanaTokenWallet provider so that
71-
// SolanaTokenSummary can access the token wallet information.
72-
WidgetsBinding.instance.addPostFrameCallback((_) {
73-
if (mounted) {
74-
_initializeSolanaTokenWallet();
75-
}
76-
});
77-
7863
super.initState();
7964
}
8065

81-
/// Initialize the Solana token wallet for this token view.
82-
///
83-
/// Creates a SolanaTokenWallet with token data from DefaultSplTokens or the database.
84-
/// First looks in DefaultSplTokens, then checks the database for custom tokens.
85-
/// Sets it as the current token wallet in the provider so that UI widgets can access it.
86-
///
87-
/// If the token is not found anywhere, sets the token wallet to null
88-
/// so the UI can display an error message.
89-
void _initializeSolanaTokenWallet() {
90-
SplToken? tokenInfo;
91-
92-
// First try to find in default tokens.
93-
try {
94-
tokenInfo = DefaultSplTokens.list.firstWhere(
95-
(token) => token.address == widget.tokenMint,
96-
);
97-
} catch (e) {
98-
// Token not found in DefaultSplTokens, try database for custom tokens.
99-
tokenInfo = null;
100-
}
101-
102-
// If not found in defaults, try database for custom tokens.
103-
if (tokenInfo == null) {
104-
try {
105-
final db = ref.read(mainDBProvider);
106-
tokenInfo = db.getSplTokenSync(widget.tokenMint);
107-
} catch (e) {
108-
tokenInfo = null;
109-
}
110-
}
111-
112-
if (tokenInfo == null) {
113-
ref.read(solanaTokenServiceStateProvider.state).state = null;
114-
debugPrint(
115-
'ERROR: Token not found in DefaultSplTokens or database: ${widget.tokenMint}',
116-
);
117-
return;
118-
}
119-
120-
// Get the parent Solana wallet.
121-
final parentWallet = ref.read(pSolanaWallet(widget.walletId));
122-
123-
if (parentWallet == null) {
124-
ref.read(solanaTokenServiceStateProvider.state).state = null;
125-
debugPrint('ERROR: Wallet is not a SolanaWallet: ${widget.walletId}');
126-
return;
127-
}
128-
129-
final solanaTokenWallet = SolanaTokenWallet(parentWallet, tokenInfo);
130-
131-
ref.read(solanaTokenServiceStateProvider.state).state = solanaTokenWallet;
132-
133-
// Fetch the token balance when the wallet is opened.
134-
solanaTokenWallet.updateBalance();
135-
}
136-
13766
@override
13867
void dispose() {
13968
super.dispose();

lib/pages/token_view/solana_token_contract_details_view.dart

Lines changed: 2 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ import 'package:isar_community/isar.dart';
1414
import '../../db/isar/main_db.dart';
1515
import '../../models/isar/models/isar_models.dart';
1616
import '../../themes/stack_colors.dart';
17-
import '../../utilities/default_spl_tokens.dart';
1817
import '../../utilities/text_styles.dart';
1918
import '../../utilities/util.dart';
2019
import '../../widgets/background.dart';
@@ -48,32 +47,10 @@ class _SolanaTokenContractDetailsViewState
4847

4948
@override
5049
void initState() {
51-
// Try to find the token in the database first.
52-
final dbToken = MainDB.instance.isar.splTokens
50+
token = MainDB.instance.isar.splTokens
5351
.where()
5452
.addressEqualTo(widget.tokenMint)
55-
.findFirstSync();
56-
57-
if (dbToken != null) {
58-
token = dbToken;
59-
} else {
60-
// If not in database, try to find it in default tokens.
61-
try {
62-
token = DefaultSplTokens.list.firstWhere(
63-
(t) => t.address == widget.tokenMint,
64-
);
65-
} catch (e) {
66-
// Token not found, create a placeholder.
67-
//
68-
// Might want to just throw here instead.
69-
token = SplToken(
70-
address: widget.tokenMint,
71-
name: 'Unknown Token',
72-
symbol: 'UNKNOWN',
73-
decimals: 0,
74-
);
75-
}
76-
}
53+
.findFirstSync()!;
7754

7855
super.initState();
7956
}

lib/pages/token_view/sub_widgets/sol_token_select_item.dart

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
*
88
*/
99

10+
import 'dart:async';
11+
1012
import 'package:flutter/material.dart';
1113
import 'package:flutter_riverpod/flutter_riverpod.dart';
1214

@@ -15,9 +17,15 @@ import '../../../pages_desktop_specific/my_stack_view/wallet_view/desktop_sol_to
1517
import '../../../providers/providers.dart';
1618
import '../../../themes/stack_colors.dart';
1719
import '../../../utilities/constants.dart';
20+
import '../../../utilities/show_loading.dart';
1821
import '../../../utilities/text_styles.dart';
1922
import '../../../utilities/util.dart';
23+
import '../../../wallets/isar/providers/solana/current_sol_token_wallet_provider.dart';
2024
import '../../../wallets/isar/providers/solana/sol_token_balance_provider.dart';
25+
import '../../../wallets/wallet/impl/solana_wallet.dart';
26+
import '../../../wallets/wallet/impl/sub_wallets/solana_token_wallet.dart';
27+
import '../../../widgets/desktop/primary_button.dart';
28+
import '../../../widgets/dialogs/basic_dialog.dart';
2129
import '../../../widgets/icon_widgets/sol_token_icon.dart';
2230
import '../../../widgets/rounded_white_container.dart';
2331
import '../sol_token_view.dart';
@@ -39,9 +47,80 @@ class SolTokenSelectItem extends ConsumerStatefulWidget {
3947
class _SolTokenSelectItemState extends ConsumerState<SolTokenSelectItem> {
4048
final bool isDesktop = Util.isDesktop;
4149

50+
Future<bool> _loadTokenWallet(BuildContext context, WidgetRef ref) async {
51+
try {
52+
await ref.read(pCurrentSolanaTokenWallet)!.init();
53+
return true;
54+
} catch (_) {
55+
await showDialog<void>(
56+
barrierDismissible: false,
57+
context: context,
58+
builder: (context) => BasicDialog(
59+
title: "Failed to load token data",
60+
desktopHeight: double.infinity,
61+
desktopWidth: 450,
62+
rightButton: PrimaryButton(
63+
label: "OK",
64+
onPressed: () {
65+
Navigator.of(context).pop();
66+
if (!isDesktop) {
67+
Navigator.of(context).pop();
68+
}
69+
},
70+
),
71+
),
72+
);
73+
return false;
74+
}
75+
}
76+
4277
void _onPressed() async {
43-
// TODO [prio=high]: Implement Solana token wallet setup and navigation.
78+
final old = ref.read(solanaTokenServiceStateProvider);
79+
// exit previous if there is one
80+
unawaited(old?.exit());
81+
82+
// Get the parent Solana wallet.
83+
final solanaWallet =
84+
ref.read(pWallets).getWallet(widget.walletId) as SolanaWallet?;
85+
if (solanaWallet == null) {
86+
if (mounted) {
87+
await showDialog<void>(
88+
barrierDismissible: false,
89+
context: context,
90+
builder: (context) => BasicDialog(
91+
title: "Error: Parent Solana wallet not found",
92+
desktopHeight: double.infinity,
93+
desktopWidth: 450,
94+
rightButton: PrimaryButton(
95+
label: "OK",
96+
onPressed: () {
97+
Navigator.of(context).pop();
98+
},
99+
),
100+
),
101+
);
102+
}
103+
return;
104+
}
105+
106+
ref.read(solanaTokenServiceStateProvider.state).state = SolanaTokenWallet(
107+
solanaWallet,
108+
widget.token,
109+
);
110+
111+
final success = await showLoading<bool>(
112+
whileFuture: _loadTokenWallet(context, ref),
113+
context: context,
114+
rootNavigator: isDesktop,
115+
message: "Loading ${widget.token.name}",
116+
);
117+
118+
if (!success!) {
119+
return;
120+
}
121+
44122
if (mounted) {
123+
unawaited(ref.read(pCurrentSolanaTokenWallet)!.refresh());
45124
await Navigator.of(context).pushNamed(
46125
isDesktop ? DesktopSolTokenView.routeName : SolTokenView.routeName,
47126
arguments: (

lib/pages/wallets_view/wallets_overview.dart

Lines changed: 2 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
*
99
*/
1010

11-
import 'dart:async';
12-
1311
import 'package:flutter/material.dart';
1412
import 'package:flutter_riverpod/flutter_riverpod.dart';
1513
import 'package:flutter_svg/svg.dart';
@@ -26,7 +24,6 @@ import '../../services/event_bus/global_event_bus.dart';
2624
import '../../themes/stack_colors.dart';
2725
import '../../utilities/assets.dart';
2826
import '../../utilities/constants.dart';
29-
import '../../utilities/default_spl_tokens.dart';
3027
import '../../utilities/text_styles.dart';
3128
import '../../utilities/util.dart';
3229
import '../../wallets/crypto_currency/crypto_currency.dart';
@@ -159,16 +156,6 @@ class _EthWalletsOverviewState extends ConsumerState<WalletsOverview> {
159156
);
160157
}
161158
} else if (widget.coin is Solana) {
162-
// Ensure default Solana tokens are loaded into database.
163-
final dbProvider = ref.read(mainDBProvider);
164-
for (final defaultToken in DefaultSplTokens.list) {
165-
final existingToken = dbProvider.getSplTokenSync(defaultToken.address);
166-
if (existingToken == null) {
167-
// Token not in database, add it asynchronously.
168-
unawaited(dbProvider.putSplToken(defaultToken));
169-
}
170-
}
171-
172159
for (final data in walletsData) {
173160
final List<Contract> contracts = [];
174161
final tokenMintAddresses = ref.read(
@@ -177,23 +164,11 @@ class _EthWalletsOverviewState extends ConsumerState<WalletsOverview> {
177164

178165
// fetch each token
179166
for (final tokenAddress in tokenMintAddresses) {
180-
final token = dbProvider.getSplTokenSync(tokenAddress);
167+
final token = ref.read(mainDBProvider).getSplTokenSync(tokenAddress);
181168

182-
// add it to list if it exists in DB or in default tokens
169+
// add it to list if it exists in DB
183170
if (token != null) {
184171
contracts.add(token);
185-
} else {
186-
// Try to find in default tokens.
187-
try {
188-
final defaultToken = DefaultSplTokens.list.firstWhere(
189-
(t) => t.address == tokenAddress,
190-
);
191-
contracts.add(defaultToken);
192-
} catch (_) {
193-
// Token not found anywhere.
194-
//
195-
// Might want to throw here or something.
196-
}
197172
}
198173
}
199174

lib/pages_desktop_specific/my_stack_view/wallet_view/desktop_sol_token_view.dart

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -13,21 +13,16 @@ import 'package:flutter_riverpod/flutter_riverpod.dart';
1313
import 'package:flutter_svg/svg.dart';
1414
import 'package:tuple/tuple.dart';
1515

16-
import '../../../models/isar/models/isar_models.dart';
1716
import '../../../pages/send_view/sub_widgets/transaction_fee_selection_sheet.dart';
1817
import '../../../pages/token_view/solana_token_contract_details_view.dart';
1918
import '../../../pages/token_view/sub_widgets/token_transaction_list_widget_sol.dart';
20-
import '../../../providers/db/main_db_provider.dart';
21-
import '../../../providers/providers.dart';
2219
import '../../../services/event_bus/events/global/wallet_sync_status_changed_event.dart';
2320
import '../../../themes/stack_colors.dart';
2421
import '../../../utilities/assets.dart';
25-
import '../../../utilities/default_spl_tokens.dart';
2622
import '../../../utilities/text_styles.dart';
2723
import '../../../wallets/isar/providers/solana/current_sol_token_wallet_provider.dart';
2824
import '../../../wallets/isar/providers/solana/solana_wallet_provider.dart';
2925
import '../../../wallets/isar/providers/wallet_info_provider.dart';
30-
import '../../../wallets/wallet/impl/sub_wallets/solana_token_wallet.dart';
3126
import '../../../widgets/coin_ticker_tag.dart';
3227
import '../../../widgets/custom_buttons/blue_text_button.dart';
3328
import '../../../widgets/desktop/desktop_app_bar.dart';
@@ -65,10 +60,6 @@ class _DesktopTokenViewState extends ConsumerState<DesktopSolTokenView> {
6560

6661
@override
6762
void initState() {
68-
// Initialize the Solana token wallet.
69-
WidgetsBinding.instance.addPostFrameCallback((_) {
70-
_initializeSolanaTokenWallet();
71-
});
7263
// Get the initial sync status from the Solana wallet's refresh mutex.
7364
final solanaWallet = ref.read(pSolanaWallet(widget.walletId));
7465
initialSyncStatus = solanaWallet?.refreshMutex.isLocked ?? false
@@ -77,61 +68,6 @@ class _DesktopTokenViewState extends ConsumerState<DesktopSolTokenView> {
7768
super.initState();
7869
}
7970

80-
/// Initialize the Solana token wallet.
81-
///
82-
/// Creates a SolanaTokenWallet with token data from DefaultSplTokens or the database.
83-
/// First looks in DefaultSplTokens, then checks the database for custom tokens.
84-
/// Sets it as the current token wallet in the provider so that UI widgets can access it.
85-
///
86-
/// If the token is not found anywhere, sets the token wallet to null
87-
/// so the UI can display an error message.
88-
void _initializeSolanaTokenWallet() {
89-
// First try to find in default tokens
90-
SplToken? tokenInfo;
91-
try {
92-
tokenInfo = DefaultSplTokens.list.firstWhere(
93-
(token) => token.address == widget.tokenMint,
94-
);
95-
} catch (e) {
96-
// Token not found in DefaultSplTokens, try database for custom tokens
97-
tokenInfo = null;
98-
}
99-
100-
// If not found in defaults, try database for custom tokens
101-
if (tokenInfo == null) {
102-
try {
103-
final db = ref.read(mainDBProvider);
104-
tokenInfo = db.getSplTokenSync(widget.tokenMint);
105-
} catch (e) {
106-
tokenInfo = null;
107-
}
108-
}
109-
110-
if (tokenInfo == null) {
111-
ref.read(solanaTokenServiceStateProvider.state).state = null;
112-
debugPrint(
113-
'ERROR: Token not found in DefaultSplTokens or database: ${widget.tokenMint}',
114-
);
115-
return;
116-
}
117-
118-
// Get the parent Solana wallet.
119-
final parentWallet = ref.read(pSolanaWallet(widget.walletId));
120-
121-
if (parentWallet == null) {
122-
ref.read(solanaTokenServiceStateProvider.state).state = null;
123-
debugPrint('ERROR: Wallet is not a SolanaWallet: ${widget.walletId}');
124-
return;
125-
}
126-
127-
final solanaTokenWallet = SolanaTokenWallet(parentWallet, tokenInfo);
128-
129-
ref.read(solanaTokenServiceStateProvider.state).state = solanaTokenWallet;
130-
131-
// Fetch the token balance when the wallet is opened
132-
solanaTokenWallet.updateBalance();
133-
}
134-
13571
@override
13672
void dispose() {
13773
super.dispose();

0 commit comments

Comments
 (0)