Skip to content

Commit

Permalink
using batch provider now
Browse files Browse the repository at this point in the history
  • Loading branch information
MrX-SNX committed Aug 12, 2024
1 parent 8ecdedb commit da50dae
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 17 deletions.
8 changes: 5 additions & 3 deletions frontend/components/Admin/AdminPanel/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 ? (
Expand Down
4 changes: 2 additions & 2 deletions frontend/lib/store/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,15 @@ 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`
);

const vesterContract = new ethers.Contract(
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
Expand Down
8 changes: 4 additions & 4 deletions frontend/lib/store/grants.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export const grantsState = atom({
export const getGrants = selectorFamily({
key: "getGrants",
get:
(address) =>
(_) =>
({ get }) => {
return Object.values(get(grantsState));
},
Expand Down Expand Up @@ -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,
});
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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,
});
Expand Down
15 changes: 8 additions & 7 deletions frontend/pages/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<div>
Expand Down
2 changes: 1 addition & 1 deletion frontend/utils/ethers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]);
}

0 comments on commit da50dae

Please sign in to comment.