Search in sources :

Example 1 with AccountUpdateTransaction

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

the class AccountUpdateIntegrationTest method cannotUpdateAccountWhenAccountIdIsNotSet.

@Test
@DisplayName("Cannot update account when account ID is not set")
void cannotUpdateAccountWhenAccountIdIsNotSet() throws Exception {
    var testEnv = new IntegrationTestEnv(1);
    assertThatExceptionOfType(ReceiptStatusException.class).isThrownBy(() -> {
        new AccountUpdateTransaction().execute(testEnv.client).getReceipt(testEnv.client);
    }).withMessageContaining(Status.ACCOUNT_ID_DOES_NOT_EXIST.toString());
    testEnv.close();
}
Also used : AccountUpdateTransaction(com.hedera.hashgraph.sdk.AccountUpdateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 2 with AccountUpdateTransaction

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

the class AutomaticAssociationTest method autoAssociateTest.

@Test
@DisplayName("Tokens automatically become associated")
void autoAssociateTest() throws Exception {
    var testEnv = new IntegrationTestEnv(1).useThrowawayAccount();
    var key = PrivateKey.generateED25519();
    var accountId = new AccountCreateTransaction().setKey(key).setInitialBalance(new Hbar(10)).setMaxAutomaticTokenAssociations(1).execute(testEnv.client).getReceipt(testEnv.client).accountId;
    Objects.requireNonNull(accountId);
    var accountInfo1 = new AccountInfoQuery().setAccountId(accountId).execute(testEnv.client);
    assertThat(accountInfo1.maxAutomaticTokenAssociations).isEqualTo(1);
    assertThat(accountInfo1.tokenRelationships.size()).isEqualTo(0);
    var tokenId1 = new TokenCreateTransaction().setTreasuryAccountId(testEnv.operatorId).setTokenName("Test Token").setTokenSymbol("T").setAdminKey(testEnv.operatorKey).setInitialSupply(1).execute(testEnv.client).getReceipt(testEnv.client).tokenId;
    var tokenId2 = new TokenCreateTransaction().setTreasuryAccountId(testEnv.operatorId).setTokenName("Test Token").setTokenSymbol("T").setAdminKey(testEnv.operatorKey).setInitialSupply(1).execute(testEnv.client).getReceipt(testEnv.client).tokenId;
    Objects.requireNonNull(tokenId1);
    Objects.requireNonNull(tokenId2);
    var transferResponse1 = new TransferTransaction().addTokenTransfer(tokenId1, testEnv.operatorId, -1).addTokenTransfer(tokenId1, accountId, 1).execute(testEnv.client);
    transferResponse1.getReceipt(testEnv.client);
    var transferRecord = transferResponse1.getRecord(testEnv.client);
    assertThat(transferRecord.automaticTokenAssociations.size()).isEqualTo(1);
    assertThat(transferRecord.automaticTokenAssociations.get(0).accountId).isEqualTo(accountId);
    assertThat(transferRecord.automaticTokenAssociations.get(0).tokenId).isEqualTo(tokenId1);
    var accountInfo2 = new AccountInfoQuery().setAccountId(accountId).execute(testEnv.client);
    assertThat(accountInfo2.tokenRelationships.size()).isEqualTo(1);
    assertThat(accountInfo2.tokenRelationships.get(tokenId1).automaticAssociation).isTrue();
    assertThatExceptionOfType(Exception.class).isThrownBy(() -> {
        new TransferTransaction().addTokenTransfer(tokenId2, testEnv.operatorId, -1).addTokenTransfer(tokenId2, accountId, 1).execute(testEnv.client).getReceipt(testEnv.client);
    }).withMessageContaining("NO_REMAINING_AUTOMATIC_ASSOCIATIONS");
    new AccountUpdateTransaction().setAccountId(accountId).setMaxAutomaticTokenAssociations(2).freezeWith(testEnv.client).sign(key).execute(testEnv.client).getReceipt(testEnv.client);
    var accountInfo3 = new AccountInfoQuery().setAccountId(accountId).execute(testEnv.client);
    assertThat(accountInfo3.maxAutomaticTokenAssociations).isEqualTo(2);
    new TokenDeleteTransaction().setTokenId(tokenId1).execute(testEnv.client).getReceipt(testEnv.client);
    new TokenDeleteTransaction().setTokenId(tokenId2).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close(accountId, key);
}
Also used : AccountUpdateTransaction(com.hedera.hashgraph.sdk.AccountUpdateTransaction) AccountInfoQuery(com.hedera.hashgraph.sdk.AccountInfoQuery) TokenDeleteTransaction(com.hedera.hashgraph.sdk.TokenDeleteTransaction) 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 3 with AccountUpdateTransaction

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

the class AccountUpdateTransactionSupplierTest method createWithMinimumData.

@Test
void createWithMinimumData() {
    AccountUpdateTransactionSupplier accountUpdateTransactionSupplier = new AccountUpdateTransactionSupplier();
    accountUpdateTransactionSupplier.setAccountId(ACCOUNT_ID.toString());
    AccountUpdateTransaction actual = accountUpdateTransactionSupplier.get();
    assertThat(actual).returns(ACCOUNT_ID, AccountUpdateTransaction::getAccountId).returns(null, AccountUpdateTransaction::getKey).returns(MAX_TRANSACTION_FEE_HBAR, AccountUpdateTransaction::getMaxTransactionFee).returns(null, AccountUpdateTransaction::getProxyAccountId).returns(false, AccountUpdateTransaction::getReceiverSignatureRequired).satisfies(a -> assertThat(a.getExpirationTime()).isNotNull()).extracting(AccountUpdateTransaction::getAccountMemo, STRING).contains("Mirror node updated test account");
}
Also used : AccountUpdateTransaction(com.hedera.hashgraph.sdk.AccountUpdateTransaction) Test(org.junit.jupiter.api.Test) AbstractTransactionSupplierTest(com.hedera.mirror.monitor.publish.transaction.AbstractTransactionSupplierTest)

Example 4 with AccountUpdateTransaction

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

the class AccountUpdateTransactionSupplierTest method createWithCustomData.

@Test
void createWithCustomData() {
    Instant expirationTime = Instant.now().plus(1, ChronoUnit.DAYS);
    PublicKey key = PrivateKey.generate().getPublicKey();
    AccountUpdateTransactionSupplier accountUpdateTransactionSupplier = new AccountUpdateTransactionSupplier();
    accountUpdateTransactionSupplier.setAccountId(ACCOUNT_ID.toString());
    accountUpdateTransactionSupplier.setExpirationTime(expirationTime);
    accountUpdateTransactionSupplier.setMaxTransactionFee(1);
    accountUpdateTransactionSupplier.setProxyAccountId(ACCOUNT_ID_2.toString());
    accountUpdateTransactionSupplier.setPublicKey(key.toString());
    accountUpdateTransactionSupplier.setReceiverSignatureRequired(true);
    AccountUpdateTransaction actual = accountUpdateTransactionSupplier.get();
    assertThat(actual).returns(ACCOUNT_ID, AccountUpdateTransaction::getAccountId).returns(expirationTime, AccountUpdateTransaction::getExpirationTime).returns(key, AccountUpdateTransaction::getKey).returns(ONE_TINYBAR, AccountUpdateTransaction::getMaxTransactionFee).returns(ACCOUNT_ID_2, AccountUpdateTransaction::getProxyAccountId).returns(true, AccountUpdateTransaction::getReceiverSignatureRequired).extracting(AccountUpdateTransaction::getAccountMemo, STRING).contains("Mirror node updated test account");
}
Also used : AccountUpdateTransaction(com.hedera.hashgraph.sdk.AccountUpdateTransaction) PublicKey(com.hedera.hashgraph.sdk.PublicKey) Instant(java.time.Instant) Test(org.junit.jupiter.api.Test) AbstractTransactionSupplierTest(com.hedera.mirror.monitor.publish.transaction.AbstractTransactionSupplierTest)

Example 5 with AccountUpdateTransaction

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

Aggregations

AccountUpdateTransaction (com.hedera.hashgraph.sdk.AccountUpdateTransaction)6 Test (org.junit.jupiter.api.Test)5 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)3 AccountInfoQuery (com.hedera.hashgraph.sdk.AccountInfoQuery)3 Hbar (com.hedera.hashgraph.sdk.Hbar)3 DisplayName (org.junit.jupiter.api.DisplayName)3 AbstractTransactionSupplierTest (com.hedera.mirror.monitor.publish.transaction.AbstractTransactionSupplierTest)2 Var (com.google.errorprone.annotations.Var)1 AccountId (com.hedera.hashgraph.sdk.AccountId)1 AccountInfo (com.hedera.hashgraph.sdk.AccountInfo)1 Client (com.hedera.hashgraph.sdk.Client)1 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)1 PublicKey (com.hedera.hashgraph.sdk.PublicKey)1 TokenCreateTransaction (com.hedera.hashgraph.sdk.TokenCreateTransaction)1 TokenDeleteTransaction (com.hedera.hashgraph.sdk.TokenDeleteTransaction)1 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)1 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)1 Instant (java.time.Instant)1