/*- * * Hedera Wallet Snap * * Copyright (C) 2024 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 type { SnapComponent } from '@metamask/snaps-sdk/jsx'; import type { AccountBalance, SimpleTransfer } from '../types/hedera'; import { CryptoUtils } from '../utils/CryptoUtils'; import { Bold, Box, CommonUI, Copyable, Divider, Heading, Row, Text, type CommonProps, } from './common'; type Props = CommonProps & { memo: string; maxFee: number | null; transfers: SimpleTransfer[]; walletBalance: AccountBalance; serviceFeesToPay: Record; mirrorNodeUrl: string; // Added for fetching token info }; const MetamaskUI: SnapComponent = ({ origin, network, mirrorNodeUrl, memo, maxFee, transfers, walletBalance, serviceFeesToPay, }) => { return ( Transfer Crypto Are you sure you want to execute the following transaction(s)? {memo && ( Memo )} {maxFee && ( {maxFee} Hbar )} Transfers: {transfers.map((transfer, index) => { const txNumber = index + 1; const asset = transfer.assetType === 'HBAR' ? 'HBAR' : ''; const feeToDisplay = 0; return ( Transaction #{txNumber} {transfer.from && ( Transaction Type: Delegated Transfer Owner Account Id: )} Asset Type: {transfer.assetType} {transfer.assetType === 'HBAR' ? ( {walletBalance.hbars < transfer.amount + (serviceFeesToPay.HBAR || 0) && ( There is not enough Hbar in the wallet to transfer the requested amount Proceed only if you are sure about the amount being transferred )} Asset: HBAR ) : ( {/* Fetch token info if it's not HBAR */} {async () => { transfer.decimals = walletBalance.tokens[ transfer.assetId as string ] ? walletBalance.tokens[transfer.assetId as string].decimals : NaN; const tokenInfo = await CryptoUtils.getTokenById( transfer.assetId as string, mirrorNodeUrl, ); if (tokenInfo) { return ( Asset Name: {tokenInfo.name} Asset Type: {tokenInfo.type} Symbol: {tokenInfo.symbol} {serviceFeesToPay[transfer.assetType] > 0 ? ( Fee: {serviceFeesToPay[transfer.assetType]} ) : ( Fee: {serviceFeesToPay[transfer.assetId as string]} )} {(transfer.decimals = Number(tokenInfo.decimals))} ); } return ( Error while trying to get token info for{' '} {transfer.assetId} from Hedera Mirror Nodes at this time Proceed only if you are sure about the asset ID being transferred ); }} {transfer.assetType === 'NFT' && ( NFT Serial Number: {transfer.assetId?.split('/')[1]} )} )} To: Amount: {transfer.amount} {asset} {feeToDisplay > 0 && ( <> Service Fee: {feeToDisplay} Total: {transfer.amount + feeToDisplay} )} ); })} ); }; export const TransferCryptoUI = MetamaskUI as any;