Hedera
Search…
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.
We recommend you complete the following introduction to get a basic understanding of Hedera transactions. This example does not build upon the previous examples.

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.
Java
JavaScript
Go
1
// CREATE FUNGIBLE TOKEN (STABLECOIN)
2
TokenCreateTransaction tokenCreateTx = new TokenCreateTransaction()
3
.setTokenName("USD Bar")
4
.setTokenSymbol("USDB")
5
.setTokenType(TokenType.FUNGIBLE_COMMON)
6
.setDecimals(2)
7
.setInitialSupply(10000)
8
.setTreasuryAccountId(treasuryId)
9
.setSupplyType(TokenSupplyType.INFINITE)
10
.setSupplyKey(supplyKey)
11
.freezeWith(client);
12
​
13
//SIGN WITH TREASURY KEY
14
TokenCreateTransaction tokenCreateSign = tokenCreateTx.sign(treasuryKey);
15
​
16
//SUBMIT THE TRANSACTION
17
TransactionResponse tokenCreateSubmit = tokenCreateSign.execute(client);
18
​
19
//GET THE TRANSACTION RECEIPT
20
TransactionReceipt tokenCreateRx = tokenCreateSubmit.getReceipt(client);
21
​
22
//GET THE TOKEN ID
23
TokenId tokenId = tokenCreateRx.tokenId;
24
​
25
//LOG THE TOKEN ID TO THE CONSOLE
26
System.out.println("Created token with ID: " +tokenId);
Copied!
1
// CREATE FUNGIBLE TOKEN (STABLECOIN)
2
let tokenCreateTx = await new TokenCreateTransaction()
3
.setTokenName("USD Bar")
4
.setTokenSymbol("USDB")
5
.setTokenType(TokenType.FungibleCommon)
6
.setDecimals(2)
7
.setInitialSupply(10000)
8
.setTreasuryAccountId(treasuryId)
9
.setSupplyType(TokenSupplyType.Infinite)
10
.setSupplyKey(supplyKey)
11
.freezeWith(client);
12
​
13
//SIGN WITH TREASURY KEY
14
let tokenCreateSign = await tokenCreateTx.sign(treasuryKey);
15
​
16
//SUBMIT THE TRANSACTION
17
let tokenCreateSubmit = await tokenCreateSign.execute(client);
18
​
19
//GET THE TRANSACTION RECEIPT
20
let tokenCreateRx = await tokenCreateSubmit.getReceipt(client);
21
​
22
//GET THE TOKEN ID
23
let tokenId = tokenCreateRx.tokenId;
24
​
25
//LOG THE TOKEN ID TO THE CONSOLE
26
console.log(`- Created token with ID: ${tokenId} \n`);
Copied!
1
//CREATE FUNGIBLE TOKEN (STABLECOIN)
2
tokenCreateTx, err := hedera.NewTokenCreateTransaction().
3
SetTokenName("USD Bar").
4
SetTokenSymbol("USDB").
5
SetTokenType(hedera.TokenTypeFungibleCommon).
6
SetDecimals(2).
7
SetInitialSupply(10000).
8
SetTreasuryAccountID(treasuryAccountId).
9
SetSupplyType(hedera.TokenSupplyTypeInfinite).
10
SetSupplyKey(supplyKey).
11
FreezeWith(client)
12
​
13
//SIGN WITH TREASURY KEY
14
tokenCreateSign := tokenCreateTx.Sign(treasuryKey)
15
​
16
//SUBMIT THE TRANSACTION
17
tokenCreateSubmit, err := tokenCreateSign.Execute(client)
18
​
19
//GET THE TRANSACTION RECEIPT
20
tokenCreateRx, err := tokenCreateSubmit.GetReceipt(client)
21
​
22
//GET THE TOKEN ID
23
tokenId := *tokenCreateRx.TokenID
24
​
25
//LOG THE TOKEN ID TO THE CONSOLE
26
fmt.Println("Created fungible token with token ID", tokenId)
Copied!

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.
Java
JavaScript
Go
1
// TOKEN ASSOCIATION WITH ALICE's ACCOUNT
2
TokenAssociateTransaction associateAliceTx = new TokenAssociateTransaction()
3
.setAccountId(aliceAccountId)
4
.setTokenIds(Collections.singletonList(tokenId))
5
.freezeWith(client)
6
.sign(aliceKey);
7
8
//SUBMIT THE TRANSACTION
9
TransactionResponse associateAliceTxSubmit = associateAliceTx.execute(client);
10
11
//GET THE RECEIPT OF THE TRANSACTION
12
TransactionReceipt associateAliceRx = associateAliceTxSubmit.getReceipt(client);
13
14
//LOG THE TRANSACTION STATUS
15
System.out.println("Token association with Alice's account: " +associateAliceRx.status);
16
​
Copied!
1
// TOKEN ASSOCIATION WITH ALICE's ACCOUNT
2
let associateAliceTx = await new TokenAssociateTransaction()
3
.setAccountId(aliceId)
4
.setTokenIds([tokenId])
5
.freezeWith(client)
6
.sign(aliceKey);
7
​
8
//SUBMIT THE TRANSACTION
9
let associateAliceTxSubmit = await associateAliceTx.execute(client);
10
​
11
//GET THE RECEIPT OF THE TRANSACTION
12
let associateAliceRx = await associateAliceTxSubmit.getReceipt(client);
13
​
14
//LOG THE TRANSACTION STATUS
15
console.log(`- Token association with Alice's account: ${associateAliceRx.status} \n`);
16
​
Copied!
1
//TOKEN ASSOCIATION WITH ALICE's ACCOUNT
2
associateAliceTx, err := hedera.NewTokenAssociateTransaction().
3
SetAccountID(aliceAccountId).
4
SetTokenIDs(tokenId).
5
FreezeWith(client)
6
​
7
//SIGN WITH ALICE'S KEY TO AUTHORIZE THE ASSOCIATION
8
signTx := associateAliceTx.Sign(aliceKey)
9
​
10
//SUBMIT THE TRANSACTION
11
associateAliceTxSubmit, err := signTx.Execute(client)
12
​
13
//GET THE RECEIPT OF THE TRANSACTION
14
associateAliceRx, err := associateAliceTxSubmit.GetReceipt(client)
15
​
16
//LOG THE TRANSACTION STATUS
17
fmt.Println("Non-fungible token association with Alice's account:", associateAliceRx.Status)
Copied!

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.
Java
JavaScript
Go
1
//BALANCE CHECK
2
AccountBalance balanceCheckTreasury = new AccountBalanceQuery().setAccountId(treasuryId).execute(client);
3
System.out.println(" Treasury balance: " +balanceCheckTreasury.tokens + " units of token ID" +tokenId);
4
AccountBalance balanceCheckAlice = new AccountBalanceQuery().setAccountId(aliceAccountId).execute(client);
5
System.out.println("Alice's balance: " + balanceCheckAlice.tokens + " units of token ID " + tokenId);
6
​
7
//TRANSFER STABLECOIN FROM TREASURY TO ALICE
8
TransferTransaction tokenTransferTx = new TransferTransaction()
9
.addTokenTransfer(tokenId, treasuryId, -2500)
10
.addTokenTransfer(tokenId, aliceAccountId, 2500)
11
.freezeWith(client)
12
.sign(treasuryKey);
13
14
//SUBMIT THE TRANSACTION
15
TransactionResponse tokenTransferSubmit = tokenTransferTx.execute(client);
16
17
//GET THE RECEIPT OF THE TRANSACTION
18
TransactionReceipt tokenTransferRx = tokenTransferSubmit.getReceipt(client);
19
20
//LOG THE TRANSACTION STATUS
21
System.out.println("Stablecoin transfer from Treasury to Alice: " + tokenTransferRx.status );
22
​
23
//BALANCE CHECK
24
AccountBalance balanceCheckTreasury2 = new AccountBalanceQuery().setAccountId(treasuryId).execute(client);
25
System.out.println("Treasury balance " + balanceCheckTreasury2.tokens + " units of token ID " + tokenId);
26
AccountBalance balanceCheckAlice2 = new AccountBalanceQuery().setAccountId(aliceAccountId).execute(client);
27
System.out.println("Alice's balance: " +balanceCheckAlice2.tokens + " units of token ID " + tokenId);
28
Copied!
1
//BALANCE CHECK
2
var balanceCheckTx = await new AccountBalanceQuery().setAccountId(treasuryId).execute(client);
3
console.log(`- Treasury balance: ${balanceCheckTx.tokens._map.get(tokenId.toString())} units of token ID ${tokenId}`);
4
var balanceCheckTx = await new AccountBalanceQuery().setAccountId(aliceId).execute(client);
5
console.log(`- Alice's balance: ${balanceCheckTx.tokens._map.get(tokenId.toString())} units of token ID ${tokenId}`);
6
​
7
// TRANSFER STABLECOIN FROM TREASURY TO ALICE
8
let tokenTransferTx = await new TransferTransaction()
9
.addTokenTransfer(tokenId, treasuryId, -2500)
10
.addTokenTransfer(tokenId, aliceId, 2500)
11
.freezeWith(client)
12
.sign(treasuryKey);
13
​
14
//SUBMIT THE TRANSACTION
15
let tokenTransferSubmit = await tokenTransferTx.execute(client);
16
​
17
//GET THE RECEIPT OF THE TRANSACTION
18
let tokenTransferRx = await tokenTransferSubmit.getReceipt(client);
19
​
20
//LOG THE TRANSACTION STATUS
21
console.log(`\n- Stablecoin transfer from Treasury to Alice: ${tokenTransferRx.status} \n`);
22
​
23
// BALANCE CHECK
24
var balanceCheckTx = await new AccountBalanceQuery().setAccountId(treasuryId).execute(client);
25
console.log(`- Treasury balance: ${balanceCheckTx.tokens._map.get(tokenId.toString())} units of token ID ${tokenId}`);
26
var balanceCheckTx = await new AccountBalanceQuery().setAccountId(aliceId).execute(client);
27
console.log(`- Alice's balance: ${balanceCheckTx.tokens._map.get(tokenId.toString())} units of token ID ${tokenId}`);
Copied!
1
//CHECK THE BALANCE BEFORE THE TRANSFER FROM THE TREASURY ACCOUNT
2
balanceCheckTreasury, err := hedera.NewAccountBalanceQuery().SetAccountID(treasuryAccountId).Execute(client)
3
fmt.Println("Treasury balance:", balanceCheckTreasury.Tokens, "units of token ID", tokenId)
4
​
5
//CHECK THE BALANCE BEFORE THE TRANSFER FOR ALICE'S ACCOUNT
6
balanceCheckAlice, err := hedera.NewAccountBalanceQuery().SetAccountID(aliceAccountId).Execute(client)
7
fmt.Println("Alice's balance:", balanceCheckAlice.Tokens, "units of token ID", tokenId)
8
​
9
//TRANSFER THE STABLECOIN TO ALICE
10
tokenTransferTx, err := hedera.NewTransferTransaction().
11
AddTokenTransfer(tokenId, treasuryAccountId, -2500).
12
AddTokenTransfer(tokenId, aliceAccountId, 2500).
13
FreezeWith(client)
14
​
15
//SIGN WITH THE TREASURY KEY TO AUTHORIZE THE TRANSFER
16
signTransferTx := tokenTransferTx.Sign(treasuryKey)
17
​
18
//SUBMIT THE TRANSACTION
19
tokenTransferSubmit, err := signTransferTx.Execute(client)
20
​
21
//GET THE TRANSACTION RECEIPT
22
tokenTransferRx, err := tokenTransferSubmit.GetReceipt(client)
23
​
24
fmt.Println("Token transfer from Treasury to Alice:", tokenTransferRx.Status)
25
​
26
//CHECK THE BALANCE AFTER THE TRANSFER FOR THE TREASURY ACCOUNT
27
balanceCheckTreasury2, err := hedera.NewAccountBalanceQuery().SetAccountID(treasuryAccountId).Execute(client)
28
fmt.Println("Treasury balance:", balanceCheckTreasury2.Tokens, "units of token", tokenId)
29
​
30
//CHECK THE BALANCE AFTER THE TRANSFER FOR ALICE'S ACCOUNT
31
balanceCheckAlice2, err := hedera.NewAccountBalanceQuery().SetAccountID(aliceAccountId).Execute(client)
32
gofmt.Println("Alice's balance:", balanceCheckAlice2.Tokens, "units of token", tokenId)
33
​
Copied!
​

