From 1b3b60934569dd14892d93a3be3a2d287ed3a877 Mon Sep 17 00:00:00 2001 From: Corban Riley Date: Mon, 29 Apr 2024 13:31:52 -0400 Subject: [PATCH] Adding hooks for getting memoized sequence clients with included projectAccessKey --- packages/kit/src/hooks/index.ts | 4 ++++ packages/kit/src/hooks/useAPIClient.ts | 13 ++++++++++++ packages/kit/src/hooks/useIndexerClient.ts | 20 +++++++++++++++++++ packages/kit/src/hooks/useMetadataClient.ts | 13 ++++++++++++ .../src/hooks/useWaasConfirmationHandler.ts | 1 - 5 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 packages/kit/src/hooks/useAPIClient.ts create mode 100644 packages/kit/src/hooks/useIndexerClient.ts create mode 100644 packages/kit/src/hooks/useMetadataClient.ts diff --git a/packages/kit/src/hooks/index.ts b/packages/kit/src/hooks/index.ts index e43950d4..57a0ba0f 100644 --- a/packages/kit/src/hooks/index.ts +++ b/packages/kit/src/hooks/index.ts @@ -2,3 +2,7 @@ export * from './useOpenConnectModal' export * from './useTheme' export * from './useWalletSettings' export * from './useWaasFeeOptions' +export * from './useProjectAccessKey' +export * from './useAPIClient' +export * from './useMetadataClient' +export * from './useIndexerClient' diff --git a/packages/kit/src/hooks/useAPIClient.ts b/packages/kit/src/hooks/useAPIClient.ts new file mode 100644 index 00000000..c7bfc53d --- /dev/null +++ b/packages/kit/src/hooks/useAPIClient.ts @@ -0,0 +1,13 @@ +import { useMemo } from 'react' +import { useProjectAccessKey } from './useProjectAccessKey' +import { SequenceAPIClient } from '@0xsequence/api' + +export const useAPIClient = () => { + const projectAccessKey = useProjectAccessKey() + + const apiClient = useMemo(() => { + return new SequenceAPIClient('https://api.sequence.app', projectAccessKey) + }, [projectAccessKey]) + + return apiClient +} diff --git a/packages/kit/src/hooks/useIndexerClient.ts b/packages/kit/src/hooks/useIndexerClient.ts new file mode 100644 index 00000000..b9ea405c --- /dev/null +++ b/packages/kit/src/hooks/useIndexerClient.ts @@ -0,0 +1,20 @@ +import { useMemo } from 'react' +import { useProjectAccessKey } from './useProjectAccessKey' +import { SequenceIndexer } from '@0xsequence/indexer' +import { ChainId, indexerURL, networks } from '@0xsequence/network' + +export const useIndexerClient = (chainId: ChainId) => { + const projectAccessKey = useProjectAccessKey() + + const indexerClients = useMemo(() => { + return new Map() + }, [projectAccessKey]) + + const network = networks[chainId] + + if (!indexerClients.has(chainId)) { + indexerClients.set(chainId, new SequenceIndexer(indexerURL(network.name), projectAccessKey)) + } + + return indexerClients.get(chainId) +} diff --git a/packages/kit/src/hooks/useMetadataClient.ts b/packages/kit/src/hooks/useMetadataClient.ts new file mode 100644 index 00000000..830713a0 --- /dev/null +++ b/packages/kit/src/hooks/useMetadataClient.ts @@ -0,0 +1,13 @@ +import { SequenceMetadata } from '@0xsequence/metadata' +import { useProjectAccessKey } from './useProjectAccessKey' +import { useMemo } from 'react' + +export const useMetadataClient = () => { + const projectAccessKey = useProjectAccessKey() + + const metadataClient = useMemo(() => { + return new SequenceMetadata('https://metadata.sequence.app', projectAccessKey) + }, [projectAccessKey]) + + return metadataClient +} diff --git a/packages/kit/src/hooks/useWaasConfirmationHandler.ts b/packages/kit/src/hooks/useWaasConfirmationHandler.ts index 57d3711d..b0181d56 100644 --- a/packages/kit/src/hooks/useWaasConfirmationHandler.ts +++ b/packages/kit/src/hooks/useWaasConfirmationHandler.ts @@ -1,5 +1,4 @@ import { commons } from '@0xsequence/core' -import { ethers } from 'ethers' import { useState, useEffect } from 'react' import { Deferred } from '../utils/deferred'