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