Skip to content

Commit

Permalink
Added delays for batch wallet operations
Browse files Browse the repository at this point in the history
  • Loading branch information
babkenmes committed Jan 16, 2025
1 parent b35a653 commit c6b3bb7
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 5 deletions.
22 changes: 19 additions & 3 deletions lib/wallets/evm/useEVM.ts
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,10 @@ export default function useEVM({ network }: Props): WalletProvider {
}

await connectAsync({
connector: connector,
connector: connector
});

const activeAccount = getAccount(config)
const activeAccount = await attemptGetAccount(config)
const connections = getConnections(config)
const connection = connections.find(c => c.connector.id === activeAccount.connector?.id)

Expand Down Expand Up @@ -332,4 +332,20 @@ const resolveSupportedNetworks = (supportedNetworks: string[], connectorId: stri

return supportedNetworks

}
}

async function attemptGetAccount(config, maxAttempts = 5) {
for (let attempt = 0; attempt < maxAttempts; attempt++) {
const account = await getAccount(config);

if (account.address) {
return account;
}
await sleep(500);
}

return await getAccount(config);
}
function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}
25 changes: 23 additions & 2 deletions lib/wallets/paradex/useParadex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { walletClientToSigner } from "../../ethersToViem/ethers"
import AuhorizeEthereum from "./Authorize/Ethereum"
import { getWalletClient } from '@wagmi/core'
import { useConfig } from "wagmi"
import { switchChain } from '@wagmi/core'
import { switchChain, getChainId } from '@wagmi/core'
import { useSettingsState } from "../../../context/settings"
import shortenAddress from "../../../components/utils/ShortenAddress"

Expand Down Expand Up @@ -67,7 +67,24 @@ export default function useParadex({ network }: Props): WalletProvider {
if (!Number(l1ChainId)) {
throw Error("Could not find ethereum network")
}
await switchChain(config, { chainId: l1ChainId })
const chainId = await getChainId(config)
if (l1ChainId !== chainId) {
try {
await sleep(1000)
await switchChain(config, { chainId: l1ChainId })
}
catch (e) {
await getChainId(config)
await sleep(1000)
const chainId = await getChainId(config)

if (l1ChainId !== chainId) {
throw Error("Could not switch to ethereum network")
}
}
await sleep(1000)
}
await sleep(1000)
const client = await getWalletClient(config)
const ethersSigner = walletClientToSigner(client)
if (!ethersSigner) {
Expand Down Expand Up @@ -174,3 +191,7 @@ const resolveSingleWallet = (wallet: Wallet, name: string, accounts: { [key: str
networkIcon
}
}

function sleep(ms) {
return new Promise(resolve => setTimeout(resolve, ms));
}

0 comments on commit c6b3bb7

Please sign in to comment.