Skip to content

Commit

Permalink
fix: excessive calls to infura provider (querying eth_chainId) (#619)
Browse files Browse the repository at this point in the history
* fix: excessive calls to infura provider (querying eth_chainId)

* Revert "fix: excessive calls to infura provider (querying eth_chainId)"

This reverts commit 20a5fcf.

* fix: create the magicProvider in the magic context hook to ensure it's created only once
  • Loading branch information
levalleux-ludo authored Dec 5, 2023
1 parent 89ae2db commit 112bdce
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 19 deletions.
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

0 comments on commit 112bdce

Please sign in to comment.