Skip to content

Commit

Permalink
feat(rwa): update the investor count (#2778)
Browse files Browse the repository at this point in the history
  • Loading branch information
sstraatemans authored Jan 8, 2025
1 parent e465bf3 commit 9722418
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -24,6 +25,7 @@ export interface IAsset extends IComplianceProps {
contractName: string;
namespace: string;
supply: number;
investorCount: number;
}

export interface IAssetContext {
Expand Down Expand Up @@ -74,6 +76,7 @@ export const AssetProvider: FC<PropsWithChildren> = ({ children }) => {
getLocalStorageKey(LOCALSTORAGE_ASSETS_SELECTED_KEY) ?? '';
const { paused } = usePaused();
const { data: supply } = useSupply();
const { data: investorCount } = useGetInvestorCount();
const { data: complianceSubscriptionData } = useEventSubscriptionSubscription(
{
variables: {
Expand Down Expand Up @@ -186,6 +189,10 @@ export const AssetProvider: FC<PropsWithChildren> = ({ children }) => {
setAsset((old) => old && { ...old, ...data });
};

useEffect(() => {
setAsset((old) => old && { ...old, investorCount });
}, [investorCount]);

useEffect(() => {
if (asset) return;
const innerAsset = getFullAsset();
Expand Down
33 changes: 33 additions & 0 deletions packages/apps/rwa-demo/src/hooks/getInvestorCount.ts
Original file line number Diff line number Diff line change
@@ -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<number>(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 };
};
24 changes: 0 additions & 24 deletions packages/apps/rwa-demo/src/services/getComplianceRules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ export interface IComplianceProps {
maxSupply: number;
maxBalance: number;
maxInvestors: number;
investorCount: number;
}

export const getMaxBalance = async (): Promise<number> => {
Expand Down Expand Up @@ -52,27 +51,6 @@ export const getMaxSupply = async (): Promise<number> => {
return result.status === 'success' ? data : INFINITE_COMPLIANCE;
};

export const getInvestorCount = async (): Promise<number> => {
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<number> => {
const client = getClient();

Expand All @@ -97,13 +75,11 @@ export const getMaxInvestors = async (): Promise<number> => {
export const getComplianceRules = async (): Promise<IComplianceProps> => {
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,
};
};
24 changes: 24 additions & 0 deletions packages/apps/rwa-demo/src/services/getInvestorCount.ts
Original file line number Diff line number Diff line change
@@ -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<number> => {
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;
};

0 comments on commit 9722418

Please sign in to comment.