Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore(rwa): add isowner check for account #2704

Merged
merged 3 commits into from
Dec 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .changeset/strange-steaks-begin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
7 changes: 7 additions & 0 deletions packages/apps/rwa-demo/src/app/(app)/SideBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {
MonoLightMode,
MonoLogout,
MonoNetworkCheck,
MonoVpnLock,
} from '@kadena/kode-icons';
import {
Button,
Expand Down Expand Up @@ -67,6 +68,12 @@ export const SideBar: FC = () => {
component={Link}
href="/"
/>
<SideBarItem
visual={<MonoVpnLock />}
label="Assets"
component={Link}
href="/assets"
/>
</>
}
appContext={
Expand Down
17 changes: 17 additions & 0 deletions packages/apps/rwa-demo/src/app/(app)/assets/[uuid]/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
'use client';
import { useAsset } from '@/hooks/asset';
import { useParams } from 'next/navigation';
import { useMemo } from 'react';

const Assets = () => {
const { getAsset } = useAsset();
const { uuid } = useParams();
const asset = useMemo(() => {
return getAsset(uuid as string);
}, [uuid]);

console.log({ asset });
return <pre>{JSON.stringify(asset, null, 2)}</pre>;
};

export default Assets;
35 changes: 35 additions & 0 deletions packages/apps/rwa-demo/src/app/(app)/assets/page.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
'use client';

import { useAsset } from '@/hooks/asset';
import { CompactTable, CompactTableFormatters } from '@kadena/kode-ui/patterns';
import Link from 'next/link';

const Assets = () => {
const { assets } = useAsset();

return (
<>
<CompactTable
fields={[
{
key: 'uuid',
label: 'id',
width: '40%',
render: CompactTableFormatters.FormatLink({
linkComponent: Link,
url: '/assets/:value',
}),
},
{
key: 'name',
label: 'name',
width: '60%',
},
]}
data={assets}
/>
</>
);
};

export default Assets;
5 changes: 2 additions & 3 deletions packages/apps/rwa-demo/src/app/(app)/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,11 @@ import { OwnerRootPage } from '@/components/HomePage/OwnerRootPage';
import { useAccount } from '@/hooks/account';

const Home = () => {
const { isAgent, isInvestor } = useAccount();
const { isAgent, isInvestor, isOwner } = useAccount();

console.log('asset', isAgent);
return (
<>
{!isAgent && <OwnerRootPage />}
{isOwner && <OwnerRootPage />}
{isAgent && <AgentRootPage />}
{isInvestor && <InvestorRootPage />}
</>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { getBalance as getBalanceFnc } from '@/services/getBalance';
import { isAgent } from '@/services/isAgent';
import { isFrozen } from '@/services/isFrozen';
import { isInvestor } from '@/services/isInvestor';
import { isOwner } from '@/services/isOwner';
import { getAccountCookieName } from '@/utils/getAccountCookieName';
import type { ICommand, IUnsignedCommand } from '@kadena/client';
import { useRouter } from 'next/navigation';
Expand All @@ -24,6 +25,7 @@ export interface IAccountContext {
logout: () => void;
sign: (tx: IUnsignedCommand) => Promise<ICommand | undefined>;
isAgent: boolean;
isOwner: boolean;
isInvestor: boolean;
isFrozen: boolean;
selectAccount: (account: IWalletAccount) => void;
Expand All @@ -38,6 +40,7 @@ export const AccountContext = createContext<IAccountContext>({
logout: () => {},
sign: async () => undefined,
isAgent: false,
isOwner: false,
isInvestor: false,
isFrozen: false,
selectAccount: () => {},
Expand All @@ -48,6 +51,7 @@ export const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
const [account, setAccount] = useState<IWalletAccount>();
const [accounts, setAccounts] = useState<IWalletAccount[]>();
const [isMounted, setIsMounted] = useState(false);
const [isOwnerState, setIsOwnerState] = useState(false);
const [isAgentState, setIsAgentState] = useState(false);
const [isInvestorState, setIsInvestorState] = useState(false);
const [isFrozenState, setIsFrozenState] = useState(false);
Expand All @@ -58,6 +62,10 @@ export const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
const resIsAgent = await isAgent({ agent: account.address });
setIsAgentState(!!resIsAgent);
};
const checkIsOwner = async (account: IWalletAccount) => {
const resIsOwner = await isOwner({ owner: account.address });
setIsOwnerState(!!resIsOwner);
};
const checkIsInvestor = async (account: IWalletAccount) => {
const resIsInvestor = await isInvestor({ account });
setIsInvestorState(!!resIsInvestor);
Expand Down Expand Up @@ -132,10 +140,13 @@ export const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
useEffect(() => {
if (!account) {
setIsAgentState(false);
setIsOwnerState(false);
setIsInvestorState(false);
return;
}

// eslint-disable-next-line @typescript-eslint/no-floating-promises
checkIsOwner(account);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
checkIsAgent(account);
// eslint-disable-next-line @typescript-eslint/no-floating-promises
Expand Down Expand Up @@ -176,6 +187,7 @@ export const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
logout,
sign,
isMounted,
isOwner: isOwnerState,
isAgent: isAgentState,
isInvestor: isInvestorState,
isFrozen: isFrozenState,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ export interface IAssetContext {
assets: IAsset[];
paused: boolean;
setAsset: (asset: IAsset) => void;
getAsset: (uuid: string) => IAsset | undefined;
}

export const AssetContext = createContext<IAssetContext>({
assets: [],
paused: false,
setAsset: () => {},
getAsset: (uuid: string) => undefined,
});

export const AssetProvider: FC<PropsWithChildren> = ({ children }) => {
Expand All @@ -39,10 +41,11 @@ export const AssetProvider: FC<PropsWithChildren> = ({ children }) => {
getLocalStorageKey(LOCALSTORAGE_ASSETS_SELECTED_KEY) ?? '';
const { paused } = usePaused();

const getAssets = () => {
const getAssets = (): IAsset[] => {
const result = localStorage.getItem(storageKey);
setAssets(JSON.parse(result ?? '[]'));
router.refresh();
const arr = JSON.parse(result ?? '[]');
setAssets(arr);
return arr ?? [];
};

const storageListener = (event: StorageEvent | Event) => {
Expand All @@ -55,9 +58,15 @@ export const AssetProvider: FC<PropsWithChildren> = ({ children }) => {
localStorage.setItem(selectedKey, JSON.stringify(data));
setAsset(data);

router.replace('/');
router.refresh();
};

const getAsset = (uuid: string) => {
const data = getAssets().find((a) => a.uuid === uuid);
return data;
};

useEffect(() => {
getAssets();
window.addEventListener(storageKey, storageListener);
Expand All @@ -79,7 +88,7 @@ export const AssetProvider: FC<PropsWithChildren> = ({ children }) => {

return (
<AssetContext.Provider
value={{ asset, assets, setAsset: handleSelectAsset, paused }}
value={{ asset, assets, setAsset: handleSelectAsset, getAsset, paused }}
>
{children}
</AssetContext.Provider>
Expand Down
1 change: 0 additions & 1 deletion packages/apps/rwa-demo/src/hooks/freeze.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ export const useFreeze = ({ investorAccount }: { investorAccount: string }) => {
useEffect(() => {
if (!data?.events?.length) return;
const params = JSON.parse(data?.events[0].parameters ?? '[]');
console.log(params);
if (params.length < 2 || params[0] !== investorAccount) return;

setFrozen(params[1]);
Expand Down
9 changes: 9 additions & 0 deletions packages/apps/rwa-demo/src/services/addAgent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ export const addAgent = async (
keys: [createPubKeyFromAccount(data.accountName)],
pred: 'keys-all',
})
.addData('roles', [
'agent-admin',
'supply-modifier',
'freezer',
'transfer-manager',
'recovery',
'compliance',
'whitelist-manager',
])

.setNetworkId(getNetwork().networkId)
.createTransaction();
Expand Down
29 changes: 29 additions & 0 deletions packages/apps/rwa-demo/src/services/isOwner.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { getClient, getNetwork } from '@/utils/client';
import { getAsset } from '@/utils/getAsset';
import { Pact } from '@kadena/client';

export interface IIsOwnerProps {
owner: string;
}

export const isOwner = async (data: IIsOwnerProps) => {
const client = getClient();
console.log({ data });

const transaction = Pact.builder
.execution(`(RWA.${getAsset()}.is-owner (read-string 'owner))`)
.setMeta({
senderAccount: data.owner,
chainId: getNetwork().chainId,
})
.addData('owner', data.owner)
.setNetworkId(getNetwork().networkId)
.createTransaction();

const { result } = await client.local(transaction, {
preflight: false,
signatureVerification: false,
});

return result.status === 'success' ? result.data : undefined;
};
1 change: 0 additions & 1 deletion packages/apps/rwa-demo/src/services/transferTokens.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export const transferTokens = async (
data: ITransferTokensProps,
account: IWalletAccount,
) => {
console.log(new PactNumber(data.amount).toPrecision(2));
return Pact.builder
.execution(
`
Expand Down