Skip to content

Commit

Permalink
disable permissions when agent roles are being changed
Browse files Browse the repository at this point in the history
  • Loading branch information
sstraatemans committed Dec 20, 2024
1 parent fc6bcb6 commit e206c4a
Show file tree
Hide file tree
Showing 12 changed files with 114 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -76,11 +76,12 @@ export const AccountProvider: FC<PropsWithChildren> = ({ 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) => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<ITransactionsContext>({
Expand All @@ -70,6 +77,7 @@ export const TransactionsContext = createContext<ITransactionsContext>({
getTransactions: () => [],
setTxsButtonRef: () => {},
setTxsAnimationRef: () => {},
isActiveAccountChangeTx: false,
});

const interpretMessage = (str: string, data?: ITransaction): string => {
Expand Down Expand Up @@ -132,7 +140,6 @@ export const TransactionsProvider: FC<PropsWithChildren> = ({ children }) => {
});
})
.finally(() => {
console.log(data);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
store.removeTransaction(data);
});
Expand Down Expand Up @@ -208,6 +215,12 @@ export const TransactionsProvider: FC<PropsWithChildren> = ({ 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);
};
Expand All @@ -225,6 +238,7 @@ export const TransactionsProvider: FC<PropsWithChildren> = ({ children }) => {
txsButtonRef,
setTxsAnimationRef,
txsAnimationRef,
isActiveAccountChangeTx,
}}
>
{children}
Expand Down
14 changes: 11 additions & 3 deletions packages/apps/rwa-demo/src/hooks/addInvestor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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 };
};
8 changes: 5 additions & 3 deletions packages/apps/rwa-demo/src/hooks/deleteInvestor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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('');
Expand Down Expand Up @@ -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 };
};
18 changes: 15 additions & 3 deletions packages/apps/rwa-demo/src/hooks/distributeTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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 };
};
20 changes: 16 additions & 4 deletions packages/apps/rwa-demo/src/hooks/editAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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({
Expand All @@ -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 };
};
14 changes: 11 additions & 3 deletions packages/apps/rwa-demo/src/hooks/freezeInvestor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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 };
};
1 change: 0 additions & 1 deletion packages/apps/rwa-demo/src/hooks/getInvestors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
11 changes: 8 additions & 3 deletions packages/apps/rwa-demo/src/hooks/removeAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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 };
};
14 changes: 11 additions & 3 deletions packages/apps/rwa-demo/src/hooks/setCompliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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 };
};
18 changes: 15 additions & 3 deletions packages/apps/rwa-demo/src/hooks/togglePartiallyFreezeTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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 };
};
6 changes: 3 additions & 3 deletions packages/apps/rwa-demo/src/hooks/togglePause.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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 };
};

0 comments on commit e206c4a

Please sign in to comment.