Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom token management #1161

Open
wants to merge 11 commits into
base: dev
Choose a base branch
from
9 changes: 1 addition & 8 deletions lib/application/account/account_notifier.dart
Original file line number Diff line number Diff line change
Expand Up @@ -81,17 +81,10 @@ class _AccountNotifier extends _$AccountNotifier {
'End method refreshRecentTransactions for ${account.nameDisplayed}',
);

final accountSelected = ref
.read(
AccountProviders.accounts,
)
.valueOrNull
?.selectedAccount;

ref
..invalidate(userBalanceProvider)
..invalidate(
tokensListProvider(accountSelected!.genesisAddress),
tokensFromUserBalanceProvider,
);
},
]);
Expand Down
2 changes: 1 addition & 1 deletion lib/application/account/providers.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions lib/application/aeswap/dex_token.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,10 @@ Future<List<DexToken>> _tokensFromAccount(
return const [];
}

return ref
.watch(_dexTokenRepositoryProvider)
.getTokensFromAccount(accountSelected.genesisAddress);
return ref.watch(_dexTokenRepositoryProvider).getTokensFromAccount(
accountSelected.genesisAddress,
accountSelected.customTokenAddressList ?? [],
);
}

@riverpod
Expand Down
2 changes: 1 addition & 1 deletion lib/application/aeswap/dex_token.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

80 changes: 66 additions & 14 deletions lib/application/tokens/tokens.dart
Original file line number Diff line number Diff line change
@@ -1,50 +1,102 @@
import 'dart:developer';

import 'package:aewallet/application/account/providers.dart';
import 'package:aewallet/application/aeswap/dex_token.dart';
import 'package:aewallet/application/api_service.dart';
import 'package:aewallet/infrastructure/repositories/tokens/tokens.repository.dart';
import 'package:aewallet/modules/aeswap/application/pool/dex_pool.dart';
import 'package:aewallet/modules/aeswap/application/session/provider.dart';
import 'package:aewallet/modules/aeswap/domain/models/dex_token.dart';
import 'package:archethic_dapp_framework_flutter/archethic_dapp_framework_flutter.dart'
as aedappfm;
import 'package:archethic_lib_dart/archethic_lib_dart.dart' as archethic;
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:riverpod_annotation/riverpod_annotation.dart';

part 'tokens.g.dart';

@riverpod
Future<List<aedappfm.AEToken>> tokensList(
TokensRepositoryImpl tokensRepositoryImpl(
Ref ref,
String userGenesisAddress, {
) =>
TokensRepositoryImpl();

@riverpod
Future<Map<String, archethic.Token>> tokensFromAddresses(
Ref ref,
List<String> addresses,
) async {
final apiService = ref.watch(apiServiceProvider);

return ref
.watch(
tokensRepositoryImplProvider,
)
.getTokensFromAddresses(
addresses,
apiService,
);
}

@riverpod
Future<List<aedappfm.AEToken>> tokensFromUserBalance(
Ref ref, {
bool withUCO = true,
bool withVerified = true,
bool withLPToken = true,
bool withNotVerified = true,
bool withCustomToken = true,
}) async {
final apiService = ref.watch(apiServiceProvider);

final environment = ref.watch(environmentProvider);
final poolListRaw = await ref.watch(DexPoolProviders.getPoolListRaw.future);
final selectedAccount =
await ref.watch(AccountProviders.accounts.future).selectedAccount;

return TokensRepositoryImpl().getTokensList(
userGenesisAddress,
apiService,
poolListRaw,
environment,
withVerified: withVerified,
withLPToken: withLPToken,
withNotVerified: withNotVerified,
);
if (selectedAccount == null) return [];

final tokensRepository = ref.watch(tokensRepositoryImplProvider);
final defTokensRepository =
ref.watch(aedappfm.defTokensRepositoryImplProvider);

return ref
.watch(
tokensRepositoryImplProvider,
)
.getTokensFromUserBalance(
selectedAccount.genesisAddress,
selectedAccount.customTokenAddressList ?? [],
apiService,
poolListRaw,
environment,
withUCO: withUCO,
withVerified: withVerified,
withLPToken: withLPToken,
withNotVerified: withNotVerified,
withCustomToken: withCustomToken,
defTokensRepository,
tokensRepository,
);
}

@riverpod
Future<double> tokensTotalUSD(
Ref ref,
String userGenesisAddress,
) async {
var total = 0.0;
const _logName = 'tokensTotalUSD';
final selectedAccount =
await ref.watch(AccountProviders.accounts.future).selectedAccount;

if (selectedAccount == null) return 0.0;

final tokensList = await ref.watch(
tokensListProvider(userGenesisAddress).future,
tokensFromUserBalanceProvider(
withLPToken: false,
withNotVerified: false,
withVerified: false,
).future,
);

for (final token in tokensList) {
Expand All @@ -63,7 +115,7 @@ Future<double> tokensTotalUSD(
)
: (await ref.watch(
DexTokensProviders.estimateTokenInFiat(
token.address != null ? token.address! : 'UCO',
token.address != null ? token.address! : kUCOAddress,
).future,
) *
token.balance);
Expand Down
Loading
Loading