Search in sources :

Example 6 with TransactionResponse

use of com.hedera.hashgraph.sdk.TransactionResponse in project hedera-sdk-java by hashgraph.

the class TopicWithAdminKeyExample method createTopicWithAdminKey.

private void createTopicWithAdminKey() throws TimeoutException, PrecheckStatusException, ReceiptStatusException {
    // Generate the initial keys that are part of the adminKey's thresholdKey.
    // 3 ED25519 keys part of a 2-of-3 threshold key.
    initialAdminKeys = new PrivateKey[3];
    J8Arrays.setAll(initialAdminKeys, i -> PrivateKey.generate());
    KeyList thresholdKey = KeyList.withThreshold(2);
    Collections.addAll(thresholdKey, initialAdminKeys);
    Transaction<?> transaction = new TopicCreateTransaction().setTopicMemo("demo topic").setAdminKey(thresholdKey).freezeWith(hapiClient);
    // Sign the transaction with 2 of 3 keys that are part of the adminKey threshold key.
    J8Arrays.stream(initialAdminKeys, 0, 2).forEach(k -> {
        System.out.println("Signing ConsensusTopicCreateTransaction with key " + k);
        transaction.sign(k);
    });
    TransactionResponse transactionResponse = transaction.execute(hapiClient);
    topicId = transactionResponse.getReceipt(hapiClient).topicId;
    System.out.println("Created new topic " + topicId + " with 2-of-3 threshold key as adminKey.");
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) KeyList(com.hedera.hashgraph.sdk.KeyList)

Example 7 with TransactionResponse

use of com.hedera.hashgraph.sdk.TransactionResponse in project hedera-sdk-java by hashgraph.

the class TransferCryptoExample method main.

public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException {
    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);
    AccountId recipientId = AccountId.fromString("0.0.3");
    Hbar amount = Hbar.fromTinybars(10_000);
    Hbar senderBalanceBefore = new AccountBalanceQuery().setAccountId(OPERATOR_ID).execute(client).hbars;
    Hbar receiptBalanceBefore = new AccountBalanceQuery().setAccountId(recipientId).execute(client).hbars;
    System.out.println("" + OPERATOR_ID + " balance = " + senderBalanceBefore);
    System.out.println("" + recipientId + " balance = " + receiptBalanceBefore);
    TransactionResponse transactionResponse = new TransferTransaction().addHbarTransfer(OPERATOR_ID, amount.negated()).addHbarTransfer(recipientId, amount).setTransactionMemo("transfer test").execute(client);
    System.out.println("transaction ID: " + transactionResponse);
    TransactionRecord record = transactionResponse.getRecord(client);
    System.out.println("transferred " + amount + "...");
    Hbar senderBalanceAfter = new AccountBalanceQuery().setAccountId(OPERATOR_ID).execute(client).hbars;
    Hbar receiptBalanceAfter = new AccountBalanceQuery().setAccountId(recipientId).execute(client).hbars;
    System.out.println("" + OPERATOR_ID + " balance = " + senderBalanceAfter);
    System.out.println("" + recipientId + " balance = " + receiptBalanceAfter);
    System.out.println("Transfer memo: " + record.transactionMemo);
}
Also used : AccountId(com.hedera.hashgraph.sdk.AccountId) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) Hbar(com.hedera.hashgraph.sdk.Hbar) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) TransactionRecord(com.hedera.hashgraph.sdk.TransactionRecord)

Example 8 with TransactionResponse

use of com.hedera.hashgraph.sdk.TransactionResponse in project hedera-sdk-java by hashgraph.

the class TransferTokensExample method main.

