Create and Transfer Your First Fungible Token
Summary
Fungible tokens share a single set of properties and have interchangeable value with one another. Use cases for fungible tokens include applications like stablecoins, in-game rewards systems, crypto tokens, loyalty program points, and much more.
Prerequisites
We recommend you complete the following introduction to get a basic understanding of Hedera transactions. This example does not build upon the previous examples.
Get a Hedera testnet account.
Set up your environment here.
1. Create a Fungible Token
Use TokenCreateTransaction()
to create a fungible token and configure its properties. At a minimum, this constructor requires setting a name, symbol, and treasury account ID. All other fields are optional, so if they’re not specified then default values are used. For instance, not specifying an admin key, makes a token immutable (can’t change or add properties); not specifying a supply key, makes a token supply fixed (can’t mint new or burn existing tokens); not specifying a token type, makes a token fungible.
After submitting the transaction to the Hedera network, you can obtain the new token ID by requesting the receipt.
Unlike NFTs, fungible tokens do not require the decimals and initial supply to be set to zero during creation. In this case, we set the initial supply to 100 units, which is shown in the code as 10000 to account to 2 decimals.
// CREATE FUNGIBLE TOKEN (STABLECOIN)
TokenCreateTransaction tokenCreateTx = new TokenCreateTransaction()
.setTokenName("USD Bar")
.setTokenSymbol("USDB")
.setTokenType(TokenType.FUNGIBLE_COMMON)
.setDecimals(2)
.setInitialSupply(10000)
.setTreasuryAccountId(treasuryId)
.setSupplyType(TokenSupplyType.INFINITE)
.setSupplyKey(supplyKey)
.freezeWith(client);
//SIGN WITH TREASURY KEY
TokenCreateTransaction tokenCreateSign = tokenCreateTx.sign(treasuryKey);
//SUBMIT THE TRANSACTION
TransactionResponse tokenCreateSubmit = tokenCreateSign.execute(client);
//GET THE TRANSACTION RECEIPT
TransactionReceipt tokenCreateRx = tokenCreateSubmit.getReceipt(client);
//GET THE TOKEN ID
TokenId tokenId = tokenCreateRx.tokenId;
//LOG THE TOKEN ID TO THE CONSOLE
System.out.println("Created token with ID: " +tokenId);
2. Associate User Accounts with Token
Before an account that is not the treasury for a token can receive or send this specific token ID, the account must become “associated” with the token.
// TOKEN ASSOCIATION WITH ALICE's ACCOUNT
TokenAssociateTransaction associateAliceTx = new TokenAssociateTransaction()
.setAccountId(aliceAccountId)
.setTokenIds(Collections.singletonList(tokenId))
.freezeWith(client)
.sign(aliceKey);
//SUBMIT THE TRANSACTION
TransactionResponse associateAliceTxSubmit = associateAliceTx.execute(client);
//GET THE RECEIPT OF THE TRANSACTION
TransactionReceipt associateAliceRx = associateAliceTxSubmit.getReceipt(client);
//LOG THE TRANSACTION STATUS
System.out.println("Token association with Alice's account: " +associateAliceRx.status);
3. Transfer the Token
Now, transfer 25 units of the token from the Treasury to Alice and check the account balances before and after the transfer.
//BALANCE CHECK
AccountBalance balanceCheckTreasury = new AccountBalanceQuery().setAccountId(treasuryId).execute(client);
System.out.println(" Treasury balance: " +balanceCheckTreasury.tokens + " units of token ID" +tokenId);
AccountBalance balanceCheckAlice = new AccountBalanceQuery().setAccountId(aliceAccountId).execute(client);
System.out.println("Alice's balance: " + balanceCheckAlice.tokens + " units of token ID " + tokenId);
//TRANSFER STABLECOIN FROM TREASURY TO ALICE
TransferTransaction tokenTransferTx = new TransferTransaction()
.addTokenTransfer(tokenId, treasuryId, -5)
.addTokenTransfer(tokenId, aliceAccountId, 5)
.freezeWith(client)
.sign(treasuryKey);
//SUBMIT THE TRANSACTION
TransactionResponse tokenTransferSubmit = tokenTransferTx.execute(client);
//GET THE RECEIPT OF THE TRANSACTION
TransactionReceipt tokenTransferRx = tokenTransferSubmit.getReceipt(client);
//LOG THE TRANSACTION STATUS
System.out.println("Stablecoin transfer from Treasury to Alice: " + tokenTransferRx.status );
//BALANCE CHECK
AccountBalance balanceCheckTreasury2 = new AccountBalanceQuery().setAccountId(treasuryId).execute(client);
System.out.println("Treasury balance " + balanceCheckTreasury2.tokens + " units of token ID " + tokenId);
AccountBalance balanceCheckAlice2 = new AccountBalanceQuery().setAccountId(aliceAccountId).execute(client);
System.out.println("Alice's balance: " +balanceCheckAlice2.tokens + " units of token ID " + tokenId);
Code Check ✅
Last updated
Was this helpful?