Skip to content

Commit 54b0802

Browse files
authored
fix the login and faucet when no asset has been created yet (#2796)
1 parent 73fa550 commit 54b0802

File tree

9 files changed

+39
-65
lines changed

9 files changed

+39
-65
lines changed

packages/apps/rwa-demo/src/app/(loggedout)/assets/create/page.tsx

-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ const Home = () => {
1111
const router = useRouter();
1212

1313
useEffect(() => {
14-
console.log(66666, { isMounted, account });
1514
if (isMounted && !account) {
1615
router.replace('/login');
1716
return;

packages/apps/rwa-demo/src/app/(loggedout)/layout.tsx

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
'use client';
22

3+
import { GasPayableBanner } from '@/components/GasPayableBanner/GasPayableBanner';
34
import { Card, Stack, Text } from '@kadena/kode-ui';
45
import React from 'react';
56
import {
@@ -23,7 +24,10 @@ const RootLayout = ({
2324
className={wrapperClass}
2425
>
2526
<Stack flexDirection="column" className={cardWrapperClass}>
26-
<Card className={cardClass}>{children}</Card>
27+
<Card className={cardClass}>
28+
<GasPayableBanner />
29+
{children}
30+
</Card>
2731
<Stack
2832
className={footerClass}
2933
width="100%"

packages/apps/rwa-demo/src/components/AccountProvider/AccountProvider.tsx

+2-1
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,8 @@ export const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
165165

166166
useEffect(() => {
167167
const storage = localStorage.getItem(getAccountCookieName());
168-
if (storage) {
168+
169+
if (storage && storage !== 'undefined') {
169170
console.log({ account: JSON.parse(storage) });
170171
try {
171172
setAccount(JSON.parse(storage));

packages/apps/rwa-demo/src/hooks/__tests__/faucet.test.ts

+4-41
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,6 @@ describe('faucet hook', () => {
1010
},
1111
sign: vi.fn(),
1212
isMounted: true,
13-
isAgent: true,
14-
isOwner: true,
15-
isInvestor: true,
1613
isGasPayable: true,
1714
}),
1815
useNetwork: vi.fn().mockReturnValue({
@@ -76,15 +73,12 @@ describe('faucet hook', () => {
7673
});
7774

7875
describe('isAllowed', () => {
79-
it('should return true, when account is mounted, when it is owner, when gas is NOT payable, when network is development', () => {
76+
it('should return true, when account is mounted, when gas is NOT payable, when network is development', () => {
8077
mocksHook.useAccount.mockImplementation(() => ({
8178
account: {
8279
address: 'k:he-man',
8380
},
8481
isMounted: true,
85-
isAgent: false,
86-
isOwner: true,
87-
isInvestor: false,
8882
isGasPayable: false,
8983
}));
9084

@@ -99,15 +93,12 @@ describe('faucet hook', () => {
9993
});
10094
});
10195

102-
it('should return false, when account is NOT mounted, when it is owner, when gas is NOT payable, when network is development', () => {
96+
it('should return false, when account is NOT mounted, when gas is NOT payable, when network is development', () => {
10397
mocksHook.useAccount.mockImplementation(() => ({
10498
account: {
10599
address: 'k:he-man',
106100
},
107101
isMounted: false,
108-
isAgent: false,
109-
isOwner: true,
110-
isInvestor: false,
111102
isGasPayable: false,
112103
}));
113104

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

124-
it('should return false, when account is mounted, when account has no role, when gas is NOT payable, when network is development', () => {
115+
it('should return false, when account is mounted, when gas is payable, when network is development', () => {
125116
mocksHook.useAccount.mockImplementation(() => ({
126117
account: {
127118
address: 'k:he-man',
128119
},
129120
isMounted: true,
130-
isAgent: false,
131-
isOwner: false,
132-
isInvestor: false,
133-
isGasPayable: false,
134-
}));
135-
136-
mocksHook.useNetwork.mockImplementation(() => ({
137-
...mocksHook.useNetwork.getMockImplementation(),
138-
activeNetwork: { networkId: 'development' },
139-
}));
140-
141-
const { result } = renderHook(() => useFaucet());
142-
143-
expect(result.current.isAllowed).toBe(false);
144-
});
145-
146-
it('should return false, when account is mounted, when account isinvestor, when gas is payable, when network is development', () => {
147-
mocksHook.useAccount.mockImplementation(() => ({
148-
account: {
149-
address: 'k:he-man',
150-
},
151-
isMounted: true,
152-
isAgent: false,
153-
isOwner: false,
154-
isInvestor: true,
155121
isGasPayable: true,
156122
}));
157123

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

168-
it('should return false, when account is mounted, when account isagent, when gas is NOT payable, when network is mainnet', () => {
134+
it('should return false, when account is mounted, when gas is NOT payable, when network is mainnet', () => {
169135
mocksHook.useAccount.mockImplementation(() => ({
170136
account: {
171137
address: 'k:he-man',
172138
},
173139
isMounted: false,
174-
isAgent: true,
175-
isOwner: false,
176-
isInvestor: true,
177140
isGasPayable: false,
178141
}));
179142

packages/apps/rwa-demo/src/hooks/addInvestor.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export const useAddInvestor = ({
2121
}) => {
2222
const { frozen } = useFreeze({ investorAccount });
2323
const { paused } = useAsset();
24-
const { account, sign, accountRoles, isMounted } = useAccount();
24+
const { account, isOwner, sign, accountRoles, isMounted } = useAccount();
2525
const { addTransaction, isActiveAccountChangeTx } = useTransactions();
2626
const { addNotification } = useNotifications();
2727
const [isAllowed, setIsAllowed] = useState(false);
@@ -63,14 +63,15 @@ export const useAddInvestor = ({
6363
setIsAllowed(
6464
((!!investorAccount && !frozen) || frozen) &&
6565
!paused &&
66-
accountRoles.isAgentAdmin() &&
66+
(accountRoles.isAgentAdmin() || isOwner) &&
6767
!isActiveAccountChangeTx,
6868
);
6969
}, [
7070
frozen,
7171
paused,
7272
isMounted,
7373
investorAccount,
74+
isOwner,
7475
accountRoles,
7576
isActiveAccountChangeTx,
7677
]);

packages/apps/rwa-demo/src/hooks/batchAddInvestors.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { useTransactions } from './transactions';
1515

1616
export const useBatchAddInvestors = () => {
1717
const { paused } = useAsset();
18-
const { account, sign, accountRoles, isMounted } = useAccount();
18+
const { account, isOwner, sign, accountRoles, isMounted } = useAccount();
1919
const { addTransaction, isActiveAccountChangeTx } = useTransactions();
2020
const { addNotification } = useNotifications();
2121
const [isAllowed, setIsAllowed] = useState(false);
@@ -57,9 +57,11 @@ export const useBatchAddInvestors = () => {
5757
if (!isMounted) return;
5858

5959
setIsAllowed(
60-
!paused && accountRoles.isAgentAdmin() && !isActiveAccountChangeTx,
60+
!paused &&
61+
(accountRoles.isAgentAdmin() || isOwner) &&
62+
!isActiveAccountChangeTx,
6163
);
62-
}, [paused, isMounted, accountRoles, isActiveAccountChangeTx]);
64+
}, [paused, isOwner, isMounted, accountRoles, isActiveAccountChangeTx]);
6365

6466
return { submit, isAllowed };
6567
};

packages/apps/rwa-demo/src/hooks/deleteInvestor.ts

+12-3
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ export const useDeleteInvestor = ({
1919
investorAccount?: string;
2020
}) => {
2121
useGetInvestorBalance;
22-
const { account, sign, accountRoles, isMounted } = useAccount();
22+
const { account, isOwner, sign, accountRoles, isMounted } = useAccount();
2323
const { data: balance } = useGetInvestorBalance({ investorAccount });
2424
const { paused } = useAsset();
2525
const { addTransaction, isActiveAccountChangeTx } = useTransactions();
@@ -75,9 +75,18 @@ export const useDeleteInvestor = ({
7575
}
7676

7777
setIsAllowed(
78-
!paused && accountRoles.isAgentAdmin() && !isActiveAccountChangeTx,
78+
!paused &&
79+
(accountRoles.isAgentAdmin() || isOwner) &&
80+
!isActiveAccountChangeTx,
7981
);
80-
}, [paused, account?.address, isMounted, balance, isActiveAccountChangeTx]);
82+
}, [
83+
paused,
84+
isOwner,
85+
account?.address,
86+
isMounted,
87+
balance,
88+
isActiveAccountChangeTx,
89+
]);
8190

8291
return { submit, isAllowed, notAllowedReason };
8392
};

packages/apps/rwa-demo/src/hooks/faucet.ts

+3-11
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,7 @@ import { useNetwork } from './networks';
1212
import { useTransactions } from './transactions';
1313

1414
export const useFaucet = () => {
15-
const {
16-
account,
17-
sign,
18-
isMounted,
19-
isAgent,
20-
isOwner,
21-
isInvestor,
22-
isGasPayable,
23-
} = useAccount();
15+
const { account, sign, isMounted, isGasPayable } = useAccount();
2416
const { addTransaction } = useTransactions();
2517
const { activeNetwork } = useNetwork();
2618
const { addNotification } = useNotifications();
@@ -54,8 +46,8 @@ export const useFaucet = () => {
5446
useEffect(() => {
5547
if (!isMounted || activeNetwork.networkId !== 'development') return;
5648

57-
setIsAllowed((isAgent || isOwner || isInvestor) && !isGasPayable);
58-
}, [isOwner, isInvestor, isAgent, isMounted, isOwner, isGasPayable]);
49+
setIsAllowed(!!account?.address && !isGasPayable);
50+
}, [account?.address, isMounted, isGasPayable]);
5951

6052
return { submit, isAllowed };
6153
};

packages/apps/rwa-demo/src/hooks/setCompliance.ts

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import { useAsset } from './asset';
1515
import { useTransactions } from './transactions';
1616

1717
export const useSetCompliance = () => {
18-
const { account, sign, isMounted, accountRoles } = useAccount();
18+
const { account, isOwner, sign, isMounted, accountRoles } = useAccount();
1919
const { asset, paused } = useAsset();
2020
const { addTransaction, isActiveAccountChangeTx } = useTransactions();
2121
const { addNotification } = useNotifications();
@@ -78,13 +78,16 @@ export const useSetCompliance = () => {
7878
useEffect(() => {
7979
if (!isMounted) return;
8080
setIsAllowed(
81-
!paused && accountRoles.isAgentAdmin() && !isActiveAccountChangeTx,
81+
!paused &&
82+
(accountRoles.isAgentAdmin() || isOwner) &&
83+
!isActiveAccountChangeTx,
8284
);
8385
}, [
8486
paused,
8587
account?.address,
8688
isMounted,
8789
accountRoles,
90+
isOwner,
8891
isActiveAccountChangeTx,
8992
]);
9093

0 commit comments

Comments
 (0)