use of com.hedera.hashgraph.sdk.AccountInfoQuery 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.AccountInfoQuery in project hedera-sdk-java by hashgraph.
the class AccountCreateIntegrationTest method canCreateAccountWithOnlyInitialBalanceAndKey.
@Test
@DisplayName("Can create account with only initial balance and key")
void canCreateAccountWithOnlyInitialBalanceAndKey() 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);
var info = new AccountInfoQuery().setAccountId(accountId).execute(testEnv.client);
assertThat(info.accountId).isEqualTo(accountId);
assertThat(info.isDeleted).isFalse();
assertThat(info.key.toString()).isEqualTo(key.getPublicKey().toString());
assertThat(info.balance).isEqualTo(new Hbar(1));
assertThat(info.autoRenewPeriod).isEqualTo(Duration.ofDays(90));
assertThat(info.proxyAccountId).isNull();
assertThat(info.proxyReceived).isEqualTo(Hbar.ZERO);
testEnv.close(accountId, key);
}
use of com.hedera.hashgraph.sdk.AccountInfoQuery in project hedera-sdk-java by hashgraph.
the class AccountCreateIntegrationTest method canCreateWithAliasKey.
@Test
@DisplayName("Can create account using aliasKey")
void canCreateWithAliasKey() throws Exception {
var testEnv = new IntegrationTestEnv(1);
var key = PrivateKey.generateED25519();
var aliasId = key.toAccountId(0, 0);
new TransferTransaction().addHbarTransfer(testEnv.operatorId, new Hbar(10).negated()).addHbarTransfer(aliasId, new Hbar(10)).execute(testEnv.client).getReceipt(testEnv.client);
var info = new AccountInfoQuery().setAccountId(aliasId).execute(testEnv.client);
assertThat(key.getPublicKey()).isEqualTo(info.aliasKey);
testEnv.close(info.accountId, key);
}
use of com.hedera.hashgraph.sdk.AccountInfoQuery in project hedera-sdk-java by hashgraph.
the class AccountDeleteIntegrationTest method canDeleteAccount.
@Test
@DisplayName("Can delete account")
void canDeleteAccount() 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);
var info = new AccountInfoQuery().setAccountId(accountId).execute(testEnv.client);
assertThat(info.accountId).isEqualTo(accountId);
assertThat(info.isDeleted).isFalse();
assertThat(info.key.toString()).isEqualTo(key.getPublicKey().toString());
assertThat(info.balance).isEqualTo(new Hbar(1));
assertThat(info.autoRenewPeriod).isEqualTo(Duration.ofDays(90));
assertThat(info.proxyAccountId).isNull();
assertThat(info.proxyReceived).isEqualTo(Hbar.ZERO);
testEnv.close(accountId, key);
}
use of com.hedera.hashgraph.sdk.AccountInfoQuery 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