public static void main(String[] args) throws Exception {
    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);
    // Generate a Ed25519 private, public key pair
    PrivateKey key1 = PrivateKey.generateED25519();
    PrivateKey key2 = PrivateKey.generateED25519();
    System.out.println("private key = " + key1);
    System.out.println("public key = " + key1.getPublicKey());
    System.out.println("private key = " + key2);
    System.out.println("public key = " + key2.getPublicKey());
    @Var TransactionResponse response = new AccountCreateTransaction().setKey(key1.getPublicKey()).setInitialBalance(Hbar.fromTinybars(1000)).execute(client);
    // This will wait for the receipt to become available
    @Var TransactionReceipt receipt = response.getReceipt(client);
    AccountId accountId1 = Objects.requireNonNull(receipt.accountId);
    System.out.println("accountId1 = " + accountId1);
    response = new AccountCreateTransaction().setKey(key2.getPublicKey()).setInitialBalance(Hbar.fromTinybars(1000)).execute(client);
    // This will wait for the receipt to become available
    receipt = response.getReceipt(client);
    AccountId accountId2 = Objects.requireNonNull(receipt.accountId);
    System.out.println("accountId2 = " + accountId1);
    response = new TokenCreateTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setTokenName("ffff").setTokenSymbol("F").setDecimals(3).setInitialSupply(1000000).setTreasuryAccountId(OPERATOR_ID).setAdminKey(OPERATOR_KEY.getPublicKey()).setFreezeKey(OPERATOR_KEY.getPublicKey()).setWipeKey(OPERATOR_KEY.getPublicKey()).setKycKey(OPERATOR_KEY.getPublicKey()).setSupplyKey(OPERATOR_KEY.getPublicKey()).setFreezeDefault(false).execute(client);
    TokenId tokenId = Objects.requireNonNull(response.getReceipt(client).tokenId);
    System.out.println("token = " + tokenId);
    new TokenAssociateTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setAccountId(accountId1).setTokenIds(Collections.singletonList(tokenId)).freezeWith(client).sign(OPERATOR_KEY).sign(key1).execute(client).getReceipt(client);
    System.out.println("Associated account " + accountId1 + " with token " + tokenId);
    new TokenAssociateTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setAccountId(accountId2).setTokenIds(Collections.singletonList(tokenId)).freezeWith(client).sign(OPERATOR_KEY).sign(key2).execute(client).getReceipt(client);
    System.out.println("Associated account " + accountId2 + " with token " + tokenId);
    new TokenGrantKycTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setAccountId(accountId1).setTokenId(tokenId).execute(client).getReceipt(client);
    System.out.println("Granted KYC for account " + accountId1 + " on token " + tokenId);
    new TokenGrantKycTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setAccountId(accountId2).setTokenId(tokenId).execute(client).getReceipt(client);
    System.out.println("Granted KYC for account " + accountId2 + " on token " + tokenId);
    new TransferTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).addTokenTransfer(tokenId, OPERATOR_ID, -10).addTokenTransfer(tokenId, accountId1, 10).execute(client).getReceipt(client);
    System.out.println("Sent 10 tokens from account " + OPERATOR_ID + " to account " + accountId1 + " on token " + tokenId);
    new TransferTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).addTokenTransfer(tokenId, accountId1, -10).addTokenTransfer(tokenId, accountId2, 10).freezeWith(client).sign(key1).execute(client).getReceipt(client);
    System.out.println("Sent 10 tokens from account " + accountId1 + " to account " + accountId2 + " on token " + tokenId);
    new TransferTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).addTokenTransfer(tokenId, accountId2, -10).addTokenTransfer(tokenId, accountId1, 10).freezeWith(client).sign(key2).execute(client).getReceipt(client);
    System.out.println("Sent 10 tokens from account " + accountId2 + " to account " + accountId1 + " on token " + tokenId);
    new TokenWipeTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setTokenId(tokenId).setAccountId(accountId1).setAmount(10).execute(client).getReceipt(client);
    System.out.println("Wiped balance of account " + accountId1);
    new TokenDeleteTransaction().setNodeAccountIds(Collections.singletonList(response.nodeId)).setTokenId(tokenId).execute(client).getReceipt(client);
    System.out.println("Deleted token " + tokenId);
    new AccountDeleteTransaction().setAccountId(accountId1).setTransferAccountId(OPERATOR_ID).freezeWith(client).sign(OPERATOR_KEY).sign(key1).execute(client).getReceipt(client);
    System.out.println("Deleted accountId1 " + accountId1);
    new AccountDeleteTransaction().setAccountId(accountId2).setTransferAccountId(OPERATOR_ID).freezeWith(client).sign(OPERATOR_KEY).sign(key2).execute(client).getReceipt(client);
    System.out.println("Deleted accountId2" + accountId2);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) Var(com.google.errorprone.annotations.Var) TokenAssociateTransaction(com.hedera.hashgraph.sdk.TokenAssociateTransaction) TokenDeleteTransaction(com.hedera.hashgraph.sdk.TokenDeleteTransaction) AccountDeleteTransaction(com.hedera.hashgraph.sdk.AccountDeleteTransaction) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) TokenCreateTransaction(com.hedera.hashgraph.sdk.TokenCreateTransaction) TokenGrantKycTransaction(com.hedera.hashgraph.sdk.TokenGrantKycTransaction) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) Client(com.hedera.hashgraph.sdk.Client) TokenId(com.hedera.hashgraph.sdk.TokenId) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) TokenWipeTransaction(com.hedera.hashgraph.sdk.TokenWipeTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction)

