diff --git a/.changeset/wise-clocks-kick.md b/.changeset/wise-clocks-kick.md new file mode 100644 index 0000000000..a845151cc8 --- /dev/null +++ b/.changeset/wise-clocks-kick.md @@ -0,0 +1,2 @@ +--- +--- diff --git a/packages/apps/rwa-demo/src/app/(app)/(isAgent)/investors/[investorAccount]/page.tsx b/packages/apps/rwa-demo/src/app/(app)/(isAgent)/investors/[investorAccount]/page.tsx index cac222aba4..c197ffb266 100644 --- a/packages/apps/rwa-demo/src/app/(app)/(isAgent)/investors/[investorAccount]/page.tsx +++ b/packages/apps/rwa-demo/src/app/(app)/(isAgent)/investors/[investorAccount]/page.tsx @@ -2,52 +2,28 @@ import { DistributionForm } from '@/components/DistributionForm/DistributionForm'; import { FreezeInvestor } from '@/components/FreezeInvestor/FreezeInvestor'; -import { InvestorBalance } from '@/components/InvestorBalance/InvestorBalance'; +import { InvestorInfo } from '@/components/InvestorInfo/InvestorInfo'; import { SideBarBreadcrumbs } from '@/components/SideBarBreadcrumbs/SideBarBreadcrumbs'; -import { TransferForm } from '@/components/TransferForm/TransferForm'; -import { useAccount } from '@/hooks/account'; -import { isFrozen } from '@/services/isFrozen'; -import { MonoAdd, MonoCompareArrows } from '@kadena/kode-icons'; -import { Button, Heading, Stack } from '@kadena/kode-ui'; +import { useAsset } from '@/hooks/asset'; +import { useFreeze } from '@/hooks/freeze'; +import { MonoAdd } from '@kadena/kode-icons'; +import { Button, Stack } from '@kadena/kode-ui'; import { SideBarBreadcrumbsItem, useLayout } from '@kadena/kode-ui/patterns'; import { useParams } from 'next/navigation'; -import { useEffect, useState } from 'react'; +import { useState } from 'react'; const InvestorPage = () => { const { isRightAsideExpanded, setIsRightAsideExpanded } = useLayout(); - const { account } = useAccount(); + const { paused } = useAsset(); const params = useParams(); const [hasOpenDistributeForm, setHasOpenDistributeForm] = useState(false); - const [hasOpenTransferForm, setHasOpenTransferForm] = useState(false); const investorAccount = decodeURIComponent(params.investorAccount as string); - const [frozen, setFrozen] = useState(false); + const { frozen } = useFreeze({ investorAccount }); const handleDistributeTokens = () => { setIsRightAsideExpanded(true); setHasOpenDistributeForm(true); }; - const handleTransferTokens = () => { - setIsRightAsideExpanded(true); - setHasOpenTransferForm(true); - }; - - const init = async () => { - const res = await isFrozen({ - investorAccount: investorAccount, - account: account!, - }); - - if (typeof res === 'boolean') { - setFrozen(res); - } - }; - - useEffect(() => { - // eslint-disable-next-line @typescript-eslint/no-floating-promises - init(); - }, []); - - const handlePauseChange = (pausedResult: boolean) => setFrozen(pausedResult); return ( <> @@ -66,38 +42,18 @@ const InvestorPage = () => { }} /> )} - {isRightAsideExpanded && hasOpenTransferForm && ( - { - setIsRightAsideExpanded(false); - setHasOpenTransferForm(false); - }} - /> - )} - Investor: {investorAccount} - + - - + diff --git a/packages/apps/rwa-demo/src/app/(app)/layout.tsx b/packages/apps/rwa-demo/src/app/(app)/layout.tsx index 6fcb943c61..f68cb42d52 100644 --- a/packages/apps/rwa-demo/src/app/(app)/layout.tsx +++ b/packages/apps/rwa-demo/src/app/(app)/layout.tsx @@ -1,8 +1,8 @@ 'use client'; import { SideBarLayout } from '@kadena/kode-ui/patterns'; +import { AssetInfo } from '@/components/AssetInfo/AssetInfo'; import { AssetForm } from '@/components/AssetSwitch/AssetForm'; -import { SupplyCount } from '@/components/SupplyCount/SupplyCount'; import { getAsset } from '@/utils/getAsset'; import { Heading, Link, Stack } from '@kadena/kode-ui'; import React from 'react'; @@ -41,7 +41,8 @@ const RootLayout = ({ sidebar={} > - + + {children} diff --git a/packages/apps/rwa-demo/src/app/(app)/page.tsx b/packages/apps/rwa-demo/src/app/(app)/page.tsx index 1120213f34..6065326a8e 100644 --- a/packages/apps/rwa-demo/src/app/(app)/page.tsx +++ b/packages/apps/rwa-demo/src/app/(app)/page.tsx @@ -1,18 +1,20 @@ 'use client'; import { AgentRootPage } from '@/components/HomePage/AgentRootPage'; +import { InvestorRootPage } from '@/components/HomePage/InvestorRootPage'; import { OwnerRootPage } from '@/components/HomePage/OwnerRootPage'; import { useAccount } from '@/hooks/account'; import { getAsset } from '@/utils/getAsset'; const Home = () => { - const { isAgent } = useAccount(); + const { isAgent, isInvestor } = useAccount(); console.log('asset', getAsset()); return ( <> {!isAgent && } {isAgent && } + {isInvestor && } ); }; diff --git a/packages/apps/rwa-demo/src/components/AccountProvider/AccountProvider.tsx b/packages/apps/rwa-demo/src/components/AccountProvider/AccountProvider.tsx index f895c7eaa9..ccb71cae48 100644 --- a/packages/apps/rwa-demo/src/components/AccountProvider/AccountProvider.tsx +++ b/packages/apps/rwa-demo/src/components/AccountProvider/AccountProvider.tsx @@ -1,5 +1,8 @@ 'use client'; +import { getBalance as getBalanceFnc } from '@/services/getBalance'; import { isAgent } from '@/services/isAgent'; +import { isFrozen } from '@/services/isFrozen'; +import { isInvestor } from '@/services/isInvestor'; import { getAccountCookieName } from '@/utils/getAccountCookieName'; import type { ICommand, IUnsignedCommand } from '@kadena/client'; import { useRouter } from 'next/navigation'; @@ -21,7 +24,10 @@ export interface IAccountContext { logout: () => void; sign: (tx: IUnsignedCommand) => Promise; isAgent: boolean; + isInvestor: boolean; + isFrozen: boolean; selectAccount: (account: IWalletAccount) => void; + getBalance: () => Promise; } export const AccountContext = createContext({ @@ -32,7 +38,10 @@ export const AccountContext = createContext({ logout: () => {}, sign: async () => undefined, isAgent: false, + isInvestor: false, + isFrozen: false, selectAccount: () => {}, + getBalance: async () => 0, }); export const AccountProvider: FC = ({ children }) => { @@ -40,6 +49,8 @@ export const AccountProvider: FC = ({ children }) => { const [accounts, setAccounts] = useState(); const [isMounted, setIsMounted] = useState(false); const [isAgentState, setIsAgentState] = useState(false); + const [isInvestorState, setIsInvestorState] = useState(false); + const [isFrozenState, setIsFrozenState] = useState(false); const router = useRouter(); @@ -47,6 +58,20 @@ export const AccountProvider: FC = ({ children }) => { const resIsAgent = await isAgent({ agent: account.address }); setIsAgentState(!!resIsAgent); }; + const checkIsInvestor = async (account: IWalletAccount) => { + const resIsInvestor = await isInvestor({ account }); + setIsInvestorState(!!resIsInvestor); + }; + const checkIsFrozen = async (account: IWalletAccount) => { + const res = await isFrozen({ + investorAccount: account.address, + account: account!, + }); + + if (typeof res === 'boolean') { + setIsFrozenState(res); + } + }; const selectAccount = (account: IWalletAccount) => { setAccount(account); @@ -76,7 +101,12 @@ export const AccountProvider: FC = ({ children }) => { setAccounts(undefined); setAccount(payload.accounts[0]); + localStorage.setItem( + getAccountCookieName(), + JSON.stringify(payload.accounts[0]), + ); close(); + router.replace('/'); }, [router]); const logout = useCallback(() => { @@ -102,11 +132,16 @@ export const AccountProvider: FC = ({ children }) => { useEffect(() => { if (!account) { setIsAgentState(false); + setIsInvestorState(false); return; } // eslint-disable-next-line @typescript-eslint/no-floating-promises checkIsAgent(account); + // eslint-disable-next-line @typescript-eslint/no-floating-promises + checkIsInvestor(account); + // eslint-disable-next-line @typescript-eslint/no-floating-promises + checkIsFrozen(account); }, [account]); const sign = async (tx: IUnsignedCommand): Promise => { @@ -121,6 +156,17 @@ export const AccountProvider: FC = ({ children }) => { return payload.transaction; }; + const getBalance = async () => { + if (!account) return 0; + const res = await getBalanceFnc({ + investorAccount: account.address, + account, + }); + + if (typeof res !== 'number') return 0; + return res; + }; + return ( = ({ children }) => { sign, isMounted, isAgent: isAgentState, + isInvestor: isInvestorState, + isFrozen: isFrozenState, selectAccount, + getBalance, }} > {children} diff --git a/packages/apps/rwa-demo/src/components/AgentsList/AgentsList.tsx b/packages/apps/rwa-demo/src/components/AgentsList/AgentsList.tsx index aa7642802c..aba2d69ec0 100644 --- a/packages/apps/rwa-demo/src/components/AgentsList/AgentsList.tsx +++ b/packages/apps/rwa-demo/src/components/AgentsList/AgentsList.tsx @@ -1,15 +1,18 @@ import { useAccount } from '@/hooks/account'; import { useGetAgents } from '@/hooks/getAgents'; +import { useTransactions } from '@/hooks/transactions'; import { removeAgent } from '@/services/removeAgent'; import { getClient } from '@/utils/client'; import { MonoDelete } from '@kadena/kode-icons'; -import { Button } from '@kadena/kode-ui'; +import { Button, Heading } from '@kadena/kode-ui'; import { CompactTable, CompactTableFormatters } from '@kadena/kode-ui/patterns'; import type { FC } from 'react'; +import { Confirmation } from '../Confirmation/Confirmation'; export const AgentsList: FC = () => { const { data } = useGetAgents(); const { account, sign } = useAccount(); + const { addTransaction } = useTransactions(); const handleDelete = async (accountName: any) => { try { @@ -21,34 +24,49 @@ export const AgentsList: FC = () => { const client = getClient(); const res = await client.submit(signedTransaction); + console.log({ res }); + addTransaction({ + ...res, + type: 'REMOVEAGENT', + data: { ...res, ...data }, + }); + await client.listen(res); console.log('DONE'); } catch (e: any) {} }; return ( - } onPress={handleDelete} /> - ), - }), - }, - ]} - data={data} - /> + <> + Agents + } />} + > + Are you sure you want to delete this agent? + + ), + }), + }, + ]} + data={data} + /> + ); }; diff --git a/packages/apps/rwa-demo/src/components/AssetInfo/AssetInfo.tsx b/packages/apps/rwa-demo/src/components/AssetInfo/AssetInfo.tsx new file mode 100644 index 0000000000..08224202ef --- /dev/null +++ b/packages/apps/rwa-demo/src/components/AssetInfo/AssetInfo.tsx @@ -0,0 +1,31 @@ +import { useAsset } from '@/hooks/asset'; +import { MonoPause, MonoPlayArrow } from '@kadena/kode-icons'; +import { Button, Heading, Stack } from '@kadena/kode-ui'; +import type { FC } from 'react'; +import { SupplyCount } from '../SupplyCount/SupplyCount'; + +export const AssetInfo: FC = () => { + const { paused, asset } = useAsset(); + if (!asset) return; + return ( + + {asset.name} + + + + + + ); +}; diff --git a/packages/apps/rwa-demo/src/components/AssetPausedMessage/AssetPausedMessage.tsx b/packages/apps/rwa-demo/src/components/AssetPausedMessage/AssetPausedMessage.tsx new file mode 100644 index 0000000000..30ae6e8041 --- /dev/null +++ b/packages/apps/rwa-demo/src/components/AssetPausedMessage/AssetPausedMessage.tsx @@ -0,0 +1,14 @@ +import { useAsset } from '@/hooks/asset'; +import { Notification } from '@kadena/kode-ui'; +import type { FC } from 'react'; + +export const AssetPausedMessage: FC = () => { + const { paused } = useAsset(); + + if (!paused) return; + return ( + + The asset is on pause + + ); +}; diff --git a/packages/apps/rwa-demo/src/components/Confirmation/Confirmation.tsx b/packages/apps/rwa-demo/src/components/Confirmation/Confirmation.tsx new file mode 100644 index 0000000000..3e7fbfa842 --- /dev/null +++ b/packages/apps/rwa-demo/src/components/Confirmation/Confirmation.tsx @@ -0,0 +1,45 @@ +import type { PressEvent } from '@kadena/kode-ui'; +import { Button, Dialog, DialogContent, DialogFooter } from '@kadena/kode-ui'; + +import type { FC, PropsWithChildren } from 'react'; +import React, { useState } from 'react'; + +interface IProps extends PropsWithChildren { + trigger: React.ReactElement; + label?: string; + onPress: (e: PressEvent) => void; +} +export const Confirmation: FC = ({ + children, + trigger, + label = 'ok', + onPress, +}) => { + const [isOpen, setIsOpen] = useState(false); + + const handleAction = (e: any) => { + setIsOpen(false); + onPress(e); + }; + + return ( + <> + {React.cloneElement(trigger, { + ...trigger.props, + onPress: () => setIsOpen(true), + })} + + setIsOpen(false)}> + {children} + + + + + + + ); +}; diff --git a/packages/apps/rwa-demo/src/components/DistributionForm/DistributionForm.tsx b/packages/apps/rwa-demo/src/components/DistributionForm/DistributionForm.tsx index 34f1ada49b..1abcbfaec1 100644 --- a/packages/apps/rwa-demo/src/components/DistributionForm/DistributionForm.tsx +++ b/packages/apps/rwa-demo/src/components/DistributionForm/DistributionForm.tsx @@ -1,4 +1,6 @@ +import { useAsset } from '@/hooks/asset'; import { useDistributeTokens } from '@/hooks/distributeTokens'; +import { useFreeze } from '@/hooks/freeze'; import type { IDistributeTokensProps } from '@/services/distributeTokens'; import { Button, TextField } from '@kadena/kode-ui'; import { @@ -9,6 +11,8 @@ import { } from '@kadena/kode-ui/patterns'; import type { FC } from 'react'; import { useForm } from 'react-hook-form'; +import { AssetPausedMessage } from '../AssetPausedMessage/AssetPausedMessage'; +import { InvestorFrozenMessage } from '../InvestorFrozenMessage/InvestorFrozenMessage'; interface IProps { onClose: () => void; @@ -16,6 +20,8 @@ interface IProps { } export const DistributionForm: FC = ({ onClose, investorAccount }) => { + const { frozen } = useFreeze({ investorAccount }); + const { paused } = useAsset(); const { submit } = useDistributeTokens(); const { register, handleSubmit } = useForm({ values: { @@ -26,7 +32,6 @@ export const DistributionForm: FC = ({ onClose, investorAccount }) => { const onSubmit = async (data: IDistributeTokensProps) => { await submit(data); - onClose(); }; @@ -42,11 +47,20 @@ export const DistributionForm: FC = ({ onClose, investorAccount }) => { {...register('amount', { required: true })} /> - + + + + + } + > - + diff --git a/packages/apps/rwa-demo/src/components/FreezeInvestor/FreezeInvestor.tsx b/packages/apps/rwa-demo/src/components/FreezeInvestor/FreezeInvestor.tsx index 552eec9bc3..a5ba65e59f 100644 --- a/packages/apps/rwa-demo/src/components/FreezeInvestor/FreezeInvestor.tsx +++ b/packages/apps/rwa-demo/src/components/FreezeInvestor/FreezeInvestor.tsx @@ -1,6 +1,6 @@ import { useAccount } from '@/hooks/account'; +import { useFreeze } from '@/hooks/freeze'; import { useTransactions } from '@/hooks/transactions'; -import { isFrozen } from '@/services/isFrozen'; import { setAddressFrozen } from '@/services/setAddressFrozen'; import { getClient } from '@/utils/client'; import { MonoPause, MonoPlayArrow } from '@kadena/kode-icons'; @@ -11,20 +11,19 @@ import { TransactionPendingIcon } from '../TransactionPendingIcon/TransactionPen interface IProps { investorAccount: string; - onChanged: (paused: boolean) => void; } -const getVisual = (paused?: boolean) => { - if (typeof paused !== 'boolean') { +const getVisual = (frozen: boolean, isLoading: boolean) => { + if (isLoading) { return ; } - return paused ? : ; + return frozen ? : ; }; -export const FreezeInvestor: FC = ({ investorAccount, onChanged }) => { +export const FreezeInvestor: FC = ({ investorAccount }) => { const { account, sign } = useAccount(); const { addTransaction } = useTransactions(); - const [frozen, setFrozen] = useState(); + const { frozen } = useFreeze({ investorAccount }); const [isLoading, setIsLoading] = useState(true); const handleFreeze = async () => { @@ -44,7 +43,7 @@ export const FreezeInvestor: FC = ({ investorAccount, onChanged }) => { const client = getClient(); const res = await client.submit(signedTransaction); - const transaction = addTransaction({ + addTransaction({ ...res, type: 'FREEZE-ADDRESS', data: { @@ -52,34 +51,17 @@ export const FreezeInvestor: FC = ({ investorAccount, onChanged }) => { ...data, }, }); - - await transaction.listener; - setFrozen(undefined); - } catch (e: any) {} - }; - - const fetchData = async () => { - const res = await isFrozen({ - investorAccount: investorAccount, - account: account!, - }); - - if (typeof res === 'boolean') { - setFrozen(res); - onChanged(res); + } catch (e: any) { + setIsLoading(false); } - setIsLoading(false); }; useEffect(() => { - if (isLoading) { - // eslint-disable-next-line @typescript-eslint/no-floating-promises - fetchData(); - } + setIsLoading(false); }, [frozen]); return ( - ); diff --git a/packages/apps/rwa-demo/src/components/HomePage/AgentRootPage.tsx b/packages/apps/rwa-demo/src/components/HomePage/AgentRootPage.tsx index 6fafe63b51..4174a36ed8 100644 --- a/packages/apps/rwa-demo/src/components/HomePage/AgentRootPage.tsx +++ b/packages/apps/rwa-demo/src/components/HomePage/AgentRootPage.tsx @@ -1,5 +1,6 @@ import { SideBarBreadcrumbs } from '@/components/SideBarBreadcrumbs/SideBarBreadcrumbs'; import { useAccount } from '@/hooks/account'; +import { useAsset } from '@/hooks/asset'; import { MonoAdd } from '@kadena/kode-icons'; import { Button, Stack } from '@kadena/kode-ui'; import { useLayout } from '@kadena/kode-ui/patterns'; @@ -11,6 +12,7 @@ import { PauseForm } from '../PauseForm/PauseForm'; export const AgentRootPage: FC = () => { const { isAgent, account } = useAccount(); + const { paused } = useAsset(); const { setIsRightAsideExpanded, isRightAsideExpanded } = useLayout(); const [hasOpenInvestorForm, setHasOpenInvestorForm] = useState(false); @@ -35,7 +37,11 @@ export const AgentRootPage: FC = () => { )} - diff --git a/packages/apps/rwa-demo/src/components/HomePage/InvestorRootPage.tsx b/packages/apps/rwa-demo/src/components/HomePage/InvestorRootPage.tsx new file mode 100644 index 0000000000..7157e4e598 --- /dev/null +++ b/packages/apps/rwa-demo/src/components/HomePage/InvestorRootPage.tsx @@ -0,0 +1,50 @@ +import { SideBarBreadcrumbs } from '@/components/SideBarBreadcrumbs/SideBarBreadcrumbs'; +import { useAccount } from '@/hooks/account'; +import { MonoCompareArrows } from '@kadena/kode-icons'; +import { Button, Stack } from '@kadena/kode-ui'; +import { useLayout } from '@kadena/kode-ui/patterns'; +import type { FC } from 'react'; +import { useState } from 'react'; +import { InvestorInfo } from '../InvestorInfo/InvestorInfo'; +import { TransferForm } from '../TransferForm/TransferForm'; + +export const InvestorRootPage: FC = () => { + const { isInvestor, account, isFrozen } = useAccount(); + const { setIsRightAsideExpanded, isRightAsideExpanded } = useLayout(); + const [hasOpenTransferForm, setHasOpenTransferForm] = useState(false); + + const handleTransferTokens = () => { + setIsRightAsideExpanded(true); + setHasOpenTransferForm(true); + }; + + if (!isInvestor || !account) return null; + + return ( + <> + {isRightAsideExpanded && hasOpenTransferForm && ( + { + setIsRightAsideExpanded(false); + setHasOpenTransferForm(false); + }} + /> + )} + + + + + + + + + + + ); +}; diff --git a/packages/apps/rwa-demo/src/components/HomePage/OwnerRootPage.tsx b/packages/apps/rwa-demo/src/components/HomePage/OwnerRootPage.tsx index 7a1cf0d6c8..c9d7e0769b 100644 --- a/packages/apps/rwa-demo/src/components/HomePage/OwnerRootPage.tsx +++ b/packages/apps/rwa-demo/src/components/HomePage/OwnerRootPage.tsx @@ -3,6 +3,7 @@ import { AddAgentForm } from '@/components/AddAgentForm/AddAgentForm'; import { AgentsList } from '@/components/AgentsList/AgentsList'; import { SetComplianceForm } from '@/components/SetComplianceForm/SetComplianceForm'; import { SideBarBreadcrumbs } from '@/components/SideBarBreadcrumbs/SideBarBreadcrumbs'; +import { useAsset } from '@/hooks/asset'; import { MonoAdd } from '@kadena/kode-icons'; import { Button, Stack } from '@kadena/kode-ui'; import { useLayout } from '@kadena/kode-ui/patterns'; @@ -10,7 +11,7 @@ import { useState } from 'react'; export const OwnerRootPage = () => { const { setIsRightAsideExpanded, isRightAsideExpanded } = useLayout(); - + const { paused } = useAsset(); const [hasOpenAgentForm, setHasOpenAgentForm] = useState(false); const [hasOpenComplianceForm, setHasOpenComplianceForm] = useState(false); @@ -48,11 +49,19 @@ export const OwnerRootPage = () => { )} - - diff --git a/packages/apps/rwa-demo/src/components/InvestorFrozenMessage/InvestorFrozenMessage.tsx b/packages/apps/rwa-demo/src/components/InvestorFrozenMessage/InvestorFrozenMessage.tsx new file mode 100644 index 0000000000..0ae8b6eb17 --- /dev/null +++ b/packages/apps/rwa-demo/src/components/InvestorFrozenMessage/InvestorFrozenMessage.tsx @@ -0,0 +1,18 @@ +import { useFreeze } from '@/hooks/freeze'; +import { Notification } from '@kadena/kode-ui'; +import type { FC } from 'react'; + +interface IProps { + investorAccount: string; +} + +export const InvestorFrozenMessage: FC = ({ investorAccount }) => { + const { frozen } = useFreeze({ investorAccount }); + + if (!frozen) return; + return ( + + The investor account is frozen + + ); +}; diff --git a/packages/apps/rwa-demo/src/components/InvestorInfo/InvestorInfo.tsx b/packages/apps/rwa-demo/src/components/InvestorInfo/InvestorInfo.tsx new file mode 100644 index 0000000000..57cc0467c7 --- /dev/null +++ b/packages/apps/rwa-demo/src/components/InvestorInfo/InvestorInfo.tsx @@ -0,0 +1,37 @@ +import { useFreeze } from '@/hooks/freeze'; +import { MonoPause, MonoPlayArrow } from '@kadena/kode-icons'; +import { Button, Heading, Stack } from '@kadena/kode-ui'; +import type { FC } from 'react'; +import React from 'react'; +import { InvestorBalance } from '../InvestorBalance/InvestorBalance'; + +interface IProps { + investorAccount: string; +} + +export const InvestorInfo: FC = ({ investorAccount }) => { + const { frozen } = useFreeze({ investorAccount }); + + return ( + + investor: {investorAccount} + + + + + + + ); +}; diff --git a/packages/apps/rwa-demo/src/components/TransactionsProvider/TransactionsProvider.tsx b/packages/apps/rwa-demo/src/components/TransactionsProvider/TransactionsProvider.tsx index 9ba7c71736..2d4a006425 100644 --- a/packages/apps/rwa-demo/src/components/TransactionsProvider/TransactionsProvider.tsx +++ b/packages/apps/rwa-demo/src/components/TransactionsProvider/TransactionsProvider.tsx @@ -28,12 +28,26 @@ export const TransactionsContext = createContext({ getTransactions: () => [], }); -const interpretErrorMessage = (result: any, data: ITransaction): string => { - if (result.result.error?.message?.includes('Insert: row found for key')) { - return `{data.type}: This key already exists`; +const interpretMessage = (str: string, data?: ITransaction): string => { + if (str?.includes('Insert: row found for key')) { + return `${data?.type}: This key already exists`; + } + if (str?.includes('buy gas failed')) { + return `This account does not have enough balance to pay for Gas`; + } + + return `${data?.type}: ${str}`; +}; + +export const interpretErrorMessage = ( + result: any, + data?: ITransaction, +): string => { + if (typeof result === 'string') { + return interpretMessage(result); } - return `${data.type}: ${result.result.error.message}`; + return interpretMessage(result.result.error?.message!, data); }; export const TransactionsProvider: FC = ({ children }) => { diff --git a/packages/apps/rwa-demo/src/components/TransferForm/TransferForm.tsx b/packages/apps/rwa-demo/src/components/TransferForm/TransferForm.tsx index 4113755f5b..a11ca276ec 100644 --- a/packages/apps/rwa-demo/src/components/TransferForm/TransferForm.tsx +++ b/packages/apps/rwa-demo/src/components/TransferForm/TransferForm.tsx @@ -1,18 +1,9 @@ +import { useAccount } from '@/hooks/account'; +import { useAsset } from '@/hooks/asset'; import { useGetInvestors } from '@/hooks/getInvestors'; import { useTransferTokens } from '@/hooks/transferTokens'; import type { ITransferTokensProps } from '@/services/transferTokens'; -import type { IUnsignedCommand } from '@kadena/client'; -import { - Button, - Dialog, - DialogContent, - DialogFooter, - DialogHeader, - Select, - SelectItem, - TextareaField, - TextField, -} from '@kadena/kode-ui'; +import { Button, Select, SelectItem, TextField } from '@kadena/kode-ui'; import { RightAside, RightAsideContent, @@ -20,67 +11,58 @@ import { RightAsideHeader, } from '@kadena/kode-ui/patterns'; import type { FC } from 'react'; -import { useRef, useState } from 'react'; +import { useEffect, useState } from 'react'; import { Controller, useForm } from 'react-hook-form'; +import { AssetPausedMessage } from '../AssetPausedMessage/AssetPausedMessage'; interface IProps { onClose: () => void; - investorAccount: string; } -export const TransferForm: FC = ({ onClose, investorAccount }) => { +export const TransferForm: FC = ({ onClose }) => { + const { paused } = useAsset(); + const [balance, setBalance] = useState(0); + const { account, getBalance } = useAccount(); const { data: investors } = useGetInvestors(); - const textareaRef = useRef(null); - - const { createTx, submit } = useTransferTokens(); - const [tx, setTx] = useState(); - const [openModal, setOpenModal] = useState(false); + const { submit } = useTransferTokens(); - const { register, control, handleSubmit } = useForm({ + const { + register, + control, + handleSubmit, + formState: { errors }, + } = useForm({ values: { amount: 0, - investorFromAccount: investorAccount, + investorFromAccount: account?.address!, investorToAccount: '', }, }); - const handleSign = async () => { - const value = textareaRef.current?.value as unknown as IUnsignedCommand; - console.log(value); - await submit(value); - }; - const onSubmit = async (data: ITransferTokensProps) => { - const result = await createTx(data); - setTx(result); - setOpenModal(true); + await submit(data); + onClose(); }; const filteredInvestors = investors.filter( - (i) => i.accountName !== investorAccount, + (i) => i.accountName !== account?.address, ); + const init = async () => { + const res = await getBalance(); + setBalance(res); + }; + + useEffect(() => { + // eslint-disable-next-line @typescript-eslint/no-floating-promises + init(); + }, []); + + if (!account) return; + + console.log({ errors }); return ( <> - {openModal && ( - { - setOpenModal(false); - }} - > - Transaction - - - - - - - - )}
@@ -88,19 +70,38 @@ export const TransferForm: FC = ({ onClose, investorAccount }) => { (