Skip to content

Commit ae475c8

Browse files
authored
feat: init client with mirror network (#2085)
Signed-off-by: Ivan Ivanov <[email protected]>
1 parent b8ae85c commit ae475c8

File tree

4 files changed

+127
-3
lines changed

4 files changed

+127
-3
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*-
2+
*
3+
* Hedera Java SDK
4+
*
5+
* Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
6+
*
7+
* Licensed under the Apache License, Version 2.0 (the "License");
8+
* you may not use this file except in compliance with the License.
9+
* You may obtain a copy of the License at
10+
*
11+
* http://www.apache.org/licenses/LICENSE-2.0
12+
*
13+
* Unless required by applicable law or agreed to in writing, software
14+
* distributed under the License is distributed on an "AS IS" BASIS,
15+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16+
* See the License for the specific language governing permissions and
17+
* limitations under the License.
18+
*
19+
*/
20+
21+
package com.hedera.hashgraph.sdk.examples;
22+
23+
import com.hedera.hashgraph.sdk.AccountCreateTransaction;
24+
import com.hedera.hashgraph.sdk.AccountId;
25+
import com.hedera.hashgraph.sdk.Client;
26+
import com.hedera.hashgraph.sdk.Hbar;
27+
import com.hedera.hashgraph.sdk.PrivateKey;
28+
import com.hedera.hashgraph.sdk.logger.LogLevel;
29+
import com.hedera.hashgraph.sdk.logger.Logger;
30+
import io.github.cdimascio.dotenv.Dotenv;
31+
import java.util.List;
32+
import java.util.Objects;
33+
34+
public class InitializeClientWithMirrorNetworkExample {
35+
/*
36+
* See .env.sample in the examples folder root for how to specify values below
37+
* or set environment variables with the same names.
38+
*/
39+
40+
/**
41+
* Operator's account ID. Used to sign and pay for operations on Hedera.
42+
*/
43+
private static final AccountId OPERATOR_ID = AccountId.fromString(
44+
Objects.requireNonNull(Dotenv.load().get("OPERATOR_ID")));
45+
46+
/**
47+
* Operator's private key.
48+
*/
49+
private static final PrivateKey OPERATOR_KEY = PrivateKey.fromString(
50+
Objects.requireNonNull(Dotenv.load().get("OPERATOR_KEY")));
51+
52+
/**
53+
* SDK_LOG_LEVEL defaults to SILENT if not specified in dotenv file. Log levels can be: TRACE, DEBUG, INFO, WARN,
54+
* ERROR, SILENT.
55+
* <p>
56+
* Important pre-requisite: set simple logger log level to same level as the SDK_LOG_LEVEL, for example via VM
57+
* options: -Dorg.slf4j.simpleLogger.log.com.hedera.hashgraph=trace
58+
*/
59+
private static final String SDK_LOG_LEVEL = Dotenv.load().get("SDK_LOG_LEVEL", "SILENT");
60+
61+
public static void main(String[] args) throws Exception {
62+
/*
63+
* Step 0:
64+
* Create and configure the SDK Client.
65+
*/
66+
Client client = Client.forMirrorNetwork(List.of("testnet.mirrornode.hedera.com:443"));
67+
// All generated transactions will be paid by this account and signed by this key.
68+
client.setOperator(OPERATOR_ID, OPERATOR_KEY);
69+
// Attach logger to the SDK Client.
70+
client.setLogger(new Logger(LogLevel.valueOf(SDK_LOG_LEVEL)));
71+
72+
/*
73+
* Step 1:
74+
* Generate ED25519 key pair.
75+
*/
76+
System.out.println("Generating ED25519 key pair...");
77+
PrivateKey privateKey = PrivateKey.generateED25519();
78+
79+
/*
80+
* Step 2:
81+
* Create account
82+
*/
83+
AccountId aliceId = new AccountCreateTransaction()
84+
.setKey(privateKey)
85+
.setInitialBalance(Hbar.from(5))
86+
.execute(client)
87+
.getReceipt(client)
88+
.accountId;
89+
Objects.requireNonNull(aliceId);
90+
System.out.println("Alice's account ID: " + aliceId);
91+
}
92+
}

gradle/plugins/src/main/kotlin/com.hedera.gradle.examples.java.gradle.kts

+1
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ abstract class RunAllExample : DefaultTask() {
6969
.map { it.name.replace(".java", "") }
7070
.filter { it != "ValidateChecksumExample" } // disabled this example, because it needs user input (but it WORKS)
7171
.filter { it != "ConsensusPubSubChunkedExample" } // is flaky on local-node env, will be investigated
72+
.filter { it != "InitializeClientWithMirrorNetworkExample" } // disabled - cannot run on localnode
7273
.toList()
7374

7475
exampleClasses.forEach { className ->

sdk/src/main/java/com/hedera/hashgraph/sdk/Client.java

+20-3
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,24 @@ public static Client forNetwork(Map<String, AccountId> networkMap) {
185185
return new Client(executor, network, mirrorNetwork, null, true, null);
186186
}
187187

188+
/**
189+
* Set up the client from selected mirror network.
190+
*
191+
* @param mirrorNetworkList
192+
* @return
193+
*/
194+
public static Client forMirrorNetwork(List<String> mirrorNetworkList) throws InterruptedException, TimeoutException {
195+
var executor = createExecutor();
196+
var network = Network.forNetwork(executor, new HashMap<>());
197+
var mirrorNetwork = MirrorNetwork.forNetwork(executor, mirrorNetworkList);
198+
var client = new Client(executor, network, mirrorNetwork, null, true, null);
199+
var addressBook = new AddressBookQuery()
200+
.setFileId(FileId.ADDRESS_BOOK)
201+
.execute(client);
202+
client.setNetworkFromAddressBook(addressBook);
203+
return client;
204+
}
205+
188206
/**
189207
* Set up the client for the selected network.
190208
*
@@ -1418,8 +1436,8 @@ private Client toClient() throws Exception, InterruptedException {
14181436
private Client initializeWithNetwork() throws Exception {
14191437
if (network == null) {
14201438
throw new Exception("Network is not set in provided json object");
1421-
}
1422-
1439+
}
1440+
14231441
Client client;
14241442
if (network.isJsonObject()) {
14251443
client = clientFromNetworkJson();
@@ -1501,7 +1519,6 @@ private void setOperatorOn(Client client) {
15011519
if (operator != null) {
15021520
AccountId operatorAccount = AccountId.fromString(operator.accountId);
15031521
PrivateKey privateKey = PrivateKey.fromString(operator.privateKey);
1504-
15051522
client.setOperator(operatorAccount, privateKey);
15061523
}
15071524
}

sdk/src/testIntegration/java/com/hedera/hashgraph/sdk/test/integration/ClientIntegrationTest.java

+14
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import com.hedera.hashgraph.sdk.*;
2323
import java.util.Collections;
24+
import java.util.List;
2425
import org.junit.jupiter.api.DisplayName;
2526
import org.junit.jupiter.api.Test;
2627
import java.time.Duration;
@@ -248,4 +249,17 @@ void pingAllAsync() throws Exception {
248249

249250
}
250251
}
252+
253+
@Test
254+
@DisplayName("`forMirrorNetwork()`")
255+
void testClientInitWithMirrorNetwork() throws Exception {
256+
var mirrorNetworkString = "testnet.mirrornode.hedera.com:443";
257+
var client = Client.forMirrorNetwork(List.of(mirrorNetworkString));
258+
var mirrorNetwork = client.getMirrorNetwork();
259+
260+
assertThat(mirrorNetwork).hasSize(1);
261+
assertThat(mirrorNetwork.get(0)).isEqualTo(mirrorNetworkString);
262+
assertThat(client.getNetwork()).isNotNull();
263+
assertThat(client.getNetwork()).isNotEmpty();
264+
}
251265
}

0 commit comments

Comments
 (0)