From 3d3ed7ccf67c040a6abe51fde527b42f55ec9797 Mon Sep 17 00:00:00 2001 From: Steven Straatemans Date: Tue, 19 Nov 2024 14:24:21 +0100 Subject: [PATCH] add investor page --- .changeset/quiet-baboons-obey.md | 2 + .../pages/contacts/Components/ContactForm.tsx | 1 - .../investors/[investorAccount]/page.tsx | 52 +++++++++++++++++ .../DistributionForm/DistributionForm.tsx | 58 +++++++++++++++++++ .../components/InvestorList/InvestorList.tsx | 11 +++- .../SetComplianceForm/SetComplianceForm.tsx | 3 - .../rwa-demo/src/hooks/distributeTokens.ts | 40 +++++++++++++ .../rwa-demo/src/services/distributeTokens.ts | 48 +++++++++++++++ .../TableFormatters/FormatLinkWrapper.tsx | 2 - 9 files changed, 210 insertions(+), 7 deletions(-) create mode 100644 .changeset/quiet-baboons-obey.md create mode 100644 packages/apps/rwa-demo/src/app/(app)/(isAgent)/investors/[investorAccount]/page.tsx create mode 100644 packages/apps/rwa-demo/src/components/DistributionForm/DistributionForm.tsx create mode 100644 packages/apps/rwa-demo/src/hooks/distributeTokens.ts create mode 100644 packages/apps/rwa-demo/src/services/distributeTokens.ts diff --git a/.changeset/quiet-baboons-obey.md b/.changeset/quiet-baboons-obey.md new file mode 100644 index 0000000000..a845151cc8 --- /dev/null +++ b/.changeset/quiet-baboons-obey.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/packages/apps/dev-wallet/src/pages/contacts/Components/ContactForm.tsx b/packages/apps/dev-wallet/src/pages/contacts/Components/ContactForm.tsx index b3d5325706..030cef651c 100644 --- a/packages/apps/dev-wallet/src/pages/contacts/Components/ContactForm.tsx +++ b/packages/apps/dev-wallet/src/pages/contacts/Components/ContactForm.tsx @@ -126,7 +126,6 @@ export function ContactForm({ rules={{ required: true }} defaultValue={getValues('discoverdAccount')} render={({ field }) => { - console.log({ field }); return ( { + 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 ( + <> + + + Investor + + + + {isRightAsideExpanded && hasOpenDistributeForm && ( + { + setIsRightAsideExpanded(false); + setHasOpenDistributeForm(false); + }} + /> + )} + + + Investor: {investorAccount} + + + + + + ); +}; + +export default InvestorPage; diff --git a/packages/apps/rwa-demo/src/components/DistributionForm/DistributionForm.tsx b/packages/apps/rwa-demo/src/components/DistributionForm/DistributionForm.tsx new file mode 100644 index 0000000000..6fb857629b --- /dev/null +++ b/packages/apps/rwa-demo/src/components/DistributionForm/DistributionForm.tsx @@ -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 = ({ onClose, investorAccount }) => { + const { submit } = useDistributeTokens(); + const { register, handleSubmit } = useForm({ + defaultValues: { + amount: '', + investorAccount, + }, + values: { + investorAccount, + }, + }); + + const onSubmit = async (data: IDistributeTokensProps) => { + await submit(data); + + onClose(); + }; + + return ( + <> + +
+ + + + + + + + + +
+ + ); +}; diff --git a/packages/apps/rwa-demo/src/components/InvestorList/InvestorList.tsx b/packages/apps/rwa-demo/src/components/InvestorList/InvestorList.tsx index 3a900a40bd..c976b14720 100644 --- a/packages/apps/rwa-demo/src/components/InvestorList/InvestorList.tsx +++ b/packages/apps/rwa-demo/src/components/InvestorList/InvestorList.tsx @@ -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 = () => { @@ -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: '', diff --git a/packages/apps/rwa-demo/src/components/SetComplianceForm/SetComplianceForm.tsx b/packages/apps/rwa-demo/src/components/SetComplianceForm/SetComplianceForm.tsx index d4edeb4d7a..11f1e3665d 100644 --- a/packages/apps/rwa-demo/src/components/SetComplianceForm/SetComplianceForm.tsx +++ b/packages/apps/rwa-demo/src/components/SetComplianceForm/SetComplianceForm.tsx @@ -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 { @@ -17,8 +16,6 @@ interface IProps { export const SetComplianceForm: FC = ({ onClose }) => { const { submit } = useSetCompliance(); - // eslint-disable-next-line @typescript-eslint/no-unused-vars - const [_, setError] = useState(null); const { register, handleSubmit } = useForm({ defaultValues: { maxBalance: 0, diff --git a/packages/apps/rwa-demo/src/hooks/distributeTokens.ts b/packages/apps/rwa-demo/src/hooks/distributeTokens.ts new file mode 100644 index 0000000000..4c8c3b2c11 --- /dev/null +++ b/packages/apps/rwa-demo/src/hooks/distributeTokens.ts @@ -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(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 }; +}; diff --git a/packages/apps/rwa-demo/src/services/distributeTokens.ts b/packages/apps/rwa-demo/src/services/distributeTokens.ts new file mode 100644 index 0000000000..06c94a7909 --- /dev/null +++ b/packages/apps/rwa-demo/src/services/distributeTokens.ts @@ -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(); +}; diff --git a/packages/libs/kode-ui/src/patterns/CompactTable/TableFormatters/FormatLinkWrapper.tsx b/packages/libs/kode-ui/src/patterns/CompactTable/TableFormatters/FormatLinkWrapper.tsx index 0e58b8cd65..e88cb2d47b 100644 --- a/packages/libs/kode-ui/src/patterns/CompactTable/TableFormatters/FormatLinkWrapper.tsx +++ b/packages/libs/kode-ui/src/patterns/CompactTable/TableFormatters/FormatLinkWrapper.tsx @@ -41,8 +41,6 @@ export const FormatLinkWrapper = ({