Search in sources :

Example 6 with AccountDeleteTransaction

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

the class AccountDeleteIntegrationTest method cannotDeleteAccountThatHasNotSignedTransaction.

@Test
@DisplayName("Cannot delete account that has not signed transaction")
void cannotDeleteAccountThatHasNotSignedTransaction() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    var key = PrivateKey.generateED25519();
    var response = new AccountCreateTransaction().setKey(key).setInitialBalance(new Hbar(1)).execute(testEnv.client);
    var accountId = Objects.requireNonNull(response.getReceipt(testEnv.client).accountId);
    assertThatExceptionOfType(ReceiptStatusException.class).isThrownBy(() -> {
        new AccountDeleteTransaction().setAccountId(accountId).setTransferAccountId(testEnv.operatorId).execute(testEnv.client).getReceipt(testEnv.client);
    }).withMessageContaining(Status.INVALID_SIGNATURE.toString());
    testEnv.close(accountId, key);
}
Also used : AccountDeleteTransaction(com.hedera.hashgraph.sdk.AccountDeleteTransaction) 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 7 with AccountDeleteTransaction

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

the class AccountDeleteTransactionSupplierTest method createWithMinimumData.

@Test
void createWithMinimumData() {
    AccountDeleteTransactionSupplier accountDeleteTransactionSupplier = new AccountDeleteTransactionSupplier();
    accountDeleteTransactionSupplier.setAccountId(ACCOUNT_ID.toString());
    AccountDeleteTransaction actual = accountDeleteTransactionSupplier.get();
    assertThat(actual).returns(ACCOUNT_ID, AccountDeleteTransaction::getAccountId).returns(MAX_TRANSACTION_FEE_HBAR, AccountDeleteTransaction::getMaxTransactionFee).returns(AccountId.fromString("0.0.2"), AccountDeleteTransaction::getTransferAccountId);
}
Also used : AccountDeleteTransaction(com.hedera.hashgraph.sdk.AccountDeleteTransaction) Test(org.junit.jupiter.api.Test) AbstractTransactionSupplierTest(com.hedera.mirror.monitor.publish.transaction.AbstractTransactionSupplierTest)

Example 8 with AccountDeleteTransaction

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

the class AccountDeleteTransactionSupplierTest method createWithCustomData.

@Test
void createWithCustomData() {
    AccountDeleteTransactionSupplier accountDeleteTransactionSupplier = new AccountDeleteTransactionSupplier();
    accountDeleteTransactionSupplier.setAccountId(ACCOUNT_ID.toString());
    accountDeleteTransactionSupplier.setTransferAccountId(ACCOUNT_ID_2.toString());
    accountDeleteTransactionSupplier.setMaxTransactionFee(1);
    AccountDeleteTransaction actual = accountDeleteTransactionSupplier.get();
    assertThat(actual).returns(ACCOUNT_ID, AccountDeleteTransaction::getAccountId).returns(ONE_TINYBAR, AccountDeleteTransaction::getMaxTransactionFee).returns(ACCOUNT_ID_2, AccountDeleteTransaction::getTransferAccountId);
}
Also used : AccountDeleteTransaction(com.hedera.hashgraph.sdk.AccountDeleteTransaction) Test(org.junit.jupiter.api.Test) AbstractTransactionSupplierTest(com.hedera.mirror.monitor.publish.transaction.AbstractTransactionSupplierTest)

Example 9 with AccountDeleteTransaction

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

the class ScheduleCreateIntegrationTest method canSignSchedule2.

