From da50daed8c5a9f1c9038dfa8a86c6f94f570036a Mon Sep 17 00:00:00 2001 From: MrX-SNX Date: Mon, 12 Aug 2024 12:52:42 +0100 Subject: [PATCH] using batch provider now --- frontend/components/Admin/AdminPanel/index.js | 8 +++++--- frontend/lib/store/events.js | 4 ++-- frontend/lib/store/grants.js | 8 ++++---- frontend/pages/admin.js | 15 ++++++++------- frontend/utils/ethers.js | 2 +- 5 files changed, 20 insertions(+), 17 deletions(-) diff --git a/frontend/components/Admin/AdminPanel/index.js b/frontend/components/Admin/AdminPanel/index.js index 2c599b6..801ce19 100644 --- a/frontend/components/Admin/AdminPanel/index.js +++ b/frontend/components/Admin/AdminPanel/index.js @@ -20,9 +20,11 @@ export default function AdminPanel() { Promise.all([ fetchEvents(setEvents, networkId), fetchGrants(setGrant, networkId), - ]).finally(() => { - setLoadingData(false); - }); + ]) + .catch((error) => console.error(error)) + .finally(() => { + setLoadingData(false); + }); }, [networkId]); return loadingData ? ( diff --git a/frontend/lib/store/events.js b/frontend/lib/store/events.js index 3b5a892..b7308fa 100644 --- a/frontend/lib/store/events.js +++ b/frontend/lib/store/events.js @@ -51,7 +51,7 @@ export const fetchEvents = async (setEvents, networkId) => { throw Error("Invalid network id:" + networkId); } // Always use infura for fetching events, provider from wallet can be really slow - const provider = new ethers.providers.JsonRpcProvider( + const provider = new ethers.providers.JsonRpcBatchProvider( `https://${infuraName}.infura.io/v3/8abb2592d8d344daafc5362ddd33efd1` ); @@ -59,7 +59,7 @@ export const fetchEvents = async (setEvents, networkId) => { process.env.NEXT_PUBLIC_VESTER_CONTRACT_ADDRESS, vesterAbi.abi, provider - ); // should be provider.getSigner() ? + ); let newEvents = {}; // TODO: Make below more abstract, just gather all events diff --git a/frontend/lib/store/grants.js b/frontend/lib/store/grants.js index cbd8900..9b88227 100644 --- a/frontend/lib/store/grants.js +++ b/frontend/lib/store/grants.js @@ -19,7 +19,7 @@ export const grantsState = atom({ export const getGrants = selectorFamily({ key: "getGrants", get: - (address) => + (_) => ({ get }) => { return Object.values(get(grantsState)); }, @@ -77,7 +77,7 @@ export const fetchGrant = async (setGrant, tokenId, networkId) => { throw Error("Invalid network id:" + networkId); } // Always use infura for fetching events, provider from wallet can be really slow - const provider = new ethers.providers.JsonRpcProvider({ + const provider = new ethers.providers.JsonRpcBatchProvider({ url: `https://${infuraName}.infura.io/v3/8abb2592d8d344daafc5362ddd33efd1`, skipFetchSetup: true, }); @@ -162,7 +162,7 @@ export const fetchGrants = async (setGrant, networkId) => { throw Error("Invalid network id:" + networkId); } // Always use infura for fetching events, provider from wallet can be really slow - const provider = new ethers.providers.JsonRpcProvider( + const provider = new ethers.providers.JsonRpcBatchProvider( `https://${infuraName}.infura.io/v3/8c6bfe963db94518b16b17114e29e628` ); const vesterContract = new ethers.Contract( @@ -190,7 +190,7 @@ export const fetchGrantsByUser = async (setGrant, owner, networkId) => { throw Error("Invalid network id:" + networkId); } // Always use infura for fetching events, provider from wallet can be really slow - const provider = new ethers.providers.JsonRpcProvider({ + const provider = new ethers.providers.JsonRpcBatchProvider({ url: `https://${infuraName}.infura.io/v3/8abb2592d8d344daafc5362ddd33efd1`, skipFetchSetup: true, }); diff --git a/frontend/pages/admin.js b/frontend/pages/admin.js index fa479e6..8f95fc4 100644 --- a/frontend/pages/admin.js +++ b/frontend/pages/admin.js @@ -4,26 +4,27 @@ import { useAccount } from "wagmi"; import vesterAbi from "../abis/Vester.json"; import { useState, useEffect } from "react"; import { ethers } from "ethers"; +import { useEthersSigner } from "../utils/ethers"; export default function Admin() { - const { address } = useAccount(); - - const [_, setOwner] = useState(null); + const { address, chain } = useAccount(); + const signer = useEthersSigner({ chainId: chain?.id || "10" }); + const [owner, setOwner] = useState(null); useEffect(() => { async function getOwner() { - const provider = new ethers.providers.Web3Provider(window?.ethereum); + if (!signer) return; const vesterContract = new ethers.Contract( process.env.NEXT_PUBLIC_VESTER_CONTRACT_ADDRESS, vesterAbi.abi, - provider + signer ); // should be provider.getSigner() ? const owner = await vesterContract.owner(); setOwner(owner); } getOwner(); - }, []); + }, [signer]); - const shouldRender = address; // && address == owner; + const shouldRender = address; return (
diff --git a/frontend/utils/ethers.js b/frontend/utils/ethers.js index 0be2733..8608d39 100644 --- a/frontend/utils/ethers.js +++ b/frontend/utils/ethers.js @@ -16,6 +16,6 @@ export function clientToSigner(client) { /** Hook to convert a Viem Client to an ethers.js Signer. */ export function useEthersSigner({ chainId } = {}) { - const { data: client } = useConnectorClient < Config > { chainId }; + const { data: client } = useConnectorClient({ chainId }); return useMemo(() => (client ? clientToSigner(client) : undefined), [client]); }