Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix issue with token balance update on transfer flow #1064

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
44 changes: 13 additions & 31 deletions src/apps/popup/pages/transfer/components/token-swticher-row.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,13 @@ import React from 'react';
import { Trans, useTranslation } from 'react-i18next';
import styled from 'styled-components';

import {
AlignedFlexRow,
FlexColumn,
ParagraphContainer,
SpaceBetweenFlexRow,
SpacingSize
} from '@libs/layout';
import { AlignedFlexRow, SpaceBetweenFlexRow, SpacingSize } from '@libs/layout';
import { SvgIcon, Tile, Typography } from '@libs/ui/components';

const Container = styled(SpaceBetweenFlexRow)`
padding: 16px;
`;

const RowContainer = styled(FlexColumn)`
width: 100%;
`;

interface TokenSwitcherRowProps {
tokenName: string | undefined;
iconUrl: string | null | undefined;
Expand All @@ -31,26 +21,18 @@ export const TokenSwitcherRow = ({
const { t } = useTranslation();

return (
<RowContainer gap={SpacingSize.Small}>
<ParagraphContainer top={SpacingSize.XXL}>
<Typography type="bodySemiBold">
<Trans t={t}>Token</Trans>
</Typography>
</ParagraphContainer>

<Tile>
<Container gap={SpacingSize.Medium}>
<AlignedFlexRow gap={SpacingSize.Large} flexGrow={1}>
<SvgIcon src={iconUrl || ''} size={24} />
<Typography dataTestId="token-row" type="body">
{tokenName}
</Typography>
</AlignedFlexRow>
<Typography type="bodySemiBold" color="contentAction">
<Trans t={t}>Change</Trans>
<Tile style={{ marginTop: '8px' }}>
<Container gap={SpacingSize.Medium}>
<AlignedFlexRow gap={SpacingSize.Large} flexGrow={1}>
<SvgIcon src={iconUrl || ''} size={24} />
<Typography dataTestId="token-row" type="body">
{tokenName}
</Typography>
</Container>
</Tile>
</RowContainer>
</AlignedFlexRow>
<Typography type="bodySemiBold" color="contentAction">
<Trans t={t}>Change</Trans>
</Typography>
</Container>
</Tile>
);
};
20 changes: 18 additions & 2 deletions src/apps/popup/pages/transfer/token-step.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,13 @@
const { t } = useTranslation();

const casperToken = useCasperToken();
const { tokens } = useActiveAccountErc20Tokens();
const { tokens, isLoading } = useActiveAccountErc20Tokens();

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

if (isLoading) return;

if (casperToken) {
tokensList.push(casperToken);
}
Expand All @@ -51,7 +53,15 @@
}

setTokenList(tokensList);
}, [casperToken, tokens]);

const token = tokensList.find(token => token.id === selectedToken?.id);

if (token) {
setSelectedToken(token);
} else {
setSelectedToken(casperToken);
}
}, [casperToken, tokens, isLoading]);

Check warning on line 64 in src/apps/popup/pages/transfer/token-step.tsx

View workflow job for this annotation

GitHub Actions / build (18.x)

React Hook useEffect has missing dependencies: 'selectedToken?.id' and 'setSelectedToken'. Either include them or remove the dependency array. If 'setSelectedToken' changes too often, find the parent component that defines it and wrap that definition in useCallback

return (
<ContentContainer>
Expand All @@ -61,6 +71,12 @@
</Typography>
</ParagraphContainer>

<ParagraphContainer top={SpacingSize.XXL}>
<Typography type="bodySemiBold">
<Trans t={t}>Token</Trans>
</Typography>
</ParagraphContainer>

<Modal
style={{ height: '528px' }}
renderContent={({ closeModal }) => (
Expand Down
7 changes: 5 additions & 2 deletions src/hooks/use-active-account-erc20-tokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export const useActiveAccountErc20Tokens = () => {
const effectTimeoutRef = useRef<NodeJS.Timeout>();
const forceUpdate = useForceUpdate();
const [tokens, setTokens] = useState<TokenType[] | undefined>(undefined);
const [isLoading, setIsLoading] = useState(true);
const { t } = useTranslation();

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

useEffect(() => {
setIsLoading(true);
dispatchFetchErc20TokensRequest(
getAccountHashFromPublicKey(activeAccount?.publicKey)
)
Expand All @@ -40,7 +42,8 @@ export const useActiveAccountErc20Tokens = () => {
})
.catch(error => {
console.error('Balance request failed:', error);
});
})
.finally(() => setIsLoading(false));

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

return { tokens: tokens };
return { tokens, isLoading };
};
5 changes: 0 additions & 5 deletions src/libs/services/erc20-service/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,3 @@ export const getErc20TokensUrl = (
accountHash: string
) =>
`${casperClarityApiUrl}/accounts/${accountHash}/erc20-tokens?fields=latest_contract,contract_package`;

export const getContractPackageUrl = (
casperClarityApiUrl: string,
contractPackageHash: string
) => `${casperClarityApiUrl}/contract-packages/${contractPackageHash}`;
33 changes: 2 additions & 31 deletions src/libs/services/erc20-service/erc20-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,8 @@ import { queryClient } from '@libs/services/query-client';
import { DataResponse, Payload } from '@libs/services/types';
import { handleError, toJson } from '@libs/services/utils';

import { getContractPackageUrl, getErc20TokensUrl } from './constants';
import {
ContractPackage,
ContractPackageWithBalance,
Erc20Token
} from './types';
import { getErc20TokensUrl } from './constants';
import { ContractPackageWithBalance, Erc20Token } from './types';

export const erc20TokensRequest = (
casperClarityApiUrl: string,
Expand All @@ -37,31 +33,6 @@ export const fetchErc20Tokens = ({
{ staleTime: TOKENS_REFRESH_RATE }
);

export const contractPackageRequest = (
casperClarityApiUrl: string,
contractPackageHash: string,
signal?: AbortSignal
): Promise<ContractPackage> =>
fetch(getContractPackageUrl(casperClarityApiUrl, contractPackageHash), {
signal
})
.then(toJson)
.catch(handleError);

export const fetchContractPackage = ({
casperClarityApiUrl,
contractPackageHash
}: {
casperClarityApiUrl: string;
contractPackageHash: string;
}) =>
queryClient.fetchQuery(
['contractPackageRequest', casperClarityApiUrl, contractPackageHash],
({ signal }) =>
contractPackageRequest(casperClarityApiUrl, contractPackageHash, signal),
{ staleTime: TOKENS_REFRESH_RATE }
);

export const dispatchFetchErc20TokensRequest = (
accountHash: string
): Promise<Payload<ContractPackageWithBalance[]>> =>
Expand Down
Loading