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

dapp-feat: TokenGeneralInfo feature complete (#318) #374

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
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
/*-
*
* Hedera Smart Contracts
*
* Copyright (C) 2023 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

import Link from 'next/link';
import React, { Dispatch, SetStateAction } from 'react';
import { convertCalmelCaseFunctionName } from '@/utils/common/helpers';
import { prepareInfoValuesToShow } from '../methods/prepareInfoValuesToShow';
import {
Tr,
Th,
Td,
Modal,
Table,
Tbody,
ModalBody,
ModalFooter,
ModalHeader,
ModalOverlay,
ModalContent,
TableContainer,
ModalCloseButton,
} from '@chakra-ui/react';
import {
TOKEN_INFO_ADVANCED_KEYS,
TOKEN_INFO_BASIC_KEYS,
TOKEN_INFO_NFT_KEYS,
} from '@/utils/contract-interactions/HTS/token-query/constant';

interface PageProps {
tokenInfo: any;
isOpen: boolean;
onClose: () => void;
hederaNetwork: string;
initialParamValues?: any;
hederaTokenAddress: string;
setTokenInfo?: Dispatch<any>;
setParamValues?: Dispatch<any>;
setShowToken?: Dispatch<SetStateAction<boolean>>;
setIsSuccessful?: Dispatch<SetStateAction<boolean>>;
APIMethods: 'TOKEN' | 'FUNGIBLE' | 'NON_FUNFIBLE';
}

const TokenGeneralInfoModal = ({
isOpen,
onClose,
tokenInfo,
APIMethods,
setTokenInfo,
setShowToken,
hederaNetwork,
setParamValues,
setIsSuccessful,
initialParamValues,
hederaTokenAddress,
}: PageProps) => {
const commonBasicKeysTokenInfo =
APIMethods === 'TOKEN' ? tokenInfo.token : tokenInfo.tokenInfo.token;

const commonAdvancedKeysTokenInfo = APIMethods === 'TOKEN' ? tokenInfo : tokenInfo.tokenInfo;

return (
<Modal
isOpen={isOpen}
onClose={() => {
onClose();
if (setTokenInfo) setTokenInfo({});
if (setShowToken) setShowToken(false);
if (setIsSuccessful) setIsSuccessful(false);
if (setParamValues) setParamValues(initialParamValues);
}}
isCentered
>
<ModalOverlay />
<ModalContent
className="h-fit flex flex-col gap-3 rounded-xl drop-shadow-xl
bg-secondary text-white font-styrene w-[30rem]"
>
{/* title */}
<ModalHeader>Token Information</ModalHeader>
<ModalCloseButton />

{/* break line */}
<hr className="border-t border-white/40 -mt-3" />

{/* body */}
<ModalBody>
<TableContainer className="flex flex-col gap-3 overflow-x-hidden">
<Table variant="simple" size={'sm'}>
<Tbody>
{/* basic keys */}
{TOKEN_INFO_BASIC_KEYS.map((key) => {
const keyToShow = convertCalmelCaseFunctionName(key);
const valueToShow = prepareInfoValuesToShow(key, commonBasicKeysTokenInfo);

return (
<Tr key={key}>
<Th color={'#82ACF9'}>{keyToShow}</Th>
<Td color={'#82ACF9'} className="flex justify-end text-white">
{valueToShow}
</Td>
</Tr>
);
})}

{/* advanced keys */}
{TOKEN_INFO_ADVANCED_KEYS.map((key) => {
const keyToShow = convertCalmelCaseFunctionName(key);
const valueToShow = prepareInfoValuesToShow(key, commonAdvancedKeysTokenInfo);

return (
<Tr key={key}>
<Th color={'#82ACF9'}>{keyToShow}</Th>
<Td color={'#82ACF9'} className="flex justify-end text-white">
{valueToShow}
</Td>
</Tr>
);
})}

