Skip to content

Commit

Permalink
feat: getAllPositiveBalance for wallet storage
Browse files Browse the repository at this point in the history
  • Loading branch information
iGroza committed Jun 21, 2024
1 parent ef8dbe4 commit 1eed8b7
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 17 deletions.
20 changes: 20 additions & 0 deletions src/models/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ import {
STORE_REHYDRATION_TIMEOUT_MS,
} from '@app/variables/common';

import {Token} from './tokens';

import {realm} from './index';
import {
AddWalletParams,
Expand Down Expand Up @@ -274,6 +276,24 @@ class WalletStore implements MobXStoreFromRealm, RPCObserver {
return this.wallets;
}

// returns wallets with positive balance or positive token balance
getAllPositiveBalance() {
return this.getAll().filter(wallet => {
if (wallet.isHidden) {
return false;
}
const balance = app.getAvailableBalance(wallet.address);
const isPositiveBalance = balance.isPositive();

const tokens = Token.tokens[wallet.address] || [];
const isPositiveTokenBalance = tokens.some(
token => token.value?.isPositive?.(),
);

return isPositiveBalance || isPositiveTokenBalance;
});
}

getAllVisible() {
return this.wallets.filter(w => !w.isHidden);
}
Expand Down
5 changes: 4 additions & 1 deletion src/screens/SwapStack/swap-screen.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -953,9 +953,12 @@ export const SwapScreen = observer(() => {
};

const onPressChangeWallet = useCallback(async () => {
const wallets = Wallet.getAll();
const walletsWithBalances = Wallet.getAllPositiveBalance();

const address = await awaitForWallet({
title: I18N.selectAccount,
wallets: Wallet.getAllVisible(),
wallets: walletsWithBalances?.length ? walletsWithBalances : wallets,
initialAddress: currentWallet.address,
});
await refreshTokenBalances(address as HaqqEthereumAddress, tokenIn);
Expand Down
17 changes: 1 addition & 16 deletions src/widgets/swap-widget/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,9 @@ import {Color} from '@app/colors';
import {Spacer, Text, TextVariant} from '@app/components/ui';
import {ShadowCard} from '@app/components/ui/shadow-card';
import {WidgetHeader} from '@app/components/ui/widget-header';
import {app} from '@app/contexts';
import {awaitForWallet, createTheme} from '@app/helpers';
import {useTypedNavigation} from '@app/hooks';
import {I18N, getText} from '@app/i18n';
import {Token} from '@app/models/tokens';
import {Wallet} from '@app/models/wallet';
import {HomeStackRoutes} from '@app/route-types';

Expand All @@ -20,20 +18,7 @@ export const SwapWidget = observer(() => {

const onPress = useCallback(async () => {
const wallets = Wallet.getAll();
const walletsWithBalances = wallets.filter(wallet => {
if (wallet.isHidden) {
return false;
}
const balance = app.getAvailableBalance(wallet.address);
const isPositiveBalance = balance.isPositive();

const tokens = Token.tokens[wallet.address] || [];
const isPositiveTokenBalance = tokens.some(
token => token.value?.isPositive?.(),
);

return isPositiveBalance || isPositiveTokenBalance;
});
const walletsWithBalances = Wallet.getAllPositiveBalance();

const address = await awaitForWallet({
wallets: walletsWithBalances?.length ? walletsWithBalances : wallets,
Expand Down

0 comments on commit 1eed8b7

Please sign in to comment.