@pushchain/ui-kit
Version:
Push Chain is a true universal L1 that is 100% EVM compatible. It allows developers to deploy once and make their apps instantly compatible with users from all other L1s (Ethereum, Solana, etc) with zero on-chain code change.
131 lines • 6.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.usePushChainClient = void 0;
const core_1 = require("@pushchain/core");
const progress_hook_types_1 = require("@pushchain/core/src/lib/progress-hook/progress-hook.types");
const usePushWallet_1 = require("./usePushWallet");
const react_1 = require("react");
const txnAuthGuard_1 = require("../helpers/txnAuthGuard");
const constructPushChainSigner_1 = require("./constructPushChainSigner");
const usePushChainClient = (uid) => {
const { universalAccount, handleSignMessage, handleSignAndSendTransaction, handleSignTypedData, handleSignAuthorization, handleExternalWalletConnection, requestPushWalletConnection, config, setProgress, isReadOnly, setIsReadOnly, checkAndShowUpgradeIfNeeded, } = (0, usePushWallet_1.usePushWalletContext)(uid);
const [pushChain, setPushChain] = (0, react_1.useState)(null);
const [error, setError] = (0, react_1.useState)(null);
const MIN_VISIBLE_MS = 50;
const SUCCESS_HIDE_MS = 10000;
const queueRef = (0, react_1.useRef)([]);
const lockRef = (0, react_1.useRef)(false);
const upgradeCheckRef = (0, react_1.useRef)(false);
const minTimerRef = (0, react_1.useRef)(null);
const successTimerRef = (0, react_1.useRef)(null);
const clearProgressTimers = () => {
if (minTimerRef.current) {
clearTimeout(minTimerRef.current);
minTimerRef.current = null;
}
if (successTimerRef.current) {
clearTimeout(successTimerRef.current);
successTimerRef.current = null;
}
};
const showProgress = (p) => {
clearProgressTimers();
setProgress(p);
lockRef.current = true;
minTimerRef.current = setTimeout(() => {
lockRef.current = false;
minTimerRef.current = null;
if (queueRef.current.length > 0) {
const next = queueRef.current.shift();
if (next)
showProgress(next);
}
}, MIN_VISIBLE_MS);
if (p.id === progress_hook_types_1.PROGRESS_HOOK.SEND_TX_199_01 ||
p.id === progress_hook_types_1.PROGRESS_HOOK.SEND_TX_299_01 ||
p.id === progress_hook_types_1.PROGRESS_HOOK.SEND_TX_399_01 ||
p.id === progress_hook_types_1.PROGRESS_HOOK.SEND_TX_999_01 ||
p.id === progress_hook_types_1.PROGRESS_HOOK.UEA_MIG_9901) {
successTimerRef.current = setTimeout(() => setProgress(null), SUCCESS_HIDE_MS);
}
};
(0, react_1.useEffect)(() => {
const initializePushChain = async () => {
if (!universalAccount) {
setPushChain(null);
return;
}
const CHAINS = core_1.PushChain.CONSTANTS.CHAIN;
const isSolana = [
CHAINS.SOLANA_DEVNET,
CHAINS.SOLANA_MAINNET,
CHAINS.SOLANA_TESTNET,
].includes(universalAccount.chain);
try {
const signerSkeleton = (0, constructPushChainSigner_1.constructPushChainSigner)(universalAccount, isSolana, {
signMessage: handleSignMessage,
signAndSendTransaction: handleSignAndSendTransaction,
signTypedData: handleSignTypedData,
signAuthorization: handleSignAuthorization,
});
const universalSigner = await core_1.PushChain.utils.signer.toUniversal(signerSkeleton);
const intializeProps = {
network: config.network,
progressHook: async (progress) => {
if (successTimerRef.current) {
clearTimeout(successTimerRef.current);
}
setProgress(progress);
console.log("Progress:", progress);
if (progress.id === progress_hook_types_1.PROGRESS_HOOK.SEND_TX_199_01 ||
progress.id === progress_hook_types_1.PROGRESS_HOOK.SEND_TX_299_01 ||
progress.id === progress_hook_types_1.PROGRESS_HOOK.SEND_TX_399_01 ||
progress.id === progress_hook_types_1.PROGRESS_HOOK.SEND_TX_999_01 ||
progress.id === progress_hook_types_1.PROGRESS_HOOK.UEA_MIG_9901) {
successTimerRef.current = setTimeout(() => setProgress(null), SUCCESS_HIDE_MS);
}
},
rpcUrls: config.chainConfig?.rpcUrls,
blockExplorers: config.chainConfig?.blockExplorers,
printTraces: config.chainConfig?.printTraces,
};
let pushChainClient;
if (isReadOnly) {
pushChainClient = await core_1.PushChain.initialize(universalAccount, {
network: config.network,
});
}
else {
pushChainClient = await core_1.PushChain.initialize(universalSigner, intializeProps);
}
const guardedPushChainClient = (0, txnAuthGuard_1.createGuardedPushChain)(pushChainClient, handleExternalWalletConnection, requestPushWalletConnection, checkAndShowUpgradeIfNeeded, universalSigner, intializeProps, config?.uid || 'default', () => {
setIsReadOnly(false);
});
setPushChain(guardedPushChainClient);
if (!upgradeCheckRef.current) {
upgradeCheckRef.current = true;
await checkAndShowUpgradeIfNeeded(guardedPushChainClient);
}
setError(null);
}
catch (err) {
console.log('Error occured when initialising Push chain', err);
setError(err instanceof Error ? err : new Error('Failed to initialize PushChain'));
setPushChain(null);
}
};
initializePushChain();
return () => {
clearProgressTimers();
queueRef.current = [];
lockRef.current = false;
};
}, [universalAccount, config, isReadOnly]);
return {
pushChainClient: pushChain,
error,
isInitialized: !!pushChain,
};
};
exports.usePushChainClient = usePushChainClient;
//# sourceMappingURL=usePushChainClient.js.map