Search in sources :

Example 36 with TransferTransaction

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

the class TokenWipeIntegrationTest method cannotWipeAccountsBalanceWhenAmountIsNotSet.

@Test
@DisplayName("Cannot wipe accounts balance when amount is not set")
void cannotWipeAccountsBalanceWhenAmountIsNotSet() 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);
    new TokenAssociateTransaction().setAccountId(accountId).setTokenIds(Collections.singletonList(tokenId)).freezeWith(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);
    assertThatExceptionOfType(PrecheckStatusException.class).isThrownBy(() -> {
        new TokenWipeTransaction().setTokenId(tokenId).setAccountId(accountId).execute(testEnv.client).getReceipt(testEnv.client);
    }).withMessageContaining(Status.INVALID_WIPING_AMOUNT.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) TokenGrantKycTransaction(com.hedera.hashgraph.sdk.TokenGrantKycTransaction) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) TokenWipeTransaction(com.hedera.hashgraph.sdk.TokenWipeTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 37 with TransferTransaction

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

the class TokenWipeIntegrationTest method canWipeAccountsNfts.

@Test
@DisplayName("Can wipe accounts NFTs")
void canWipeAccountsNfts() 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").setTokenType(TokenType.NON_FUNGIBLE_UNIQUE).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);
    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).sign(key).execute(testEnv.client).getReceipt(testEnv.client);
    new TokenGrantKycTransaction().setAccountId(accountId).setTokenId(tokenId).execute(testEnv.client).getReceipt(testEnv.client);
    var serialsToTransfer = mintReceipt.serials.subList(0, 4);
    var transfer = new TransferTransaction();
    for (var serial : serialsToTransfer) {
        transfer.addNftTransfer(tokenId.nft(serial), testEnv.operatorId, accountId);
    }
    transfer.execute(testEnv.client).getReceipt(testEnv.client);
    new TokenWipeTransaction().setTokenId(tokenId).setAccountId(accountId).setSerials(serialsToTransfer).execute(testEnv.client).getReceipt(testEnv.client);
    testEnv.close(tokenId, accountId, key);
}
Also used : TokenMintTransaction(com.hedera.hashgraph.sdk.TokenMintTransaction) 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) TokenWipeTransaction(com.hedera.hashgraph.sdk.TokenWipeTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 38 with TransferTransaction

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

the class TokenWipeIntegrationTest method cannotWipeAccountsBalanceWhenTokenIDIsNotSet.

@Test
@DisplayName("Cannot wipe accounts balance when token ID is not set")
void cannotWipeAccountsBalanceWhenTokenIDIsNotSet() 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);
    new TokenAssociateTransaction().setAccountId(accountId).setTokenIds(Collections.singletonList(tokenId)).freezeWith(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);
    assertThatExceptionOfType(PrecheckStatusException.class).isThrownBy(() -> {
        new TokenWipeTransaction().setAccountId(accountId).setAmount(10).execute(testEnv.client).getReceipt(testEnv.client);
    }).withMessageContaining(Status.INVALID_TOKEN_ID.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) TokenGrantKycTransaction(com.hedera.hashgraph.sdk.TokenGrantKycTransaction) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) TokenWipeTransaction(com.hedera.hashgraph.sdk.TokenWipeTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName)

Example 39 with TransferTransaction

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

the class TransactionIntegrationTest method transactionFromToBytes2.

