-
Notifications
You must be signed in to change notification settings - Fork 44
Expand file tree
/
Copy pathCreateTokenDemo.java
More file actions
89 lines (75 loc) · 3.69 KB
/
Copy pathCreateTokenDemo.java
File metadata and controls
89 lines (75 loc) · 3.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
import com.hedera.hashgraph.sdk.*;
import java.net.http.*;
import java.net.URI;
import com.google.gson.*;
public class CreateTokenDemo {
public static void main(String[] args ) throws Exception {
// .env-provided
AccountId operatorId = AccountId.fromString(System.getenv("OPERATOR_ID"));
PrivateKey operatorKey = PrivateKey.fromStringECDSA(System.getenv("OPERATOR_KEY"));
String network = System.getenv().getOrDefault("HEDERA_NETWORK", "local"); // "local" for Solo
String mirrorNode = System.getenv().getOrDefault(
"MIRROR_NODE_URL",
"http://localhost:38081/api/v1"
);
if (operatorId == null || operatorKey == null) {
throw new IllegalStateException("OPERATOR_ID / OPERATOR_KEY not set");
}
Client client =
"local".equalsIgnoreCase(network)
? Client.forNetwork(java.util.Map.of("127.0.0.1:50211", new AccountId(3))) // Solo default node + node account (adjust if needed)
: Client.forTestnet();
client.setOperator(operatorId, operatorKey);
System.out.println(operatorId);
// generate token keys
PrivateKey supplyKey = PrivateKey.generateECDSA();
PrivateKey adminKey = supplyKey;
// build & execute the token creation transaction
TokenCreateTransaction transaction = new TokenCreateTransaction()
.setTokenName("Demo Token")
.setTokenSymbol("DEMO")
.setDecimals(2)
.setInitialSupply(100_000)
.setSupplyType(TokenSupplyType.FINITE)
.setMaxSupply(100_000)
.setTreasuryAccountId(operatorId)
.setAdminKey(adminKey.getPublicKey())
.setSupplyKey(supplyKey.getPublicKey())
.setTokenMemo("Created via tutorial")
.freezeWith(client);
TokenCreateTransaction signedTx = transaction.sign(adminKey);
TransactionResponse txResponse = signedTx.execute(client);
TransactionReceipt receipt = txResponse.getReceipt(client);
TokenId tokenId = receipt.tokenId;
System.out.println("\nFungible token created: " + tokenId );
// Wait for Mirror Node to populate data
System.out.println("\nWaiting for Mirror Node to update...");
Thread.sleep(10000);
// query balance using Mirror Node
String mirrorNodeUrl = String.format(
"%s/accounts/%s/tokens?token.id=%s",
mirrorNode, operatorId, tokenId
);
HttpClient httpClient = HttpClient.newHttpClient( );
HttpRequest request = HttpRequest.newBuilder().uri(URI.create(mirrorNodeUrl)).build();
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString( ));
try {
if (response.statusCode() != 200) {
System.out.println("Mirror Node returned status " + response.statusCode() + ". Skipping balance read.");
} else {
JsonObject data = new Gson().fromJson(response.body(), JsonObject.class);
JsonArray tokens = (data != null) ? data.getAsJsonArray("tokens") : null;
if (tokens != null && tokens.size() > 0) {
long balance = tokens.get(0).getAsJsonObject().get("balance").getAsLong();
System.out.println("\nTreasury holds: " + balance + " DEMO\n");
} else {
System.out.println("Token balance not yet available in Mirror Node");
}
}
} catch (Exception e) {
System.out.println("Error reading token balance from Mirror Node: " + e.getMessage());
// continue without failing
}
client.close();
}
}