Search in sources :

Example 51 with Hbar

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

the class ScheduleCreateIntegrationTest method canScheduleTokenTransfer.

@Test
@DisplayName("Can schedule token transfer")
void canScheduleTokenTransfer() throws Exception {
    var testEnv = new IntegrationTestEnv(1).useThrowawayAccount();
    PrivateKey key = PrivateKey.generateED25519();
    var accountId = new AccountCreateTransaction().setReceiverSignatureRequired(true).setKey(key).setInitialBalance(new Hbar(10)).freezeWith(testEnv.client).sign(key).execute(testEnv.client).getReceipt(testEnv.client).accountId;
    Objects.requireNonNull(accountId);
    var tokenId = new TokenCreateTransaction().setTokenName("ffff").setTokenSymbol("F").setInitialSupply(100).setTreasuryAccountId(testEnv.operatorId).setAdminKey(testEnv.operatorKey).execute(testEnv.client).getReceipt(testEnv.client).tokenId;
    Objects.requireNonNull(tokenId);
    new TokenAssociateTransaction().setAccountId(accountId).setTokenIds(Collections.singletonList(tokenId)).freezeWith(testEnv.client).sign(key).execute(testEnv.client).getReceipt(testEnv.client);
    var scheduleId = new TransferTransaction().addTokenTransfer(tokenId, testEnv.operatorId, -10).addTokenTransfer(tokenId, accountId, 10).schedule().execute(testEnv.client).getReceipt(testEnv.client).scheduleId;
    Objects.requireNonNull(scheduleId);
    var balanceQuery1 = new AccountBalanceQuery().setAccountId(accountId).execute(testEnv.client);
    assertThat(balanceQuery1.tokens.get(tokenId)).isEqualTo(0);
    new ScheduleSignTransaction().setScheduleId(scheduleId).freezeWith(testEnv.client).sign(key).execute(testEnv.client).getReceipt(testEnv.client);
    var balanceQuery2 = new AccountBalanceQuery().setAccountId(accountId).execute(testEnv.client);
    assertThat(balanceQuery2.tokens.get(tokenId)).isEqualTo(10);
    testEnv.close(tokenId, accountId, key);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) TokenAssociateTransaction(com.hedera.hashgraph.sdk.TokenAssociateTransaction) ScheduleSignTransaction(com.hedera.hashgraph.sdk.ScheduleSignTransaction) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) 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 52 with Hbar

use of com.hedera.hashgraph.sdk.Hbar 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 53 with Hbar

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

the class TokenAssociateIntegrationTest method canExecuteTokenAssociateTransactionEvenWhenTokenIDsAreNotSet.

@Test
@Disabled
@DisplayName("Can execute token associate transaction even when token IDs are not set")
void canExecuteTokenAssociateTransactionEvenWhenTokenIDsAreNotSet() 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);
    new TokenAssociateTransaction().setAccountId(accountId).freezeWith(testEnv.client).sign(key).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close(accountId, key);
}
Also used : TokenAssociateTransaction(com.hedera.hashgraph.sdk.TokenAssociateTransaction) Hbar(com.hedera.hashgraph.sdk.Hbar) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName) Disabled(org.junit.jupiter.api.Disabled)

Example 54 with Hbar

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

the class TokenAssociateIntegrationTest method cannotAssociateAccountWhenAccountDoesNotSignTransaction.

@Test
@DisplayName("Cannot associate account with tokens when account does not sign transaction")
void cannotAssociateAccountWhenAccountDoesNotSignTransaction() 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 TokenAssociateTransaction().setAccountId(accountId).setTokenIds(Collections.singletonList(tokenId)).execute(testEnv.client).getReceipt(testEnv.client);
    }).withMessageContaining(Status.INVALID_SIGNATURE.toString());
    testEnv.close(tokenId, accountId, key);
}
Also used : TokenAssociateTransaction(com.hedera.hashgraph.sdk.TokenAssociateTransaction) 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 55 with Hbar

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

the class TokenAssociateIntegrationTest method cannotAssociateAccountWithTokensWhenAccountIDIsNotSet.

@Test
@DisplayName("Cannot associate account with tokens when account ID is not set")
void cannotAssociateAccountWithTokensWhenAccountIDIsNotSet() 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);
    assertThatExceptionOfType(PrecheckStatusException.class).isThrownBy(() -> {
        new TokenAssociateTransaction().freezeWith(testEnv.client).sign(key).execute(testEnv.client).getReceipt(testEnv.client);
    }).withMessageContaining(Status.INVALID_ACCOUNT_ID.toString());
    testEnv.close(accountId, key);
}
Also used : TokenAssociateTransaction(com.hedera.hashgraph.sdk.TokenAssociateTransaction) Hbar(com.hedera.hashgraph.sdk.Hbar) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Aggregations

Hbar (com.hedera.hashgraph.sdk.Hbar)106 Test (org.junit.jupiter.api.Test)77 DisplayName (org.junit.jupiter.api.DisplayName)75 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)62 TokenCreateTransaction (com.hedera.hashgraph.sdk.TokenCreateTransaction)34 TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)31 Client (com.hedera.hashgraph.sdk.Client)22 TokenAssociateTransaction (com.hedera.hashgraph.sdk.TokenAssociateTransaction)22 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)19 Var (com.google.errorprone.annotations.Var)18 FileCreateTransaction (com.hedera.hashgraph.sdk.FileCreateTransaction)16 AccountId (com.hedera.hashgraph.sdk.AccountId)15 AccountBalanceQuery (com.hedera.hashgraph.sdk.AccountBalanceQuery)13 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)13 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)12 AccountInfoQuery (com.hedera.hashgraph.sdk.AccountInfoQuery)11 FileDeleteTransaction (com.hedera.hashgraph.sdk.FileDeleteTransaction)11 TokenGrantKycTransaction (com.hedera.hashgraph.sdk.TokenGrantKycTransaction)11 ContractCreateTransaction (com.hedera.hashgraph.sdk.ContractCreateTransaction)9 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)9