Skip to content

Commit

Permalink
fix the permissions for forced transfer
Browse files Browse the repository at this point in the history
  • Loading branch information
sstraatemans committed Jan 8, 2025
1 parent 0830d3c commit 5ff737d
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 12 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { useAccount } from '@/hooks/account';
import { useForcedTransferTokens } from '@/hooks/forcedTransferTokens';
import { useGetFrozenTokens } from '@/hooks/getFrozenTokens';
import { useGetInvestorBalance } from '@/hooks/getInvestorBalance';
import { useGetInvestors } from '@/hooks/getInvestors';
Expand Down Expand Up @@ -43,9 +44,11 @@ export const TransferForm: FC<IProps> = ({
const { account } = useAccount();
const { data: balance } = useGetInvestorBalance({ investorAccount });
const { data: investors } = useGetInvestors();
const { submit: forcedSubmit, isAllowed: isForcedAllowed } =
useForcedTransferTokens();
const { submit, isAllowed } = useTransferTokens();
const { data: frozenAmount } = useGetFrozenTokens({
investorAccount: investorAccount,
investorAccount,
});

const {
Expand All @@ -56,7 +59,7 @@ export const TransferForm: FC<IProps> = ({
} = useForm<ITransferTokensProps>({
values: {
amount: 0,
investorFromAccount: account?.address!,
investorFromAccount: investorAccount,
investorToAccount: '',
isForced,
},
Expand All @@ -75,16 +78,19 @@ export const TransferForm: FC<IProps> = ({
};

const onSubmit = async (data: ITransferTokensProps) => {
await submit(data);
if (data.isForced) {
await forcedSubmit(data);
} else {
await submit(data);
}
handleOnClose();
};

const filteredInvestors = investors.filter(
(i) => i.accountName !== account?.address,
(i) => i.accountName !== investorAccount,
);

if (!account) return;

const maxAmount = isForced ? balance : balance - frozenAmount;

return (
Expand Down Expand Up @@ -156,7 +162,12 @@ export const TransferForm: FC<IProps> = ({
<Button onPress={handleOnClose} variant="transparent">
Cancel
</Button>
<Button isDisabled={!isAllowed || !isValid} type="submit">
<Button
isDisabled={
(isForced ? !isForcedAllowed : !isAllowed) || !isValid
}
type="submit"
>
Transfer
</Button>
</RightAsideFooter>
Expand Down
50 changes: 50 additions & 0 deletions packages/apps/rwa-demo/src/hooks/forcedTransferTokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import {
interpretErrorMessage,
TXTYPES,
} from '@/components/TransactionsProvider/TransactionsProvider';
import { forcedTransferTokens } from '@/services/forcedTransferTokens';
import type { ITransferTokensProps } from '@/services/transferTokens';
import { getClient } from '@/utils/client';
import { useNotifications } from '@kadena/kode-ui/patterns';
import { useEffect, useState } from 'react';
import { useAccount } from './account';
import { useAsset } from './asset';
import { useTransactions } from './transactions';

export const useForcedTransferTokens = () => {
const { account, sign, isMounted, accountRoles } = useAccount();
const { paused } = useAsset();
const { addTransaction } = useTransactions();
const { addNotification } = useNotifications();
const [isAllowed, setIsAllowed] = useState(false);

const submit = async (data: ITransferTokensProps) => {
try {
const tx = await forcedTransferTokens(data, account!);
const signedTransaction = await sign(tx);
if (!signedTransaction) return;

const client = getClient();
const res = await client.submit(signedTransaction);

return addTransaction({
...res,
type: TXTYPES.TRANSFERTOKENS,
accounts: [data.investorFromAccount, data.investorToAccount],
});
} catch (e: any) {
addNotification({
intent: 'negative',
label: 'there was an error',
message: interpretErrorMessage(e.message),
});
}
};

useEffect(() => {
if (!isMounted) return;
setIsAllowed(!paused && accountRoles.isTransferManager());
}, [paused, isMounted, accountRoles]);

return { submit, isAllowed };
};
5 changes: 1 addition & 4 deletions packages/apps/rwa-demo/src/hooks/transferTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import {
interpretErrorMessage,
TXTYPES,
} from '@/components/TransactionsProvider/TransactionsProvider';
import { forcedTransferTokens } from '@/services/forcedTransferTokens';
import type { ITransferTokensProps } from '@/services/transferTokens';
import { transferTokens } from '@/services/transferTokens';
import { getClient } from '@/utils/client';
Expand All @@ -23,9 +22,7 @@ export const useTransferTokens = () => {

const submit = async (data: ITransferTokensProps) => {
try {
const tx = data.isForced
? await forcedTransferTokens(data, account!)
: await transferTokens(data, account!);
const tx = await transferTokens(data, account!);
const signedTransaction = await sign(tx);
if (!signedTransaction) return;

Expand Down
3 changes: 1 addition & 2 deletions packages/apps/rwa-demo/src/services/forcedTransferTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,7 @@ export const forcedTransferTokens = async (
senderAccount: account.address,
chainId: getNetwork().chainId,
})
.addSigner(createPubKeyFromAccount(data.investorFromAccount), (withCap) => [
withCap(`${getAsset()}.FORCED-TRANSFER`),
.addSigner(createPubKeyFromAccount(account.address), (withCap) => [
withCap(`${getAsset()}.ONLY-AGENT`, 'transfer-manager'),
withCap(
`${getAsset()}.TRANSFER`,
Expand Down

0 comments on commit 5ff737d

Please sign in to comment.