Code Check βœ…

Java
JavaScript
Go
1
import com.hedera.hashgraph.sdk.*;
2
import io.github.cdimascio.dotenv.Dotenv;
3
​
4
import java.util.Collections;
5
import java.util.Objects;
6
import java.util.concurrent.TimeoutException;
7
​
8
public class CreateFungibleTutorial {
9
public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException {
10
​
11
//Grab your Hedera testnet account ID and private key
12
AccountId myAccountId = AccountId.fromString(Objects.requireNonNull(Dotenv.load().get("MY_ACCOUNT_ID")));
13
PrivateKey myPrivateKey = PrivateKey.fromString(Objects.requireNonNull(Dotenv.load().get("MY_PRIVATE_KEY")));
14
​
15
//Create your Hedera testnet client
16
Client client = Client.forTestnet();
17
client.setOperator(myAccountId, myPrivateKey);
18
​
19
//Treasury Key
20
PrivateKey treasuryKey = PrivateKey.generate();
21
PublicKey treasuryPublicKey = treasuryKey.getPublicKey();
22
​
23
//Create treasury account
24
TransactionResponse treasuryAccount = new AccountCreateTransaction()
25
.setKey(treasuryPublicKey)
26
.setInitialBalance(new Hbar(10))
27
.execute(client);
28
​
29
AccountId treasuryId = treasuryAccount.getReceipt(client).accountId;
30
​
31
//Alice Key
32
PrivateKey aliceKey = PrivateKey.generate();
33
PublicKey alicePublicKey = aliceKey.getPublicKey();
34
​
35
//Create Alice's account
36
TransactionResponse aliceAccount = new AccountCreateTransaction()
37
.setKey(alicePublicKey)
38
.setInitialBalance(new Hbar(10))
39
.execute(client);
40
​
41
AccountId aliceAccountId = aliceAccount.getReceipt(client).accountId;
42
​
43
//Alice Key
44
PrivateKey supplyKey = PrivateKey.generate();
45
PublicKey supplyPublicKey = supplyKey.getPublicKey();
46
​
47
// CREATE FUNGIBLE TOKEN (STABLECOIN)
48
TokenCreateTransaction tokenCreateTx = new TokenCreateTransaction()
49
.setTokenName("USD Bar")
50
.setTokenSymbol("USDB")
51
.setTokenType(TokenType.FUNGIBLE_COMMON)
52
.setDecimals(2)
53
.setInitialSupply(10000)
54
.setTreasuryAccountId(treasuryId)
55
.setSupplyType(TokenSupplyType.INFINITE)
56
.setSupplyKey(supplyKey)
57
.freezeWith(client);
58
​
59
//Sign with the treasury key
60
TokenCreateTransaction tokenCreateSign = tokenCreateTx.sign(treasuryKey);
61
​
62
//Submit the transaction
63
TransactionResponse tokenCreateSubmit = tokenCreateSign.execute(client);
64
​
65
//Get the transaction receipt
66
TransactionReceipt tokenCreateRx = tokenCreateSubmit.getReceipt(client);
67
​
68
//Get the token ID
69
TokenId tokenId = tokenCreateRx.tokenId;
70
​
71
//Log the token ID to the console
72
System.out.println("Created token with ID: " +tokenId);
73
​
74
// TOKEN ASSOCIATION WITH ALICE's ACCOUNT
75
TokenAssociateTransaction associateAliceTx = new TokenAssociateTransaction()
76
.setAccountId(aliceAccountId)
77
.setTokenIds(Collections.singletonList(tokenId))
78
.freezeWith(client)
79
.sign(aliceKey);
80
​
81
//Submit the transaction
82
TransactionResponse associateAliceTxSubmit = associateAliceTx.execute(client);
83
​
84
//Get the receipt of the transaction
85
TransactionReceipt associateAliceRx = associateAliceTxSubmit.getReceipt(client);
86
​
87
//Get the transaction status
88
System.out.println("Token association with Alice's account: " +associateAliceRx.status);
89
​
90
// BALANCE CHECK
91
AccountBalance balanceCheckTreasury = new AccountBalanceQuery().setAccountId(treasuryId).execute(client);
92
System.out.println(" Treasury balance: " +balanceCheckTreasury.tokens + " units of token ID" +tokenId);
93
AccountBalance balanceCheckAlice = new AccountBalanceQuery().setAccountId(aliceAccountId).execute(client);
94
System.out.println("Alice's balance: " + balanceCheckAlice.tokens + " units of token ID " + tokenId);
95
​
96
// TRANSFER STABLECOIN FROM TREASURY TO ALICE
97
TransferTransaction tokenTransferTx = new TransferTransaction()
98
.addTokenTransfer(tokenId, treasuryId, -2500)
99
.addTokenTransfer(tokenId, aliceAccountId, 2500)
100
.freezeWith(client)
101
.sign(treasuryKey);
102
​
103
//SUBMIT THE TRANSACTION
104
TransactionResponse tokenTransferSubmit = tokenTransferTx.execute(client);
105
​
106
//GET THE RECEIPT OF THE TRANSACTION
107
TransactionReceipt tokenTransferRx = tokenTransferSubmit.getReceipt(client);
108
​
109
//LOG THE TRANSACTION STATUS
110
System.out.println("Stablecoin transfer from Treasury to Alice: " + tokenTransferRx.status );
111
​
112
// BALANCE CHECK
113
AccountBalance balanceCheckTreasury2 = new AccountBalanceQuery().setAccountId(treasuryId).execute(client);
114
System.out.println("Treasury balance " + balanceCheckTreasury2.tokens + " units of token ID " + tokenId);
115
AccountBalance balanceCheckAlice2 = new AccountBalanceQuery().setAccountId(aliceAccountId).execute(client);
116
System.out.println("Alice's balance: " +balanceCheckAlice2.tokens + " units of token ID " + tokenId);
117
​
118
}
119
}
Copied!
​
1
console.clear();
2
require("dotenv").config();
3
const {
4
AccountId,
5
PrivateKey,
6
Client,
7
TokenCreateTransaction,
8
TokenType,
9
TokenSupplyType,
10
TransferTransaction,
11
AccountBalanceQuery,
12
TokenAssociateTransaction,
13
} = require("@hashgraph/sdk");
14
​
15
// Configure accounts and client, and generate needed keys
16
const operatorId = AccountId.fromString(process.env.OPERATOR_ID);
17
const operatorKey = PrivateKey.fromString(process.env.OPERATOR_PVKEY);
18
const treasuryId = AccountId.fromString(process.env.TREASURY_ID);
19
const treasuryKey = PrivateKey.fromString(process.env.TREASURY_PVKEY);
20
const aliceId = AccountId.fromString(process.env.ALICE_ID);
21
const aliceKey = PrivateKey.fromString(process.env.ALICE_PVKEY);
22
​
23
const client = Client.forTestnet().setOperator(operatorId, operatorKey);
24
​
25
const supplyKey = PrivateKey.generate();
26
​
27
async function main() {
28
//CREATE FUNGIBLE TOKEN (STABLECOIN)
29
let tokenCreateTx = await new TokenCreateTransaction()
30
.setTokenName("USD Bar")
31
.setTokenSymbol("USDB")
32
.setTokenType(TokenType.FungibleCommon)
33
.setDecimals(2)
34
.setInitialSupply(10000)
35
.setTreasuryAccountId(treasuryId)
36
.setSupplyType(TokenSupplyType.Infinite)
37
.setSupplyKey(supplyKey)
38
.freezeWith(client);
39
​
40
let tokenCreateSign = await tokenCreateTx.sign(treasuryKey);
41
let tokenCreateSubmit = await tokenCreateSign.execute(client);
42
let tokenCreateRx = await tokenCreateSubmit.getReceipt(client);
43
let tokenId = tokenCreateRx.tokenId;
44
console.log(`- Created token with ID: ${tokenId} \n`);
45
​
46
//TOKEN ASSOCIATION WITH ALICE's ACCOUNT
47
let associateAliceTx = await new TokenAssociateTransaction()
48
.setAccountId(aliceId)
49
.setTokenIds([tokenId])
50
.freezeWith(client)
51
.sign(aliceKey);
52
let associateAliceTxSubmit = await associateAliceTx.execute(client);
53
let associateAliceRx = await associateAliceTxSubmit.getReceipt(client);
54
console.log(`- Token association with Alice's account: ${associateAliceRx.status} \n`);
55
​
56
//BALANCE CHECK
57
var balanceCheckTx = await new AccountBalanceQuery().setAccountId(treasuryId).execute(client);
58
console.log(`- Treasury balance: ${balanceCheckTx.tokens._map.get(tokenId.toString())} units of token ID ${tokenId}`);
59
var balanceCheckTx = await new AccountBalanceQuery().setAccountId(aliceId).execute(client);
60
console.log(`- Alice's balance: ${balanceCheckTx.tokens._map.get(tokenId.toString())} units of token ID ${tokenId}`);
61
​
62
//TRANSFER STABLECOIN FROM TREASURY TO ALICE
63
let tokenTransferTx = await new TransferTransaction()
64
.addTokenTransfer(tokenId, treasuryId, -2500)
65
.addTokenTransfer(tokenId, aliceId, 2500)
66
.freezeWith(client)
67
.sign(treasuryKey);
68
let tokenTransferSubmit = await tokenTransferTx.execute(client);
69
let tokenTransferRx = await tokenTransferSubmit.getReceipt(client);
70
console.log(`\n- Stablecoin transfer from Treasury to Alice: ${tokenTransferRx.status} \n`);
71
​
72
//BALANCE CHECK
73
var balanceCheckTx = await new AccountBalanceQuery().setAccountId(treasuryId).execute(client);
74
console.log(`- Treasury balance: ${balanceCheckTx.tokens._map.get(tokenId.toString())} units of token ID ${tokenId}`);
75
var balanceCheckTx = await new AccountBalanceQuery().setAccountId(aliceId).execute(client);
76
console.log(`- Alice's balance: ${balanceCheckTx.tokens._map.get(tokenId.toString())} units of token ID ${tokenId}`);
77
}
78
main();
79
​
Copied!
1
package main
2
​
3
import (
4
"fmt"
5
"os"
6
​
7
"github.com/hashgraph/hedera-sdk-go/v2"
8
"github.com/joho/godotenv"
9
)
10
​
11
func main() {
12
​
13
//LOADS THE .ENV FILE AND THROWS AN EROOR IF IT CANNOT LOAD THE VARIABLES
14
err := godotenv.Load(".env")
15
if err != nil {
16
panic(fmt.Errorf("Unable to load enviroment variables from .env file. Error:\n%v\n", err))
17
}
18
​
19
//GRAB YOUR TESTNET ACCOUNT ID AND KEY FROMZ THE .ENV FILE
20
myAccountId, err := hedera.AccountIDFromString(os.Getenv("MY_ACCOUNT_ID"))
21
if err != nil {
22
panic(err)
23
}
24
​
25
myPrivateKey, err := hedera.PrivateKeyFromString(os.Getenv("MY_PRIVATE_KEY"))
26
if err != nil {
27
panic(err)
28
}
29
​
30
//PRINT ACCOUNT ID AND KEY TO MAKE SURE THERE WASN'T AN ERROR READING FROM THE .ENV FILE
31
fmt.Printf("The account ID is = %v\n", myAccountId)
32
fmt.Printf("The private key is = %v\n", myPrivateKey)
33
​
34
//CREATE TESTNET CLIENT
35
client := hedera.ClientForTestnet()
36
client.SetOperator(myAccountId, myPrivateKey)
37
​
38
//CREATE TREASURY KEY
39
treasuryKey, err := hedera.GeneratePrivateKey()
40
treasuryPublicKey := treasuryKey.PublicKey()
41
​
42
//CREATE TREASURY ACCOUNT
43
treasuryAccount, err := hedera.NewAccountCreateTransaction().
44
SetKey(treasuryPublicKey).
45
SetInitialBalance(hedera.NewHbar(10)).
46
Execute(client)
47
48
//GET THE RECEIPT OF THE TRANSACTION
49
receipt, err := treasuryAccount.GetReceipt(client)
50
​
51
//GET THE ACCOUNT ID
52
treasuryAccountId := *receipt.AccountID
53
​
54
//ALICE'S KEY
55
aliceKey, err := hedera.GeneratePrivateKey()
56
alicePublicKey := aliceKey.PublicKey()
57
​
58
//CREATE AILICE'S ACCOUNT
59
aliceAccount, err := hedera.NewAccountCreateTransaction().
60
SetKey(alicePublicKey).
61
SetInitialBalance(hedera.NewHbar(10)).
62
Execute(client)
63
​
64
//GET THE RECEIPT OF THE TRANSACTION
65
receipt2, err := aliceAccount.GetReceipt(client)
66
​
67
//GET ALICE'S ACCOUNT ID
68
aliceAccountId := *receipt2.AccountID
69
​
70
//CREATE SUPPLY KEY
71
supplyKey, err := hedera.GeneratePrivateKey()
72
​
73
//CREATE FUNGIBLE TOKEN (STABLECOIN)
74
tokenCreateTx, err := hedera.NewTokenCreateTransaction().
75
SetTokenName("USD Bar").
76
SetTokenSymbol("USDB").
77
SetTokenType(hedera.TokenTypeFungibleCommon).
78
SetDecimals(2).
79
SetInitialSupply(10000).
80
SetTreasuryAccountID(treasuryAccountId).
81
SetSupplyType(hedera.TokenSupplyTypeInfinite).
82
SetSupplyKey(supplyKey).
83
FreezeWith(client)
84
​
85
//SIGN WITH TREASURY KEY
86
tokenCreateSign := tokenCreateTx.Sign(treasuryKey)
87
​
88
//SUBMIT THE TRANSACTION
89
tokenCreateSubmit, err := tokenCreateSign.Execute(client)
90
​
91
//GET THE TRANSACTION RECEIPT
92
tokenCreateRx, err := tokenCreateSubmit.GetReceipt(client)
93
​
94
//GET THE TOKEN ID
95
tokenId := *tokenCreateRx.TokenID
96
​
97
//LOG THE TOKEN ID TO THE CONSOLE
98
fmt.Println("Created fungible token with token ID", tokenId)
99
​
100
//TOKEN ASSOCIATION WITH ALICE's ACCOUNT
101
associateAliceTx, err := hedera.NewTokenAssociateTransaction().
102
SetAccountID(aliceAccountId).
103
SetTokenIDs(tokenId).
104
FreezeWith(client)
105
​
106
//SIGN WITH ALICE'S KEY TO AUTHORIZE THE ASSOCIATION
107
signTx := associateAliceTx.Sign(aliceKey)
108
​
109
//SUBMIT THE TRANSACTION
110
associateAliceTxSubmit, err := signTx.Execute(client)
111
​
112
//GET THE RECEIPT OF THE TRANSACTION
113
associateAliceRx, err := associateAliceTxSubmit.GetReceipt(client)
114
​
115
//LOG THE TRANSACTION STATUS
116
fmt.Println("STABLECOIN token association with Alice's account:", associateAliceRx.Status)
117
​
118
//Check the balance before the transfer for the treasury account
119
balanceCheckTreasury, err := hedera.NewAccountBalanceQuery().SetAccountID(treasuryAccountId).Execute(client)
120
fmt.Println("Treasury balance:", balanceCheckTreasury.Tokens, "units of token ID", tokenId)
121
​
122
//Check the balance before the transfer for Alice's account
123
balanceCheckAlice, err := hedera.NewAccountBalanceQuery().SetAccountID(aliceAccountId).Execute(client)
124
fmt.Println("Alice's balance:", balanceCheckAlice.Tokens, "units of token ID", tokenId)
125
​
126
//Transfer the STABLECOIN from treasury to Alice
127
tokenTransferTx, err := hedera.NewTransferTransaction().
128
AddTokenTransfer(tokenId, treasuryAccountId, -2500).
129
AddTokenTransfer(tokenId, aliceAccountId, 2500).
130
FreezeWith(client)
131
​
132
//SIGN WITH THE TREASURY KEY TO AUTHORIZE THE TRANSFER
133
signTransferTx := tokenTransferTx.Sign(treasuryKey)
134
​
135
//SUBMIT THE TRANSACTION
136
tokenTransferSubmit, err := signTransferTx.Execute(client)
137
​
138
//GET THE TRANSACTION RECEIPT
139
tokenTransferRx, err := tokenTransferSubmit.GetReceipt(client)
140
​
141
fmt.Println("Token transfer from Treasury to Alice:", tokenTransferRx.Status)
142
​
143
//CHECK THE BALANCE AFTER THE TRANSFER FOR THE TREASURY ACCOUNT
144
balanceCheckTreasury2, err := hedera.NewAccountBalanceQuery().SetAccountID(treasuryAccountId).Execute(client)
145
fmt.Println("Treasury balance:", balanceCheckTreasury2.Tokens, "units of token", tokenId)
146
​
147
//CHECK THE BALANCE AFTER THE TRANSFER FOR ALICE'S ACCOUNT
148
balanceCheckAlice2, err := hedera.NewAccountBalanceQuery().SetAccountID(aliceAccountId).Execute(client)
149
fmt.Println("Alice's balance:", balanceCheckAlice2.Tokens, "units of token", tokenId)
150
​
151
}
152
​
Copied!
​
Last modified 5d ago