{/* Fungible extra keys */}
{APIMethods === 'FUNGIBLE' && (
<Tr>
<Th color={'#82ACF9'}>Decimals</Th>
<Td color={'#82ACF9'} className="flex justify-end text-white">
{tokenInfo.decimals.toString()}
</Td>
</Tr>
)}

{/* Non-Fungible extra keys */}
{APIMethods === 'NON_FUNFIBLE' &&
TOKEN_INFO_NFT_KEYS.map((key) => {
const keyToShow = convertCalmelCaseFunctionName(key);
const valueToShow = prepareInfoValuesToShow(key, tokenInfo);
return (
<Tr key={key}>
<Th color={'#82ACF9'}>{keyToShow}</Th>
<Td color={'#82ACF9'} className="flex justify-end text-white">
{valueToShow}
</Td>
</Tr>
);
})}
</Tbody>
</Table>
</TableContainer>

{/* tip */}
<p className="text-sm whitespace-normal pt-3">
**note: For a more comprehensive view of this token&apos;s information,{' '}
<Link
href={`https://hashscan.io/${hederaNetwork}/token/${hederaTokenAddress}`}
target="_blank"
className=" text-hedera-purple underline"
>
explore it on HashScan
</Link>
.
</p>
</ModalBody>

{/* footer */}
<ModalFooter className="flex flex-col gap-3">
<button
onClick={() => {
onClose();
if (setTokenInfo) setTokenInfo({});
if (setShowToken) setShowToken(false);
if (setIsSuccessful) setIsSuccessful(false);
if (setParamValues) setParamValues(initialParamValues);
}}
className="border border-button-stroke-violet px-6 py-2 rounded-lg font-medium hover:bg-button-stroke-violet hover:text-white transition duration-300"
>
Acknowledge
</button>
</ModalFooter>
</ModalContent>
</Modal>
);
};

export default TokenGeneralInfoModal;
Original file line number Diff line number Diff line change
Expand Up @@ -24,42 +24,61 @@ import { FiExternalLink } from 'react-icons/fi';
import { AiOutlineMinus } from 'react-icons/ai';
import { Dispatch, SetStateAction } from 'react';
import { MdNavigateBefore, MdNavigateNext } from 'react-icons/md';
import { TransactionResult } from '@/types/contract-interactions/HTS';
import { IHederaTokenServiceKeyType, TransactionResult } from '@/types/contract-interactions/HTS';
import {
TableContainer,
Table,
Thead,
Tr,
Th,
Tbody,
Td,
Table,
Thead,
Tbody,
Popover,
PopoverTrigger,
Tooltip,
TableContainer,
PopoverTrigger,
PopoverContent,
} from '@chakra-ui/react';

/** @dev shared component representing the list of transactions */
interface TransactionResultTablePageProps {
onOpen?: () => void;
hederaNetwork: string;
TRANSACTION_PAGE_SIZE: number;
currentTransactionPage: number;
transactionResultStorageKey: string;
transactionResults: TransactionResult[];
setTokenInfoFromTxResult?: Dispatch<any>;
paginatedTransactionResults: TransactionResult[];
setShowTokenInfo?: Dispatch<SetStateAction<boolean>>;
setAPIMethodsFromTxResult?: Dispatch<SetStateAction<any>>;
setCurrentTransactionPage: Dispatch<SetStateAction<number>>;
setTokenAddressFromTxResult?: Dispatch<SetStateAction<string>>;
setTransactionResults: Dispatch<SetStateAction<TransactionResult[]>>;
API: 'TokenCreate' | 'TokenMint' | 'TokenAssociate' | 'GrantKYC' | 'QueryValidity';
setKeyTypeFromTxResult?: Dispatch<SetStateAction<IHederaTokenServiceKeyType>>;
API:
| 'TokenCreate'
| 'TokenMint'
| 'TokenAssociate'
| 'GrantKYC'
| 'QueryValidity'
| 'QueryTokenGeneralInfo'
| 'QuerySpecificInfo';
}

