Skip to content

Commit

Permalink
temp
Browse files Browse the repository at this point in the history
  • Loading branch information
Nodonisko committed Sep 26, 2024
1 parent c26f202 commit 33ed1aa
Show file tree
Hide file tree
Showing 22 changed files with 316 additions and 27 deletions.
6 changes: 6 additions & 0 deletions suite-common/wallet-utils/src/stakingUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,9 @@ export const getAccountTotalStakingBalance = (account?: Account) => {
.plus(pool?.withdrawTotalAmount ?? '0')
.toFixed();
};

export const getEthereumCryptoBalanceWithStaking = (account: Account) => {
const stakingBalance = getAccountTotalStakingBalance(account);

return new BigNumber(account.formattedBalance).plus(stakingBalance).toString();
};
1 change: 1 addition & 0 deletions suite-native/accounts/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
"@suite-native/navigation": "workspace:*",
"@suite-native/settings": "workspace:*",
"@suite-native/tokens": "workspace:*",
"@suite-native/staking": "workspace:*",
"@trezor/styles": "workspace:*",
"jotai": "1.9.1",
"proxy-memoize": "2.0.2",
Expand Down
9 changes: 8 additions & 1 deletion suite-native/accounts/src/components/AccountSectionTitle.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ import React, { useMemo } from 'react';
import { useSelector } from 'react-redux';

import { HStack, VStack, Text } from '@suite-native/atoms';
import { FiatAmountFormatter } from '@suite-native/formatters';
import { CryptoAmountFormatter, FiatAmountFormatter } from '@suite-native/formatters';
import { Account } from '@suite-common/wallet-types';
import { getAccountFiatBalance } from '@suite-common/wallet-utils';
import { getCryptoBalanceWithStaking } from '@suite-native/staking';
import { selectFiatCurrencyCode } from '@suite-native/settings';
import { selectCurrentFiatRates } from '@suite-common/wallet-core';

Expand Down Expand Up @@ -36,6 +37,12 @@ export const AccountSectionTitle: React.FC<AccountSectionTitleProps> = ({
adjustsFontSizeToFit
value={fiatBalance}
/>
<CryptoAmountFormatter
value={getCryptoBalanceWithStaking(account)}
network={account.symbol}
numberOfLines={1}
adjustsFontSizeToFit
/>
</VStack>
)}
</HStack>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { AccountSelectBottomSheetSection, OnSelectAccount } from '../types';
import { AccountsListItem } from './AccountsList/AccountsListItem';
import { AccountSectionTitle } from './AccountSectionTitle';
import { AccountsListTokenItem } from './AccountsList/AccountsListTokenItem';
import { AccountsListStakingItem } from './AccountsList/AccountsListStakingItem';

type AccountSelectBottomSheetProps = {
data: AccountSelectBottomSheetSection[];
Expand Down Expand Up @@ -39,8 +40,9 @@ export const AccountSelectBottomSheet = React.memo(
/>
);
case 'staking':
// TODO: Implement staking section
return null;
return (
<AccountsListStakingItem {...item} hasBackground onPress={() => {}} />
);
case 'token':
const { token, account } = item;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ export const AccountsList = ({
<AccountsListItem
key={account.key}
account={account}
hideTokens={hideTokensIntoModal}
hideBadges={hideTokensIntoModal}
onPress={handleSetBottomSheetAccount}
/>
))}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,19 @@ import {
selectNumberOfAccountTokensWithFiatRates,
TokensRootState,
} from '@suite-native/tokens';
import {
NativeStakingRootState,
selectAccountHasStaking,
StakingBadge,
} from '@suite-native/staking';

import { NativeAccountsRootState, selectAccountFiatBalance } from '../../selectors';
import { OnSelectAccount } from '../../types';
import { AccountsListItemBase } from './AccountsListItemBase';

