From f21bb86fb342aa3ffb7f4603a74d0d816070a3f2 Mon Sep 17 00:00:00 2001 From: Samuele Agostinelli Date: Tue, 11 Jun 2024 11:59:43 -0400 Subject: [PATCH] check whitelist status hook (#87) --- packages/checkout/src/hooks/index.ts | 1 + .../src/hooks/useCheckoutWhitelistStatus.ts | 20 ++++++ packages/checkout/src/index.ts | 1 + packages/checkout/src/utils/index.ts | 1 + packages/checkout/src/utils/sardine.ts | 67 +++++++++++++++++++ 5 files changed, 90 insertions(+) create mode 100644 packages/checkout/src/hooks/useCheckoutWhitelistStatus.ts create mode 100644 packages/checkout/src/utils/sardine.ts diff --git a/packages/checkout/src/hooks/index.ts b/packages/checkout/src/hooks/index.ts index fdc3b30d..4396c28c 100644 --- a/packages/checkout/src/hooks/index.ts +++ b/packages/checkout/src/hooks/index.ts @@ -2,3 +2,4 @@ export * from './useAddFundsModal' export * from './useCheckoutModal' export * from './useNavigation' export * from './useModalTheme' +export * from './useCheckoutWhitelistStatus' diff --git a/packages/checkout/src/hooks/useCheckoutWhitelistStatus.ts b/packages/checkout/src/hooks/useCheckoutWhitelistStatus.ts new file mode 100644 index 00000000..941939af --- /dev/null +++ b/packages/checkout/src/hooks/useCheckoutWhitelistStatus.ts @@ -0,0 +1,20 @@ +import { useProjectAccessKey } from '@0xsequence/kit' +import { useQuery } from '@tanstack/react-query' + +import { checkSardineWhitelistStatus, CheckSardineWhitelistStatusArgs } from '../utils' + + +export const useCheckoutWhitelistStatus = (args: CheckSardineWhitelistStatusArgs) => { + const projectAccessKey = useProjectAccessKey() + + return useQuery({ + queryKey: ['useCheckoutWhitelistStatus', args], + queryFn: async () => { + const res = await checkSardineWhitelistStatus(args, projectAccessKey) + + return res + }, + retry: false, + staleTime: 1800 * 1000, + }) +} diff --git a/packages/checkout/src/index.ts b/packages/checkout/src/index.ts index af76a557..0a3a245f 100644 --- a/packages/checkout/src/index.ts +++ b/packages/checkout/src/index.ts @@ -4,6 +4,7 @@ export { KitCheckoutProvider } from './shared/components/KitCheckoutProvider' // Hooks export { useCheckoutModal } from './hooks/useCheckoutModal' export { useAddFundsModal } from './hooks/useAddFundsModal' +export { useCheckoutWhitelistStatus } from './hooks/useCheckoutWhitelistStatus' export { type CheckoutSettings } from './contexts/CheckoutModal' export { type AddFundsSettings } from './contexts/AddFundsModal' diff --git a/packages/checkout/src/utils/index.ts b/packages/checkout/src/utils/index.ts index c897b9cc..975e9f39 100644 --- a/packages/checkout/src/utils/index.ts +++ b/packages/checkout/src/utils/index.ts @@ -1,2 +1,3 @@ export * from './helpers' export * from './networks' +export * from './sardine' \ No newline at end of file diff --git a/packages/checkout/src/utils/sardine.ts b/packages/checkout/src/utils/sardine.ts new file mode 100644 index 00000000..c9fda4cc --- /dev/null +++ b/packages/checkout/src/utils/sardine.ts @@ -0,0 +1,67 @@ +import { ChainId, networks } from '@0xsequence/network' +import { constants } from 'ethers' + +export interface CheckSardineWhitelistStatusArgs { + isDev: boolean + chainId: number + marketplaceAddress: string +} + +export const checkSardineWhitelistStatus = async ({ + isDev, + chainId, + marketplaceAddress, +}: CheckSardineWhitelistStatusArgs, +projectAccessKey: string) => { + const referenceId = `sequence-kit-sardine-whitelist-check` + + const accessKey = isDev ? '17xhjK4yjRf1fr0am8kgKfICAAAAAAAAA' : projectAccessKey + + const url = isDev + ? 'https://dev-api.sequence.app/rpc/API/GetSardineNFTCheckoutToken' + : 'https://api.sequence.app/rpc/API/GetSardineNFTCheckoutToken' + + const res = await fetch(url, { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + 'X-Access-Key': `${accessKey || ''}` + }, + body: JSON.stringify({ + params: { + referenceId, + expiresIn: 3600, + paymentMethodTypeConfig: { + enabled: ['us_debit', 'us_credit', 'international_debit', 'international_credit', 'ach'], + default: 'us_debit' + }, + name: 'whitelist-check', + imageUrl: 'https://www.sequence.market/images/placeholder.png', + network: networks[chainId as ChainId].name, + recipientAddress: constants.AddressZero, + contractAddress: marketplaceAddress, + platform: "calldata_execution", + executionType: 'smart_contract', + blockchainNftId: '42', + quantity: 1, + decimals: 0, + tokenAmount: '1000000', + tokenAddress: '0xa0b86991c6218b36c1d19d4a2e9eb0ce3606eb48', + tokenSymbol: 'USDC', + tokenDecimals: 6, + callData: '0x1', + } + }) + }) + + const resJson = await res.json() + + if ( + typeof resJson?.cause === 'string' && + resJson.cause.includes('It must me allow listed') + ) { + return false + } + + return true +} \ No newline at end of file