export const TransactionResultTable = ({
API,
onOpen,
hederaNetwork,
setShowTokenInfo,
transactionResults,
TRANSACTION_PAGE_SIZE,
setTransactionResults,
TRANSACTION_PAGE_SIZE,
currentTransactionPage,
setKeyTypeFromTxResult,
setTokenInfoFromTxResult,
setAPIMethodsFromTxResult,
setCurrentTransactionPage,
setTokenAddressFromTxResult,
transactionResultStorageKey,
paginatedTransactionResults,
}: TransactionResultTablePageProps) => {
Expand All @@ -70,11 +89,13 @@ export const TransactionResultTable = ({
endingHashIndex = -12;
break;
case 'TokenMint':
case 'QuerySpecificInfo':
case 'QueryTokenGeneralInfo':
beginingHashIndex = 8;
endingHashIndex = -4;
break;
case 'TokenAssociate':
case 'GrantKYC':
case 'TokenAssociate':
case 'QueryValidity':
beginingHashIndex = 10;
endingHashIndex = -5;
Expand All @@ -96,6 +117,12 @@ export const TransactionResultTable = ({
{API === 'TokenAssociate' && <Th color={'#82ACF9'}>Associated Account</Th>}
{API === 'GrantKYC' && <Th color={'#82ACF9'}>KYCed Account</Th>}
{API === 'QueryValidity' && <Th color={'#82ACF9'}>Valid Token</Th>}
{(API === 'QueryTokenGeneralInfo' || API === 'QuerySpecificInfo') && (
<Th color={'#82ACF9'}>Token Info</Th>
)}
{(API === 'QueryTokenGeneralInfo' || API === 'QuerySpecificInfo') && (
<Th color={'#82ACF9'}>API called</Th>
)}
<Th />
</Tr>
</Thead>
Expand Down Expand Up @@ -179,7 +206,9 @@ export const TransactionResultTable = ({
{(API === 'TokenCreate' ||
API === 'TokenMint' ||
API === 'GrantKYC' ||
API === 'QueryValidity') && (
API === 'QueryValidity' ||
API === 'QueryTokenGeneralInfo' ||
API === 'QuerySpecificInfo') && (
<Td className="cursor-pointer">
{transactionResult.tokenAddress ? (
<div className="flex gap-1 items-center">
Expand Down Expand Up @@ -403,6 +432,57 @@ export const TransactionResultTable = ({
</Td>
)}

{/* query - token info */}
{(API === 'QueryTokenGeneralInfo' || API === 'QuerySpecificInfo') && (
<Td className="cursor-pointer">
<div className="flex gap-1 items-center">
{transactionResult.tokenInfo ? (
<div
onClick={() => {
onOpen!();
if (setShowTokenInfo) setShowTokenInfo(true);
if (setTokenInfoFromTxResult)
setTokenInfoFromTxResult(transactionResult.tokenInfo);
if (setAPIMethodsFromTxResult)
setAPIMethodsFromTxResult(transactionResult.APICalled);
if (setKeyTypeFromTxResult)
setKeyTypeFromTxResult(transactionResult.keyTypeCalled);
if (setTokenAddressFromTxResult)
setTokenAddressFromTxResult(transactionResult.tokenAddress as string);
}}
>
<div className="flex gap-1 items-center">
<Tooltip label="click to show token info">
<p>Token Info</p>
</Tooltip>
</div>
</div>
) : (
<>NULL</>
)}
</div>
</Td>
)}

{/* query - API called */}
{(API === 'QueryTokenGeneralInfo' || API === 'QuerySpecificInfo') && (
<Td>
{transactionResult.APICalled ? (
<>
<p>
{transactionResult.APICalled === 'TOKEN_KEYS'
? `${transactionResult.APICalled.replace('TOKEN_', '')}_${
transactionResult.keyTypeCalled
}`
: transactionResult.APICalled}
</p>
</>
) : (
<>NULL</>
)}
</Td>
)}

{/* delete button */}
<Td>
<Tooltip label="delete this record" placement="top">
Expand Down
Loading