@ordao/privy-react-orclient
Version:
"Helpers for using orclient with privy and react"
71 lines (70 loc) • 3.16 kB
JavaScript
import { useEffect, useState } from "react";
import { createOrclient, createOrclientReader } 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, providerUrl, orclientCfg) {
console.log("Creating orclient reader");
const orclient = await createOrclientReader(deployment, providerUrl, 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(backupProviderURL, deployment, wallet, orclientConfig, timeout = 3000) {
const [orclient, setOrclient] = useState(undefined);
const { ready: privyReady } = usePrivy();
const fullOrclient = useOrclient(deployment, wallet, orclientConfig);
useEffect(() => {
const createBackup = () => {
createReader(deployment, backupProviderURL, orclientConfig).then(orclient => {
setOrclient(orclient);
window.orclient = orclient;
}, err => console.error(err));
};
if (fullOrclient === undefined) {
const timer = setTimeout(createBackup, timeout);
return () => clearTimeout(timer);
}
else {
setOrclient(fullOrclient);
}
}, [fullOrclient, deployment, backupProviderURL, orclientConfig]);
return orclient;
}