Skip to content

Commit

Permalink
Refactor IQ login provider and Web3Auth connector functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Royal-lobster committed Nov 26, 2024
1 parent 16efa40 commit 6c88d37
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 39 deletions.
67 changes: 35 additions & 32 deletions src/components/iq-login-provider.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,61 +4,43 @@ import {
RainbowKitProvider,
} from "@rainbow-me/rainbowkit";
import { QueryClient, QueryClientProvider } from "@tanstack/react-query";
import { structuralSharing } from "@wagmi/core/query";
import type React from "react";
import { cookieToInitialState, WagmiProvider } from "wagmi";
import { polygon, type Chain } from "wagmi/chains";
import { iqTestnet } from "../lib/data/iq-testnet";
import type { Chain } from "wagmi/chains";
import { iqWikiTheme } from "../lib/data/rainbow-kit-theme";
import {
rainbowWeb3AuthConnector,
createWeb3AuthInstance,
rainbowWeb3AuthConnector,
} from "../lib/integrations/web3-auth-connector";
import { structuralSharing } from "@wagmi/core/query";
import { Web3AuthProvider } from "./web3-auth-provider";

function makeQueryClient() {
return new QueryClient({
defaultOptions: {
queries: {
staleTime: 60 * 1000,
structuralSharing,
},
},
});
}

let browserQueryClient: QueryClient | undefined = undefined;

function getQueryClient() {
if (typeof window === "undefined") {
return makeQueryClient();
}
if (!browserQueryClient) browserQueryClient = makeQueryClient();
return browserQueryClient;
interface IqLoginProviderProps {
children: React.ReactNode;
cookie?: string;
chain: Chain;
walletConnectProjectId: string;
web3AuthProjectId: string;
}

export function IqLoginProvider({
children,
cookie,
chain,
walletConnectProjectId,
web3AuthProjectId,
}: {
children: React.ReactNode;
cookie?: string;
chain: Chain;
web3AuthProjectId: string;
}) {
}: IqLoginProviderProps) {
const queryClient = getQueryClient();
const web3AuthInstance = createWeb3AuthInstance(chain);
const web3AuthInstance = createWeb3AuthInstance(chain, web3AuthProjectId);

const config = getDefaultConfig({
appName: "IQ.wiki",
projectId: web3AuthProjectId,
projectId: walletConnectProjectId,
chains: [chain],
wallets: [
...getDefaultWallets({
appName: "IQ.wiki",
projectId: web3AuthProjectId,
projectId: walletConnectProjectId,
}).wallets,
{
groupName: "More",
Expand All @@ -83,3 +65,24 @@ export function IqLoginProvider({
</QueryClientProvider>
);
}

function makeQueryClient() {
return new QueryClient({
defaultOptions: {
queries: {
staleTime: 60 * 1000,
structuralSharing,
},
},
});
}

let browserQueryClient: QueryClient | undefined = undefined;

function getQueryClient() {
if (typeof window === "undefined") {
return makeQueryClient();
}
if (!browserQueryClient) browserQueryClient = makeQueryClient();
return browserQueryClient;
}
8 changes: 1 addition & 7 deletions src/lib/integrations/web3-auth-connector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,6 @@ import * as Web3AuthWagmiConnector from "@web3auth/web3auth-wagmi-connector";
import type { Chain } from "viem";
import { createConnector } from "wagmi";

if (!process.env.NEXT_PUBLIC_WEB3_AUTH_CLIENT_ID) {
throw new Error("NEXT_PUBLIC_WEB3_AUTH_CLIENT_ID is not set");
}

const clientId = process.env.NEXT_PUBLIC_WEB3_AUTH_CLIENT_ID;

/**
* Creates a RainbowKit wallet adapter for the Web3Auth integration.
*
Expand Down Expand Up @@ -57,7 +51,7 @@ export const rainbowWeb3AuthConnector = ({
* @param chain - The chain configuration to use for the Web3Auth instance.
* @returns - A new Web3Auth instance with the specified configuration.
*/
export function createWeb3AuthInstance(chain: Chain) {
export function createWeb3AuthInstance(chain: Chain, clientId: string) {
const chainConfig = {
chainNamespace: Web3AuthBase.CHAIN_NAMESPACES.EIP155,
chainId: `0x${chain.id.toString(16)}`,
Expand Down

0 comments on commit 6c88d37

Please sign in to comment.