Skip to content

Commit

Permalink
fix: bugfix for switch chain on lifi widget (#11509)
Browse files Browse the repository at this point in the history
  • Loading branch information
nuanyang233 committed Mar 21, 2024
1 parent 2a7e52c commit 078276c
Showing 1 changed file with 16 additions and 16 deletions.
32 changes: 16 additions & 16 deletions packages/plugins/Trader/src/SiteAdaptor/trader/ExchangeDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ export interface ExchangeDialogProps {
export const ExchangeDialog = memo<ExchangeDialogProps>(function ExchangeDialog({ open, onClose }) {
const t = useTraderTrans()
const { Provider } = useWeb3State(NetworkPluginID.PLUGIN_EVM)
const { providerType } = useChainContext<NetworkPluginID.PLUGIN_EVM>()
const { providerType, chainId, account } = useChainContext<NetworkPluginID.PLUGIN_EVM>()
const { theme, classes } = useStyles()
const [containerRef, setContainerRef] = useState<HTMLElement>()
const networks = useNetworks(NetworkPluginID.PLUGIN_EVM)
Expand All @@ -48,15 +48,18 @@ export const ExchangeDialog = memo<ExchangeDialogProps>(function ExchangeDialog(
}
}, [onClose])

const getSigner = useCallback(() => {
const providerType = Provider?.providerType?.getCurrentValue()
const provider = EVMWeb3.getWeb3Provider({ providerType })
const getSigner = useCallback(
(requiredChainId?: ChainId) => {
const providerType = Provider?.providerType?.getCurrentValue()
const provider = EVMWeb3.getWeb3Provider({ providerType, chainId: requiredChainId ?? chainId, account })

// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const signer = new Web3Provider(provider, 'any').getSigner()
return signer
}, [Provider])
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-expect-error
const signer = new Web3Provider(provider, 'any').getSigner()
return signer
},
[Provider, chainId, account],
)

const widgetConfig = useMemo<WidgetConfig>(() => {
return {
Expand All @@ -70,13 +73,10 @@ export const ExchangeDialog = memo<ExchangeDialogProps>(function ExchangeDialog(
},
walletManagement: {
signer: getSigner(),
beforeSwitchChain: async (chainId: ChainId) => {
switchChain: async (chainId: ChainId) => {
const providerType = Provider?.providerType?.getCurrentValue()
if (providerType === ProviderType.MaskWallet) {
await EVMWeb3.switchChain(chainId, { silent: true })
}

return
await EVMWeb3.switchChain(chainId, { silent: providerType === ProviderType.MaskWallet })
return getSigner(chainId)
},
connect: async () => {
if (providerType === ProviderType.None) await SelectProviderModal.openAndWaitForClose()
Expand All @@ -87,7 +87,7 @@ export const ExchangeDialog = memo<ExchangeDialogProps>(function ExchangeDialog(
hiddenUI: [HiddenUI.Header],
appearance: theme.palette.mode,
}
}, [theme, providerType])
}, [theme, providerType, getSigner])

return (
<InjectedDialog
Expand Down

0 comments on commit 078276c

Please sign in to comment.