Search in sources :

Example 1 with TransactionReceiptQuery

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

the class ScheduleIdenticalTransactionExample method main.

public static void main(String[] args) throws PrecheckStatusException, TimeoutException, 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);
    System.out.println("threshold key example");
    System.out.println("Keys:");
    PrivateKey[] privKeys = new PrivateKey[3];
    PublicKey[] pubKeys = new PublicKey[3];
    Client[] clients = new Client[3];
    AccountId[] accounts = new AccountId[3];
    @Var ScheduleId scheduleID = null;
    // Loop to generate keys, clients, and accounts
    for (int i = 0; i < 3; i++) {
        PrivateKey newKey = PrivateKey.generateED25519();
        privKeys[i] = newKey;
        pubKeys[i] = newKey.getPublicKey();
        System.out.println("Key #" + i + ":");
        System.out.println("private = " + privKeys[i]);
        System.out.println("public = " + pubKeys[i]);
        TransactionResponse createResponse = new AccountCreateTransaction().setKey(newKey).setInitialBalance(new Hbar(1)).execute(client);
        // Make sure the transaction succeeded
        TransactionReceipt transactionReceipt = createResponse.getReceipt(client);
        Client newClient = Client.forName(HEDERA_NETWORK);
        newClient.setOperator(Objects.requireNonNull(transactionReceipt.accountId), newKey);
        clients[i] = newClient;
        accounts[i] = transactionReceipt.accountId;
        System.out.println("account = " + accounts[i]);
    }
    // Loop to generate keys, clients, and accounts
    // A threshold key with a threshold of 2 and length of 3 requires
    // at least 2 of the 3 keys to sign anything modifying the account
    KeyList keyList = KeyList.withThreshold(2);
    Collections.addAll(keyList, pubKeys);
    // We are using all of these keys, so the scheduled transaction doesn't automatically go through
    // It works perfectly fine with just one key
    TransactionResponse createResponse = new AccountCreateTransaction().setKey(keyList).setInitialBalance(new Hbar(10)).execute(client);
    // Make sure the transaction succeeded
    TransactionReceipt receipt = createResponse.getReceipt(client);
    AccountId thresholdAccount = receipt.accountId;
    System.out.println("threshold account = " + thresholdAccount);
    for (Client loopClient : clients) {
        AccountId operatorId = loopClient.getOperatorAccountId();
        // Each loopClient creates an identical transaction, sending 1 hbar to each of the created accounts,
        // sent from the threshold Account
        TransferTransaction tx = new TransferTransaction();
        for (AccountId account : accounts) {
            tx.addHbarTransfer(account, new Hbar(1));
        }
        tx.addHbarTransfer(Objects.requireNonNull(thresholdAccount), new Hbar(3).negated());
        ScheduleCreateTransaction scheduledTx = new ScheduleCreateTransaction().setScheduledTransaction(tx);
        scheduledTx.setPayerAccountId(thresholdAccount);
        TransactionResponse response = scheduledTx.execute(loopClient);
        TransactionReceipt loopReceipt = new TransactionReceiptQuery().setTransactionId(response.transactionId).setNodeAccountIds(Collections.singletonList(response.nodeId)).execute(loopClient);
        System.out.println("operator [" + operatorId + "]: scheduleID = " + loopReceipt.scheduleId);
        // Save the schedule ID, so that it can be asserted for each loopClient submission
        if (scheduleID == null) {
            scheduleID = loopReceipt.scheduleId;
        }
        if (!scheduleID.equals(Objects.requireNonNull(loopReceipt.scheduleId))) {
            System.out.println("invalid generated schedule id, expected " + scheduleID + ", got " + loopReceipt.scheduleId);
            return;
        }
        // If the status return by the receipt is related to already created, execute a schedule sign transaction
        if (loopReceipt.status == Status.IDENTICAL_SCHEDULE_ALREADY_CREATED) {
            TransactionResponse signTransaction = new ScheduleSignTransaction().setScheduleId(scheduleID).setNodeAccountIds(Collections.singletonList(createResponse.nodeId)).setScheduleId(loopReceipt.scheduleId).execute(loopClient);
            TransactionReceipt signReceipt = new TransactionReceiptQuery().setTransactionId(signTransaction.transactionId).execute(client);
            if (signReceipt.status != Status.SUCCESS && signReceipt.status != Status.SCHEDULE_ALREADY_EXECUTED) {
                System.out.println("Bad status while getting receipt of schedule sign with operator " + operatorId + ": " + signReceipt.status);
                return;
            }
        }
    }
    System.out.println(new ScheduleInfoQuery().setScheduleId(scheduleID).execute(client));
    AccountDeleteTransaction thresholdDeleteTx = new AccountDeleteTransaction().setAccountId(thresholdAccount).setTransferAccountId(OPERATOR_ID).freezeWith(client);
    for (int i = 0; i < 3; i++) {
        thresholdDeleteTx.sign(privKeys[i]);
        new AccountDeleteTransaction().setAccountId(accounts[i]).setTransferAccountId(OPERATOR_ID).freezeWith(client).sign(privKeys[i]).execute(client).getReceipt(client);
    }
    thresholdDeleteTx.execute(client).getReceipt(client);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) PublicKey(com.hedera.hashgraph.sdk.PublicKey) Var(com.google.errorprone.annotations.Var) ScheduleSignTransaction(com.hedera.hashgraph.sdk.ScheduleSignTransaction) KeyList(com.hedera.hashgraph.sdk.KeyList) AccountDeleteTransaction(com.hedera.hashgraph.sdk.AccountDeleteTransaction) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) ScheduleInfoQuery(com.hedera.hashgraph.sdk.ScheduleInfoQuery) ScheduleId(com.hedera.hashgraph.sdk.ScheduleId) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) TransactionReceiptQuery(com.hedera.hashgraph.sdk.TransactionReceiptQuery) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) ScheduleCreateTransaction(com.hedera.hashgraph.sdk.ScheduleCreateTransaction)

