Skip to content

Commit e48e776

Browse files
committed
Get balance for operator account more easily
Signed-off-by: Hendrik Ebbers <[email protected]>
1 parent 57bd49d commit e48e776

File tree

4 files changed

+27
-4
lines changed

4 files changed

+27
-4
lines changed

hiero-enterprise-base/src/main/java/com/openelements/hiero/base/AccountClient.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,4 +90,13 @@ default Hbar getAccountBalance(@NonNull String accountId) throws HieroException
9090
Objects.requireNonNull(accountId, "newAccountId must not be null");
9191
return getAccountBalance(AccountId.fromString(accountId));
9292
}
93+
94+
/**
95+
* Returns the balance of the operator account.
96+
*
97+
* @return the balance of the operator account
98+
* @throws HieroException if the balance could not be retrieved
99+
*/
100+
@NonNull
101+
Hbar getOperatorAccountBalance() throws HieroException;
93102
}

hiero-enterprise-base/src/main/java/com/openelements/hiero/base/implementation/AccountClientImpl.java

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package com.openelements.hiero.base.implementation;
2+
23
import com.hedera.hashgraph.sdk.AccountId;
34
import com.hedera.hashgraph.sdk.Hbar;
4-
import com.openelements.hiero.base.data.Account;
55
import com.openelements.hiero.base.AccountClient;
66
import com.openelements.hiero.base.HieroException;
7+
import com.openelements.hiero.base.data.Account;
78
import com.openelements.hiero.base.protocol.AccountBalanceRequest;
89
import com.openelements.hiero.base.protocol.AccountBalanceResponse;
910
import com.openelements.hiero.base.protocol.AccountCreateRequest;
@@ -27,11 +28,11 @@ public Account createAccount(@NonNull Hbar initialBalance) throws HieroException
2728
if (initialBalance == null) {
2829
throw new NullPointerException("initialBalance must not be null");
2930
}
30-
31+
3132
if (initialBalance.toTinybars() < 0) {
3233
throw new HieroException("Invalid initial balance: must be non-negative");
3334
}
34-
35+
3536
try {
3637
final AccountCreateRequest request = AccountCreateRequest.of(initialBalance);
3738
final AccountCreateResult result = client.executeAccountCreateTransaction(request);
@@ -40,7 +41,7 @@ public Account createAccount(@NonNull Hbar initialBalance) throws HieroException
4041
throw new HieroException("Error while creating Account", e);
4142
}
4243
}
43-
44+
4445

4546
@Override
4647
public void deleteAccount(@NonNull Account account) throws HieroException {
@@ -61,4 +62,9 @@ public Hbar getAccountBalance(@NonNull AccountId account) throws HieroException
6162
final AccountBalanceResponse response = client.executeAccountBalanceQuery(request);
6263
return response.hbars();
6364
}
65+
66+
@Override
67+
public @NonNull Hbar getOperatorAccountBalance() throws HieroException {
68+
return getAccountBalance(client.getOperatorAccountId());
69+
}
6470
}

hiero-enterprise-base/src/main/java/com/openelements/hiero/base/implementation/ProtocolLayerClientImpl.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.hedera.hashgraph.sdk.AccountBalanceQuery;
66
import com.hedera.hashgraph.sdk.AccountCreateTransaction;
77
import com.hedera.hashgraph.sdk.AccountDeleteTransaction;
8+
import com.hedera.hashgraph.sdk.AccountId;
89
import com.hedera.hashgraph.sdk.ContractCreateTransaction;
910
import com.hedera.hashgraph.sdk.ContractDeleteTransaction;
1011
import com.hedera.hashgraph.sdk.ContractExecuteTransaction;
@@ -573,4 +574,9 @@ public Runnable addTransactionListener(@NonNull TransactionListener listener) {
573574
listeners.add(listener);
574575
return () -> listeners.remove(listener);
575576
}
577+
578+
@Override
579+
public AccountId getOperatorAccountId() {
580+
return hieroContext.getOperatorAccount().accountId();
581+
}
576582
}

hiero-enterprise-base/src/main/java/com/openelements/hiero/base/protocol/ProtocolLayerClient.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package com.openelements.hiero.base.protocol;
22

3+
import com.hedera.hashgraph.sdk.AccountId;
34
import com.openelements.hiero.base.HieroException;
45
import org.jspecify.annotations.NonNull;
56

@@ -226,4 +227,5 @@ TopicSubmitMessageResult executeTopicMessageSubmitTransaction(@NonNull TopicSubm
226227
@NonNull
227228
Runnable addTransactionListener(@NonNull TransactionListener listener);
228229

230+
AccountId getOperatorAccountId();
229231
}

0 commit comments

Comments
 (0)