export type AccountListItemProps = {
account: Account;
hideTokens?: boolean;
hideBadges?: boolean;

onPress?: OnSelectAccount;
disabled?: boolean;
Expand Down Expand Up @@ -53,7 +58,7 @@ export const AccountsListItem = ({
account,
onPress,
disabled,
hideTokens = false,
hideBadges = false,
hasBackground = false,
isFirst = false,
isLast = false,
Expand All @@ -69,6 +74,10 @@ export const AccountsListItem = ({
selectAccountHasAnyKnownToken(state, account.key),
);

const accountHasStaking = useSelector((state: NativeStakingRootState) =>
selectAccountHasStaking(state, account.key),
);

const fiatBalance = useSelector((state: NativeAccountsRootState) =>
selectAccountFiatBalance(state, account.key),
);
Expand All @@ -81,8 +90,9 @@ export const AccountsListItem = ({
}, [account, accountHasAnyTokens, onPress]);

const doesCoinSupportTokens = isCoinWithTokens(account.symbol);
const shouldShowAccountLabel = !doesCoinSupportTokens || hideTokens;
const shouldShowTokenBadge = accountHasAnyTokens && hideTokens;
const shouldShowAccountLabel = !doesCoinSupportTokens || hideBadges;
const shouldShowTokenBadge = accountHasAnyTokens && hideBadges;
const shouldShowStakingBadge = accountHasStaking && hideBadges;

return (
<AccountsListItemBase
Expand All @@ -105,6 +115,7 @@ export const AccountsListItem = ({
{formattedAccountType && (
<Badge label={formattedAccountType} size="small" elevation="1" />
)}
{shouldShowStakingBadge && <StakingBadge />}
{shouldShowTokenBadge && <TokenBadge accountKey={account.key} />}
</>
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Account } from '@suite-common/wallet-types';
import { RoundedIcon } from '@suite-native/atoms';
import { CryptoAmountFormatter, CryptoToFiatAmountFormatter } from '@suite-native/formatters';

import { AccountsListItemBase } from './AccountsListItemBase';

type AccountsListStakingItemProps = {
account: Account;
stakingCryptoBalance: string;
onPress: () => void;

hasBackground?: boolean;
isFirst?: boolean;
isLast?: boolean;
};

export const AccountsListStakingItem = ({
account,
onPress,
stakingCryptoBalance,
hasBackground,
isFirst,
isLast,
}: AccountsListStakingItemProps) => {
return (
<AccountsListItemBase
hasBackground={hasBackground}
isFirst={isFirst}
isLast={isLast}
showDivider={!isLast}
onPress={onPress}
icon={<RoundedIcon name="piggyBank" />}
title="Staking"
mainValue={
<CryptoToFiatAmountFormatter
value={stakingCryptoBalance}
network={account.symbol}
isBalance
/>
}
secondaryValue={
<CryptoAmountFormatter
value={stakingCryptoBalance}
network={account.symbol}
numberOfLines={1}
adjustsFontSizeToFit
/>
}
/>
);
};
19 changes: 13 additions & 6 deletions suite-native/accounts/src/selectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
selectVisibleDeviceAccounts,
} from '@suite-common/wallet-core';
import { AccountKey, TokenInfoBranded } from '@suite-common/wallet-types';
import { getAccountFiatBalance } from '@suite-common/wallet-utils';
import { getAccountFiatBalance, getAccountTotalStakingBalance } from '@suite-common/wallet-utils';
import { SettingsSliceRootState, selectFiatCurrencyCode } from '@suite-native/settings';
import { isCoinWithTokens } from '@suite-native/tokens';

Expand Down Expand Up @@ -78,12 +78,10 @@ export const selectAccountFiatBalance = (state: NativeAccountsRootState, account
return '0';
}

// Staking should be true once we support it in Trezor Suite Lite
const totalBalance = getAccountFiatBalance({
account,
rates: fiatRates,
localCurrency,
shouldIncludeStaking: false,
});

return totalBalance;
Expand All @@ -92,7 +90,7 @@ export const selectAccountFiatBalance = (state: NativeAccountsRootState, account
const EMPTY_ARRAY: any[] = [];

export const selectAccountListSections = memoizeWithArgs(
(state: NativeAccountsRootState, accountKey?: AccountKey | null) => {
(state: NativeAccountsRootState, accountKey?: AccountKey | null, hideStaking?: boolean) => {
if (!accountKey) return EMPTY_ARRAY;
const account = selectAccountByKey(state, accountKey);
if (!account) return EMPTY_ARRAY;
Expand All @@ -102,6 +100,8 @@ export const selectAccountListSections = memoizeWithArgs(
const canHasTokens = isCoinWithTokens(account.symbol);
const tokens = selectFilterKnownTokens(state, account.symbol, account.tokens ?? []);
const hasAnyKnownTokens = canHasTokens && !!tokens.length;
const stakingBalance = getAccountTotalStakingBalance(account);
const hasStaking = stakingBalance !== '0' && !hideStaking;

if (canHasTokens) {
sections.push({
Expand All @@ -113,12 +113,19 @@ export const selectAccountListSections = memoizeWithArgs(
sections.push({
type: 'account',
account,
isLast: !hasAnyKnownTokens,
isLast: !hasAnyKnownTokens && !hasStaking,
isFirst: true,
hasAnyKnownTokens,
});

// TODO: staking here
if (hasStaking) {
sections.push({
type: 'staking',
account,
stakingCryptoBalance: stakingBalance,
isLast: !hasAnyKnownTokens,
});
}

if (hasAnyKnownTokens) {
tokens.forEach((token, index) => {
Expand Down
1 change: 1 addition & 0 deletions suite-native/accounts/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export type AccountSelectBottomSheetSection = (
| {
type: 'staking';
account: Account;
stakingCryptoBalance: string;
}
| {
type: 'token';
Expand Down
1 change: 1 addition & 0 deletions suite-native/assets/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"@suite-native/settings": "workspace:*",
"@suite-native/tokens": "workspace:*",
"@trezor/styles": "workspace:*",
"@suite-native/staking": "workspace:*",
"proxy-memoize": "2.0.2",
"react": "^18.2.0",
"react-native": "0.75.2",
Expand Down
2 changes: 0 additions & 2 deletions suite-native/assets/src/assetsSelectors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,6 @@ const selectDeviceAssetsWithBalances = memoize((state: AssetsRootState) => {
account,
localCurrency: fiatCurrencyCode,
rates,
// TODO: this should be removed once Trezor Suite Lite supports staking
shouldIncludeStaking: false,
});

return {
Expand Down
17 changes: 9 additions & 8 deletions suite-native/assets/src/components/AssetItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,11 @@ import {
TabToStackCompositeNavigationProp,
} from '@suite-native/navigation';
import { selectHasDeviceAnyTokens } from '@suite-native/tokens';
import { prepareNativeStyle, useNativeStyles } from '@trezor/styles';
import {
selectHasAnyDeviceAccountsWithStaking,
NativeStakingRootState,
StakingBadge,
} from '@suite-native/staking';

import {
AssetsRootState,
Expand All @@ -36,10 +40,6 @@ type AssetItemProps = {
onPress?: (symbol: NetworkSymbol) => void;
};

const tokenBadgeStyle = prepareNativeStyle(() => ({
marginLeft: 10,
}));

type NavigationType = TabToStackCompositeNavigationProp<
AppTabsParamList,
AppTabsRoutes.HomeStack,
Expand Down Expand Up @@ -83,7 +83,6 @@ const PercentageIcon = React.memo(({ network }: { network: NetworkSymbol }) => {
});

export const AssetItem = React.memo(({ cryptoCurrencySymbol, onPress }: AssetItemProps) => {
const { applyStyle } = useNativeStyles();
const navigation = useNavigation<NavigationType>();
const { NetworkNameFormatter } = useFormatters();
const accountsKeysForNetworkSymbol = useSelectorDeepComparison((state: AssetsRootState) =>
Expand All @@ -95,6 +94,9 @@ export const AssetItem = React.memo(({ cryptoCurrencySymbol, onPress }: AssetIte
(state: AccountsRootState & DeviceRootState & TokenDefinitionsRootState) =>
selectHasDeviceAnyTokens(state, cryptoCurrencySymbol),
);
const hasAnyAccountsWithStaking = useSelector((state: NativeStakingRootState) =>
selectHasAnyDeviceAccountsWithStaking(state, cryptoCurrencySymbol),
);

const handleAssetPress = () => {
if (accountsPerAsset === 1 && !hasAnyTokens) {
Expand All @@ -121,10 +123,9 @@ export const AssetItem = React.memo(({ cryptoCurrencySymbol, onPress }: AssetIte
<Text variant="hint" color="textSubdued">
{accountsPerAsset}
</Text>

{hasAnyAccountsWithStaking && <StakingBadge />}
{hasAnyTokens && (
<Badge
style={applyStyle(tokenBadgeStyle)}
elevation="1"
size="small"
label={<Translation id="generic.tokens" />}
Expand Down
13 changes: 10 additions & 3 deletions suite-native/atoms/src/RoundedIcon.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { StyleProp, ViewStyle } from 'react-native';

import { G } from '@mobily/ts-belt';

import {
Expand All @@ -11,15 +13,15 @@ import {
import { prepareNativeStyle, useNativeStyles } from '@trezor/styles';
import { Color } from '@trezor/theme';

import { Box } from './Box';
import { Box, BoxProps } from './Box';

type RoundedIconProps = {
name: IconName | CoinSymbol;
color?: Color;
iconSize?: IconSize;
containerSize?: number;
backgroundColor?: Color;
};
} & BoxProps;

const DEFAULT_CONTAINER_SIZE = 48;

Expand Down Expand Up @@ -47,11 +49,16 @@ export const RoundedIcon = ({
iconSize,
backgroundColor,
containerSize,
style,
...boxProps
}: RoundedIconProps) => {
const { applyStyle } = useNativeStyles();

return (
<Box style={applyStyle(iconContainerStyle, { backgroundColor, containerSize })}>
<Box
style={[applyStyle(iconContainerStyle, { backgroundColor, containerSize }), style]}
{...boxProps}
>
{name in icons ? (
<Icon name={name as IconName} color={color} size={iconSize} />
) : (
Expand Down
20 changes: 20 additions & 0 deletions suite-native/staking/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "@suite-native/staking",
"version": "1.0.0",
"private": true,
"license": "See LICENSE.md in repo root",
"sideEffects": false,
"main": "src/index",
"scripts": {
"depcheck": "yarn g:depcheck",
"lint:js": "yarn g:eslint '**/*.{ts,tsx,js}'",
"test:unit": "jest -c ../../jest.config.base.js",
"type-check": "yarn g:tsc --build"
},
"dependencies": {
"@suite-common/wallet-config": "workspace:*",
"@suite-common/wallet-core": "workspace:*",
"@suite-common/wallet-types": "workspace:*",
"@suite-common/wallet-utils": "workspace:*"
}
}
Loading

0 comments on commit 33ed1aa

Please sign in to comment.