Skip to content

Commit

Permalink
add investor page
Browse files Browse the repository at this point in the history
  • Loading branch information
sstraatemans committed Nov 19, 2024
1 parent 6ba39ac commit 3d3ed7c
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 7 deletions.
2 changes: 2 additions & 0 deletions .changeset/quiet-baboons-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
---
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,6 @@ export function ContactForm({
rules={{ required: true }}
defaultValue={getValues('discoverdAccount')}
render={({ field }) => {
console.log({ field });
return (
<Stack flexDirection={'column'} gap={'sm'}>
<AccountInput
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
'use client';

import { DistributionForm } from '@/components/DistributionForm/DistributionForm';
import { SideBarBreadcrumbs } from '@/components/SideBarBreadcrumbs/SideBarBreadcrumbs';
import { MonoAdd } from '@kadena/kode-icons';
import { Button, Heading, Stack } from '@kadena/kode-ui';
import { SideBarBreadcrumbsItem, useLayout } from '@kadena/kode-ui/patterns';
import { useParams } from 'next/navigation';
import { useState } from 'react';

const InvestorPage = () => {
const { isRightAsideExpanded, setIsRightAsideExpanded } = useLayout();
const params = useParams();
const [hasOpenDistributeForm, setHasOpenDistributeForm] = useState(false);
const investorAccount = decodeURIComponent(params.investorAccount as string);

const handleDistributeTokens = () => {
setIsRightAsideExpanded(true);
setHasOpenDistributeForm(true);
};

return (
<>
<SideBarBreadcrumbs>
<SideBarBreadcrumbsItem href={`/investors/${investorAccount}`}>
Investor
</SideBarBreadcrumbsItem>
</SideBarBreadcrumbs>

{isRightAsideExpanded && hasOpenDistributeForm && (
<DistributionForm
investorAccount={investorAccount}
onClose={() => {
setIsRightAsideExpanded(false);
setHasOpenDistributeForm(false);
}}
/>
)}

<Stack width="100%" flexDirection="column">
<Heading>Investor: {investorAccount}</Heading>
<Stack gap="sm">
<Button startVisual={<MonoAdd />} onPress={handleDistributeTokens}>
Distribute Tokens
</Button>
</Stack>
</Stack>
</>
);
};

export default InvestorPage;
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { useDistributeTokens } from '@/hooks/distributeTokens';
import type { IDistributeTokensProps } from '@/services/distributeTokens';
import { Button, TextField } from '@kadena/kode-ui';
import {
RightAside,
RightAsideContent,
RightAsideFooter,
RightAsideHeader,
} from '@kadena/kode-ui/patterns';
import type { FC } from 'react';
import { useForm } from 'react-hook-form';

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

export const DistributionForm: FC<IProps> = ({ onClose, investorAccount }) => {
const { submit } = useDistributeTokens();
const { register, handleSubmit } = useForm<IDistributeTokensProps>({
defaultValues: {
amount: '',
investorAccount,
},
values: {
investorAccount,
},
});

const onSubmit = async (data: IDistributeTokensProps) => {
await submit(data);

onClose();
};

return (
<>
<RightAside isOpen onClose={onClose}>
<form onSubmit={handleSubmit(onSubmit)}>
<RightAsideHeader label="Distribute Tokens" />
<RightAsideContent>
<TextField
label="Amount"
type="number"
{...register('amount', { required: true })}
/>
</RightAsideContent>
<RightAsideFooter>
<Button onPress={onClose} variant="transparent">
Cancel
</Button>
<Button type="submit">Distribute</Button>
</RightAsideFooter>
</form>
</RightAside>
</>
);
};
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ 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 Link from 'next/link';
import type { FC } from 'react';

export const InvestorList: FC = () => {
Expand Down Expand Up @@ -39,7 +40,15 @@ export const InvestorList: FC = () => {
width: '10%',
render: CompactTableFormatters.FormatStatus(),
},
{ label: 'Account', key: 'accountName', width: '50%' },
{
label: 'Account',
key: 'accountName',
width: '50%',
render: CompactTableFormatters.FormatLinkWrapper({
url: '/investors/:value',
linkComponent: Link,
}),
},
{ label: 'Requestkey', key: 'requestKey', width: '30%' },
{
label: '',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import {
RightAsideHeader,
} from '@kadena/kode-ui/patterns';
import type { FC } from 'react';
import { useState } from 'react';
import { useForm } from 'react-hook-form';

interface IProps {
Expand All @@ -17,8 +16,6 @@ interface IProps {

export const SetComplianceForm: FC<IProps> = ({ onClose }) => {
const { submit } = useSetCompliance();
// eslint-disable-next-line @typescript-eslint/no-unused-vars
const [_, setError] = useState<string | null>(null);
const { register, handleSubmit } = useForm<ISetComplianceProps>({
defaultValues: {
maxBalance: 0,
Expand Down
40 changes: 40 additions & 0 deletions packages/apps/rwa-demo/src/hooks/distributeTokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import type { IDistributeTokensProps } from '@/services/distributeTokens';
import { distributeTokens } from '@/services/distributeTokens';
import { getClient } from '@/utils/client';
import { useState } from 'react';
import { useAccount } from './account';
import { useTransactions } from './transactions';

export const useDistributeTokens = () => {
const [error, setError] = useState<string | null>(null);
const { account, sign } = useAccount();
const { addTransaction } = useTransactions();

const submit = async (data: IDistributeTokensProps) => {
setError(null);
try {
const tx = await distributeTokens(data, account!);

console.log(tx);

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

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

addTransaction({
...res,
type: 'DISTRIBUTETOKENS',
data: { ...res, ...data },
});

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

return { submit, error };
};
48 changes: 48 additions & 0 deletions packages/apps/rwa-demo/src/services/distributeTokens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import type { IWalletAccount } from '@/components/AccountProvider/utils';
import { getNetwork } from '@/utils/client';
import { Pact } from '@kadena/client';
import { PactNumber } from '@kadena/pactjs';

export interface IDistributeTokensProps {
amount: number;
investorAccount: string;
}

const createPubKeyFromAccount = (account: string): string => {
return account.replace('k:', '').replace('r:', '');
};

export const distributeTokens = async (
data: IDistributeTokensProps,
account: IWalletAccount,
) => {
console.log(new PactNumber(data.amount).toPrecision(2));
return Pact.builder
.execution(
`
(RWA.mvp-token.mint (read-string 'investor) ${new PactNumber(data.amount).toPactDecimal().decimal} (read-string 'agent))`,
)
.addData('agent', account.address)
.addData('investor', data.investorAccount)
.addData('investor-keyset', {
keys: [createPubKeyFromAccount(data.investorAccount)],
pred: 'keys-all',
})
.setMeta({
senderAccount: account.address,
chainId: getNetwork().chainId,
})
.addSigner(account.keyset.guard.keys[0], (withCap) => [
withCap(`RWA.agent-role.ONLY-AGENT`, account.address),
withCap(
`RWA.mvp-token.TRANSFER`,
'c:soLScugf2M-6r5L-leMUCGvgddyB1qaKh9s7ka4upGU',
data.investorAccount,

{ decimal: data.amount },
),
withCap(`coin.GAS`),
])
.setNetworkId(getNetwork().networkId)
.createTransaction();
};
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ export const FormatLinkWrapper = ({
<LinkWrapper
href={formatURL(url, valueToString(value))}
to={formatURL(url, valueToString(value))}
passHref
legacyBehavior
className={linkClass}
>
<Text variant="code" className={dataFieldClass}>
Expand Down

0 comments on commit 3d3ed7c

Please sign in to comment.