diff --git a/packages/apps/rwa-demo/src/components/AccountProvider/AccountProvider.tsx b/packages/apps/rwa-demo/src/components/AccountProvider/AccountProvider.tsx index 01a79ceac8..edd2ab04d6 100644 --- a/packages/apps/rwa-demo/src/components/AccountProvider/AccountProvider.tsx +++ b/packages/apps/rwa-demo/src/components/AccountProvider/AccountProvider.tsx @@ -76,11 +76,12 @@ export const AccountProvider: FC = ({ children }) => { const [isAgentState, setIsAgentState] = useState(false); const [isInvestorState, setIsInvestorState] = useState(false); const [isFrozenState, setIsFrozenState] = useState(false); - const accountRoles = useGetAgentRoles({ agent: account?.address }); + const { ...accountRoles } = useGetAgentRoles({ + agent: account?.address, + }); const { data: balance } = useGetInvestorBalance({ investorAccount: account?.address, }); - const router = useRouter(); const checkIsAgent = async (account: IWalletAccount) => { diff --git a/packages/apps/rwa-demo/src/components/TransactionsProvider/TransactionsProvider.tsx b/packages/apps/rwa-demo/src/components/TransactionsProvider/TransactionsProvider.tsx index ada06a0227..908c159734 100644 --- a/packages/apps/rwa-demo/src/components/TransactionsProvider/TransactionsProvider.tsx +++ b/packages/apps/rwa-demo/src/components/TransactionsProvider/TransactionsProvider.tsx @@ -7,7 +7,13 @@ import { store } from '@/utils/store'; import type { ICommandResult } from '@kadena/client'; import { useNotifications } from '@kadena/kode-ui/patterns'; import type { FC, PropsWithChildren } from 'react'; -import { createContext, useCallback, useEffect, useState } from 'react'; +import { + createContext, + useCallback, + useEffect, + useMemo, + useState, +} from 'react'; export interface ITxType { name: keyof typeof TXTYPES; @@ -60,6 +66,7 @@ export interface ITransactionsContext { setTxsButtonRef: (value: HTMLButtonElement) => void; txsAnimationRef?: HTMLDivElement | null; setTxsAnimationRef: (value: HTMLDivElement) => void; + isActiveAccountChangeTx: boolean; //checks if the agentroles for this user are being changed. if so, stop all permissions until the tx is resolved } export const TransactionsContext = createContext({ @@ -70,6 +77,7 @@ export const TransactionsContext = createContext({ getTransactions: () => [], setTxsButtonRef: () => {}, setTxsAnimationRef: () => {}, + isActiveAccountChangeTx: false, }); const interpretMessage = (str: string, data?: ITransaction): string => { @@ -132,7 +140,6 @@ export const TransactionsProvider: FC = ({ children }) => { }); }) .finally(() => { - console.log(data); // eslint-disable-next-line @typescript-eslint/no-floating-promises store.removeTransaction(data); }); @@ -208,6 +215,12 @@ export const TransactionsProvider: FC = ({ children }) => { }); }, [transactions.length, account]); + const isActiveAccountChangeTx: boolean = useMemo(() => { + if (!account?.address) return false; + const txs = getTransactions(TXTYPES.ADDAGENT); + return !!txs.find((tx) => tx.accounts.indexOf(account.address) >= 0); + }, [getTransactions(TXTYPES.ADDAGENT), account?.address]); + const setTxsButtonRef = (ref: HTMLButtonElement) => { setTxsButtonRefData(ref); }; @@ -225,6 +238,7 @@ export const TransactionsProvider: FC = ({ children }) => { txsButtonRef, setTxsAnimationRef, txsAnimationRef, + isActiveAccountChangeTx, }} > {children} diff --git a/packages/apps/rwa-demo/src/hooks/addInvestor.ts b/packages/apps/rwa-demo/src/hooks/addInvestor.ts index 704c3c11db..15f4f5c82d 100644 --- a/packages/apps/rwa-demo/src/hooks/addInvestor.ts +++ b/packages/apps/rwa-demo/src/hooks/addInvestor.ts @@ -22,7 +22,7 @@ export const useAddInvestor = ({ const { frozen } = useFreeze({ investorAccount }); const { paused } = useAsset(); const { account, sign, accountRoles, isMounted } = useAccount(); - const { addTransaction } = useTransactions(); + const { addTransaction, isActiveAccountChangeTx } = useTransactions(); const { addNotification } = useNotifications(); const [isAllowed, setIsAllowed] = useState(false); @@ -63,9 +63,17 @@ export const useAddInvestor = ({ setIsAllowed( ((!!investorAccount && !frozen) || frozen) && !paused && - accountRoles.isWhitelistManager(), + accountRoles.isWhitelistManager() && + !isActiveAccountChangeTx, ); - }, [frozen, paused, isMounted, investorAccount, accountRoles]); + }, [ + frozen, + paused, + isMounted, + investorAccount, + accountRoles, + isActiveAccountChangeTx, + ]); return { submit, isAllowed }; }; diff --git a/packages/apps/rwa-demo/src/hooks/deleteInvestor.ts b/packages/apps/rwa-demo/src/hooks/deleteInvestor.ts index 8617dd7602..b6ce8e94b7 100644 --- a/packages/apps/rwa-demo/src/hooks/deleteInvestor.ts +++ b/packages/apps/rwa-demo/src/hooks/deleteInvestor.ts @@ -19,7 +19,7 @@ export const useDeleteInvestor = ({ }) => { const { account, sign, accountRoles, isMounted, balance } = useAccount(); const { paused } = useAsset(); - const { addTransaction } = useTransactions(); + const { addTransaction, isActiveAccountChangeTx } = useTransactions(); const { addNotification } = useNotifications(); const [isAllowed, setIsAllowed] = useState(false); const [notAllowedReason, setNotAllowedReason] = useState(''); @@ -70,8 +70,10 @@ export const useDeleteInvestor = ({ return; } - setIsAllowed(!paused && accountRoles.isWhitelistManager()); - }, [paused, account?.address, isMounted, balance]); + setIsAllowed( + !paused && accountRoles.isWhitelistManager() && !isActiveAccountChangeTx, + ); + }, [paused, account?.address, isMounted, balance, isActiveAccountChangeTx]); return { submit, isAllowed, notAllowedReason }; }; diff --git a/packages/apps/rwa-demo/src/hooks/distributeTokens.ts b/packages/apps/rwa-demo/src/hooks/distributeTokens.ts index fb2c76068b..0b0019ce9f 100644 --- a/packages/apps/rwa-demo/src/hooks/distributeTokens.ts +++ b/packages/apps/rwa-demo/src/hooks/distributeTokens.ts @@ -21,7 +21,7 @@ export const useDistributeTokens = ({ const { paused } = useAsset(); const { account, sign, accountRoles, isMounted } = useAccount(); - const { addTransaction } = useTransactions(); + const { addTransaction, isActiveAccountChangeTx } = useTransactions(); const { addNotification } = useNotifications(); const [isAllowed, setIsAllowed] = useState(false); @@ -51,8 +51,20 @@ export const useDistributeTokens = ({ useEffect(() => { if (!isMounted) return; - setIsAllowed(!frozen && !paused && accountRoles.isSupplyModifier()); - }, [frozen, paused, account?.address, isMounted, accountRoles]); + setIsAllowed( + !frozen && + !paused && + accountRoles.isSupplyModifier() && + !isActiveAccountChangeTx, + ); + }, [ + frozen, + paused, + account?.address, + isMounted, + accountRoles, + isActiveAccountChangeTx, + ]); return { submit, isAllowed }; }; diff --git a/packages/apps/rwa-demo/src/hooks/editAgent.ts b/packages/apps/rwa-demo/src/hooks/editAgent.ts index c72de4d2a6..b3587e4101 100644 --- a/packages/apps/rwa-demo/src/hooks/editAgent.ts +++ b/packages/apps/rwa-demo/src/hooks/editAgent.ts @@ -17,7 +17,7 @@ import { useTransactions } from './transactions'; export const useEditAgent = () => { const { account, sign, isMounted, accountRoles, isOwner } = useAccount(); const { paused } = useAsset(); - const { addTransaction } = useTransactions(); + const { addTransaction, isActiveAccountChangeTx } = useTransactions(); const { addNotification } = useNotifications(); const [isAllowed, setIsAllowed] = useState(false); @@ -38,7 +38,7 @@ export const useEditAgent = () => { return addTransaction({ ...res, type: TXTYPES.ADDAGENT, - accounts: [account?.address!, data.accountName], + accounts: [data.accountName], }); } catch (e: any) { addNotification({ @@ -53,8 +53,20 @@ export const useEditAgent = () => { useEffect(() => { if (!isMounted) return; - setIsAllowed(!paused && (accountRoles.isAgentAdmin() || isOwner)); - }, [paused, account?.address, isMounted, isOwner, accountRoles]); + + setIsAllowed( + !paused && + !isActiveAccountChangeTx && + (accountRoles.isAgentAdmin() || isOwner), + ); + }, [ + paused, + account?.address, + isMounted, + isOwner, + accountRoles, + isActiveAccountChangeTx, + ]); return { submit, isAllowed }; }; diff --git a/packages/apps/rwa-demo/src/hooks/freezeInvestor.ts b/packages/apps/rwa-demo/src/hooks/freezeInvestor.ts index c7e6a833d6..248873a2bb 100644 --- a/packages/apps/rwa-demo/src/hooks/freezeInvestor.ts +++ b/packages/apps/rwa-demo/src/hooks/freezeInvestor.ts @@ -15,7 +15,7 @@ import { useTransactions } from './transactions'; export const useFreezeInvestor = () => { const { account, sign, isMounted, accountRoles } = useAccount(); const { paused } = useAsset(); - const { addTransaction } = useTransactions(); + const { addTransaction, isActiveAccountChangeTx } = useTransactions(); const { addNotification } = useNotifications(); const [isAllowed, setIsAllowed] = useState(false); @@ -46,8 +46,16 @@ export const useFreezeInvestor = () => { useEffect(() => { if (!isMounted) return; - setIsAllowed(!paused && accountRoles.isFreezer()); - }, [paused, account?.address, isMounted, accountRoles]); + setIsAllowed( + !paused && accountRoles.isFreezer() && !isActiveAccountChangeTx, + ); + }, [ + paused, + account?.address, + isMounted, + accountRoles, + isActiveAccountChangeTx, + ]); return { submit, isAllowed }; }; diff --git a/packages/apps/rwa-demo/src/hooks/getInvestors.ts b/packages/apps/rwa-demo/src/hooks/getInvestors.ts index 78b3b90a69..1b2a83b752 100644 --- a/packages/apps/rwa-demo/src/hooks/getInvestors.ts +++ b/packages/apps/rwa-demo/src/hooks/getInvestors.ts @@ -110,7 +110,6 @@ export const useGetInvestors = () => { const investorsSubscriptionAdded: IRecord[] = addedSubscriptionData?.events?.map((edge: any) => { const params = JSON.parse(edge.parameters); - console.log('added', { params }); return { isRemoved: false, accountName: params[0], diff --git a/packages/apps/rwa-demo/src/hooks/removeAgent.ts b/packages/apps/rwa-demo/src/hooks/removeAgent.ts index 16273a3e31..8456724852 100644 --- a/packages/apps/rwa-demo/src/hooks/removeAgent.ts +++ b/packages/apps/rwa-demo/src/hooks/removeAgent.ts @@ -15,7 +15,7 @@ import { useTransactions } from './transactions'; export const useRemoveAgent = () => { const { account, sign, isMounted, accountRoles, isOwner } = useAccount(); const { paused } = useAsset(); - const { addTransaction } = useTransactions(); + const { addTransaction, isActiveAccountChangeTx } = useTransactions(); const { addNotification } = useNotifications(); const [isAllowed, setIsAllowed] = useState(false); @@ -47,8 +47,13 @@ export const useRemoveAgent = () => { useEffect(() => { if (!isMounted) return; - setIsAllowed(!paused && (accountRoles.isAgentAdmin() || isOwner)); - }, [paused, account?.address, isMounted, isOwner]); + + setIsAllowed( + !paused && + !isActiveAccountChangeTx && + (accountRoles.isAgentAdmin() || isOwner), + ); + }, [paused, account?.address, isMounted, isOwner, isActiveAccountChangeTx]); return { submit, isAllowed }; }; diff --git a/packages/apps/rwa-demo/src/hooks/setCompliance.ts b/packages/apps/rwa-demo/src/hooks/setCompliance.ts index 55bec603e9..0ca6ff0b21 100644 --- a/packages/apps/rwa-demo/src/hooks/setCompliance.ts +++ b/packages/apps/rwa-demo/src/hooks/setCompliance.ts @@ -14,7 +14,7 @@ import { useTransactions } from './transactions'; export const useSetCompliance = () => { const { account, sign, isMounted, accountRoles } = useAccount(); const { paused } = useAsset(); - const { addTransaction } = useTransactions(); + const { addTransaction, isActiveAccountChangeTx } = useTransactions(); const { addNotification } = useNotifications(); const [isAllowed, setIsAllowed] = useState(false); @@ -44,8 +44,16 @@ export const useSetCompliance = () => { useEffect(() => { if (!isMounted) return; - setIsAllowed(!paused && accountRoles.isComplianceManager()); - }, [paused, account?.address, isMounted, accountRoles]); + setIsAllowed( + !paused && accountRoles.isComplianceManager() && !isActiveAccountChangeTx, + ); + }, [ + paused, + account?.address, + isMounted, + accountRoles, + isActiveAccountChangeTx, + ]); return { submit, isAllowed }; }; diff --git a/packages/apps/rwa-demo/src/hooks/togglePartiallyFreezeTokens.ts b/packages/apps/rwa-demo/src/hooks/togglePartiallyFreezeTokens.ts index 97caedaacb..bebd6c9df9 100644 --- a/packages/apps/rwa-demo/src/hooks/togglePartiallyFreezeTokens.ts +++ b/packages/apps/rwa-demo/src/hooks/togglePartiallyFreezeTokens.ts @@ -20,7 +20,7 @@ export const useTogglePartiallyFreezeTokens = ({ const { frozen } = useFreeze({ investorAccount }); const { paused } = useAsset(); const { account, sign, accountRoles, isMounted } = useAccount(); - const { addTransaction } = useTransactions(); + const { addTransaction, isActiveAccountChangeTx } = useTransactions(); const { addNotification } = useNotifications(); const [isAllowed, setIsAllowed] = useState(false); @@ -50,8 +50,20 @@ export const useTogglePartiallyFreezeTokens = ({ useEffect(() => { if (!isMounted) return; - setIsAllowed(!frozen && !paused && accountRoles.isFreezer()); - }, [frozen, paused, account?.address, isMounted, accountRoles]); + setIsAllowed( + !frozen && + !paused && + accountRoles.isFreezer() && + !isActiveAccountChangeTx, + ); + }, [ + frozen, + paused, + account?.address, + isMounted, + accountRoles, + isActiveAccountChangeTx, + ]); return { submit, isAllowed }; }; diff --git a/packages/apps/rwa-demo/src/hooks/togglePause.ts b/packages/apps/rwa-demo/src/hooks/togglePause.ts index 99fff9a8cb..e4b32f4e0b 100644 --- a/packages/apps/rwa-demo/src/hooks/togglePause.ts +++ b/packages/apps/rwa-demo/src/hooks/togglePause.ts @@ -13,7 +13,7 @@ import { useTransactions } from './transactions'; export const useTogglePause = () => { const { account, sign, isMounted, accountRoles } = useAccount(); - const { addTransaction } = useTransactions(); + const { addTransaction, isActiveAccountChangeTx } = useTransactions(); const { addNotification } = useNotifications(); const [isAllowed, setIsAllowed] = useState(false); @@ -44,8 +44,8 @@ export const useTogglePause = () => { useEffect(() => { if (!isMounted) return; - setIsAllowed(accountRoles.isFreezer()); - }, [account?.address, isMounted, accountRoles]); + setIsAllowed(accountRoles.isFreezer() && !isActiveAccountChangeTx); + }, [account?.address, isMounted, accountRoles, isActiveAccountChangeTx]); return { submit, isAllowed }; };