fix: fixed the autoconnect (#355)
This commit is contained in:
parent
223b55bff8
commit
be671efd64
@ -30,22 +30,19 @@ const mapErrorMessages = (providerId: string, errorMessage: string) => {
|
|||||||
}
|
}
|
||||||
|
|
||||||
export default function WalletConnecting(props: Props) {
|
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 [isConnecting, setIsConnecting] = useToggle()
|
||||||
const providerId = props.providerId ?? recentWallet?.providerId
|
const providerId = props.providerId ?? recentWallet?.providerId
|
||||||
const isAutoConnect = props.autoConnect
|
const isAutoConnect = props.autoConnect
|
||||||
|
const client = useStore((s) => s.client)
|
||||||
|
|
||||||
const handleConnect = useCallback(
|
const handleConnect = useCallback(
|
||||||
(extensionProviderId: string) => {
|
(extensionProviderId: string) => {
|
||||||
async function handleConnectAsync() {
|
async function handleConnectAsync() {
|
||||||
|
if (client || isConnecting) return
|
||||||
setIsConnecting(true)
|
setIsConnecting(true)
|
||||||
|
|
||||||
try {
|
try {
|
||||||
const provider = extensionProviders.find((p) => p.id === providerId)
|
const response = await connect({ extensionProviderId, chainId: currentChainId })
|
||||||
const response =
|
|
||||||
isAutoConnect && provider
|
|
||||||
? await provider.connect({ chainId: currentChainId })
|
|
||||||
: await connect({ extensionProviderId, chainId: currentChainId })
|
|
||||||
const cosmClient = await CosmWasmClient.connect(response.network.rpc)
|
const cosmClient = await CosmWasmClient.connect(response.network.rpc)
|
||||||
const walletClient: WalletClient = {
|
const walletClient: WalletClient = {
|
||||||
broadcast,
|
broadcast,
|
||||||
@ -61,8 +58,11 @@ export default function WalletConnecting(props: Props) {
|
|||||||
focusComponent: { component: <WalletFetchBalancesAndAccounts /> },
|
focusComponent: { component: <WalletFetchBalancesAndAccounts /> },
|
||||||
})
|
})
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
|
setIsConnecting(false)
|
||||||
|
|
||||||
|
if (isAutoConnect) return
|
||||||
|
|
||||||
if (error instanceof Error) {
|
if (error instanceof Error) {
|
||||||
setIsConnecting(false)
|
|
||||||
useStore.setState({
|
useStore.setState({
|
||||||
client: undefined,
|
client: undefined,
|
||||||
address: undefined,
|
address: undefined,
|
||||||
@ -81,19 +81,15 @@ export default function WalletConnecting(props: Props) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
setTimeout(
|
||||||
handleConnectAsync()
|
() => {
|
||||||
|
if (isConnecting) return
|
||||||
|
handleConnectAsync()
|
||||||
|
},
|
||||||
|
isAutoConnect ? 1000 : 0,
|
||||||
|
)
|
||||||
},
|
},
|
||||||
[
|
[broadcast, connect, client, isAutoConnect, isConnecting, setIsConnecting, sign, simulate],
|
||||||
broadcast,
|
|
||||||
connect,
|
|
||||||
extensionProviders,
|
|
||||||
isAutoConnect,
|
|
||||||
providerId,
|
|
||||||
setIsConnecting,
|
|
||||||
sign,
|
|
||||||
simulate,
|
|
||||||
],
|
|
||||||
)
|
)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
@ -12,10 +12,10 @@ import {
|
|||||||
ExecuteMsg as CreditManagerExecuteMsg,
|
ExecuteMsg as CreditManagerExecuteMsg,
|
||||||
} from 'types/generated/mars-credit-manager/MarsCreditManager.types'
|
} from 'types/generated/mars-credit-manager/MarsCreditManager.types'
|
||||||
import { getSingleValueFromBroadcastResult } from 'utils/broadcast'
|
import { getSingleValueFromBroadcastResult } from 'utils/broadcast'
|
||||||
|
import { defaultFee } from 'utils/constants'
|
||||||
import { formatAmountWithSymbol } from 'utils/formatters'
|
import { formatAmountWithSymbol } from 'utils/formatters'
|
||||||
import getTokenOutFromSwapResponse from 'utils/getTokenOutFromSwapResponse'
|
import getTokenOutFromSwapResponse from 'utils/getTokenOutFromSwapResponse'
|
||||||
import { BN } from 'utils/helpers'
|
import { BN } from 'utils/helpers'
|
||||||
import { defaultFee } from 'utils/constants'
|
|
||||||
|
|
||||||
function generateExecutionMessage(
|
function generateExecutionMessage(
|
||||||
sender: string | undefined = '',
|
sender: string | undefined = '',
|
||||||
@ -56,8 +56,11 @@ export default function createBroadcastSlice(
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const getEstimatedFee = async (messages: MsgExecuteContract[]) => {
|
const getEstimatedFee = async (messages: MsgExecuteContract[]) => {
|
||||||
|
if (!get().client) {
|
||||||
|
console.warn('Client not initialized')
|
||||||
|
return defaultFee
|
||||||
|
}
|
||||||
try {
|
try {
|
||||||
const simulateResult = await get().client?.simulate({
|
const simulateResult = await get().client?.simulate({
|
||||||
messages,
|
messages,
|
||||||
|
Loading…
Reference in New Issue
Block a user