diff --git a/src/consts.ts b/src/consts.ts index 26b2afe5..f569771f 100644 --- a/src/consts.ts +++ b/src/consts.ts @@ -12,6 +12,8 @@ export const ERC1155PolicyFactoryAddress = '0x0f31377cBd078C82b34Ab586A3f8d973fE export const JoinPolicyRegistryAddress = '0x3eB2Ec4d2876d22EE103aBc1e0A7CCEefD117CD3' +export const StreamRegistryAddress = '0x0D483E10612F327FC11965Fc82E90dC19b141641' + export enum PermissionType { Edit = 0, Delete, diff --git a/src/hooks/useListenForInvitesEffect.ts b/src/hooks/useListenForInvitesEffect.ts index 40a6f535..46a857f9 100644 --- a/src/hooks/useListenForInvitesEffect.ts +++ b/src/hooks/useListenForInvitesEffect.ts @@ -3,13 +3,13 @@ import { useEffect, useRef } from 'react' import { Address, OptionalAddress, Prefix } from '$/types' import { RoomId } from '$/features/room/types' import isSameAddress from '$/utils/isSameAddress' -import useStreamRegistry from './useStreamRegistry' +import { getStreamRegistry } from '$/utils' export default function useListenForInvitesEffect( address: OptionalAddress, onInvite: (roomId: RoomId, address: Address) => void ) { - const registry = useStreamRegistry() + const registry = getStreamRegistry() const onInviteRef = useRef(onInvite) diff --git a/src/hooks/useStreamRegistry.ts b/src/hooks/useStreamRegistry.ts deleted file mode 100644 index c6fa1609..00000000 --- a/src/hooks/useStreamRegistry.ts +++ /dev/null @@ -1,67 +0,0 @@ -import { Contract, providers } from 'ethers' -import { useEffect, useState } from 'react' -import { abi } from '../contracts/StreamRegistryV3.sol/StreamRegistryV3.json' -import { StreamRegistryV3 } from '../contracts/StreamRegistryV3.sol/StreamRegistryV3' - -const StreamRegistryAddress = '0x0D483E10612F327FC11965Fc82E90dC19b141641' - -export default function useStreamRegistry() { - const [registry, setRegistry] = useState() - - useEffect(() => { - let mounted = true - - let provider: providers.WebSocketProvider | null = null - - let interval: number | null = null - - async function teardown() { - if (provider) { - try { - await provider.destroy() - } catch (e) { - // Shush! - } - provider = null - } - - if (interval) { - window.clearInterval(interval) - interval = null - } - } - - function fn() { - teardown() - - provider = new providers.WebSocketProvider('wss://ws-matic-mainnet.chainstacklabs.com') - - // Keep the provider alive by… replacing it when it becomes waste. - interval = window.setInterval(() => { - switch (provider?.websocket.readyState) { - case WebSocket.OPEN: - case WebSocket.CONNECTING: - return - default: - break - } - - // We need a new instance. - if (mounted) { - fn() - } - }, 2500) - - setRegistry(new Contract(StreamRegistryAddress, abi, provider) as StreamRegistryV3) - } - - fn() - - return () => { - mounted = false - teardown() - } - }, []) - - return registry -} diff --git a/src/utils/index.tsx b/src/utils/index.tsx new file mode 100644 index 00000000..8f35d7bb --- /dev/null +++ b/src/utils/index.tsx @@ -0,0 +1,18 @@ +import { JSON_RPC_URL, StreamRegistryAddress } from '$/consts' +import { Contract, providers } from 'ethers' +import { StreamRegistryV3 } from '../contracts/StreamRegistryV3.sol/StreamRegistryV3' +import { abi } from '../contracts/StreamRegistryV3.sol/StreamRegistryV3.json' + +let streamRegistry: undefined | StreamRegistryV3 + +export function getStreamRegistry() { + if (!streamRegistry) { + streamRegistry = new Contract( + StreamRegistryAddress, + abi, + new providers.JsonRpcProvider(JSON_RPC_URL) + ) as StreamRegistryV3 + } + + return streamRegistry +}