// TODO: this test has a bunch of things hard-coded into it, which is kinda dumb, but it's a good idea for a test.
// Any way to fix it and bring it back?
@Disabled
@Test
@DisplayName("transaction can be serialized into bytes, deserialized, signature added and executed")
void transactionFromToBytes2() {
    assertThatNoException().isThrownBy(() -> {
        var id = TransactionId.generate(new AccountId(542348));
        var transactionBodyBuilder = TransactionBody.newBuilder();
        transactionBodyBuilder.setTransactionID(TransactionID.newBuilder().setTransactionValidStart(Timestamp.newBuilder().setNanos(id.validStart.getNano()).setSeconds(id.validStart.getEpochSecond()).build()).setAccountID(AccountID.newBuilder().setAccountNum(542348).setRealmNum(0).setShardNum(0).build()).build()).setNodeAccountID(AccountID.newBuilder().setAccountNum(3).setRealmNum(0).setShardNum(0).build()).setTransactionFee(200_000_000).setTransactionValidDuration(Duration.newBuilder().setSeconds(120).build()).setGenerateRecord(false).setMemo("").setCryptoTransfer(CryptoTransferTransactionBody.newBuilder().setTransfers(TransferList.newBuilder().addAccountAmounts(AccountAmount.newBuilder().setAccountID(AccountID.newBuilder().setAccountNum(47439).setRealmNum(0).setShardNum(0).build()).setAmount(10).build()).addAccountAmounts(AccountAmount.newBuilder().setAccountID(AccountID.newBuilder().setAccountNum(542348).setRealmNum(0).setShardNum(0).build()).setAmount(-10).build()).build()).build());
        var bodyBytes = transactionBodyBuilder.build().toByteString();
        var key1 = PrivateKey.fromString("302e020100300506032b6570042204203e7fda6dde63c3cdb3cb5ecf5264324c5faad7c9847b6db093c088838b35a110");
        var key2 = PrivateKey.fromString("302e020100300506032b65700422042032d3d5a32e9d06776976b39c09a31fbda4a4a0208223da761c26a2ae560c1755");
        var key3 = PrivateKey.fromString("302e020100300506032b657004220420195a919056d1d698f632c228dbf248bbbc3955adf8a80347032076832b8299f9");
        var key4 = PrivateKey.fromString("302e020100300506032b657004220420b9962f17f94ffce73a23649718a11638cac4b47095a7a6520e88c7563865be62");
        var key5 = PrivateKey.fromString("302e020100300506032b657004220420fef68591819080cd9d48b0cbaa10f65f919752abb50ffb3e7411ac66ab22692e");
        var publicKey1 = key1.getPublicKey();
        var publicKey2 = key2.getPublicKey();
        var publicKey3 = key3.getPublicKey();
        var publicKey4 = key4.getPublicKey();
        var publicKey5 = key5.getPublicKey();
        var signature1 = key1.sign(bodyBytes.toByteArray());
        var signature2 = key2.sign(bodyBytes.toByteArray());
        var signature3 = key3.sign(bodyBytes.toByteArray());
        var signature4 = key4.sign(bodyBytes.toByteArray());
        var signature5 = key5.sign(bodyBytes.toByteArray());
        var signedBuilder = SignedTransaction.newBuilder();
        signedBuilder.setBodyBytes(bodyBytes).setSigMap(SignatureMap.newBuilder().addSigPair(SignaturePair.newBuilder().setEd25519(ByteString.copyFrom(signature1)).setPubKeyPrefix(ByteString.copyFrom(publicKey1.toBytes())).build()).addSigPair(SignaturePair.newBuilder().setEd25519(ByteString.copyFrom(signature2)).setPubKeyPrefix(ByteString.copyFrom(publicKey2.toBytes())).build()).addSigPair(SignaturePair.newBuilder().setEd25519(ByteString.copyFrom(signature3)).setPubKeyPrefix(ByteString.copyFrom(publicKey3.toBytes())).build()).addSigPair(SignaturePair.newBuilder().setEd25519(ByteString.copyFrom(signature4)).setPubKeyPrefix(ByteString.copyFrom(publicKey4.toBytes())).build()).addSigPair(SignaturePair.newBuilder().setEd25519(ByteString.copyFrom(signature5)).setPubKeyPrefix(ByteString.copyFrom(publicKey5.toBytes())).build()));
        @Var var byts = signedBuilder.build().toByteString();
        byts = TransactionList.newBuilder().addTransactionList(com.hedera.hashgraph.sdk.proto.Transaction.newBuilder().setSignedTransactionBytes(byts).build()).build().toByteString();
        var tx = (TransferTransaction) Transaction.fromBytes(byts.toByteArray());
        var testEnv = new IntegrationTestEnv(1);
        assertThat(tx.getHbarTransfers().get(new AccountId(542348)).toTinybars()).isEqualTo(-10);
        assertThat(tx.getHbarTransfers().get(new AccountId(47439)).toTinybars()).isEqualTo(10);
        assertThat(tx.getNodeAccountIds()).isNotNull();
        assertThat(tx.getNodeAccountIds().size()).isEqualTo(1);
        assertThat(tx.getNodeAccountIds().get(0)).isEqualTo(new AccountId(3));
        var signatures = tx.getSignatures();
        assertThat(Arrays.toString(signatures.get(new AccountId(3)).get(publicKey1))).isEqualTo(Arrays.toString(signature1));
        assertThat(Arrays.toString(signatures.get(new AccountId(3)).get(publicKey2))).isEqualTo(Arrays.toString(signature2));
        assertThat(Arrays.toString(signatures.get(new AccountId(3)).get(publicKey3))).isEqualTo(Arrays.toString(signature3));
        assertThat(Arrays.toString(signatures.get(new AccountId(3)).get(publicKey4))).isEqualTo(Arrays.toString(signature4));
        assertThat(Arrays.toString(signatures.get(new AccountId(3)).get(publicKey5))).isEqualTo(Arrays.toString(signature5));
        var resp = tx.execute(testEnv.client);
        resp.getReceipt(testEnv.client);
        testEnv.close();
    });
}
Also used : AccountId(com.hedera.hashgraph.sdk.AccountId) Var(com.google.errorprone.annotations.Var) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) Test(org.junit.jupiter.api.Test) DisplayName(org.junit.jupiter.api.DisplayName) Disabled(org.junit.jupiter.api.Disabled)

