From 9f4cb35a75c666a6ce2f6c383c8368d5718e8617 Mon Sep 17 00:00:00 2001 From: poi-son-ivy <146914988+poi-son-ivy@users.noreply.github.com> Date: Thu, 14 Mar 2024 07:12:12 -0700 Subject: [PATCH] API: Delete Token (#132) Signed-off-by: Ivy Astrix --- .../src/components/cards/hts/DeleteToken.tsx | 121 ++++++++++++++++++ .../packages/site/src/pages/index.tsx | 7 + .../packages/site/src/types/snap.ts | 4 + .../packages/site/src/utils/snap.ts | 32 +++++ .../packages/snap/snap.manifest.json | 2 +- .../{ => hts}/AssociateTokensCommand.ts | 6 +- .../commands/{ => hts}/BurnTokenCommand.ts | 6 +- .../commands/{ => hts}/CreateTokenCommand.ts | 8 +- .../src/commands/hts/DeleteTokenCommand.ts | 78 +++++++++++ .../{ => hts}/DissociateTokensCommand.ts | 6 +- .../commands/{ => hts}/MintTokenCommand.ts | 6 +- .../{ => hts}/AssociateTokensFacade.ts | 14 +- .../src/facades/{ => hts}/BurnTokenFacade.ts | 14 +- .../facades/{ => hts}/CreateTokenFacade.ts | 12 +- .../snap/src/facades/hts/DeleteTokenFacade.ts | 109 ++++++++++++++++ .../{ => hts}/DissociateTokensFacade.ts | 14 +- .../src/facades/{ => hts}/MintTokenFacade.ts | 14 +- .../packages/snap/src/index.ts | 22 +++- .../packages/snap/src/types/params.ts | 4 + .../packages/snap/src/utils/HederaUtils.ts | 23 ++++ 20 files changed, 446 insertions(+), 56 deletions(-) create mode 100644 packages/hedera-wallet-snap/packages/site/src/components/cards/hts/DeleteToken.tsx rename packages/hedera-wallet-snap/packages/snap/src/commands/{ => hts}/AssociateTokensCommand.ts (94%) rename packages/hedera-wallet-snap/packages/snap/src/commands/{ => hts}/BurnTokenCommand.ts (95%) rename packages/hedera-wallet-snap/packages/snap/src/commands/{ => hts}/CreateTokenCommand.ts (97%) create mode 100644 packages/hedera-wallet-snap/packages/snap/src/commands/hts/DeleteTokenCommand.ts rename packages/hedera-wallet-snap/packages/snap/src/commands/{ => hts}/DissociateTokensCommand.ts (92%) rename packages/hedera-wallet-snap/packages/snap/src/commands/{ => hts}/MintTokenCommand.ts (95%) rename packages/hedera-wallet-snap/packages/snap/src/facades/{ => hts}/AssociateTokensFacade.ts (91%) rename packages/hedera-wallet-snap/packages/snap/src/facades/{ => hts}/BurnTokenFacade.ts (92%) rename packages/hedera-wallet-snap/packages/snap/src/facades/{ => hts}/CreateTokenFacade.ts (95%) create mode 100644 packages/hedera-wallet-snap/packages/snap/src/facades/hts/DeleteTokenFacade.ts rename packages/hedera-wallet-snap/packages/snap/src/facades/{ => hts}/DissociateTokensFacade.ts (90%) rename packages/hedera-wallet-snap/packages/snap/src/facades/{ => hts}/MintTokenFacade.ts (92%) diff --git a/packages/hedera-wallet-snap/packages/site/src/components/cards/hts/DeleteToken.tsx b/packages/hedera-wallet-snap/packages/site/src/components/cards/hts/DeleteToken.tsx new file mode 100644 index 00000000..1e2781e9 --- /dev/null +++ b/packages/hedera-wallet-snap/packages/site/src/components/cards/hts/DeleteToken.tsx @@ -0,0 +1,121 @@ +/*- + * + * 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 { FC, useContext, useRef, useState } from 'react'; +import { + MetaMaskContext, + MetamaskActions, +} from '../../../contexts/MetamaskContext'; +import useModal from '../../../hooks/useModal'; +import { Account, DeleteTokenRequestParams } from '../../../types/snap'; +import { deleteToken, shouldDisplayReconnectButton } from '../../../utils'; +import { Card, SendHelloButton } from '../../base'; +import ExternalAccount, { + GetExternalAccountRef, +} from '../../sections/ExternalAccount'; + +type Props = { + network: string; + mirrorNodeUrl: string; + setAccountInfo: React.Dispatch>; +}; + +const DeleteToken: FC = ({ network, mirrorNodeUrl, setAccountInfo }) => { + const [state, dispatch] = useContext(MetaMaskContext); + const [loading, setLoading] = useState(false); + const { showModal } = useModal(); + const [tokenId, setTokenId] = useState(); + + const externalAccountRef = useRef(null); + + const handleDeleteTokenClick = async () => { + setLoading(true); + try { + const externalAccountParams = + externalAccountRef.current?.handleGetAccountParams(); + + const deleteTokenParams = { + tokenId, + } as DeleteTokenRequestParams; + + const response: any = await deleteToken( + network, + mirrorNodeUrl, + deleteTokenParams, + externalAccountParams, + ); + + const { receipt, currentAccount } = response; + + setAccountInfo(currentAccount); + console.log('receipt: ', receipt); + + showModal({ + title: 'Transaction Receipt', + content: JSON.stringify({ receipt }, null, 4), + }); + } catch (error) { + console.error(error); + dispatch({ type: MetamaskActions.SetError, payload: error }); + } + setLoading(false); + }; + + return ( + + + +
+ + ), + button: ( + + ), + }} + disabled={!state.installedSnap} + fullWidth={ + state.isFlask && + Boolean(state.installedSnap) && + !shouldDisplayReconnectButton(state.installedSnap) + } + /> + ); +}; + +export { DeleteToken }; diff --git a/packages/hedera-wallet-snap/packages/site/src/pages/index.tsx b/packages/hedera-wallet-snap/packages/site/src/pages/index.tsx index 920aa2e2..b8390f4e 100644 --- a/packages/hedera-wallet-snap/packages/site/src/pages/index.tsx +++ b/packages/hedera-wallet-snap/packages/site/src/pages/index.tsx @@ -57,6 +57,7 @@ import { import { MetaMaskContext, MetamaskActions } from '../contexts/MetamaskContext'; import { Account } from '../types/snap'; import { connectSnap, getSnap } from '../utils'; +import {DeleteToken} from "../components/cards/hts/DeleteToken"; const Index = () => { const [state, dispatch] = useContext(MetaMaskContext); @@ -206,6 +207,12 @@ const Index = () => { setAccountInfo={setAccountInfo} /> + + { + return await window.ethereum.request({ + method: 'wallet_invokeSnap', + params: { + snapId: defaultSnapOrigin, + request: { + method: 'hts/deleteToken', + params: { + network, + mirrorNodeUrl, + ...deleteTokenRequestParams, + ...externalAccountparams, + }, + }, + }, + }); +}; + /** * Invoke the "freezeAccount" method from the snap. * diff --git a/packages/hedera-wallet-snap/packages/snap/snap.manifest.json b/packages/hedera-wallet-snap/packages/snap/snap.manifest.json index dfe0da39..e5b3efdf 100644 --- a/packages/hedera-wallet-snap/packages/snap/snap.manifest.json +++ b/packages/hedera-wallet-snap/packages/snap/snap.manifest.json @@ -7,7 +7,7 @@ "url": "git+https://github.com/hashgraph/hedera-metamask-snaps.git" }, "source": { - "shasum": "kRIaknNsqGISYu34WSSYINHgQzaZJpZKPhNJDcposIw=", + "shasum": "CmjZhzdElS0nKOZTNa0JYjaBmNATyqvbF52KPjWqgPQ=", "location": { "npm": { "filePath": "dist/snap.js", diff --git a/packages/hedera-wallet-snap/packages/snap/src/commands/AssociateTokensCommand.ts b/packages/hedera-wallet-snap/packages/snap/src/commands/hts/AssociateTokensCommand.ts similarity index 94% rename from packages/hedera-wallet-snap/packages/snap/src/commands/AssociateTokensCommand.ts rename to packages/hedera-wallet-snap/packages/snap/src/commands/hts/AssociateTokensCommand.ts index 47fce7bd..2035d0d5 100644 --- a/packages/hedera-wallet-snap/packages/snap/src/commands/AssociateTokensCommand.ts +++ b/packages/hedera-wallet-snap/packages/snap/src/commands/hts/AssociateTokensCommand.ts @@ -19,9 +19,9 @@ */ import { AccountId, Client, TokenAssociateTransaction } from '@hashgraph/sdk'; -import { TxReceipt } from '../types/hedera'; -import { Utils } from '../utils/Utils'; -import { CryptoUtils } from '../utils/CryptoUtils'; +import { TxReceipt } from '../../types/hedera'; +import { Utils } from '../../utils/Utils'; +import { CryptoUtils } from '../../utils/CryptoUtils'; export class AssociateTokensCommand { readonly #tokenIds: string[]; diff --git a/packages/hedera-wallet-snap/packages/snap/src/commands/BurnTokenCommand.ts b/packages/hedera-wallet-snap/packages/snap/src/commands/hts/BurnTokenCommand.ts similarity index 95% rename from packages/hedera-wallet-snap/packages/snap/src/commands/BurnTokenCommand.ts rename to packages/hedera-wallet-snap/packages/snap/src/commands/hts/BurnTokenCommand.ts index d755763a..883859d0 100644 --- a/packages/hedera-wallet-snap/packages/snap/src/commands/BurnTokenCommand.ts +++ b/packages/hedera-wallet-snap/packages/snap/src/commands/hts/BurnTokenCommand.ts @@ -19,9 +19,9 @@ */ import { Client, TokenBurnTransaction } from '@hashgraph/sdk'; -import { TxReceipt } from '../types/hedera'; -import { CryptoUtils } from '../utils/CryptoUtils'; -import { Utils } from '../utils/Utils'; +import { TxReceipt } from '../../types/hedera'; +import { CryptoUtils } from '../../utils/CryptoUtils'; +import { Utils } from '../../utils/Utils'; export class BurnTokenCommand { readonly #assetType: 'TOKEN' | 'NFT'; diff --git a/packages/hedera-wallet-snap/packages/snap/src/commands/CreateTokenCommand.ts b/packages/hedera-wallet-snap/packages/snap/src/commands/hts/CreateTokenCommand.ts similarity index 97% rename from packages/hedera-wallet-snap/packages/snap/src/commands/CreateTokenCommand.ts rename to packages/hedera-wallet-snap/packages/snap/src/commands/hts/CreateTokenCommand.ts index 27494559..41a69dfc 100644 --- a/packages/hedera-wallet-snap/packages/snap/src/commands/CreateTokenCommand.ts +++ b/packages/hedera-wallet-snap/packages/snap/src/commands/hts/CreateTokenCommand.ts @@ -18,7 +18,7 @@ * */ -import { TokenCustomFee } from '../types/params'; +import { TokenCustomFee } from '../../types/params'; import { AccountId, Client, @@ -30,9 +30,9 @@ import { TokenSupplyType, TokenType, } from '@hashgraph/sdk'; -import { TxReceipt } from '../types/hedera'; -import { Utils } from '../utils/Utils'; -import { CryptoUtils } from '../utils/CryptoUtils'; +import { TxReceipt } from '../../types/hedera'; +import { Utils } from '../../utils/Utils'; +import { CryptoUtils } from '../../utils/CryptoUtils'; export class CreateTokenCommand { readonly #assetType: 'TOKEN' | 'NFT'; diff --git a/packages/hedera-wallet-snap/packages/snap/src/commands/hts/DeleteTokenCommand.ts b/packages/hedera-wallet-snap/packages/snap/src/commands/hts/DeleteTokenCommand.ts new file mode 100644 index 00000000..861e1728 --- /dev/null +++ b/packages/hedera-wallet-snap/packages/snap/src/commands/hts/DeleteTokenCommand.ts @@ -0,0 +1,78 @@ +/*- + * + * 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 { Client, PrivateKey, TokenDeleteTransaction } from '@hashgraph/sdk'; +import { TxReceipt } from '../../types/hedera'; +import { CryptoUtils } from '../../utils/CryptoUtils'; +import { Utils } from '../../utils/Utils'; + +export class DeleteTokenCommand { + readonly #tokenId: string; + + readonly #adminKey: PrivateKey; + + constructor(tokenId: string, adminKey: PrivateKey) { + this.#tokenId = tokenId; + this.#adminKey = adminKey; + } + + public async execute(client: Client): Promise { + const transaction = await new TokenDeleteTransaction() + .setTokenId(this.#tokenId) + .freezeWith(client) + .sign(this.#adminKey); + + const txResponse = await transaction.execute(client); + + const receipt = await txResponse.getReceipt(client); + + let newExchangeRate; + if (receipt.exchangeRate) { + newExchangeRate = { + ...receipt.exchangeRate, + expirationTime: Utils.timestampToString( + receipt.exchangeRate.expirationTime, + ), + }; + } + + return { + status: receipt.status.toString(), + accountId: receipt.accountId ? receipt.accountId.toString() : '', + fileId: receipt.fileId ? receipt.fileId : '', + contractId: receipt.contractId ? receipt.contractId : '', + topicId: receipt.topicId ? receipt.topicId : '', + tokenId: receipt.tokenId ? receipt.tokenId : '', + scheduleId: receipt.scheduleId ? receipt.scheduleId : '', + exchangeRate: newExchangeRate, + topicSequenceNumber: receipt.topicSequenceNumber + ? String(receipt.topicSequenceNumber) + : '', + topicRunningHash: CryptoUtils.uint8ArrayToHex(receipt.topicRunningHash), + totalSupply: receipt.totalSupply ? String(receipt.totalSupply) : '', + scheduledTransactionId: receipt.scheduledTransactionId + ? receipt.scheduledTransactionId.toString() + : '', + serials: JSON.parse(JSON.stringify(receipt.serials)), + duplicates: JSON.parse(JSON.stringify(receipt.duplicates)), + children: JSON.parse(JSON.stringify(receipt.children)), + } as TxReceipt; + } +} diff --git a/packages/hedera-wallet-snap/packages/snap/src/commands/DissociateTokensCommand.ts b/packages/hedera-wallet-snap/packages/snap/src/commands/hts/DissociateTokensCommand.ts similarity index 92% rename from packages/hedera-wallet-snap/packages/snap/src/commands/DissociateTokensCommand.ts rename to packages/hedera-wallet-snap/packages/snap/src/commands/hts/DissociateTokensCommand.ts index f60e01e3..12fdcda7 100644 --- a/packages/hedera-wallet-snap/packages/snap/src/commands/DissociateTokensCommand.ts +++ b/packages/hedera-wallet-snap/packages/snap/src/commands/hts/DissociateTokensCommand.ts @@ -1,7 +1,7 @@ import { AccountId, Client, TokenDissociateTransaction } from '@hashgraph/sdk'; -import { TxReceipt } from '../types/hedera'; -import { Utils } from '../utils/Utils'; -import { CryptoUtils } from '../utils/CryptoUtils'; +import { TxReceipt } from '../../types/hedera'; +import { Utils } from '../../utils/Utils'; +import { CryptoUtils } from '../../utils/CryptoUtils'; export class DissociateTokensCommand { readonly #tokenIds: string[]; diff --git a/packages/hedera-wallet-snap/packages/snap/src/commands/MintTokenCommand.ts b/packages/hedera-wallet-snap/packages/snap/src/commands/hts/MintTokenCommand.ts similarity index 95% rename from packages/hedera-wallet-snap/packages/snap/src/commands/MintTokenCommand.ts rename to packages/hedera-wallet-snap/packages/snap/src/commands/hts/MintTokenCommand.ts index 9b83a6dc..68e4ca41 100644 --- a/packages/hedera-wallet-snap/packages/snap/src/commands/MintTokenCommand.ts +++ b/packages/hedera-wallet-snap/packages/snap/src/commands/hts/MintTokenCommand.ts @@ -19,9 +19,9 @@ */ import { Client, TokenMintTransaction } from '@hashgraph/sdk'; -import { TxReceipt } from '../types/hedera'; -import { CryptoUtils } from '../utils/CryptoUtils'; -import { Utils } from '../utils/Utils'; +import { TxReceipt } from '../../types/hedera'; +import { CryptoUtils } from '../../utils/CryptoUtils'; +import { Utils } from '../../utils/Utils'; export class MintTokenCommand { readonly #assetType: 'TOKEN' | 'NFT'; diff --git a/packages/hedera-wallet-snap/packages/snap/src/facades/AssociateTokensFacade.ts b/packages/hedera-wallet-snap/packages/snap/src/facades/hts/AssociateTokensFacade.ts similarity index 91% rename from packages/hedera-wallet-snap/packages/snap/src/facades/AssociateTokensFacade.ts rename to packages/hedera-wallet-snap/packages/snap/src/facades/hts/AssociateTokensFacade.ts index ecf2d40f..7903be46 100644 --- a/packages/hedera-wallet-snap/packages/snap/src/facades/AssociateTokensFacade.ts +++ b/packages/hedera-wallet-snap/packages/snap/src/facades/hts/AssociateTokensFacade.ts @@ -18,16 +18,16 @@ * */ -import { WalletSnapParams, SnapDialogParams } from '../types/state'; -import { AssociateTokensRequestParams } from '../types/params'; -import { TxReceipt } from '../types/hedera'; +import { WalletSnapParams, SnapDialogParams } from '../../types/state'; +import { AssociateTokensRequestParams } from '../../types/params'; +import { TxReceipt } from '../../types/hedera'; import { divider, heading, text } from '@metamask/snaps-ui'; -import { CryptoUtils } from '../utils/CryptoUtils'; +import { CryptoUtils } from '../../utils/CryptoUtils'; import _ from 'lodash'; -import { SnapUtils } from '../utils/SnapUtils'; +import { SnapUtils } from '../../utils/SnapUtils'; import { providerErrors } from '@metamask/rpc-errors'; -import { HederaClientImplFactory } from '../client/HederaClientImplFactory'; -import { AssociateTokensCommand } from '../commands/AssociateTokensCommand'; +import { HederaClientImplFactory } from '../../client/HederaClientImplFactory'; +import { AssociateTokensCommand } from '../../commands/hts/AssociateTokensCommand'; export class AssociateTokensFacade { /** diff --git a/packages/hedera-wallet-snap/packages/snap/src/facades/BurnTokenFacade.ts b/packages/hedera-wallet-snap/packages/snap/src/facades/hts/BurnTokenFacade.ts similarity index 92% rename from packages/hedera-wallet-snap/packages/snap/src/facades/BurnTokenFacade.ts rename to packages/hedera-wallet-snap/packages/snap/src/facades/hts/BurnTokenFacade.ts index a017a500..468bcc3c 100644 --- a/packages/hedera-wallet-snap/packages/snap/src/facades/BurnTokenFacade.ts +++ b/packages/hedera-wallet-snap/packages/snap/src/facades/hts/BurnTokenFacade.ts @@ -21,13 +21,13 @@ import { providerErrors } from '@metamask/rpc-errors'; import { divider, heading, text } from '@metamask/snaps-ui'; import _ from 'lodash'; -import { HederaClientImplFactory } from '../client/HederaClientImplFactory'; -import { BurnTokenCommand } from '../commands/BurnTokenCommand'; -import { TxReceipt } from '../types/hedera'; -import { BurnTokenRequestParams } from '../types/params'; -import { SnapDialogParams, WalletSnapParams } from '../types/state'; -import { CryptoUtils } from '../utils/CryptoUtils'; -import { SnapUtils } from '../utils/SnapUtils'; +import { HederaClientImplFactory } from '../../client/HederaClientImplFactory'; +import { BurnTokenCommand } from '../../commands/hts/BurnTokenCommand'; +import { TxReceipt } from '../../types/hedera'; +import { BurnTokenRequestParams } from '../../types/params'; +import { SnapDialogParams, WalletSnapParams } from '../../types/state'; +import { CryptoUtils } from '../../utils/CryptoUtils'; +import { SnapUtils } from '../../utils/SnapUtils'; export class BurnTokenFacade { /** diff --git a/packages/hedera-wallet-snap/packages/snap/src/facades/CreateTokenFacade.ts b/packages/hedera-wallet-snap/packages/snap/src/facades/hts/CreateTokenFacade.ts similarity index 95% rename from packages/hedera-wallet-snap/packages/snap/src/facades/CreateTokenFacade.ts rename to packages/hedera-wallet-snap/packages/snap/src/facades/hts/CreateTokenFacade.ts index b7b266ff..2acae57e 100644 --- a/packages/hedera-wallet-snap/packages/snap/src/facades/CreateTokenFacade.ts +++ b/packages/hedera-wallet-snap/packages/snap/src/facades/hts/CreateTokenFacade.ts @@ -21,12 +21,12 @@ import { providerErrors } from '@metamask/rpc-errors'; import { divider, heading, text } from '@metamask/snaps-ui'; import _ from 'lodash'; -import { HederaClientImplFactory } from '../client/HederaClientImplFactory'; -import { CreateTokenCommand } from '../commands/CreateTokenCommand'; -import { TxReceipt } from '../types/hedera'; -import { CreateTokenRequestParams } from '../types/params'; -import { SnapDialogParams, WalletSnapParams } from '../types/state'; -import { SnapUtils } from '../utils/SnapUtils'; +import { HederaClientImplFactory } from '../../client/HederaClientImplFactory'; +import { CreateTokenCommand } from '../../commands/hts/CreateTokenCommand'; +import { TxReceipt } from '../../types/hedera'; +import { CreateTokenRequestParams } from '../../types/params'; +import { SnapDialogParams, WalletSnapParams } from '../../types/state'; +import { SnapUtils } from '../../utils/SnapUtils'; export class CreateTokenFacade { /** diff --git a/packages/hedera-wallet-snap/packages/snap/src/facades/hts/DeleteTokenFacade.ts b/packages/hedera-wallet-snap/packages/snap/src/facades/hts/DeleteTokenFacade.ts new file mode 100644 index 00000000..45636b1b --- /dev/null +++ b/packages/hedera-wallet-snap/packages/snap/src/facades/hts/DeleteTokenFacade.ts @@ -0,0 +1,109 @@ +import { WalletSnapParams, SnapDialogParams } from '../../types/state'; +import { DeleteTokenRequestParams } from '../../types/params'; +import { TxReceipt } from '../../types/hedera'; +import { divider, heading, text } from '@metamask/snaps-ui'; +import { CryptoUtils } from '../../utils/CryptoUtils'; +import _ from 'lodash'; +import { SnapUtils } from '../../utils/SnapUtils'; +import { providerErrors } from '@metamask/rpc-errors'; +import { HederaClientImplFactory } from '../../client/HederaClientImplFactory'; +import { DeleteTokenCommand } from '../../commands/hts/DeleteTokenCommand'; +import { PrivateKey } from '@hashgraph/sdk'; + +export class DeleteTokenFacade { + /** + * Deletes the provided Hedera token. + * + * @param walletSnapParams - Wallet snap params. + * @param deleteTokenRequestParams - Parameters for deleting tokens. + * @returns Receipt of the transaction. + */ + public static async deleteToken( + walletSnapParams: WalletSnapParams, + deleteTokenRequestParams: DeleteTokenRequestParams, + ): Promise { + const { origin, state } = walletSnapParams; + + const { tokenId } = deleteTokenRequestParams; + + const { hederaEvmAddress, hederaAccountId, network, mirrorNodeUrl } = + state.currentAccount; + + const { privateKey, curve } = + state.accountState[hederaEvmAddress][network].keyStore; + + let txReceipt = {} as TxReceipt; + try { + const panelToShow = [ + heading('Delete Token'), + text( + 'Are you sure you want to dissociate the following tokens from your account?', + ), + divider(), + ]; + + panelToShow.push(text(`Token ID: ${tokenId}`)); + panelToShow.push(divider()); + + panelToShow.push(text(`Asset Id: ${tokenId}`)); + const tokenInfo = await CryptoUtils.getTokenById(tokenId, mirrorNodeUrl); + if (_.isEmpty(tokenInfo)) { + const errMessage = `Error while trying to get token info for ${tokenId} from Hedera Mirror Nodes at this time`; + console.error(errMessage); + panelToShow.push(text(errMessage)); + panelToShow.push( + text(`Proceed only if you are sure this asset ID exists`), + ); + } else { + panelToShow.push(text(`Asset Name: ${tokenInfo.name}`)); + panelToShow.push(text(`Asset Type: ${tokenInfo.type}`)); + panelToShow.push(text(`Symbol: ${tokenInfo.symbol}`)); + panelToShow.push( + text( + `Total Supply: ${( + Number(tokenInfo.total_supply) / + Math.pow(10, Number(tokenInfo.decimals)) + ).toString()}`, + ), + ); + } + panelToShow.push(text(tokenId)); + panelToShow.push(divider()); + + const dialogParams: SnapDialogParams = { + type: 'confirmation', + content: await SnapUtils.generateCommonPanel(origin, panelToShow), + }; + const confirmed = await SnapUtils.snapDialog(dialogParams); + if (!confirmed) { + console.error(`User rejected the transaction`); + throw providerErrors.userRejectedRequest(); + } + + const hederaClientFactory = new HederaClientImplFactory( + hederaAccountId, + network, + curve, + privateKey, + ); + + const hederaClient = await hederaClientFactory.createClient(); + if (hederaClient === null) { + throw new Error('hedera client returned null'); + } + const deleteTokensCommand = new DeleteTokenCommand( + tokenId, + PrivateKey.fromStringECDSA(privateKey), + ); + txReceipt = await deleteTokensCommand.execute(hederaClient.getClient()); + } catch (error: any) { + const errMessage = `Error while trying to delete token ${tokenId} from the account: ${String( + error, + )}`; + console.error(errMessage); + throw providerErrors.unsupportedMethod(errMessage); + } + + return txReceipt; + } +} diff --git a/packages/hedera-wallet-snap/packages/snap/src/facades/DissociateTokensFacade.ts b/packages/hedera-wallet-snap/packages/snap/src/facades/hts/DissociateTokensFacade.ts similarity index 90% rename from packages/hedera-wallet-snap/packages/snap/src/facades/DissociateTokensFacade.ts rename to packages/hedera-wallet-snap/packages/snap/src/facades/hts/DissociateTokensFacade.ts index 9fd07b93..4b71fe54 100644 --- a/packages/hedera-wallet-snap/packages/snap/src/facades/DissociateTokensFacade.ts +++ b/packages/hedera-wallet-snap/packages/snap/src/facades/hts/DissociateTokensFacade.ts @@ -1,13 +1,13 @@ -import { WalletSnapParams, SnapDialogParams } from '../types/state'; -import { DissociateTokensRequestParams } from '../types/params'; -import { TxReceipt } from '../types/hedera'; +import { WalletSnapParams, SnapDialogParams } from '../../types/state'; +import { DissociateTokensRequestParams } from '../../types/params'; +import { TxReceipt } from '../../types/hedera'; import { divider, heading, text } from '@metamask/snaps-ui'; -import { CryptoUtils } from '../utils/CryptoUtils'; +import { CryptoUtils } from '../../utils/CryptoUtils'; import _ from 'lodash'; -import { SnapUtils } from '../utils/SnapUtils'; +import { SnapUtils } from '../../utils/SnapUtils'; import { providerErrors } from '@metamask/rpc-errors'; -import { HederaClientImplFactory } from '../client/HederaClientImplFactory'; -import { DissociateTokensCommand } from '../commands/DissociateTokensCommand'; +import { HederaClientImplFactory } from '../../client/HederaClientImplFactory'; +import { DissociateTokensCommand } from '../../commands/hts/DissociateTokensCommand'; export class DissociateTokensFacade { /** diff --git a/packages/hedera-wallet-snap/packages/snap/src/facades/MintTokenFacade.ts b/packages/hedera-wallet-snap/packages/snap/src/facades/hts/MintTokenFacade.ts similarity index 92% rename from packages/hedera-wallet-snap/packages/snap/src/facades/MintTokenFacade.ts rename to packages/hedera-wallet-snap/packages/snap/src/facades/hts/MintTokenFacade.ts index f39f8305..b2c0a7fc 100644 --- a/packages/hedera-wallet-snap/packages/snap/src/facades/MintTokenFacade.ts +++ b/packages/hedera-wallet-snap/packages/snap/src/facades/hts/MintTokenFacade.ts @@ -21,13 +21,13 @@ import { providerErrors } from '@metamask/rpc-errors'; import { divider, heading, text } from '@metamask/snaps-ui'; import _ from 'lodash'; -import { HederaClientImplFactory } from '../client/HederaClientImplFactory'; -import { MintTokenCommand } from '../commands/MintTokenCommand'; -import { TxReceipt } from '../types/hedera'; -import { MintTokenRequestParams } from '../types/params'; -import { SnapDialogParams, WalletSnapParams } from '../types/state'; -import { CryptoUtils } from '../utils/CryptoUtils'; -import { SnapUtils } from '../utils/SnapUtils'; +import { HederaClientImplFactory } from '../../client/HederaClientImplFactory'; +import { MintTokenCommand } from '../../commands/hts/MintTokenCommand'; +import { TxReceipt } from '../../types/hedera'; +import { MintTokenRequestParams } from '../../types/params'; +import { SnapDialogParams, WalletSnapParams } from '../../types/state'; +import { CryptoUtils } from '../../utils/CryptoUtils'; +import { SnapUtils } from '../../utils/SnapUtils'; export class MintTokenFacade { /** diff --git a/packages/hedera-wallet-snap/packages/snap/src/index.ts b/packages/hedera-wallet-snap/packages/snap/src/index.ts index 4390b211..46c0dbd2 100644 --- a/packages/hedera-wallet-snap/packages/snap/src/index.ts +++ b/packages/hedera-wallet-snap/packages/snap/src/index.ts @@ -25,16 +25,14 @@ import { divider, heading, panel, text } from '@metamask/snaps-ui'; import _ from 'lodash'; import { SignMessageCommand } from './commands/SignMessageCommand'; import { ApproveAllowanceFacade } from './facades/ApproveAllowanceFacade'; -import { AssociateTokensFacade } from './facades/AssociateTokensFacade'; -import { BurnTokenFacade } from './facades/BurnTokenFacade'; -import { CreateTokenFacade } from './facades/CreateTokenFacade'; import { DeleteAccountFacade } from './facades/DeleteAccountFacade'; import { DeleteAllowanceFacade } from './facades/DeleteAllowanceFacade'; -import { DissociateTokensFacade } from './facades/DissociateTokensFacade'; +import { AssociateTokensFacade } from './facades/hts/AssociateTokensFacade'; +import { CreateTokenFacade } from './facades/hts/CreateTokenFacade'; +import { DissociateTokensFacade } from './facades/hts/DissociateTokensFacade'; import { FreezeAccountFacade } from './facades/FreezeAccountFacade'; import { GetAccountBalanceFacade } from './facades/GetAccountBalanceFacade'; import { GetAccountInfoFacade } from './facades/GetAccountInfoFacade'; -import { MintTokenFacade } from './facades/MintTokenFacade'; import { StakeHbarFacade } from './facades/StakeHbarFacade'; import { TransferCryptoFacade } from './facades/TransferCryptoFacade'; import { WipeTokenFacade } from './facades/WipeTokenFacade'; @@ -44,6 +42,9 @@ import { HederaTransactionsStrategy } from './strategies/HederaTransactionsStrat import { StakeHbarRequestParams } from './types/params'; import { WalletSnapParams } from './types/state'; import { HederaUtils } from './utils/HederaUtils'; +import { MintTokenFacade } from './facades/hts/MintTokenFacade'; +import { BurnTokenFacade } from './facades/hts/BurnTokenFacade'; +import { DeleteTokenFacade } from './facades/hts/DeleteTokenFacade'; /** * Handle incoming JSON-RPC requests, sent through `wallet_invokeSnap`. @@ -303,6 +304,17 @@ export const onRpcRequest: OnRpcRequestHandler = async ({ }; } + case 'hts/deleteToken': { + HederaUtils.isValidDeleteTokenParams(request.params); + return { + currentAccount: state.currentAccount, + receipt: await DeleteTokenFacade.deleteToken( + walletSnapParams, + request.params, + ), + }; + } + default: throw providerErrors.unsupportedMethod(); } diff --git a/packages/hedera-wallet-snap/packages/snap/src/types/params.ts b/packages/hedera-wallet-snap/packages/snap/src/types/params.ts index 1f4097ab..cef8adff 100644 --- a/packages/hedera-wallet-snap/packages/snap/src/types/params.ts +++ b/packages/hedera-wallet-snap/packages/snap/src/types/params.ts @@ -120,6 +120,10 @@ export type BurnTokenRequestParams = { serialNumbers?: number[]; }; +export type DeleteTokenRequestParams = { + tokenId: string; +}; + export type AssociateTokensRequestParams = { tokenIds: string[]; }; diff --git a/packages/hedera-wallet-snap/packages/snap/src/utils/HederaUtils.ts b/packages/hedera-wallet-snap/packages/snap/src/utils/HederaUtils.ts index e6a8f000..4a79d857 100644 --- a/packages/hedera-wallet-snap/packages/snap/src/utils/HederaUtils.ts +++ b/packages/hedera-wallet-snap/packages/snap/src/utils/HederaUtils.ts @@ -48,6 +48,7 @@ import { CreateTokenRequestParams, DeleteAccountRequestParams, DeleteAllowanceRequestParams, + DeleteTokenRequestParams, DissociateTokensRequestParams, FreezeAccountRequestParams, GetAccountInfoRequestParams, @@ -1480,6 +1481,28 @@ export class HederaUtils { } } + /** + * Check Validation of deleteToken request. + * + * @param params - Request params. + */ + public static isValidDeleteTokenParams( + params: unknown, + ): asserts params is DeleteTokenRequestParams { + if (params === null || _.isEmpty(params) || !('tokenId' in params)) { + console.error( + 'Invalid deleteToken Params passed. "tokenId" must be passed as a parameter', + ); + throw providerErrors.unsupportedMethod( + 'Invalid deleteToken Params passed. "tokenId" must be passed as a parameter', + ); + } + + const parameter = params as DeleteTokenRequestParams; + + HederaUtils.checkValidString(parameter, 'deleteToken', 'tokenId', true); + } + /** * Check Validation of freezeAccount/unfreezeAccount request. *