Example 2 with TransactionReceiptQuery

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

the class ReceiptQueryIntegrationTest method getCostTransactionRecord.

@Test
@DisplayName("Can get Record cost")
@SuppressWarnings("UnusedVariable")
void getCostTransactionRecord() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    var key = PrivateKey.generateED25519();
    var response = new AccountCreateTransaction().setKey(key).execute(testEnv.client);
    new TransactionReceiptQuery().setTransactionId(response.transactionId).execute(testEnv.client);
    var recordQuery = new TransactionRecordQuery().setTransactionId(response.transactionId);
    var cost = recordQuery.getCost(testEnv.client);
    var record = recordQuery.execute(testEnv.client);
    testEnv.close(record.receipt.accountId, key);
}
Also used : TransactionRecordQuery(com.hedera.hashgraph.sdk.TransactionRecordQuery) TransactionReceiptQuery(com.hedera.hashgraph.sdk.TransactionReceiptQuery) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 3 with TransactionReceiptQuery

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

the class ReceiptQueryIntegrationTest method getCostBigMaxTransactionRecord.

@Test
@DisplayName("Can get Record cost with big max set")
@SuppressWarnings("UnusedVariable")
void getCostBigMaxTransactionRecord() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    var key = PrivateKey.generateED25519();
    var response = new AccountCreateTransaction().setKey(key).execute(testEnv.client);
    new TransactionReceiptQuery().setTransactionId(response.transactionId).execute(testEnv.client);
    var recordQuery = new TransactionRecordQuery().setTransactionId(response.transactionId).setMaxQueryPayment(new Hbar(1000));
    var cost = recordQuery.getCost(testEnv.client);
    var record = recordQuery.execute(testEnv.client);
    testEnv.close(record.receipt.accountId, key);
}
Also used : TransactionRecordQuery(com.hedera.hashgraph.sdk.TransactionRecordQuery) TransactionReceiptQuery(com.hedera.hashgraph.sdk.TransactionReceiptQuery) Hbar(com.hedera.hashgraph.sdk.Hbar) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 4 with TransactionReceiptQuery

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

