UNPKG

@ordao/privy-react-orclient

Version:

"Helpers for using orclient with privy and react"

77 lines (76 loc) 3.42 kB
import { useEffect, useState } from "react"; import { createOrclient, createOrclientReaderWithFallback } from "@ordao/orclient/createOrclient.js"; import { usePrivy } from "@privy-io/react-auth"; import { ordaoLibVersions } from "./libVersions.js"; function attachVersion(orclient) { orclient['version'] = () => { console.log("versions: ", JSON.stringify(ordaoLibVersions, undefined, 2)); }; return orclient; } export async function create(deployment, wallet, orclientCfg) { console.log("Creating full orclient"); const provider = await wallet.getEthereumProvider(); const orclient = await createOrclient(deployment, provider, orclientCfg); // Change version function to include privy-react-orclient version attachVersion(orclient); return orclient; } export async function createReader(deployment, providerUrls, orclientCfg) { console.log("Creating orclient reader"); const orclient = await createOrclientReaderWithFallback(deployment, providerUrls, orclientCfg); attachVersion(orclient); return orclient; } export function useOrclient(deployment, wallet, orclientConfig) { const [orclient, setOrclient] = useState(undefined); useEffect(() => { // For seeing debug logs of orclient console.debug = console.log; if (deployment && wallet) { create(deployment, wallet, orclientConfig).then(orclient => setOrclient(orclient), err => console.error(err)); } else if (orclient !== undefined) { setOrclient(undefined); } }, [deployment, wallet, orclientConfig]); return orclient; } /** * Returns an ORClientReader that is created only if the full * ORClient returned by useOrclient is undefined. Otherwise returns full ORClient * * @param {string} backupProviderURL The provider URL for the ORClientReader * @param {DeploymentSpec} [deployment] The deployment specification * @param {ConnectedWallet} [wallet] The connected wallet * @param {CreateOrclientConfig} [orclientConfig] The configuration for the ORClient * @param {number} [timeout] The timeout in milliseconds for when to create the backup. This is useful to give some time for wallet connections to happen needed for the full orclient * @returns {ORClientReader | undefined} */ export function useOrclientWithBackup(backupProviderURLs, deployment, wallet, orclientConfig, timeout = 3000) { const [orclient, setOrclient] = useState(undefined); const [rpcError, setRpcError] = useState(undefined); const { ready: privyReady } = usePrivy(); const fullOrclient = useOrclient(deployment, wallet, orclientConfig); useEffect(() => { const createBackup = () => { createReader(deployment, backupProviderURLs, orclientConfig).then(orclient => { setOrclient(orclient); setRpcError(undefined); window.orclient = orclient; }, err => { console.error("All RPC URLs failed: ", err); setRpcError(err); }); }; if (fullOrclient === undefined) { const timer = setTimeout(createBackup, timeout); return () => clearTimeout(timer); } else { setOrclient(fullOrclient); setRpcError(undefined); } }, [fullOrclient, deployment, backupProviderURLs, orclientConfig]); return { orclient, rpcError }; }