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

feat(rwa): add compliance #2665

Merged
merged 7 commits into from
Nov 19, 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/twelve-poets-chew.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
15 changes: 15 additions & 0 deletions packages/apps/rwa-demo/src/app/(app)/(isAgent)/layout.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
'use client';
import { useAccount } from '@/hooks/account';
import type React from 'react';

const AgentLayout = ({
children,
}: Readonly<{
children: React.ReactNode;
}>) => {
const { isAgent, account } = useAccount();
if (!account && !isAgent) return null;
return children;
};

export default AgentLayout;
63 changes: 9 additions & 54 deletions packages/apps/rwa-demo/src/app/(app)/page.tsx
Original file line number Diff line number Diff line change
@@ -1,62 +1,17 @@
'use client';
import { AddAgentForm } from '@/components/AddAgentForm/AddAgentForm';
import { AgentsList } from '@/components/AgentsList/AgentsList';
import { InitTokenForm } from '@/components/InitTokenForm/InitTokenForm';
import { SideBarBreadcrumbs } from '@/components/SideBarBreadcrumbs/SideBarBreadcrumbs';
import { MonoAdd } from '@kadena/kode-icons';
import { Button, Stack } from '@kadena/kode-ui';
import { SideBarBreadcrumbsItem, useLayout } from '@kadena/kode-ui/patterns';
import { useState } from 'react';