the class ReceiptQueryIntegrationTest method getCostInsufficientTxFeeTransactionRecord.

@Test
@DisplayName("Insufficient transaction fee error for transaction record query")
void getCostInsufficientTxFeeTransactionRecord() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    var key = PrivateKey.generateED25519();
    var response = new AccountCreateTransaction().setKey(key).execute(testEnv.client);
    var receipt = new TransactionReceiptQuery().setTransactionId(response.transactionId).execute(testEnv.client);
    var recordQuery = new TransactionRecordQuery().setTransactionId(response.transactionId);
    assertThatExceptionOfType(PrecheckStatusException.class).isThrownBy(() -> {
        recordQuery.setQueryPayment(Hbar.fromTinybars(1)).execute(testEnv.client);
    }).satisfies(error -> assertThat(error.status.toString()).isEqualTo("INSUFFICIENT_TX_FEE"));
    testEnv.close(receipt.accountId, key);
}
Also used : TransactionRecordQuery(com.hedera.hashgraph.sdk.TransactionRecordQuery) TransactionReceiptQuery(com.hedera.hashgraph.sdk.TransactionReceiptQuery) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 5 with TransactionReceiptQuery

use of com.hedera.hashgraph.sdk.TransactionReceiptQuery in project hedera-mirror-node by hashgraph.

the class TransactionPublisher method processTransactionResponse.

private Mono<PublishResponse.PublishResponseBuilder> processTransactionResponse(Client client, PublishRequest request, TransactionResponse transactionResponse) {
    TransactionId transactionId = transactionResponse.transactionId;
    PublishResponse.PublishResponseBuilder builder = PublishResponse.builder().request(request).timestamp(Instant.now()).transactionId(transactionId);
    if (request.isRecord()) {
        // TransactionId.getRecord() is inefficient doing a get receipt, a cost query, then the get record
        TransactionRecordQuery transactionRecordQuery = new TransactionRecordQuery().setQueryPayment(Hbar.from(1, HbarUnit.HBAR)).setTransactionId(transactionId);
        return execute(client, transactionRecordQuery).map(r -> builder.record(r).receipt(r.receipt));
    } else if (request.isReceipt()) {
        var receiptQuery = new TransactionReceiptQuery().setTransactionId(transactionId);
        return execute(client, receiptQuery).map(builder::receipt);
    }
    return Mono.just(builder);
}
Also used : TransactionRecordQuery(com.hedera.hashgraph.sdk.TransactionRecordQuery) TransactionReceiptQuery(com.hedera.hashgraph.sdk.TransactionReceiptQuery) TransactionId(com.hedera.hashgraph.sdk.TransactionId)

Aggregations

TransactionReceiptQuery (com.hedera.hashgraph.sdk.TransactionReceiptQuery)5 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)4 TransactionRecordQuery (com.hedera.hashgraph.sdk.TransactionRecordQuery)4 DisplayName (org.junit.jupiter.api.DisplayName)3 Test (org.junit.jupiter.api.Test)3 Hbar (com.hedera.hashgraph.sdk.Hbar)2 Var (com.google.errorprone.annotations.Var)1 AccountDeleteTransaction (com.hedera.hashgraph.sdk.AccountDeleteTransaction)1 AccountId (com.hedera.hashgraph.sdk.AccountId)1 Client (com.hedera.hashgraph.sdk.Client)1 KeyList (com.hedera.hashgraph.sdk.KeyList)1 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)1 PublicKey (com.hedera.hashgraph.sdk.PublicKey)1 ScheduleCreateTransaction (com.hedera.hashgraph.sdk.ScheduleCreateTransaction)1 ScheduleId (com.hedera.hashgraph.sdk.ScheduleId)1 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)1 ScheduleSignTransaction (com.hedera.hashgraph.sdk.ScheduleSignTransaction)1 TransactionId (com.hedera.hashgraph.sdk.TransactionId)1 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)1 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)1