Skip to content

Commit 3111d1f

Browse files
authored
fix issue with token balance update on transfer flow (#1064)
1 parent 8f163d0 commit 3111d1f

File tree

5 files changed

+38
-71
lines changed

5 files changed

+38
-71
lines changed

src/apps/popup/pages/transfer/components/token-swticher-row.tsx

Lines changed: 13 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,13 @@ import React from 'react';
22
import { Trans, useTranslation } from 'react-i18next';
33
import styled from 'styled-components';
44

5-
import {
6-
AlignedFlexRow,
7-
FlexColumn,
8-
ParagraphContainer,
9-
SpaceBetweenFlexRow,
10-
SpacingSize
11-
} from '@libs/layout';
5+
import { AlignedFlexRow, SpaceBetweenFlexRow, SpacingSize } from '@libs/layout';
126
import { SvgIcon, Tile, Typography } from '@libs/ui/components';
137

148
const Container = styled(SpaceBetweenFlexRow)`
159
padding: 16px;
1610
`;
1711

18-
const RowContainer = styled(FlexColumn)`
19-
width: 100%;
20-
`;
21-
2212
interface TokenSwitcherRowProps {
2313
tokenName: string | undefined;
2414
iconUrl: string | null | undefined;
@@ -31,26 +21,18 @@ export const TokenSwitcherRow = ({
3121
const { t } = useTranslation();
3222

3323
return (
34-
<RowContainer gap={SpacingSize.Small}>
35-
<ParagraphContainer top={SpacingSize.XXL}>
36-
<Typography type="bodySemiBold">
37-
<Trans t={t}>Token</Trans>
38-
</Typography>
39-
</ParagraphContainer>
40-
41-
<Tile>
42-
<Container gap={SpacingSize.Medium}>
43-
<AlignedFlexRow gap={SpacingSize.Large} flexGrow={1}>
44-
<SvgIcon src={iconUrl || ''} size={24} />
45-
<Typography dataTestId="token-row" type="body">
46-
{tokenName}
47-
</Typography>
48-
</AlignedFlexRow>
49-
<Typography type="bodySemiBold" color="contentAction">
50-
<Trans t={t}>Change</Trans>
24+
<Tile style={{ marginTop: '8px' }}>
25+
<Container gap={SpacingSize.Medium}>
26+
<AlignedFlexRow gap={SpacingSize.Large} flexGrow={1}>
27+
<SvgIcon src={iconUrl || ''} size={24} />
28+
<Typography dataTestId="token-row" type="body">
29+
{tokenName}
5130
</Typography>
52-
</Container>
53-
</Tile>
54-
</RowContainer>
31+
</AlignedFlexRow>
32+
<Typography type="bodySemiBold" color="contentAction">
33+
<Trans t={t}>Change</Trans>
34+
</Typography>
35+
</Container>
36+
</Tile>
5537
);
5638
};

src/apps/popup/pages/transfer/token-step.tsx

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,13 @@ export const TokenStep = ({
3737
const { t } = useTranslation();
3838

3939
const casperToken = useCasperToken();
40-
const { tokens } = useActiveAccountErc20Tokens();
40+
const { tokens, isLoading } = useActiveAccountErc20Tokens();
4141

4242
useEffect(() => {
4343
const tokensList: TokenType[] = [];
4444

45+
if (isLoading) return;
46+
4547
if (casperToken) {
4648
tokensList.push(casperToken);
4749
}
@@ -51,7 +53,15 @@ export const TokenStep = ({
5153
}
5254

5355
setTokenList(tokensList);
54-
}, [casperToken, tokens]);
56+
57+
const token = tokensList.find(token => token.id === selectedToken?.id);
58+
59+
if (token) {
60+
setSelectedToken(token);
61+
} else {
62+
setSelectedToken(casperToken);
63+
}
64+
}, [casperToken, tokens, isLoading]);
5565

5666
return (
5767
<ContentContainer>
@@ -61,6 +71,12 @@ export const TokenStep = ({
6171
</Typography>
6272
</ParagraphContainer>
6373

74+
<ParagraphContainer top={SpacingSize.XXL}>
75+
<Typography type="bodySemiBold">
76+
<Trans t={t}>Token</Trans>
77+
</Typography>
78+
</ParagraphContainer>
79+
6480
<Modal
6581
style={{ height: '528px' }}
6682
renderContent={({ closeModal }) => (

src/hooks/use-active-account-erc20-tokens.ts

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ export const useActiveAccountErc20Tokens = () => {
2222
const effectTimeoutRef = useRef<NodeJS.Timeout>();
2323
const forceUpdate = useForceUpdate();
2424
const [tokens, setTokens] = useState<TokenType[] | undefined>(undefined);
25+
const [isLoading, setIsLoading] = useState(true);
2526
const { t } = useTranslation();
2627

2728
const activeAccount = useSelector(selectVaultActiveAccount);
@@ -30,6 +31,7 @@ export const useActiveAccountErc20Tokens = () => {
3031
);
3132

3233
useEffect(() => {
34+
setIsLoading(true);
3335
dispatchFetchErc20TokensRequest(
3436
getAccountHashFromPublicKey(activeAccount?.publicKey)
3537
)
@@ -40,7 +42,8 @@ export const useActiveAccountErc20Tokens = () => {
4042
})
4143
.catch(error => {
4244
console.error('Balance request failed:', error);
43-
});
45+
})
46+
.finally(() => setIsLoading(false));
4447

4548
// will cause effect to run again after timeout
4649
effectTimeoutRef.current = setTimeout(() => {
@@ -53,5 +56,5 @@ export const useActiveAccountErc20Tokens = () => {
5356
// eslint-disable-next-line react-hooks/exhaustive-deps
5457
}, [activeAccount?.publicKey, casperClarityApiUrl, t, forceUpdate]);
5558

56-
return { tokens: tokens };
59+
return { tokens, isLoading };
5760
};

src/libs/services/erc20-service/constants.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,3 @@ export const getErc20TokensUrl = (
33
accountHash: string
44
) =>
55
`${casperClarityApiUrl}/accounts/${accountHash}/erc20-tokens?fields=latest_contract,contract_package`;
6-
7-
export const getContractPackageUrl = (
8-
casperClarityApiUrl: string,
9-
contractPackageHash: string
10-
) => `${casperClarityApiUrl}/contract-packages/${contractPackageHash}`;

src/libs/services/erc20-service/erc20-service.ts

Lines changed: 2 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,8 @@ import { queryClient } from '@libs/services/query-client';
77
import { DataResponse, Payload } from '@libs/services/types';
88
import { handleError, toJson } from '@libs/services/utils';
99

10-
import { getContractPackageUrl, getErc20TokensUrl } from './constants';
11-
import {
12-
ContractPackage,
13-
ContractPackageWithBalance,
14-
Erc20Token
15-
} from './types';
10+
import { getErc20TokensUrl } from './constants';
11+
import { ContractPackageWithBalance, Erc20Token } from './types';
1612

1713
export const erc20TokensRequest = (
1814
casperClarityApiUrl: string,
@@ -37,31 +33,6 @@ export const fetchErc20Tokens = ({
3733
{ staleTime: TOKENS_REFRESH_RATE }
3834
);
3935

40-
export const contractPackageRequest = (
41-
casperClarityApiUrl: string,
42-
contractPackageHash: string,
43-
signal?: AbortSignal
44-
): Promise<ContractPackage> =>
45-
fetch(getContractPackageUrl(casperClarityApiUrl, contractPackageHash), {
46-
signal
47-
})
48-
.then(toJson)
49-
.catch(handleError);
50-
51-
export const fetchContractPackage = ({
52-
casperClarityApiUrl,
53-
contractPackageHash
54-
}: {
55-
casperClarityApiUrl: string;
56-
contractPackageHash: string;
57-
}) =>
58-
queryClient.fetchQuery(
59-
['contractPackageRequest', casperClarityApiUrl, contractPackageHash],
60-
({ signal }) =>
61-
contractPackageRequest(casperClarityApiUrl, contractPackageHash, signal),
62-
{ staleTime: TOKENS_REFRESH_RATE }
63-
);
64-
6536
export const dispatchFetchErc20TokensRequest = (
6637
accountHash: string
6738
): Promise<Payload<ContractPackageWithBalance[]>> =>

0 commit comments

Comments
 (0)