@Test
@DisplayName("Can sign schedule")
void canSignSchedule2() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    PrivateKey key1 = PrivateKey.generateED25519();
    PrivateKey key2 = PrivateKey.generateED25519();
    PrivateKey key3 = PrivateKey.generateED25519();
    KeyList keyList = new KeyList();
    keyList.add(key1.getPublicKey());
    keyList.add(key2.getPublicKey());
    keyList.add(key3.getPublicKey());
    // Creat the account with the `KeyList`
    TransactionResponse response = new AccountCreateTransaction().setKey(keyList).setInitialBalance(new Hbar(10)).execute(testEnv.client);
    // This will wait for the receipt to become available
    @Var TransactionReceipt receipt = response.getReceipt(testEnv.client);
    AccountId accountId = Objects.requireNonNull(receipt.accountId);
    // Generate a `TransactionId`. This id is used to query the inner scheduled transaction
    // after we expect it to have been executed
    TransactionId transactionId = TransactionId.generate(testEnv.operatorId);
    // Create a transfer transaction with 2/3 signatures.
    TransferTransaction transfer = new TransferTransaction().setTransactionId(transactionId).addHbarTransfer(accountId, new Hbar(1).negated()).addHbarTransfer(testEnv.operatorId, new Hbar(1));
    // Schedule the transactoin
    ScheduleCreateTransaction scheduled = transfer.schedule();
    receipt = scheduled.execute(testEnv.client).getReceipt(testEnv.client);
    // Get the schedule ID from the receipt
    ScheduleId scheduleId = Objects.requireNonNull(receipt.scheduleId);
    // Get the schedule info to see if `signatories` is populated with 2/3 signatures
    @Var ScheduleInfo info = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(testEnv.client);
    assertThat(info.executedAt).isNull();
    // Finally send this last signature to Hedera. This last signature _should_ mean the transaction executes
    // since all 3 signatures have been provided.
    ScheduleSignTransaction signTransaction = new ScheduleSignTransaction().setScheduleId(scheduleId).freezeWith(testEnv.client);
    signTransaction.sign(key1).sign(key2).sign(key3).execute(testEnv.client).getReceipt(testEnv.client);
    info = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(testEnv.client);
    assertThat(info.executedAt).isNotNull();
    new AccountDeleteTransaction().setAccountId(accountId).setTransferAccountId(testEnv.operatorId).freezeWith(testEnv.client).sign(key1).sign(key2).sign(key3).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close();
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) 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) ScheduleInfo(com.hedera.hashgraph.sdk.ScheduleInfo) TransactionId(com.hedera.hashgraph.sdk.TransactionId) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) ScheduleCreateTransaction(com.hedera.hashgraph.sdk.ScheduleCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 10 with AccountDeleteTransaction

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

the class ScheduledTransferExample 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);
    Objects.requireNonNull(client.getOperatorAccountId());
    /*
         * A scheduled transaction is a transaction that has been proposed by an account,
         * but which requires more signatures before it will actually execute on the Hedera network.
         *
         * For example, if Alice wants to transfer an amount of Hbar to Bob, and Bob has
         * receiverSignatureRequired set to true, then that transaction must be signed by
         * both Alice and Bob before the transaction will be executed.
         *
         * To solve this problem, Alice can propose the transaction by creating a scheduled
         * transaction on the Hedera network which, if executed, would transfer Hbar from
         * Alice to Bob.  That scheduled transaction will have a ScheduleId by which we can
         * refer to that scheduled transaction.  Alice can communicate the ScheduleId to Bob, and
         * then Bob can use a ScheduleSignTransaction to sign that scheduled transaction.
         *
         * Bob has a 30 minute window in which to sign the scheduled transaction, starting at the
         * moment that Alice creates the scheduled transaction.  If a scheduled transaction
         * is not signed by all of the necessary signatories within the 30 minute window,
         * that scheduled transaction will expire, and will not be executed.
         *
         * Once a scheduled transaction has all of the signatures necessary to execute, it will
         * be executed on the Hedera network automatically.  If you create a scheduled transaction
         * on the Hedera network, but that transaction only requires your signature in order to
         * execute and no one else's, that scheduled transaction will be automatically
         * executed immediately.
         */
    PrivateKey bobsKey = PrivateKey.generateED25519();
    AccountId bobsId = new AccountCreateTransaction().setReceiverSignatureRequired(true).setKey(bobsKey).setInitialBalance(new Hbar(10)).freezeWith(client).sign(bobsKey).execute(client).getReceipt(client).accountId;
    Objects.requireNonNull(bobsId);
    System.out.println("Alice's ID: " + client.getOperatorAccountId().toStringWithChecksum(client));
    System.out.println("Bob's ID: " + bobsId.toStringWithChecksum(client));
    AccountBalance bobsInitialBalance = new AccountBalanceQuery().setAccountId(bobsId).execute(client);
    System.out.println("Bob's initial balance:");
    System.out.println(bobsInitialBalance);
    TransferTransaction transferToSchedule = new TransferTransaction().addHbarTransfer(client.getOperatorAccountId(), new Hbar(-10)).addHbarTransfer(bobsId, new Hbar(10));
    System.out.println("Transfer to be scheduled:");
    System.out.println(transferToSchedule);
    /*
         * The payerAccountId is the account that will be charged the fee
         * for executing the scheduled transaction if/when it is executed.
         * That fee is separate from the fee that we will pay to execute the
         * ScheduleCreateTransaction itself.
         *
         * To clarify: Alice pays a fee to execute the ScheduleCreateTransaction,
         * which creates the scheduled transaction on the Hedera network.
         * She specifies when creating the scheduled transaction that Bob will pay
         * the fee for the scheduled transaction when it is executed.
         *
         * If payerAccountId is not specified, the account who creates the scheduled transaction
         * will be charged for executing the scheduled transaction.
         */
    ScheduleId scheduleId = new ScheduleCreateTransaction().setScheduledTransaction(transferToSchedule).setPayerAccountId(bobsId).execute(client).getReceipt(client).scheduleId;
    Objects.requireNonNull(scheduleId);
    System.out.println("The scheduleId is: " + scheduleId.toStringWithChecksum(client));
    /*
         * Bob's balance should be unchanged.  The transfer has been scheduled, but it hasn't been executed yet
         * because it requires Bob's signature.
         */
    AccountBalance bobsBalanceAfterSchedule = new AccountBalanceQuery().setAccountId(bobsId).execute(client);
    System.out.println("Bob's balance after scheduling the transfer (should be unchanged):");
    System.out.println(bobsBalanceAfterSchedule);
    /*
         * Once Alice has communicated the scheduleId to Bob, Bob can query for information about the
         * scheduled transaction.
         */
    ScheduleInfo scheduledTransactionInfo = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(client);
    System.out.println("Info about scheduled transaction:");
    System.out.println(scheduledTransactionInfo);
    /*
         * getScheduledTransaction() will return an SDK Transaction object identical to the transaction
         * that was scheduled, which Bob can then inspect like a normal transaction.
         */
    Transaction<?> scheduledTransaction = scheduledTransactionInfo.getScheduledTransaction();
    // We happen to know that this transaction is (or certainly ought to be) a TransferTransaction
    if (scheduledTransaction instanceof TransferTransaction) {
        TransferTransaction scheduledTransfer = (TransferTransaction) scheduledTransaction;
        System.out.println("The scheduled transfer transaction from Bob's POV:");
        System.out.println(scheduledTransfer);
    } else {
        System.out.println("The scheduled transaction was not a transfer transaction.");
        System.out.println("Something has gone horribly wrong.  Crashing...");
        System.exit(-1);
    }
    new ScheduleSignTransaction().setScheduleId(scheduleId).freezeWith(client).sign(bobsKey).execute(client).getReceipt(client);
    AccountBalance balanceAfterSigning = new AccountBalanceQuery().setAccountId(bobsId).execute(client);
    System.out.println("Bob's balance after signing the scheduled transaction:");
    System.out.println(balanceAfterSigning);
    ScheduleInfo postTransactionInfo = new ScheduleInfoQuery().setScheduleId(scheduleId).execute(client);
    System.out.println("Info on the scheduled transaction, executedAt should no longer be null:");
    System.out.println(postTransactionInfo);
    // Clean up
    new AccountDeleteTransaction().setTransferAccountId(client.getOperatorAccountId()).setAccountId(bobsId).freezeWith(client).sign(bobsKey).execute(client).getReceipt(client);
    client.close();
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) ScheduleSignTransaction(com.hedera.hashgraph.sdk.ScheduleSignTransaction) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) AccountDeleteTransaction(com.hedera.hashgraph.sdk.AccountDeleteTransaction) Hbar(com.hedera.hashgraph.sdk.Hbar) ScheduleInfoQuery(com.hedera.hashgraph.sdk.ScheduleInfoQuery) ScheduleId(com.hedera.hashgraph.sdk.ScheduleId) ScheduleInfo(com.hedera.hashgraph.sdk.ScheduleInfo) AccountBalance(com.hedera.hashgraph.sdk.AccountBalance) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) ScheduleCreateTransaction(com.hedera.hashgraph.sdk.ScheduleCreateTransaction)

Aggregations

AccountDeleteTransaction (com.hedera.hashgraph.sdk.AccountDeleteTransaction)11 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)9 Hbar (com.hedera.hashgraph.sdk.Hbar)7 AccountId (com.hedera.hashgraph.sdk.AccountId)6 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)6 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)6 Test (org.junit.jupiter.api.Test)6 Client (com.hedera.hashgraph.sdk.Client)5 Var (com.google.errorprone.annotations.Var)4 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)4 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)4 DisplayName (org.junit.jupiter.api.DisplayName)4 ScheduleCreateTransaction (com.hedera.hashgraph.sdk.ScheduleCreateTransaction)3 ScheduleId (com.hedera.hashgraph.sdk.ScheduleId)3 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)3 ScheduleSignTransaction (com.hedera.hashgraph.sdk.ScheduleSignTransaction)3 AccountBalanceQuery (com.hedera.hashgraph.sdk.AccountBalanceQuery)2 KeyList (com.hedera.hashgraph.sdk.KeyList)2 PublicKey (com.hedera.hashgraph.sdk.PublicKey)2 ScheduleInfo (com.hedera.hashgraph.sdk.ScheduleInfo)2