Search in sources :

Example 46 with AccountCreateTransaction

use of com.hedera.hashgraph.sdk.AccountCreateTransaction 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)

Example 47 with AccountCreateTransaction

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

the class UpdateAccountPublicKeyExample 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);
    client.setDefaultMaxTransactionFee(new Hbar(10));
    // First, we create a new account so we don't affect our account
    PrivateKey key1 = PrivateKey.generateED25519();
    PrivateKey key2 = PrivateKey.generateED25519();
    TransactionResponse acctTransactionResponse = new AccountCreateTransaction().setKey(key1.getPublicKey()).setInitialBalance(new Hbar(1)).execute(client);
    System.out.println("transaction ID: " + acctTransactionResponse);
    AccountId accountId = Objects.requireNonNull(acctTransactionResponse.getReceipt(client).accountId);
    System.out.println("account = " + accountId);
    System.out.println("key = " + key1.getPublicKey());
    // Next, we update the key
    System.out.println(" :: update public key of account " + accountId);
    System.out.println("set key = " + key2.getPublicKey());
    TransactionResponse accountUpdateTransactionResponse = new AccountUpdateTransaction().setAccountId(accountId).setKey(key2.getPublicKey()).freezeWith(client).sign(key1).sign(key2).execute(client);
    System.out.println("transaction ID: " + accountUpdateTransactionResponse);
    // (important!) wait for the transaction to complete by querying the receipt
    accountUpdateTransactionResponse.getReceipt(client);
    // Now we fetch the account information to check if the key was changed
    System.out.println(" :: getAccount and check our current key");
    AccountInfo info = new AccountInfoQuery().setAccountId(accountId).execute(client);
    System.out.println("key = " + info.key);
}
Also used : AccountUpdateTransaction(com.hedera.hashgraph.sdk.AccountUpdateTransaction) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) AccountId(com.hedera.hashgraph.sdk.AccountId) AccountInfoQuery(com.hedera.hashgraph.sdk.AccountInfoQuery) Hbar(com.hedera.hashgraph.sdk.Hbar) Client(com.hedera.hashgraph.sdk.Client) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) AccountInfo(com.hedera.hashgraph.sdk.AccountInfo)

Example 48 with AccountCreateTransaction

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

the class TokenNftTransferIntegrationTest method cannotTransferUnownedNfts.

@Test
@DisplayName("Cannot transfer NFTs you don't own")
void cannotTransferUnownedNfts() throws Exception {
    var testEnv = new IntegrationTestEnv(1).useThrowawayAccount();
    var key = PrivateKey.generateED25519();
    @Var TransactionResponse response = new AccountCreateTransaction().setKey(key).setInitialBalance(new Hbar(1)).execute(testEnv.client);
    var accountId = response.getReceipt(testEnv.client).accountId;
    assertThat(accountId).isNotNull();
    response = new TokenCreateTransaction().setTokenName("ffff").setTokenSymbol("F").setTokenType(TokenType.NON_FUNGIBLE_UNIQUE).setTreasuryAccountId(testEnv.operatorId).setAdminKey(testEnv.operatorKey).setFreezeKey(testEnv.operatorKey).setWipeKey(testEnv.operatorKey).setSupplyKey(testEnv.operatorKey).setFreezeDefault(false).execute(testEnv.client);
    var tokenId = response.getReceipt(testEnv.client).tokenId;
    assertThat(tokenId).isNotNull();
    var mintReceipt = new TokenMintTransaction().setTokenId(tokenId).setMetadata(NftMetadataGenerator.generate((byte) 10)).execute(testEnv.client).getReceipt(testEnv.client);
    new TokenAssociateTransaction().setAccountId(accountId).setTokenIds(Collections.singletonList(tokenId)).freezeWith(testEnv.client).signWithOperator(testEnv.client).sign(key).execute(testEnv.client).getReceipt(testEnv.client);
    var serialsToTransfer = new ArrayList<Long>(mintReceipt.serials.subList(0, 4));
    var transfer = new TransferTransaction();
    for (var serial : serialsToTransfer) {
        // Try to transfer in wrong direction
        transfer.addNftTransfer(tokenId.nft(serial), accountId, testEnv.operatorId);
    }
    transfer.freezeWith(testEnv.client).sign(key);
    assertThatExceptionOfType(ReceiptStatusException.class).isThrownBy(() -> {
        transfer.execute(testEnv.client).getReceipt(testEnv.client);
    }).withMessageContaining(Status.SENDER_DOES_NOT_OWN_NFT_SERIAL_NO.toString());
    testEnv.close(tokenId, accountId, key);
}
Also used : TokenMintTransaction(com.hedera.hashgraph.sdk.TokenMintTransaction) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) Var(com.google.errorprone.annotations.Var) TokenAssociateTransaction(com.hedera.hashgraph.sdk.TokenAssociateTransaction) ArrayList(java.util.ArrayList) Hbar(com.hedera.hashgraph.sdk.Hbar) TokenCreateTransaction(com.hedera.hashgraph.sdk.TokenCreateTransaction) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 49 with AccountCreateTransaction

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

the class TokenRevokeKycIntegrationTest method cannotRevokeKycToAccountOnTokenWhenAccountWasNotAssociatedWith.

