Skip to content

Commit

Permalink
fix: switch chain only if route exists
Browse files Browse the repository at this point in the history
  • Loading branch information
chybisov committed Aug 6, 2022
1 parent 909275e commit cf3c7d6
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions packages/widget/src/components/SwapButton/SwapButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,12 @@ export const SwapButton: React.FC<SwapButtonProps> = ({
const [chainId] = useWatch({
name: [SwapFormKeyHelper.getChainKey('from')],
});
const isCurrentChainMatch =
getChainById(chainId || ChainId.ETH)?.id === account.chainId;

// Allow switching chain only if execution is not started
const switchChainAllowed =
getChainById(chainId || ChainId.ETH)?.id !== account.chainId &&
currentRoute &&
!currentRoute.steps.some((step) => step.execution);

const handleSwapButtonClick = async () => {
if (!account.isActive) {
Expand All @@ -38,7 +42,7 @@ export const SwapButton: React.FC<SwapButtonProps> = ({
} else {
navigate(navigationRoutes.selectWallet);
}
} else if (!isCurrentChainMatch) {
} else if (switchChainAllowed) {
await switchChain(chainId!);
// check that the current route exists in the up to date route list
} else {
Expand All @@ -48,7 +52,7 @@ export const SwapButton: React.FC<SwapButtonProps> = ({

const getButtonText = () => {
if (account.isActive) {
if (!isCurrentChainMatch) {
if (switchChainAllowed) {
return t(`button.switchChain`);
}
if (!currentRoute) {
Expand All @@ -68,7 +72,7 @@ export const SwapButton: React.FC<SwapButtonProps> = ({
disabled={
(insufficientFunds || !!insufficientGas.length || loading) &&
currentRoute &&
isCurrentChainMatch
!switchChainAllowed
}
fullWidth
>
Expand Down

0 comments on commit cf3c7d6

Please sign in to comment.