const Home = () => {
const { setIsRightAsideExpanded, isRightAsideExpanded } = useLayout();
const [hasOpenInitForm, setHasOpenInitForm] = useState(false);
const [hasOpenAgentForm, setHasOpenAgentForm] = useState(false);

const handleAddAgent = () => {
setIsRightAsideExpanded(true);
setHasOpenAgentForm(true);
};
import { AgentRootPage } from '@/components/HomePage/AgentRootPage';
import { OwnerRootPage } from '@/components/HomePage/OwnerRootPage';
import { useAccount } from '@/hooks/account';

const handleInitToken = () => {
setIsRightAsideExpanded(true);
setHasOpenInitForm(true);
};
const Home = () => {
const { isAgent } = useAccount();

return (
<Stack width="100%" flexDirection="column" gap="md">
<SideBarBreadcrumbs>
<SideBarBreadcrumbsItem href="/">Tokens</SideBarBreadcrumbsItem>
</SideBarBreadcrumbs>
{isRightAsideExpanded && hasOpenAgentForm && (
<AddAgentForm
onClose={() => {
setIsRightAsideExpanded(false);
setHasOpenAgentForm(false);
}}
/>
)}
{isRightAsideExpanded && hasOpenInitForm && (
<InitTokenForm
onClose={() => {
setIsRightAsideExpanded(false);
setHasOpenInitForm(false);
}}
/>
)}

<Stack gap="sm">
<Button startVisual={<MonoAdd />} onClick={handleAddAgent}>
Add Agent
</Button>

<Button startVisual={<MonoAdd />} onClick={handleInitToken}>
Init Token
</Button>
</Stack>

<AgentsList />
</Stack>
<>
{!isAgent && <OwnerRootPage />}
{isAgent && <AgentRootPage />}
</>
);
};

Expand Down
5 changes: 4 additions & 1 deletion packages/apps/rwa-demo/src/app/Providers.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { AccountProvider } from '@/components/AccountProvider/AccountProvider';
import { AssetProvider } from '@/components/AssetProvider/AssetProvider';
import { NetworkProvider } from '@/components/NetworkProvider/NetworkProvider';
import { TransactionsProvider } from '@/components/TransactionsProvider/TransactionsProvider';
import { MediaContextProvider } from '@kadena/kode-ui';
import { LayoutProvider } from '@kadena/kode-ui/patterns';
import { darkThemeClass } from '@kadena/kode-ui/styles';
Expand All @@ -22,7 +23,9 @@ export const Providers: FC<PropsWithChildren> = ({ children }) => {
<NetworkProvider>
<AccountProvider>
<LayoutProvider>
<AssetProvider>{children}</AssetProvider>
<AssetProvider>
<TransactionsProvider>{children}</TransactionsProvider>
</AssetProvider>
</LayoutProvider>
</AccountProvider>
</NetworkProvider>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
'use client';
import { useNetwork } from '@/hooks/networks';
import { isAgent } from '@/services/isAgent';
import { getAccountCookieName } from '@/utils/getAccountCookieName';
import type { ICommand, IUnsignedCommand } from '@kadena/client';
import type { ConnectedAccount } from '@kadena/spirekey-sdk';
import { useRouter } from 'next/navigation';
import type { FC, PropsWithChildren } from 'react';
import { createContext, useCallback, useEffect, useState } from 'react';
Expand All @@ -13,12 +14,13 @@ interface IAccountError {
}

export interface IAccountContext {
account?: ConnectedAccount;
account?: IWalletAccount;
error?: IAccountError;
isMounted: boolean;
login: () => void;
logout: () => void;
sign: (tx: IUnsignedCommand) => Promise<ICommand | undefined>;
isAgent: boolean;
}

export const AccountContext = createContext<IAccountContext>({
Expand All @@ -27,13 +29,26 @@ export const AccountContext = createContext<IAccountContext>({
login: () => {},
logout: () => {},
sign: async () => undefined,
isAgent: false,
});

export const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
const [account, setAccount] = useState<ConnectedAccount>();
const [account, setAccount] = useState<IWalletAccount>();
const [isMounted, setIsMounted] = useState(false);
const [isAgentState, setIsAgentState] = useState(false);

const { activeNetwork } = useNetwork();
const router = useRouter();

const checkIsAgent = async (account: IWalletAccount) => {
const resIsAgent = await isAgent(
{ agent: account.address },
activeNetwork,
account,
);
setIsAgentState(!!resIsAgent);
};

const login = useCallback(async () => {
const { message, focus, close } = await getWalletConnection();
focus();
Expand Down Expand Up @@ -73,6 +88,16 @@ export const AccountProvider: FC<PropsWithChildren> = ({ children }) => {
setIsMounted(true);
}, []);

useEffect(() => {
if (!account) {
setIsAgentState(false);
return;
}

// eslint-disable-next-line @typescript-eslint/no-floating-promises
checkIsAgent(account);
}, [account]);

const sign = async (tx: IUnsignedCommand): Promise<ICommand | undefined> => {
const { message, close } = await getWalletConnection();
const response = await message('SIGN_REQUEST', tx as any);
Expand All @@ -87,7 +112,7 @@ export const AccountProvider: FC<PropsWithChildren> = ({ children }) => {

return (
<AccountContext.Provider
value={{ account, login, logout, sign, isMounted }}
value={{ account, login, logout, sign, isMounted, isAgent: isAgentState }}
>
{children}
</AccountContext.Provider>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { useAccount } from '@/hooks/account';
import { useNetwork } from '@/hooks/networks';
import { useTransactions } from '@/hooks/transactions';
import type { IAddAgentProps } from '@/services/addAgent';
import { addAgent } from '@/services/addAgent';
import { getClient } from '@/utils/client';
Expand Down Expand Up @@ -27,6 +28,7 @@ interface IProps {
export const AddAgentForm: FC<IProps> = ({ onClose }) => {
const { activeNetwork } = useNetwork();
const { account, sign } = useAccount();
const { addTransaction } = useTransactions();
const [openModal, setOpenModal] = useState(false);
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [_, setError] = useState<string | null>(null);
Expand All @@ -46,9 +48,12 @@ export const AddAgentForm: FC<IProps> = ({ onClose }) => {

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

await client.listen(res);
addTransaction({
...res,
type: 'ADDAGENT',
data: { ...res, ...data },
});
console.log('DONE');
} catch (e: any) {
setError(e?.message || e);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import { useAccount } from '@/hooks/account';
import { useNetwork } from '@/hooks/networks';
import type { IRegisterIdentityProps } from '@/services/registerIdentity';
import { registerIdentity } from '@/services/registerIdentity';
import { getClient } from '@/utils/client';
import { Button, TextField } from '@kadena/kode-ui';
import {
RightAside,
RightAsideContent,
RightAsideFooter,
RightAsideHeader,
} from '@kadena/kode-ui/patterns';
import type { FC } from 'react';
import { useState } from 'react';
import { useForm } from 'react-hook-form';

interface IProps {
onClose: () => void;
}

export const AddInvestorForm: FC<IProps> = ({ onClose }) => {
const { activeNetwork } = useNetwork();
const { account, sign } = useAccount();
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [_, setError] = useState<string | null>(null);
const { register, handleSubmit } = useForm<IRegisterIdentityProps>({
defaultValues: {
investor: '',
},
});

const onSubmit = async (data: IRegisterIdentityProps) => {
const newData: IRegisterIdentityProps = { ...data, agent: account! };
setError(null);
try {
const tx = await registerIdentity(newData, activeNetwork);

const signedTransaction = await sign(tx);
if (!signedTransaction) return;

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

await client.listen(res);
console.log('DONE');
} catch (e: any) {
setError(e?.message || e);
}

onClose();
};

return (
<>
<RightAside isOpen onClose={onClose}>
<form onSubmit={handleSubmit(onSubmit)}>
<RightAsideHeader label="Add Investor" />
<RightAsideContent>
<TextField
label="Investor Account"
{...register('investor', { required: true })}
/>
</RightAsideContent>
<RightAsideFooter>
<Button onPress={onClose} variant="transparent">
Cancel
</Button>
<Button type="submit">Add Investor</Button>
</RightAsideFooter>
</form>
</RightAside>
</>
);
};
48 changes: 45 additions & 3 deletions packages/apps/rwa-demo/src/components/AgentsList/AgentsList.tsx
Original file line number Diff line number Diff line change
@@ -1,17 +1,59 @@
import { useAccount } from '@/hooks/account';
import { useGetAgents } from '@/hooks/getAgents';
import { CompactTable } from '@kadena/kode-ui/patterns';
import { useNetwork } from '@/hooks/networks';
import { removeAgent } from '@/services/removeAgent';
import { getClient } from '@/utils/client';
import { MonoDelete } from '@kadena/kode-icons';
import { Button } from '@kadena/kode-ui';
import { CompactTable, CompactTableFormatters } from '@kadena/kode-ui/patterns';
import type { FC } from 'react';

export const AgentsList: FC = () => {
const { data } = useGetAgents();
const { account, sign } = useAccount();
const { activeNetwork } = useNetwork();

console.log(data);
const handleDelete = async (accountName: any) => {
try {
const tx = await removeAgent(
{ agent: accountName },
activeNetwork,
account!,
);

const signedTransaction = await sign(tx);
if (!signedTransaction) return;

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

await client.listen(res);
console.log('DONE');
} catch (e: any) {}
};

console.log({ data });
return (
<CompactTable
fields={[
{
label: 'status',
key: 'result',
width: '10%',
render: CompactTableFormatters.FormatStatus(),
},
{ label: 'Account', key: 'accountName', width: '50%' },
{ label: 'Requestkey', key: 'requestKey', width: '50%' },
{ label: 'Requestkey', key: 'requestKey', width: '30%' },
{
label: '',
key: 'accountName',
width: '10%',
render: CompactTableFormatters.FormatActions({
trigger: (
<Button startVisual={<MonoDelete />} onPress={handleDelete} />
),
}),
},
]}
data={data}
/>
Expand Down
Loading
Loading