From be671efd6448475934ca684566b39f0eb8100414 Mon Sep 17 00:00:00 2001 From: Linkie Link Date: Wed, 9 Aug 2023 14:51:46 +0200 Subject: [PATCH] fix: fixed the autoconnect (#355) --- src/components/Wallet/WalletConnecting.tsx | 36 ++++++++++------------ src/store/slices/broadcast.ts | 7 +++-- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/src/components/Wallet/WalletConnecting.tsx b/src/components/Wallet/WalletConnecting.tsx index 74abecf3..21df9be6 100644 --- a/src/components/Wallet/WalletConnecting.tsx +++ b/src/components/Wallet/WalletConnecting.tsx @@ -30,22 +30,19 @@ const mapErrorMessages = (providerId: string, errorMessage: string) => { } export default function WalletConnecting(props: Props) { - const { extensionProviders, recentWallet, connect, simulate, sign, broadcast } = useShuttle() + const { recentWallet, connect, simulate, sign, broadcast } = useShuttle() const [isConnecting, setIsConnecting] = useToggle() const providerId = props.providerId ?? recentWallet?.providerId const isAutoConnect = props.autoConnect + const client = useStore((s) => s.client) const handleConnect = useCallback( (extensionProviderId: string) => { async function handleConnectAsync() { + if (client || isConnecting) return setIsConnecting(true) - try { - const provider = extensionProviders.find((p) => p.id === providerId) - const response = - isAutoConnect && provider - ? await provider.connect({ chainId: currentChainId }) - : await connect({ extensionProviderId, chainId: currentChainId }) + const response = await connect({ extensionProviderId, chainId: currentChainId }) const cosmClient = await CosmWasmClient.connect(response.network.rpc) const walletClient: WalletClient = { broadcast, @@ -61,8 +58,11 @@ export default function WalletConnecting(props: Props) { focusComponent: { component: }, }) } catch (error) { + setIsConnecting(false) + + if (isAutoConnect) return + if (error instanceof Error) { - setIsConnecting(false) useStore.setState({ client: undefined, address: undefined, @@ -81,19 +81,15 @@ export default function WalletConnecting(props: Props) { } } } - - handleConnectAsync() + setTimeout( + () => { + if (isConnecting) return + handleConnectAsync() + }, + isAutoConnect ? 1000 : 0, + ) }, - [ - broadcast, - connect, - extensionProviders, - isAutoConnect, - providerId, - setIsConnecting, - sign, - simulate, - ], + [broadcast, connect, client, isAutoConnect, isConnecting, setIsConnecting, sign, simulate], ) useEffect(() => { diff --git a/src/store/slices/broadcast.ts b/src/store/slices/broadcast.ts index 00539b1d..4158a8a7 100644 --- a/src/store/slices/broadcast.ts +++ b/src/store/slices/broadcast.ts @@ -12,10 +12,10 @@ import { ExecuteMsg as CreditManagerExecuteMsg, } from 'types/generated/mars-credit-manager/MarsCreditManager.types' import { getSingleValueFromBroadcastResult } from 'utils/broadcast' +import { defaultFee } from 'utils/constants' import { formatAmountWithSymbol } from 'utils/formatters' import getTokenOutFromSwapResponse from 'utils/getTokenOutFromSwapResponse' import { BN } from 'utils/helpers' -import { defaultFee } from 'utils/constants' function generateExecutionMessage( sender: string | undefined = '', @@ -56,8 +56,11 @@ export default function createBroadcastSlice( }) } } - const getEstimatedFee = async (messages: MsgExecuteContract[]) => { + if (!get().client) { + console.warn('Client not initialized') + return defaultFee + } try { const simulateResult = await get().client?.simulate({ messages,