good morning!!!!

Skip to content
Snippets Groups Projects

web3provider

  • Clone with SSH
  • Clone with HTTPS
  • Embed
  • Share
    The snippet can be accessed without any authentication.
    Authored by a
    Edited
    index.tsx 3.52 KiB
    import { PropsWithChildren } from 'react'
    
    import { MAINNET_CHAINS as CHAINS_LIST, corn } from '@gfxlabs/oku-chains'
    import type { AppKitNetwork } from '@reown/appkit-common'
    import { CaipNetworksUtil } from '@reown/appkit-utils'
    
    import { WagmiProvider } from 'wagmi'
    
    import { createAppKit } from '@reown/appkit/react'
    import { WagmiAdapter } from '@reown/appkit-adapter-wagmi'
    import { injected, unstable_connector } from '@wagmi/core'
    import { Transport, fallback, http, webSocket } from 'viem'
    import { shouldEnableRUM } from '../../faro.main'
    
    const projectId = '0ba768db4337c817602564a355370491'
    const metadata = {
      name: 'Oku',
      description: 'Oku - The #1 Defi Interface',
      url: 'https://oku.trade',
      icons: ['https://oku.trade/favicon.ico'],
      verifyUrl: 'verify.walletconnect.com',
    }
    
    const appkitNetworks = CHAINS_LIST.map((x): AppKitNetwork => {
      return {
        chainNamespace: 'eip155',
        caipNetworkId: `eip155:${x.id}` as any,
        id: x.id,
        name: x.name,
        blockExplorers: x.blockExplorers,
        nativeCurrency: x.nativeCurrency,
        rpcUrls: x.rpcUrls,
        contracts: x.contracts,
        formatters: x.formatters,
        sourceId: x.sourceId,
        fees: x.fees as any,
        testnet: x.testnet,
        serializers: x.serializers,
        custom: x.custom,
      }
    })
    const chainImages: Record<number, string> = Object.assign(
      {},
      ...CHAINS_LIST.map((x) => ({
        [x.id]: x.logoUrl,
      }))
    )
    const wagmiAdapter = new WagmiAdapter({
      projectId,
      networks: appkitNetworks,
      batch: {
        multicall: {
          wait: 100,
        },
      },
      connectors: [injected()],
      syncConnectedChain: true,
      multiInjectedProviderDiscovery: true,
      transports: appkitNetworks.reduce((acc: { [key: number | string]: Transport }, chain) => {
        const myhttp = (url: string) => {
          return http(url, {
            retryCount: 1,
            retryDelay: 3000,
            batch: {
              batchSize: 10,
              wait: 10,
            },
          })
        }
        if (chain.id === 324) {
          acc[chain.id] = myhttp(chain.rpcUrls.default.http[0])
          return acc
        }
        const fbs: Transport[] = []
        if (chain.rpcUrls.default.webSocket && chain.rpcUrls.default.webSocket.length > 0) {
          fbs.push(webSocket(chain.rpcUrls.default.webSocket[0]))
        }
        let rpcUrl = chain.rpcUrls.default.http[0]
        if (chain.id === corn.id) {
          rpcUrl = 'https://mainnet.corn-rpc.com'
        }
        if (rpcUrl) {
          fbs.push(myhttp(rpcUrl))
        }
        if ('caipNetworkId' in chain) {
          fbs.push(myhttp(CaipNetworksUtil.getDefaultRpcUrl(chain, chain.caipNetworkId, projectId)))
        }
        fbs.push(
          unstable_connector(injected, {
            retryCount: 0,
          })
        )
    
        acc[chain.id] = fallback(fbs)
        return acc
      }, {}),
    })
    
    export const appKit = createAppKit({
      showWallets: true,
      adapters: [wagmiAdapter],
      networks: [appkitNetworks[0], ...appkitNetworks.slice(1)],
      metadata: metadata,
      chainImages: chainImages,
      featuredWalletIds: [
        '18388be9ac2d02726dbac9777c96efaac06d744b2f6d580fccdd4127a6d01fd1', // rabby
        '971e689d0a5be527bac79629b4ee9b925e82208e5168b733496a09c0faed0709', // okx
        '8a0ee50d1f22f6651afcae7eb4253e52a3310b90af5daef78a8c4929a9bb99d4', // binance wallet
      ],
      features: {
        analytics: shouldEnableRUM(),
        allWallets: true,
        emailShowWallets: true,
        smartSessions: true,
        swaps: false,
        onramp: false,
        history: true,
        email: true,
        //socials: [],
      },
      enableEIP6963: true,
      projectId,
    })
    
    export const Web3Provider = ({ children }: PropsWithChildren<object>) => {
      return <WagmiProvider config={wagmiAdapter.wagmiConfig}>{children}</WagmiProvider>
    }
    0% Loading or .
    You are about to add 0 people to the discussion. Proceed with caution.
    Please register or to comment