Skip to content

Commit

Permalink
fix the login and faucet when no asset has been created yet
Browse files Browse the repository at this point in the history
  • Loading branch information
sstraatemans committed Jan 16, 2025
1 parent 79b1374 commit dea5e53
Show file tree
Hide file tree
Showing 9 changed files with 39 additions and 65 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const Home = () => {
const router = useRouter();

useEffect(() => {
console.log(66666, { isMounted, account });
if (isMounted && !account) {
router.replace('/login');
return;
Expand Down
6 changes: 5 additions & 1 deletion packages/apps/rwa-demo/src/app/(loggedout)/layout.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
'use client';

import { GasPayableBanner } from '@/components/GasPayableBanner/GasPayableBanner';
import { Card, Stack, Text } from '@kadena/kode-ui';
import React from 'react';
import {
Expand All @@ -23,7 +24,10 @@ const RootLayout = ({
className={wrapperClass}
>
<Stack flexDirection="column" className={cardWrapperClass}>
<Card className={cardClass}>{children}</Card>
<Card className={cardClass}>
<GasPayableBanner />
{children}
</Card>
<Stack
className={footerClass}
width="100%"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,8 @@ export const AccountProvider: FC<PropsWithChildren> = ({ children }) => {

useEffect(() => {
const storage = localStorage.getItem(getAccountCookieName());
if (storage) {

if (storage && storage !== 'undefined') {
console.log({ account: JSON.parse(storage) });
try {
setAccount(JSON.parse(storage));
Expand Down
45 changes: 4 additions & 41 deletions packages/apps/rwa-demo/src/hooks/__tests__/faucet.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,6 @@ describe('faucet hook', () => {
},
sign: vi.fn(),
isMounted: true,
isAgent: true,
isOwner: true,
isInvestor: true,
isGasPayable: true,
}),
useNetwork: vi.fn().mockReturnValue({
Expand Down Expand Up @@ -76,15 +73,12 @@ describe('faucet hook', () => {
});

describe('isAllowed', () => {
it('should return true, when account is mounted, when it is owner, when gas is NOT payable, when network is development', () => {
it('should return true, when account is mounted, when gas is NOT payable, when network is development', () => {
mocksHook.useAccount.mockImplementation(() => ({
account: {
address: 'k:he-man',
},
isMounted: true,
isAgent: false,
isOwner: true,
isInvestor: false,
isGasPayable: false,
}));

Expand All @@ -99,15 +93,12 @@ describe('faucet hook', () => {
});
});

it('should return false, when account is NOT mounted, when it is owner, when gas is NOT payable, when network is development', () => {
it('should return false, when account is NOT mounted, when gas is NOT payable, when network is development', () => {
mocksHook.useAccount.mockImplementation(() => ({
account: {
address: 'k:he-man',
},
isMounted: false,
isAgent: false,
isOwner: true,
isInvestor: false,
isGasPayable: false,
}));

Expand All @@ -121,37 +112,12 @@ describe('faucet hook', () => {
expect(result.current.isAllowed).toBe(false);
});

it('should return false, when account is mounted, when account has no role, when gas is NOT payable, when network is development', () => {
it('should return false, when account is mounted, when gas is payable, when network is development', () => {
mocksHook.useAccount.mockImplementation(() => ({
account: {
address: 'k:he-man',
},
isMounted: true,
isAgent: false,
isOwner: false,
isInvestor: false,
isGasPayable: false,
}));

mocksHook.useNetwork.mockImplementation(() => ({
...mocksHook.useNetwork.getMockImplementation(),
activeNetwork: { networkId: 'development' },
}));

const { result } = renderHook(() => useFaucet());

expect(result.current.isAllowed).toBe(false);
});

it('should return false, when account is mounted, when account isinvestor, when gas is payable, when network is development', () => {
mocksHook.useAccount.mockImplementation(() => ({
account: {
address: 'k:he-man',
},
isMounted: true,
isAgent: false,
isOwner: false,
isInvestor: true,
isGasPayable: true,
}));

Expand All @@ -165,15 +131,12 @@ describe('faucet hook', () => {
expect(result.current.isAllowed).toBe(false);
});

it('should return false, when account is mounted, when account isagent, when gas is NOT payable, when network is mainnet', () => {
it('should return false, when account is mounted, when gas is NOT payable, when network is mainnet', () => {
mocksHook.useAccount.mockImplementation(() => ({
account: {
address: 'k:he-man',
},
isMounted: false,
isAgent: true,
isOwner: false,
isInvestor: true,
isGasPayable: false,
}));

Expand Down
5 changes: 3 additions & 2 deletions packages/apps/rwa-demo/src/hooks/addInvestor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export const useAddInvestor = ({
}) => {
const { frozen } = useFreeze({ investorAccount });
const { paused } = useAsset();
const { account, sign, accountRoles, isMounted } = useAccount();
const { account, isOwner, sign, accountRoles, isMounted } = useAccount();
const { addTransaction, isActiveAccountChangeTx } = useTransactions();
const { addNotification } = useNotifications();
const [isAllowed, setIsAllowed] = useState(false);
Expand Down Expand Up @@ -63,14 +63,15 @@ export const useAddInvestor = ({
setIsAllowed(
((!!investorAccount && !frozen) || frozen) &&
!paused &&
accountRoles.isAgentAdmin() &&
(accountRoles.isAgentAdmin() || isOwner) &&
!isActiveAccountChangeTx,
);
}, [
frozen,
paused,
isMounted,
investorAccount,
isOwner,
accountRoles,
isActiveAccountChangeTx,
]);
Expand Down
8 changes: 5 additions & 3 deletions packages/apps/rwa-demo/src/hooks/batchAddInvestors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useTransactions } from './transactions';

export const useBatchAddInvestors = () => {
const { paused } = useAsset();
const { account, sign, accountRoles, isMounted } = useAccount();
const { account, isOwner, sign, accountRoles, isMounted } = useAccount();
const { addTransaction, isActiveAccountChangeTx } = useTransactions();
const { addNotification } = useNotifications();
const [isAllowed, setIsAllowed] = useState(false);
Expand Down Expand Up @@ -57,9 +57,11 @@ export const useBatchAddInvestors = () => {
if (!isMounted) return;

setIsAllowed(
!paused && accountRoles.isAgentAdmin() && !isActiveAccountChangeTx,
!paused &&
(accountRoles.isAgentAdmin() || isOwner) &&
!isActiveAccountChangeTx,
);
}, [paused, isMounted, accountRoles, isActiveAccountChangeTx]);
}, [paused, isOwner, isMounted, accountRoles, isActiveAccountChangeTx]);

return { submit, isAllowed };
};
15 changes: 12 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 = ({
investorAccount?: string;
}) => {
useGetInvestorBalance;
const { account, sign, accountRoles, isMounted } = useAccount();
const { account, isOwner, sign, accountRoles, isMounted } = useAccount();
const { data: balance } = useGetInvestorBalance({ investorAccount });
const { paused } = useAsset();
const { addTransaction, isActiveAccountChangeTx } = useTransactions();
Expand Down Expand Up @@ -75,9 +75,18 @@ export const useDeleteInvestor = ({
}

setIsAllowed(
!paused && accountRoles.isAgentAdmin() && !isActiveAccountChangeTx,
!paused &&
(accountRoles.isAgentAdmin() || isOwner) &&
!isActiveAccountChangeTx,
);
}, [paused, account?.address, isMounted, balance, isActiveAccountChangeTx]);
}, [
paused,
isOwner,
account?.address,
isMounted,
balance,
isActiveAccountChangeTx,
]);

return { submit, isAllowed, notAllowedReason };
};
14 changes: 3 additions & 11 deletions packages/apps/rwa-demo/src/hooks/faucet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,7 @@ import { useNetwork } from './networks';
import { useTransactions } from './transactions';

export const useFaucet = () => {
const {
account,
sign,
isMounted,
isAgent,
isOwner,
isInvestor,
isGasPayable,
} = useAccount();
const { account, sign, isMounted, isGasPayable } = useAccount();
const { addTransaction } = useTransactions();
const { activeNetwork } = useNetwork();
const { addNotification } = useNotifications();
Expand Down Expand Up @@ -54,8 +46,8 @@ export const useFaucet = () => {
useEffect(() => {
if (!isMounted || activeNetwork.networkId !== 'development') return;

setIsAllowed((isAgent || isOwner || isInvestor) && !isGasPayable);
}, [isOwner, isInvestor, isAgent, isMounted, isOwner, isGasPayable]);
setIsAllowed(!!account?.address && !isGasPayable);
}, [account?.address, isMounted, isGasPayable]);

return { submit, isAllowed };
};
7 changes: 5 additions & 2 deletions packages/apps/rwa-demo/src/hooks/setCompliance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import { useAsset } from './asset';
import { useTransactions } from './transactions';

export const useSetCompliance = () => {
const { account, sign, isMounted, accountRoles } = useAccount();
const { account, isOwner, sign, isMounted, accountRoles } = useAccount();
const { asset, paused } = useAsset();
const { addTransaction, isActiveAccountChangeTx } = useTransactions();
const { addNotification } = useNotifications();
Expand Down Expand Up @@ -78,13 +78,16 @@ export const useSetCompliance = () => {
useEffect(() => {
if (!isMounted) return;
setIsAllowed(
!paused && accountRoles.isAgentAdmin() && !isActiveAccountChangeTx,
!paused &&
(accountRoles.isAgentAdmin() || isOwner) &&
!isActiveAccountChangeTx,
);
}, [
paused,
account?.address,
isMounted,
accountRoles,
isOwner,
isActiveAccountChangeTx,
]);

Expand Down

0 comments on commit dea5e53

Please sign in to comment.