Example 40 with TransferTransaction

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

the class CreateAccountThresholdKeyExample method main.

public static void main(String[] args) throws PrecheckStatusException, TimeoutException, 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);
    // Generate three new Ed25519 private, public key pairs.
    // You do not need the private keys to create the Threshold Key List,
    // you only need the public keys, and if you're doing things correctly,
    // you probably shouldn't have these private keys.
    PrivateKey[] privateKeys = new PrivateKey[3];
    PublicKey[] publicKeys = new PublicKey[3];
    for (int i = 0; i < 3; i++) {
        PrivateKey key = PrivateKey.generateED25519();
        privateKeys[i] = key;
        publicKeys[i] = key.getPublicKey();
    }
    System.out.println("public keys: ");
    for (Key key : publicKeys) {
        System.out.println(key);
    }
    // require 2 of the 3 keys we generated to sign on anything modifying this account
    KeyList transactionKey = KeyList.withThreshold(2);
    Collections.addAll(transactionKey, publicKeys);
    TransactionResponse transactionResponse = new AccountCreateTransaction().setKey(transactionKey).setInitialBalance(new Hbar(10)).execute(client);
    // This will wait for the receipt to become available
    TransactionReceipt receipt = transactionResponse.getReceipt(client);
    AccountId newAccountId = Objects.requireNonNull(receipt.accountId);
    System.out.println("account = " + newAccountId);
    TransactionResponse transferTransactionResponse = new TransferTransaction().addHbarTransfer(newAccountId, new Hbar(10).negated()).addHbarTransfer(new AccountId(3), new Hbar(10)).freezeWith(client).sign(privateKeys[0]).sign(privateKeys[1]).execute(client);
    // (important!) wait for the transfer to go to consensus
    transferTransactionResponse.getReceipt(client);
    Hbar balanceAfter = new AccountBalanceQuery().setAccountId(newAccountId).execute(client).hbars;
    System.out.println("account balance after transfer: " + balanceAfter);
}
Also used : PrivateKey(com.hedera.hashgraph.sdk.PrivateKey) AccountId(com.hedera.hashgraph.sdk.AccountId) PublicKey(com.hedera.hashgraph.sdk.PublicKey) KeyList(com.hedera.hashgraph.sdk.KeyList) AccountBalanceQuery(com.hedera.hashgraph.sdk.AccountBalanceQuery) TransactionReceipt(com.hedera.hashgraph.sdk.TransactionReceipt) Hbar(com.hedera.hashgraph.sdk.Hbar) TransactionResponse(com.hedera.hashgraph.sdk.TransactionResponse) Client(com.hedera.hashgraph.sdk.Client) TransferTransaction(com.hedera.hashgraph.sdk.TransferTransaction) AccountCreateTransaction(com.hedera.hashgraph.sdk.AccountCreateTransaction) Key(com.hedera.hashgraph.sdk.Key) PublicKey(com.hedera.hashgraph.sdk.PublicKey) PrivateKey(com.hedera.hashgraph.sdk.PrivateKey)

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