Example 9 with TransactionResponse

use of com.hedera.hashgraph.sdk.TransactionResponse in project hedera-sdk-java by hashgraph.

the class DeleteAccountExample method main.

public static void main(String[] args) throws TimeoutException, PrecheckStatusException, ReceiptStatusException {
    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);
    // Generate a Ed25519 private, public key pair
    PrivateKey newKey = PrivateKey.generateED25519();
    PublicKey newPublicKey = newKey.getPublicKey();
    System.out.println("private key = " + newKey);
    System.out.println("public key = " + newPublicKey);
    TransactionResponse transactionResponse = new AccountCreateTransaction().setKey(newKey).setInitialBalance(new Hbar(2)).execute(client);
    // This will wait for the receipt to become available
    TransactionReceipt receipt = transactionResponse.getReceipt(client);
    AccountId newAccountId = Objects.requireNonNull(receipt.accountId);
    System.out.println("account = " + newAccountId);
    new AccountDeleteTransaction().setTransactionId(TransactionId.generate(newAccountId)).setAccountId(newAccountId).setTransferAccountId(OPERATOR_ID).freezeWith(client).sign(newKey).execute(client).getReceipt(client);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) AccountId(com.hedera.hashgraph.sdk.AccountId) PublicKey(com.hedera.hashgraph.sdk.PublicKey) AccountDeleteTransaction(com.hedera.hashgraph.sdk.AccountDeleteTransaction) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) Client(com.hedera.hashgraph.sdk.Client) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction)

Example 10 with TransactionResponse

use of com.hedera.hashgraph.sdk.TransactionResponse in project hedera-sdk-java by hashgraph.

the class ConsensusPubSubExample method main.

public static void main(String[] args) throws TimeoutException, InterruptedException, PrecheckStatusException, ReceiptStatusException {
    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);
    TransactionResponse transactionResponse = new TopicCreateTransaction().execute(client);
    TransactionReceipt transactionReceipt = transactionResponse.getReceipt(client);
    TopicId topicId = Objects.requireNonNull(transactionReceipt.topicId);
    System.out.println("New topic created: " + topicId);
    Thread.sleep(5000);
    new TopicMessageQuery().setTopicId(topicId).subscribe(client, resp -> {
        String messageAsString = new String(resp.contents, StandardCharsets.UTF_8);
        System.out.println(resp.consensusTimestamp + " received topic message: " + messageAsString);
    });
    // noinspection InfiniteLoopStatement
    for (int i = 0; ; i++) {
        new TopicMessageSubmitTransaction().setTopicId(topicId).setMessage("hello, HCS! " + i).execute(client).getReceipt(client);
        Thread.sleep(2500);
    }
}
Also used : TopicCreateTransaction(com.hedera.hashgraph.sdk.TopicCreateTransaction) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) TopicId(com.hedera.hashgraph.sdk.TopicId) Client(com.hedera.hashgraph.sdk.Client) TopicMessageQuery(com.hedera.hashgraph.sdk.TopicMessageQuery) TopicMessageSubmitTransaction(com.hedera.hashgraph.sdk.TopicMessageSubmitTransaction)

Aggregations

TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)28 Client (com.hedera.hashgraph.sdk.Client)19 Hbar (com.hedera.hashgraph.sdk.Hbar)19 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)17 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)15 AccountId (com.hedera.hashgraph.sdk.AccountId)13 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)13 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)13 Var (com.google.errorprone.annotations.Var)9 KeyList (com.hedera.hashgraph.sdk.KeyList)9 FileCreateTransaction (com.hedera.hashgraph.sdk.FileCreateTransaction)6 FileId (com.hedera.hashgraph.sdk.FileId)6 PublicKey (com.hedera.hashgraph.sdk.PublicKey)6 AccountBalanceQuery (com.hedera.hashgraph.sdk.AccountBalanceQuery)4 AccountDeleteTransaction (com.hedera.hashgraph.sdk.AccountDeleteTransaction)4 ScheduleId (com.hedera.hashgraph.sdk.ScheduleId)4 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)4 ScheduleSignTransaction (com.hedera.hashgraph.sdk.ScheduleSignTransaction)4 TokenAssociateTransaction (com.hedera.hashgraph.sdk.TokenAssociateTransaction)4 TokenCreateTransaction (com.hedera.hashgraph.sdk.TokenCreateTransaction)4