@Test
@DisplayName("Cannot revoke kyc to account on token when account was not associated with")
void cannotRevokeKycToAccountOnTokenWhenAccountWasNotAssociatedWith() throws Exception {
    var testEnv = new IntegrationTestEnv(1).useThrowawayAccount();
    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);
    var tokenId = Objects.requireNonNull(new TokenCreateTransaction().setTokenName("ffff").setTokenSymbol("F").setDecimals(3).setInitialSupply(1000000).setTreasuryAccountId(testEnv.operatorId).setAdminKey(testEnv.operatorKey).setFreezeKey(testEnv.operatorKey).setWipeKey(testEnv.operatorKey).setKycKey(testEnv.operatorKey).setSupplyKey(testEnv.operatorKey).setFreezeDefault(false).execute(testEnv.client).getReceipt(testEnv.client).tokenId);
    assertThatExceptionOfType(ReceiptStatusException.class).isThrownBy(() -> {
        new TokenRevokeKycTransaction().setAccountId(accountId).setTokenId(tokenId).freezeWith(testEnv.client).sign(key).execute(testEnv.client).getReceipt(testEnv.client);
    }).withMessageContaining(Status.TOKEN_NOT_ASSOCIATED_TO_ACCOUNT.toString());
    testEnv.close(tokenId, accountId, key);
}
Also used : TokenRevokeKycTransaction(com.hedera.hashgraph.sdk.TokenRevokeKycTransaction) Hbar(com.hedera.hashgraph.sdk.Hbar) TokenCreateTransaction(com.hedera.hashgraph.sdk.TokenCreateTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 50 with AccountCreateTransaction

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

the class TokenTransferIntegrationTest method insufficientBalanceForFee.

@Test
@DisplayName("Cannot transfer tokens if balance is insufficient to pay fee")
void insufficientBalanceForFee() throws Exception {
    var testEnv = new IntegrationTestEnv(1).useThrowawayAccount();
    PrivateKey key1 = PrivateKey.generateED25519();
    PrivateKey key2 = PrivateKey.generateED25519();
    var accountId1 = new AccountCreateTransaction().setKey(key1).setInitialBalance(new Hbar(2)).execute(testEnv.client).getReceipt(testEnv.client).accountId;
    var accountId2 = new AccountCreateTransaction().setKey(key2).setInitialBalance(new Hbar(2)).execute(testEnv.client).getReceipt(testEnv.client).accountId;
    var tokenId = new TokenCreateTransaction().setTokenName("ffff").setTokenSymbol("F").setInitialSupply(1).setCustomFees(Collections.singletonList(new CustomFixedFee().setAmount(5000_000_000L).setFeeCollectorAccountId(testEnv.operatorId))).setTreasuryAccountId(testEnv.operatorId).setAdminKey(testEnv.operatorKey).setFeeScheduleKey(testEnv.operatorKey).execute(testEnv.client).getReceipt(testEnv.client).tokenId;
    new TokenAssociateTransaction().setAccountId(accountId1).setTokenIds(Collections.singletonList(tokenId)).freezeWith(testEnv.client).sign(key1).execute(testEnv.client).getReceipt(testEnv.client);
    new TokenAssociateTransaction().setAccountId(accountId2).setTokenIds(Collections.singletonList(tokenId)).freezeWith(testEnv.client).sign(key2).execute(testEnv.client).getReceipt(testEnv.client);
    new TransferTransaction().addTokenTransfer(tokenId, testEnv.operatorId, -1).addTokenTransfer(tokenId, accountId1, 1).freezeWith(testEnv.client).sign(key1).execute(testEnv.client).getReceipt(testEnv.client);
    assertThatExceptionOfType(ReceiptStatusException.class).isThrownBy(() -> {
        new TransferTransaction().addTokenTransfer(tokenId, accountId1, -1).addTokenTransfer(tokenId, accountId2, 1).freezeWith(testEnv.client).sign(key1).sign(key2).execute(testEnv.client).getReceipt(testEnv.client);
    }).satisfies(error -> assertThat(error.getMessage()).containsAnyOf(Status.INSUFFICIENT_SENDER_ACCOUNT_BALANCE_FOR_CUSTOM_FEE.toString(), Status.INSUFFICIENT_PAYER_BALANCE_FOR_CUSTOM_FEE.toString()));
    testEnv.wipeAccountHbars(accountId1, key1);
    testEnv.wipeAccountHbars(accountId2, key2);
    testEnv.close();
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) TokenAssociateTransaction(com.hedera.hashgraph.sdk.TokenAssociateTransaction) CustomFixedFee(com.hedera.hashgraph.sdk.CustomFixedFee) Hbar(com.hedera.hashgraph.sdk.Hbar) TokenCreateTransaction(com.hedera.hashgraph.sdk.TokenCreateTransaction) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)73 Hbar (com.hedera.hashgraph.sdk.Hbar)62 Test (org.junit.jupiter.api.Test)59 DisplayName (org.junit.jupiter.api.DisplayName)57 TokenCreateTransaction (com.hedera.hashgraph.sdk.TokenCreateTransaction)29 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)26 TokenAssociateTransaction (com.hedera.hashgraph.sdk.TokenAssociateTransaction)23 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)16 AccountId (com.hedera.hashgraph.sdk.AccountId)15 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)15 Var (com.google.errorprone.annotations.Var)12 Client (com.hedera.hashgraph.sdk.Client)12 TokenGrantKycTransaction (com.hedera.hashgraph.sdk.TokenGrantKycTransaction)12 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)11 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)10 AccountDeleteTransaction (com.hedera.hashgraph.sdk.AccountDeleteTransaction)9 KeyList (com.hedera.hashgraph.sdk.KeyList)9 TokenWipeTransaction (com.hedera.hashgraph.sdk.TokenWipeTransaction)8 AccountInfoQuery (com.hedera.hashgraph.sdk.AccountInfoQuery)7 PublicKey (com.hedera.hashgraph.sdk.PublicKey)7