UNPKG

@ordao/privy-react-orclient

Version:

"Helpers for using orclient with privy and react"

83 lines (82 loc) 3.74 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.create = create; exports.createReader = createReader; exports.useOrclient = useOrclient; exports.useOrclientWithBackup = useOrclientWithBackup; const react_1 = require("react"); const createOrclient_js_1 = require("@ordao/orclient/createOrclient.js"); const react_auth_1 = require("@privy-io/react-auth"); const libVersions_js_1 = require("./libVersions.js"); function attachVersion(orclient) { orclient['version'] = () => { console.log("versions: ", JSON.stringify(libVersions_js_1.ordaoLibVersions, undefined, 2)); }; return orclient; } async function create(deployment, wallet, orclientCfg) { console.log("Creating full orclient"); const provider = await wallet.getEthereumProvider(); const orclient = await (0, createOrclient_js_1.createOrclient)(deployment, provider, orclientCfg); // Change version function to include privy-react-orclient version attachVersion(orclient); return orclient; } async function createReader(deployment, providerUrls, orclientCfg) { console.log("Creating orclient reader"); const orclient = await (0, createOrclient_js_1.createOrclientReaderWithFallback)(deployment, providerUrls, orclientCfg); attachVersion(orclient); return orclient; } function useOrclient(deployment, wallet, orclientConfig) { const [orclient, setOrclient] = (0, react_1.useState)(undefined); (0, react_1.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} */ function useOrclientWithBackup(backupProviderURLs, deployment, wallet, orclientConfig, timeout = 3000) { const [orclient, setOrclient] = (0, react_1.useState)(undefined); const [rpcError, setRpcError] = (0, react_1.useState)(undefined); const { ready: privyReady } = (0, react_auth_1.usePrivy)(); const fullOrclient = useOrclient(deployment, wallet, orclientConfig); (0, react_1.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 }; }