use of com.hedera.hashgraph.sdk.Client in project hedera-sdk-java by hashgraph.
the class GetAccountBalanceExample method main.
public static void main(String[] args) throws PrecheckStatusException, TimeoutException {
Client client = Client.forName(HEDERA_NETWORK);
// Because AccountBalanceQuery is a free query, we can make it without setting an operator on the client.
Hbar balance = new AccountBalanceQuery().setAccountId(OPERATOR_ID).execute(client).hbars;
System.out.println("balance = " + balance);
}
use of com.hedera.hashgraph.sdk.Client in project hedera-sdk-java by hashgraph.
the class GetExchangeRatesExample method main.
public static void main(String[] args) throws ReceiptStatusException, TimeoutException, PrecheckStatusException, InvalidProtocolBufferException {
Client client = Client.forName(HEDERA_NETWORK);
// Defaults the operator account ID and key such that all generated transactions will be paid for
// by this account and be signed by this key
client.setOperator(OPERATOR_ID, OPERATOR_KEY);
// Get contents of file 0.0.112
ByteString fileContentsByteString = new FileContentsQuery().setFileId(FileId.fromString("0.0.112")).execute(client);
byte[] fileContents = fileContentsByteString.toByteArray();
ExchangeRates exchangeRateSet = ExchangeRates.fromBytes(fileContents);
// Prints query results to console
System.out.println("Current numerator: " + exchangeRateSet.currentRate.cents);
System.out.println("Current denominator: " + exchangeRateSet.currentRate.hbars);
System.out.println("Current expiration time: " + exchangeRateSet.currentRate.expirationTime.toString());
System.out.println("Current Exchange Rate: " + exchangeRateSet.currentRate.exchangeRateInCents);
System.out.println("Next numerator: " + exchangeRateSet.nextRate.cents);
System.out.println("Next denominator: " + exchangeRateSet.nextRate.hbars);
System.out.println("Next expiration time: " + exchangeRateSet.nextRate.expirationTime.toString());
System.out.println("Next Exchange Rate: " + exchangeRateSet.nextRate.exchangeRateInCents);
}
use of com.hedera.hashgraph.sdk.Client in project hedera-sdk-java by hashgraph.
the class MultiSigOfflineExample method main.
public static void main(String[] args) throws PrecheckStatusException, TimeoutException, ReceiptStatusException, InvalidProtocolBufferException {
Client client = Client.forName(HEDERA_NETWORK);
// Defaults the operator account ID and key such that all generated transactions will be paid for
// by this account and be signed by this key
client.setOperator(OPERATOR_ID, OPERATOR_KEY);
PrivateKey user1Key = PrivateKey.generateED25519();
PrivateKey user2Key = PrivateKey.generateED25519();
System.out.println("private key for user 1 = " + user1Key);
System.out.println("public key for user 1 = " + user1Key.getPublicKey());
System.out.println("private key for user 2 = " + user2Key);
System.out.println("public key for user 2 = " + user2Key.getPublicKey());
// create a multi-sig account
KeyList keylist = new KeyList();
keylist.add(user1Key);
keylist.add(user2Key);
TransactionResponse createAccountTransaction = new AccountCreateTransaction().setInitialBalance(new Hbar(2)).setKey(keylist).execute(client);
@Var TransactionReceipt receipt = createAccountTransaction.getReceipt(client);
System.out.println("account id = " + receipt.accountId);
// create a transfer from new account to 0.0.3
TransferTransaction transferTransaction = new TransferTransaction().setNodeAccountIds(Collections.singletonList(new AccountId(3))).addHbarTransfer(Objects.requireNonNull(receipt.accountId), Hbar.from(-1)).addHbarTransfer(new AccountId(3), new Hbar(1)).freezeWith(client);
// convert transaction to bytes to send to signatories
byte[] transactionBytes = transferTransaction.toBytes();
Transaction<?> transactionToExecute = Transaction.fromBytes(transactionBytes);
// ask users to sign and return signature
byte[] user1Signature = user1Key.signTransaction(Transaction.fromBytes(transactionBytes));
byte[] user2Signature = user2Key.signTransaction(Transaction.fromBytes(transactionBytes));
// recreate the transaction from bytes
transactionToExecute.signWithOperator(client);
transactionToExecute.addSignature(user1Key.getPublicKey(), user1Signature);
transactionToExecute.addSignature(user2Key.getPublicKey(), user2Signature);
TransactionResponse result = transactionToExecute.execute(client);
receipt = result.getReceipt(client);
System.out.println(receipt.status);
}
use of com.hedera.hashgraph.sdk.Client in project hedera-mirror-node by hashgraph.
the class SDKClient method getValidatedClient.
private Client getValidatedClient(Map<String, AccountId> currentNetworkMap, Client client) throws InterruptedException {
Map<String, AccountId> validNodes = new LinkedHashMap<>();
for (var nodeEntry : currentNetworkMap.entrySet()) {
try {
if (validateNode(nodeEntry.getValue().toString(), client)) {
validNodes.putIfAbsent(nodeEntry.getKey(), nodeEntry.getValue());
log.trace("Added node {} at endpoint {} to list of valid nodes", nodeEntry.getValue(), nodeEntry.getKey());
}
} catch (Exception e) {
//
}
}
log.info("{} of {} nodes are reachable", validNodes.size(), currentNetworkMap.size());
if (validNodes.size() == 0) {
throw new IllegalStateException("All provided nodes are unreachable!");
}
log.info("Creating validated client using nodes: {} nodes", validNodes);
Client validatedClient = Client.forNetwork(validNodes);
validatedClient.setOperator(expandedOperatorAccountId.getAccountId(), expandedOperatorAccountId.getPrivateKey());
validatedClient.setMirrorNetwork(List.of(mirrorNodeAddress));
return validatedClient;
}
use of com.hedera.hashgraph.sdk.Client in project hedera-mirror-node by hashgraph.
the class TransactionPublisher method getClients.
private synchronized Flux<Client> getClients() {
NodeValidationProperties validationProperties = monitorProperties.getNodeValidation();
var configuredNodes = monitorProperties.getNodes();
Map<String, AccountId> nodeMap = configuredNodes.stream().collect(Collectors.toMap(NodeProperties::getEndpoint, p -> AccountId.fromString(p.getAccountId())));
this.nodes.addAll(configuredNodes);
Client client = toClient(nodeMap);
client.setMaxAttempts(validationProperties.getMaxAttempts());
client.setMaxBackoff(validationProperties.getMaxBackoff());
client.setMinBackoff(validationProperties.getMinBackoff());
client.setRequestTimeout(validationProperties.getRequestTimeout());
this.validationClient.set(client);
if (validationProperties.isEnabled() && nodeValidator.get() == null) {
int nodeCount = configuredNodes.size();
int parallelism = Math.min(nodeCount, validationProperties.getMaxThreads());
var scheduler = Schedulers.newParallel("validator", parallelism + 1);
var disposable = Flux.interval(Duration.ZERO, validationProperties.getFrequency(), scheduler).filter(// In case it's later disabled
i -> validationProperties.isEnabled()).flatMap(i -> Flux.fromIterable(configuredNodes)).parallel(parallelism).runOn(scheduler).map(this::validateNode).sequential().buffer(nodeCount).doOnNext(i -> log.info("{} of {} nodes are functional", nodes.size(), nodeCount)).doOnSubscribe(s -> log.info("Starting node validation")).onErrorContinue((e, i) -> log.error("Exception validating nodes: ", e)).subscribe();
nodeValidator.set(disposable);
}
return Flux.range(0, publishProperties.getClients()).flatMap(i -> Mono.defer(() -> Mono.just(toClient(nodeMap))));
}
Aggregations