Skip to content

Commit

Permalink
fix: ignore usdc token on crypto portfolio (#1478)
Browse files Browse the repository at this point in the history
Co-authored-by: Kirill Bubochkin <ookami.kb@gmail.com>
  • Loading branch information
justinenerio and ookami-kb authored Jun 3, 2024
1 parent 2c92ab0 commit 0d69d0f
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import '../../conversion_rates/services/watch_token_total_balance.dart';
import '../../conversion_rates/widgets/extensions.dart';
import '../../currency/models/amount.dart';
import '../../currency/models/currency.dart';
import '../../tokens/token.dart';
import '../../tokens/widgets/token_icon.dart';

class PortfolioWidget extends StatefulWidget {
Expand All @@ -34,7 +35,9 @@ class _PortfolioWidgetState extends State<PortfolioWidget>

return ValueStreamBuilder<IList<CryptoAmount>>(
create: () => (
sl<TokenBalancesRepository>().watchTokenBalances(),
sl<TokenBalancesRepository>().watchTokenBalances(
ignoreTokens: [Token.usdc],
),
const IListConst([])
),
builder: (context, balances) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,18 +17,28 @@ class TokenBalancesRepository {
final MyDatabase _db;
final TokenList _tokens;

Future<ISet<Token>> readUserTokens() =>
_db.tokenBalanceRows.select().get().then(
(rows) => rows
.map((row) => _tokens.findTokenByMint(row.token))
.whereNotNull()
.toISet(),
);

Stream<ISet<Token>> watchUserTokens() {
Future<ISet<Token>> readUserTokens() {
final query = _db.tokenBalanceRows.select()
..where((tbl) => tbl.amount.isBiggerThanValue(0));

return query.get().then(
(rows) => rows
.map((row) => _tokens.findTokenByMint(row.token))
.whereNotNull()
.toISet(),
);
}

Stream<ISet<Token>> watchUserTokens({
Iterable<Token> ignoreTokens = const [],
}) {
final query = _db.tokenBalanceRows.select()
..where(
(tbl) =>
tbl.amount.isBiggerThanValue(0) &
tbl.token.isNotIn(ignoreTokens.map((e) => e.address).toList()),
);

return query.watch().map(
(rows) => rows
.map((row) => _tokens.findTokenByMint(row.token))
Expand All @@ -37,9 +47,15 @@ class TokenBalancesRepository {
);
}

Stream<IList<CryptoAmount>> watchTokenBalances() {
Stream<IList<CryptoAmount>> watchTokenBalances({
Iterable<Token> ignoreTokens = const [],
}) {
final query = _db.tokenBalanceRows.select()
..where((tbl) => tbl.amount.isBiggerThanValue(0));
..where(
(tbl) =>
tbl.amount.isBiggerThanValue(0) &
tbl.token.isNotIn(ignoreTokens.map((e) => e.address).toList()),
);

return query.watch().map(
(rows) => rows
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ class ConversionRatesRepository extends ChangeNotifier {
Iterable<Token> tokens,
) =>
tryEitherAsync((_) async {
final ids = await Stream.fromIterable(tokens.addresses)
final ids = await Stream.fromIterable(tokens.symbols)
.bufferCount(_maxIds)
.toList();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ class WatchTotalTokenFiatBalance {
final WatchTokenFiatBalance _watchTokenFiatBalance;

Stream<Amount> call() => _balancesRepository
.watchUserTokens()
.watchUserTokens(ignoreTokens: [Token.usdc])
.flatMap(
(tokens) => Rx.combineLatest(
tokens.where((t) => t != Token.usdc).map(_watchTokenFiatBalance.call),
tokens.map(_watchTokenFiatBalance.call),
(values) => values.whereNotNull().fold(
Amount.zero(currency: defaultFiatCurrency),
(total, next) => total + next,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,5 @@ class TokenList {
}

extension TokenExt on Iterable<Token> {
Iterable<String> get addresses => map((t) => t.symbol);
Iterable<String> get symbols => map((t) => t.symbol);
}

0 comments on commit 0d69d0f

Please sign in to comment.