mirror of
https://github.com/logos-messaging/examples.waku.org.git
synced 2026-01-02 12:53:08 +00:00
combine to useContract
This commit is contained in:
parent
1e8b2af982
commit
3ce6a7d9e9
@ -1,18 +1,15 @@
|
||||
import { Block, BlockTypes } from "@/components/Block";
|
||||
import { Title } from "@/components/Title";
|
||||
import { Button } from "@/components/Button";
|
||||
import { Status } from "@/components/Status";
|
||||
import { useStore, useWallet } from "@/hooks";
|
||||
import { useStore } from "@/hooks";
|
||||
|
||||
export const Header: React.FunctionComponent<{}> = () => {
|
||||
const { appStatus } = useStore();
|
||||
const { onConnectWallet } = useWallet();
|
||||
|
||||
return (
|
||||
<>
|
||||
<Block className="mb-5" type={BlockTypes.FlexHorizontal}>
|
||||
<Title>Waku RLN</Title>
|
||||
<Button onClick={onConnectWallet}>Connect Wallet</Button>
|
||||
</Block>
|
||||
<Status text="Application status" mark={appStatus} />
|
||||
</>
|
||||
|
||||
@ -8,25 +8,51 @@ type UseContractResult = {
|
||||
|
||||
export const useContract = (): UseContractResult => {
|
||||
const { rln } = useRLN();
|
||||
const { setLastMembershipID } = useStore();
|
||||
const { setEthAccount, setChainID, setLastMembershipID } = useStore();
|
||||
|
||||
const onFetchContract = React.useCallback(async () => {
|
||||
if (!rln?.rlnContract || !rln?.rlnInstance) {
|
||||
console.log("Cannot fetch contract info, no contract found.");
|
||||
return;
|
||||
}
|
||||
const fetchAccounts = new Promise<void>(async (resolve) => {
|
||||
if (!rln) {
|
||||
console.log("Cannot fetch wallet, not provider found.");
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
// disable button
|
||||
await rln.rlnContract.fetchMembers(rln.rlnInstance);
|
||||
// enable button
|
||||
rln.rlnContract.subscribeToMembers(rln.rlnInstance);
|
||||
try {
|
||||
const accounts = await rln.ethProvider.send("eth_requestAccounts", []);
|
||||
setEthAccount(accounts[0] || "");
|
||||
const network = await rln.ethProvider.getNetwork();
|
||||
setChainID(network.chainId);
|
||||
} catch (error) {
|
||||
console.error("Failed to connect to account: ", error);
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
|
||||
const last = rln.rlnContract.members.at(-1);
|
||||
const fetchContract = new Promise<void>(async (resolve) => {
|
||||
if (!rln?.rlnContract || !rln?.rlnInstance) {
|
||||
console.log("Cannot fetch contract info, no contract found.");
|
||||
resolve();
|
||||
return;
|
||||
}
|
||||
|
||||
if (last) {
|
||||
setLastMembershipID(last.index.toNumber());
|
||||
}
|
||||
}, [rln, setLastMembershipID]);
|
||||
try {
|
||||
await rln.rlnContract.fetchMembers(rln.rlnInstance);
|
||||
rln.rlnContract.subscribeToMembers(rln.rlnInstance);
|
||||
|
||||
const last = rln.rlnContract.members.at(-1);
|
||||
|
||||
if (last) {
|
||||
setLastMembershipID(last.index.toNumber());
|
||||
}
|
||||
} catch (error) {
|
||||
console.error("Failed to fetch contract state: ", error);
|
||||
}
|
||||
resolve();
|
||||
});
|
||||
|
||||
await Promise.any([fetchAccounts, fetchContract]);
|
||||
}, [rln, setEthAccount, setChainID, setLastMembershipID]);
|
||||
|
||||
return {
|
||||
onFetchContract,
|
||||
|
||||
@ -5,7 +5,6 @@ import { useRLN } from "./useRLN";
|
||||
import { SIGNATURE_MESSAGE } from "@/constants";
|
||||
|
||||
type UseWalletResult = {
|
||||
onConnectWallet: () => void;
|
||||
onGenerateCredentials: () => void;
|
||||
};
|
||||
|
||||
@ -37,22 +36,6 @@ export const useWallet = (): UseWalletResult => {
|
||||
};
|
||||
}, [setEthAccount, setChainID]);
|
||||
|
||||
const onConnectWallet = React.useCallback(async () => {
|
||||
if (!rln?.ethProvider) {
|
||||
console.log("Cannot connect wallet, no provider found.");
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const accounts = await rln.ethProvider.send("eth_requestAccounts", []);
|
||||
setEthAccount(accounts[0] || "");
|
||||
const network = await rln.ethProvider.getNetwork();
|
||||
setChainID(network.chainId);
|
||||
} catch (error) {
|
||||
console.error("Failed to connect to account: ", error);
|
||||
}
|
||||
}, [rln, setEthAccount, setChainID]);
|
||||
|
||||
const onGenerateCredentials = React.useCallback(async () => {
|
||||
if (!rln?.ethProvider) {
|
||||
console.log("Cannot generate credentials, no provider found.");
|
||||
@ -70,7 +53,6 @@ export const useWallet = (): UseWalletResult => {
|
||||
}, [rln, setCredentials]);
|
||||
|
||||
return {
|
||||
onConnectWallet,
|
||||
onGenerateCredentials,
|
||||
};
|
||||
};
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user