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();
}
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);
}
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");
}
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");
}
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);
}
Aggregations