Search in sources :

Example 31 with TransferTransaction

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

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

Example 33 with TransferTransaction

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

the class TokenTransferIntegrationTest method tokenTransferTest.

@Test
@DisplayName("Can transfer tokens")
void tokenTransferTest() 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").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);
    var tokenId = response.getReceipt(testEnv.client).tokenId;
    assertThat(tokenId).isNotNull();
    new TokenAssociateTransaction().setAccountId(accountId).setTokenIds(Collections.singletonList(tokenId)).freezeWith(testEnv.client).signWithOperator(testEnv.client).sign(key).execute(testEnv.client).getReceipt(testEnv.client);
    new TokenGrantKycTransaction().setAccountId(accountId).setTokenId(tokenId).execute(testEnv.client).getReceipt(testEnv.client);
    new TransferTransaction().addTokenTransfer(tokenId, testEnv.operatorId, -10).addTokenTransfer(tokenId, accountId, 10).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close(tokenId, accountId, key);
}
Also used : TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) Var(com.google.errorprone.annotations.Var) TokenAssociateTransaction(com.hedera.hashgraph.sdk.TokenAssociateTransaction) Hbar(com.hedera.hashgraph.sdk.Hbar) TokenCreateTransaction(com.hedera.hashgraph.sdk.TokenCreateTransaction) TokenGrantKycTransaction(com.hedera.hashgraph.sdk.TokenGrantKycTransaction) 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 34 with TransferTransaction

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

the class IntegrationTestEnv method wipeAccountHbars.

public void wipeAccountHbars(AccountId newAccountId, PrivateKey newAccountKey) throws Exception {
    var hbarsBalance = new AccountBalanceQuery().setAccountId(newAccountId).execute(originalClient).hbars;
    new TransferTransaction().addHbarTransfer(newAccountId, hbarsBalance.negated()).addHbarTransfer(Objects.requireNonNull(originalClient.getOperatorAccountId()), hbarsBalance).freezeWith(originalClient).sign(Objects.requireNonNull(newAccountKey)).execute(originalClient);
}
Also used : AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction)

Example 35 with TransferTransaction

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

the class IntegrationTestEnv method close.

public void close(@Nullable TokenId newTokenId, @Nullable AccountId newAccountId, @Nullable PrivateKey newAccountKey) throws Exception {
    if (newAccountId != null) {
        wipeAccountHbars(newAccountId, newAccountKey);
    }
    if (!operatorId.equals(originalClient.getOperatorAccountId())) {
        var hbarsBalance = new AccountBalanceQuery().setAccountId(operatorId).execute(originalClient).hbars;
        new TransferTransaction().addHbarTransfer(operatorId, hbarsBalance.negated()).addHbarTransfer(Objects.requireNonNull(originalClient.getOperatorAccountId()), hbarsBalance).freezeWith(originalClient).signWithOperator(client).execute(originalClient);
        client.close();
    }
    originalClient.close();
}
Also used : AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction)

Aggregations

TransferTransaction (com.hedera.hashgraph.sdk.TransferTransaction)42 Hbar (com.hedera.hashgraph.sdk.Hbar)31 AccountCreateTransaction (com.hedera.hashgraph.sdk.AccountCreateTransaction)26 Test (org.junit.jupiter.api.Test)23 DisplayName (org.junit.jupiter.api.DisplayName)18 AccountId (com.hedera.hashgraph.sdk.AccountId)15 TokenCreateTransaction (com.hedera.hashgraph.sdk.TokenCreateTransaction)14 PrivateKey (com.hedera.hashgraph.sdk.PrivateKey)13 TokenAssociateTransaction (com.hedera.hashgraph.sdk.TokenAssociateTransaction)13 TransactionResponse (com.hedera.hashgraph.sdk.TransactionResponse)13 Client (com.hedera.hashgraph.sdk.Client)12 Var (com.google.errorprone.annotations.Var)10 AccountBalanceQuery (com.hedera.hashgraph.sdk.AccountBalanceQuery)10 TokenGrantKycTransaction (com.hedera.hashgraph.sdk.TokenGrantKycTransaction)8 TransactionReceipt (com.hedera.hashgraph.sdk.TransactionReceipt)8 KeyList (com.hedera.hashgraph.sdk.KeyList)7 TokenWipeTransaction (com.hedera.hashgraph.sdk.TokenWipeTransaction)7 AccountDeleteTransaction (com.hedera.hashgraph.sdk.AccountDeleteTransaction)6 ScheduleInfoQuery (com.hedera.hashgraph.sdk.ScheduleInfoQuery)6 ScheduleSignTransaction (com.hedera.hashgraph.sdk.ScheduleSignTransaction)6