Last updated
Last updated
Explore DApp development using the Mirror Node API and Hedera Token Service (HTS). Discover how to integrate HTS functionality into your DApp for seamless token management and transactions. This guide uses React, Material UI, Ethers, and TypeScript with the integrated with walletconnect, streamlining your development process.
Before you begin, you should be familiar with the following:
This custom template eliminates setup overhead and allows you to dive straight into the core features of your project.
Open a terminal and run the following command to set up your project structure, replacing my-app-name
with your desired directory name.
Open your newly created react app project with visual studio code. You should see the following file structure.
In vscode open the file located at src/services/wallets/mirrorNodeClient.ts
.
This file creates a mirror node client and is used to fetch data from the mirror nodes. We will add new code to help us obtain information about the tokens we currently own.
This client is configured for the Hedera Testnet. For further configuration, go to src/config/network.ts.
We'll use the Mirror Node API to query information about the tokens we currently own and the quantities of those tokens.
Open src/services/wallets/mirrorNodeClient.ts
and paste the below interface outside of and above the MirrorNodeClient
class.
This interface defines the data fields we need for our DApp, filtering out any extra data from the mirror node response.
Paste the below HTTP GET request outside of and below the MirrorNodeClient
class in the src/services/wallets/mirrorNodeClient.ts
file.
In the previous step we wrote code to obtain the current token balance of an account. Next we will retieve the type of token (Non-Fungible or Fungible), decimal precision, token name and symbol.
Open src/services/wallets/mirrorNodeClient.ts
and paste the interface outside of and above the MirrorNodeClient
class.
Paste the below HTTP GET request outside of and below the getAccountTokenBalances
function in the src/services/wallets/mirrorNodeClient.ts
file.
In the previous step we wrote code to obtain the token details (token type, decimals, name, and symbol). Next we will retrieve the NFT serial numbers that are owned.
Open src/services/wallets/mirrorNodeClient.ts
and paste the interface outside of and above the MirrorNodeClient
class.
Paste the below HTTP GET request outside of and below the getTokenInfo
function in the src/services/wallets/mirrorNodeClient.ts
file.
We need to combine all of our HTTP response data in order to display our available tokens in our DApp.
Open src/services/wallets/mirrorNodeClient.ts
and paste the interface outside of and above the MirrorNodeClient
class.
Paste the function outside of and below the getNftInfo
function in the src/services/wallets/mirrorNodeClient.ts
file.
The getAccountTokenBalancesWithTokenInfo
combines token balances, token info and, NFT info in order to display our available tokens in our DApp.
Before a user can receive a new token, they must associate with it. This association helps protect users from receiving unwanted tokens.
Open src/services/wallets/mirrorNodeClient.ts
and paste the function below the getAccountTokenBalancesWithTokenInfo
function.
In this step, we'll copy and paste the home.tsx file, which contains all the necessary code for adding UI components that enable token transfers and association with a token.
Open src/pages/Home.tsx
and replace the existing code by pasting the below code:
The crucial part of the code is found within the following code:
This code fetches and updates the list of tokens a user owns providing the available tokens to the dropdown menu.
The application is ready to be started and tested. You will be testing:
You'll be creating four Hedera Testnet accounts, each with a balance of 10 HBAR. Two of these accounts will come pre-loaded with their own fungible tokens, and four accounts will come pre-loaded with their own non-fungible tokens (NFTs).
Open a new terminal window and create a new directory and change into that directory
Open hedera-test-accounts
folder in a new visual studio code window.
Create a new file and name it .env
with the following contents. Remember to enter your account ID and your private key.
Within the hedera-test-accounts
home directory, execute the following command in the terminal,
Keep this terminal open for the remainder of the tutorial, as you will refer back to it.
Rename your imported accounts within your preferred wallet to keep track which is the sender and receiver account.
Navigate back to your application in Visual Studio Code, and in the terminal, run the following command
Click the Connect Wallet
button in the upper right and select MetaMask and select the Sender account.
Open the output of the test accounts you created earlier and copy the ecdsaWithAlias
Sender's account NftTokenId
Paste the NftTokenId
in the DApps associate token textbox and click the button Associate
MetaMask will prompt you to sign the transaction. If the extension does not automatically open, you will need to manually click on the MetaMask extension.
Confirm the transaction
Disconnect as the Receiver account and reconnect with the Sender account. To do this, open the MetaMask extension, click on the three dots in the upper right, select "Connected Sites," and then disconnect the Receiver account. All other wallets disconnect by clicking on your account ID in the upper right of the DApp homepage.
Connect to the DApp as the Sender Account.
As the Sender,
Select from available tokens the HederaNFT
Select the NFT with serial number 5 from the drop-down menu.
Enter the account ID or EVM address of the Receiver account.
Click the "send" button.
Sign the transaction on MetaMask to complete the transfer of the NFT from the Sender to the receiver account.
Disconnect as the Sender account and reconnect as the Receiver account.
Check the dropdown menu and ensure the Receiver account has NFT serial number 5.
Optionally, import your accounts into any of the above Hedera native wallets and test out transferring more tokens.
🎉 Congratulations! You have successfully walked through creating a Hedera DApp that transfers HTS tokens using MetaMask, HashPack, Blade, or Kabila.
You have learned how to:
Recommended:
Recommended:
Recommended for Linux & Mac:
Recommended for Windows:
We choose to scaffold our project by using the , as it offers:
The complete TypeScript project can be found on GitHub .
The complete JavaScript project can be found on GitHub .
offer access to historical data from the Hedera network while optimizing the use of network resources. You can easily retrieve information like transactions, records, events, and balances. Visit the to learn more.
Import the sender and receiver accounts that were just outputted into your preferred wallet application. (, , , or )
For assistance on how to import a Hedera account into MetaMask refer to our documentation .
The react template uses the Hashio JSON RPC Relay URL to work with MetaMask. If you are experiencing degraded performance, follow this to switch to Arkhia or set up your own JSON RPC Relay. Edit the src/config/networks.ts
with the new JSON RPC Relay URL.