Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: excessive calls to infura provider (querying eth_chainId) #619

Merged
merged 3 commits into from
Dec 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion e2e/tests/accounts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -572,7 +572,7 @@ describe("CoreSDK - accounts", () => {
let exist = before.some((s) => s.id === seller.id);
expect(exist).toBe(false);
const after = await coreSDK.getSellers();
expect(after.length).toEqual(before.length + 1);
expect(after.length).toBeGreaterThan(before.length);
exist = after.some((s) => s.id === seller.id);
expect(exist).toBe(true);
});
Expand Down
18 changes: 11 additions & 7 deletions packages/react-kit/src/components/magicLink/MagicContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { UserProvider } from "./UserContext";
import { CONFIG } from "../../lib/config/config";
import { useConfigContext } from "../config/ConfigContext";
import type { getRpcUrls } from "../../lib/const/networks";
import { ethers } from "ethers";

export const MagicContext = createContext<
| (Magic & {
uuid: string;
})
| null
>(null);
export const MagicContext = createContext<{
magic: Magic & {
uuid: string;
};
magicProvider: ethers.providers.Web3Provider;
} | null>(null);

export const MagicProvider = ({ children }: { children: ReactNode }) => {
const { config, magicLinkKey } = useConfigContext();
Expand Down Expand Up @@ -53,7 +54,10 @@ export const InnerMagicProvider = ({
.preload()
.then(() => console.info("magic link preloaded"))
.catch(() => console.info("magic link could not be preloaded"));
return magic as typeof magic & { uuid: string };
return {
magic: magic as typeof magic & { uuid: string },
magicProvider: new ethers.providers.Web3Provider(magic.rpcProvider as any)
}; // return magic provider too
}, [chainId, magicLinkKey, rpcUrls]);
return (
<MagicContext.Provider value={magic}>
Expand Down
17 changes: 6 additions & 11 deletions packages/react-kit/src/hooks/magic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export const useMagic = () => {
if (!context) {
throw new Error("useMagic must be used within MagicContext");
}
return context;
return context.magic;
};

export const useWalletInfo = () => {
Expand All @@ -24,16 +24,11 @@ export const useWalletInfo = () => {
};

export function useMagicProvider() {
const magic = useMagic();
const magicProvider = useMemo(
() =>
magic
? // eslint-disable-next-line @typescript-eslint/no-explicit-any
new ethers.providers.Web3Provider(magic.rpcProvider as any)
: null,
[magic]
);
return magicProvider;
const context = useContext(MagicContext);
if (!context) {
throw new Error("useMagic must be used within MagicContext");
}
return context.magicProvider;
}

export function useMagicChainId() {
Expand Down