Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@ import StakingBalance from '../../../Stake/components/StakingBalance/StakingBala
import { TokenI } from '../../../Tokens/types';
import EarnLendingBalance from '../EarnLendingBalance';
import { selectTrxStakingEnabled } from '../../../../../selectors/featureFlagController/trxStakingEnabled';
import { selectTronSpecialAssetsBySelectedAccountGroup } from '../../../../../selectors/assets/assets-list';
import TronStakingButtons from '../Tron/TronStakingButtons';
import { selectIsMusdConversionFlowEnabledFlag } from '../../selectors/featureFlags';

/**
Expand All @@ -25,16 +23,6 @@ jest.mock(
}),
);

jest.mock('../../../../../selectors/assets/assets-list', () => ({
...jest.requireActual('../../../../../selectors/assets/assets-list'),
selectTronSpecialAssetsBySelectedAccountGroup: jest.fn(),
}));

jest.mock('../Tron/TronStakingButtons', () => ({
__esModule: true,
default: jest.fn(() => null),
}));

jest.mock('../../../../../selectors/earnController', () => ({
...jest.requireActual('../../../../../selectors/earnController'),
earnSelectors: {
Expand Down Expand Up @@ -128,35 +116,10 @@ const mockUseMusdConversionEligibility =
typeof useMusdConversionEligibility
>;

jest.mock('../../hooks/useTronStakeApy', () => ({
__esModule: true,
default: jest.fn().mockReturnValue({
apyPercent: '4.5%',
isLoading: false,
error: null,
}),
}));

const createEmptySpecialAssetsMap = () => ({
energy: undefined,
bandwidth: undefined,
maxEnergy: undefined,
maxBandwidth: undefined,
stakedTrxForEnergy: undefined,
stakedTrxForBandwidth: undefined,
totalStakedTrx: 0,
trxReadyForWithdrawal: undefined,
trxStakingRewards: undefined,
trxInLockPeriod: undefined,
});

describe('EarnBalance', () => {
beforeEach(() => {
jest.clearAllMocks();
(jest.mocked(selectTrxStakingEnabled) as jest.Mock).mockReturnValue(false);
(
jest.mocked(selectTronSpecialAssetsBySelectedAccountGroup) as jest.Mock
).mockReturnValue(createEmptySpecialAssetsMap());
});

describe('Ethereum Mainnet', () => {
Expand Down Expand Up @@ -253,30 +216,26 @@ describe('EarnBalance', () => {

describe('TRON', () => {
const mockFlag = selectTrxStakingEnabled as unknown as jest.Mock;
const mockTronResources =
selectTronSpecialAssetsBySelectedAccountGroup as unknown as jest.Mock;

it('renders TRON stake button with aprText for TRX without staked positions', () => {
it('renders nothing for TRX when Tron staking is enabled', () => {
const trx: Partial<TokenI> = {
chainId: 'tron:728126428',
ticker: 'TRX',
symbol: 'TRX',
};

mockFlag.mockReturnValue(true);
mockTronResources.mockReturnValue(createEmptySpecialAssetsMap());

renderWithProvider(<EarnBalance asset={trx as TokenI} />);
const { toJSON } = renderWithProvider(
<EarnBalance asset={trx as TokenI} />,
);

expect(TronStakingButtons).toHaveBeenCalled();
const props = (TronStakingButtons as jest.Mock).mock.calls[0][0];
expect(props.asset).toBe(trx);
expect(props.aprText).toBe('4.5%');
expect(props.showUnstake).toBeUndefined();
expect(props.hasStakedPositions).toBeUndefined();
expect(toJSON()).toBeNull();
expect(StakingBalance).not.toHaveBeenCalled();
expect(EarnLendingBalance).not.toHaveBeenCalled();
});

it('renders TRON stake more and unstake for sTRX with staked positions', () => {
it('renders nothing for sTRX when Tron staking is enabled', () => {
const strx: Partial<TokenI> = {
chainId: 'tron:728126428',
ticker: 'sTRX',
Expand All @@ -285,20 +244,14 @@ describe('EarnBalance', () => {
};

mockFlag.mockReturnValue(true);
mockTronResources.mockReturnValue({
...createEmptySpecialAssetsMap(),
stakedTrxForEnergy: { symbol: 'strx-energy', balance: '1' },
stakedTrxForBandwidth: { symbol: 'strx-bandwidth', balance: '2' },
totalStakedTrx: 3,
});

renderWithProvider(<EarnBalance asset={strx as TokenI} />);
const { toJSON } = renderWithProvider(
<EarnBalance asset={strx as TokenI} />,
);

expect(TronStakingButtons).toHaveBeenCalled();
const props = (TronStakingButtons as jest.Mock).mock.calls[0][0];
expect(props.asset).toBe(strx);
expect(props.showUnstake).toBe(true);
expect(props.hasStakedPositions).toBe(true);
expect(toJSON()).toBeNull();
expect(StakingBalance).not.toHaveBeenCalled();
expect(EarnLendingBalance).not.toHaveBeenCalled();
});
});

Expand Down
37 changes: 1 addition & 36 deletions app/components/UI/Earn/components/EarnBalance/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,7 @@ import { TokenI } from '../../../Tokens/types';
import EarnLendingBalance from '../EarnLendingBalance';
import { selectIsStakeableToken } from '../../../Stake/selectors/stakeableTokens';
///: BEGIN:ONLY_INCLUDE_IF(tron)
import TronStakingButtons from '../Tron/TronStakingButtons';
import { selectTronSpecialAssetsBySelectedAccountGroup } from '../../../../../selectors/assets/assets-list';
import { selectTrxStakingEnabled } from '../../../../../selectors/featureFlagController/trxStakingEnabled';
import { hasStakedTrxPositions as hasStakedTrxPositionsUtil } from '../../utils/tron';
import useTronStakeApy from '../../hooks/useTronStakeApy';
///: END:ONLY_INCLUDE_IF
import { useMusdConversionTokens } from '../../hooks/useMusdConversionTokens';
import { useMusdConversionEligibility } from '../../hooks/useMusdConversionEligibility';
Expand All @@ -38,43 +34,12 @@ const EarnBalance = ({ asset }: EarnBalanceProps) => {

const { isConversionToken } = useMusdConversionTokens();
const { isEligible: isGeoEligible } = useMusdConversionEligibility();

///: BEGIN:ONLY_INCLUDE_IF(tron)
const isTrxStakingEnabled = useSelector(selectTrxStakingEnabled);

const isTron = asset?.chainId?.startsWith('tron:');
const isNativeTrx =
isTron && (asset?.ticker === 'TRX' || asset?.symbol === 'TRX');
const isStakedTrxAsset =
isTron && (asset?.ticker === 'sTRX' || asset?.symbol === 'sTRX');

const tronSpecialAssets = useSelector(
selectTronSpecialAssetsBySelectedAccountGroup,
);
const hasStakedTrxPositions = React.useMemo(
() => hasStakedTrxPositionsUtil(tronSpecialAssets),
[tronSpecialAssets],
);

const { apyPercent: tronApyPercent } = useTronStakeApy();

if (isTron && isTrxStakingEnabled) {
if (hasStakedTrxPositions && isStakedTrxAsset) {
// sTRX row: show Unstake + Stake more
return (
<TronStakingButtons asset={asset} showUnstake hasStakedPositions />
);
}

if (!hasStakedTrxPositions && isNativeTrx) {
// TRX native row: show CTA + single Stake button
return (
<TronStakingButtons
asset={asset}
aprText={tronApyPercent ?? undefined}
/>
);
}

return null;
}
///: END:ONLY_INCLUDE_IF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,6 @@ const styleSheet = (params: { theme: Theme }) =>
borderRadius: 12,
backgroundColor: params.theme.colors.background.section,
},
ctaContent: {
alignItems: 'center',
marginBottom: 16,
gap: 4,
},
ctaTitle: {
textAlign: 'center',
},
ctaText: {
textAlign: 'center',
},
buttonsRow: {
flexDirection: 'row',
justifyContent: 'space-between',
Expand Down
Loading
Loading