diff --git a/packages/apps/rwa-demo/src/components/AssetProvider/AssetProvider.tsx b/packages/apps/rwa-demo/src/components/AssetProvider/AssetProvider.tsx index 7a46104090..dbf76148ee 100644 --- a/packages/apps/rwa-demo/src/components/AssetProvider/AssetProvider.tsx +++ b/packages/apps/rwa-demo/src/components/AssetProvider/AssetProvider.tsx @@ -7,6 +7,7 @@ import { LOCALSTORAGE_ASSETS_KEY, LOCALSTORAGE_ASSETS_SELECTED_KEY, } from '@/constants'; +import { useGetInvestorCount } from '@/hooks/getInvestorCount'; import { usePaused } from '@/hooks/paused'; import { useSupply } from '@/hooks/supply'; import type { IComplianceProps } from '@/services/getComplianceRules'; @@ -24,6 +25,7 @@ export interface IAsset extends IComplianceProps { contractName: string; namespace: string; supply: number; + investorCount: number; } export interface IAssetContext { @@ -74,6 +76,7 @@ export const AssetProvider: FC = ({ children }) => { getLocalStorageKey(LOCALSTORAGE_ASSETS_SELECTED_KEY) ?? ''; const { paused } = usePaused(); const { data: supply } = useSupply(); + const { data: investorCount } = useGetInvestorCount(); const { data: complianceSubscriptionData } = useEventSubscriptionSubscription( { variables: { @@ -186,6 +189,10 @@ export const AssetProvider: FC = ({ children }) => { setAsset((old) => old && { ...old, ...data }); }; + useEffect(() => { + setAsset((old) => old && { ...old, investorCount }); + }, [investorCount]); + useEffect(() => { if (asset) return; const innerAsset = getFullAsset(); diff --git a/packages/apps/rwa-demo/src/hooks/getInvestorCount.ts b/packages/apps/rwa-demo/src/hooks/getInvestorCount.ts new file mode 100644 index 0000000000..19ecf1391b --- /dev/null +++ b/packages/apps/rwa-demo/src/hooks/getInvestorCount.ts @@ -0,0 +1,33 @@ +import { useEventSubscriptionSubscription } from '@/__generated__/sdk'; +import { getInvestorCount } from '@/services/getInvestorCount'; +import { getAsset } from '@/utils/getAsset'; +import { useEffect, useState } from 'react'; + +export const useGetInvestorCount = () => { + const [innerData, setInnerData] = useState(0); + + const { data: subscriptionData } = useEventSubscriptionSubscription({ + variables: { + qualifiedName: `${getAsset()}.RECONCILE`, + }, + }); + + const initInnerData = async () => { + const data = await getInvestorCount(); + setInnerData(data); + }; + + useEffect(() => { + // eslint-disable-next-line @typescript-eslint/no-floating-promises + initInnerData(); + }, []); + + useEffect(() => { + if (!subscriptionData?.events?.length) return; + + // eslint-disable-next-line @typescript-eslint/no-floating-promises + initInnerData(); + }, [subscriptionData]); + + return { data: innerData }; +}; diff --git a/packages/apps/rwa-demo/src/services/getComplianceRules.ts b/packages/apps/rwa-demo/src/services/getComplianceRules.ts index 960a6e3b13..bf69b1372a 100644 --- a/packages/apps/rwa-demo/src/services/getComplianceRules.ts +++ b/packages/apps/rwa-demo/src/services/getComplianceRules.ts @@ -7,7 +7,6 @@ export interface IComplianceProps { maxSupply: number; maxBalance: number; maxInvestors: number; - investorCount: number; } export const getMaxBalance = async (): Promise => { @@ -52,27 +51,6 @@ export const getMaxSupply = async (): Promise => { return result.status === 'success' ? data : INFINITE_COMPLIANCE; }; -export const getInvestorCount = async (): Promise => { - const client = getClient(); - - const transaction = Pact.builder - .execution(`(${getAsset()}.investor-count)`) - .setMeta({ - chainId: getNetwork().chainId, - }) - .setNetworkId(getNetwork().networkId) - .createTransaction(); - - const { result } = await client.local(transaction, { - preflight: false, - signatureVerification: false, - }); - - const data = (result as any).data as any; - - return result.status === 'success' ? data : INFINITE_COMPLIANCE; -}; - export const getMaxInvestors = async (): Promise => { const client = getClient(); @@ -97,13 +75,11 @@ export const getMaxInvestors = async (): Promise => { export const getComplianceRules = async (): Promise => { const maxBalanceResult = await getMaxBalance(); const maxSupplyResult = await getMaxSupply(); - const investorCount = await getInvestorCount(); const maxInvestors = await getMaxInvestors(); return { maxBalance: maxBalanceResult, maxSupply: maxSupplyResult, maxInvestors: maxInvestors, - investorCount: investorCount, }; }; diff --git a/packages/apps/rwa-demo/src/services/getInvestorCount.ts b/packages/apps/rwa-demo/src/services/getInvestorCount.ts new file mode 100644 index 0000000000..f56d8f218b --- /dev/null +++ b/packages/apps/rwa-demo/src/services/getInvestorCount.ts @@ -0,0 +1,24 @@ +import { getClient, getNetwork } from '@/utils/client'; +import { getAsset } from '@/utils/getAsset'; +import { Pact } from '@kadena/client'; + +export const getInvestorCount = async (): Promise => { + const client = getClient(); + + const transaction = Pact.builder + .execution(`(${getAsset()}.investor-count)`) + .setMeta({ + chainId: getNetwork().chainId, + }) + .setNetworkId(getNetwork().networkId) + .createTransaction(); + + const { result } = await client.local(transaction, { + preflight: false, + signatureVerification: false, + }); + + const data = (result as any).data as any; + + return result.status === 'success' ? data : 0; +};