fix: fixed the autoconnect (#355)

This commit is contained in:
Linkie Link 2023-08-09 14:51:46 +02:00 committed by GitHub
parent 223b55bff8
commit be671efd64
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 21 additions and 22 deletions

View File

@ -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: <WalletFetchBalancesAndAccounts /> },
})
} 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(() => {

View File

@ -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,