Skip to content

Commit

Permalink
feat(HIP-745): Optionally send transaction data without required tran…
Browse files Browse the repository at this point in the history
…saction fields (#1739)

Signed-off-by: Nikita Lebedev <[email protected]>
  • Loading branch information
thenswan authored Mar 14, 2024
1 parent fa24683 commit 6f1239c
Show file tree
Hide file tree
Showing 6 changed files with 873 additions and 97 deletions.
81 changes: 81 additions & 0 deletions examples/src/main/java/TransactionSerializationExample.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import com.hedera.hashgraph.sdk.AccountBalanceQuery;
import com.hedera.hashgraph.sdk.AccountId;
import com.hedera.hashgraph.sdk.Client;
import com.hedera.hashgraph.sdk.Hbar;
import com.hedera.hashgraph.sdk.PrivateKey;
import com.hedera.hashgraph.sdk.Transaction;
import com.hedera.hashgraph.sdk.TransactionRecord;
import com.hedera.hashgraph.sdk.TransferTransaction;
import io.github.cdimascio.dotenv.Dotenv;
import java.util.Objects;

public class TransactionSerializationExample {

// see `.env.sample` in the repository root for how to specify these values
// or set environment variables with the same names
private static final AccountId OPERATOR_ID = AccountId.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_ID")));
private static final PrivateKey OPERATOR_KEY = PrivateKey.fromString(Objects.requireNonNull(Dotenv.load().get("OPERATOR_KEY")));
// HEDERA_NETWORK defaults to testnet if not specified in dotenv
private static final String HEDERA_NETWORK = Dotenv.load().get("HEDERA_NETWORK", "testnet");

public TransactionSerializationExample() {
}

public static void main(String[] args) throws Exception {
Client client = ClientHelper.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);

AccountId recipientId = AccountId.fromString("0.0.3");
Hbar amount = Hbar.fromTinybars(10_000);

Hbar senderBalanceBefore = new AccountBalanceQuery()
.setAccountId(OPERATOR_ID)
.execute(client)
.hbars;

Hbar receiptBalanceBefore = new AccountBalanceQuery()
.setAccountId(recipientId)
.execute(client)
.hbars;

System.out.println("" + OPERATOR_ID + " balance = " + senderBalanceBefore);
System.out.println("" + recipientId + " balance = " + receiptBalanceBefore);

var transferTransaction = new TransferTransaction()
// .addSender and .addRecipient can be called as many times as you want as long as the total sum from
// both sides is equivalent
.addHbarTransfer(OPERATOR_ID, amount.negated());

var transactionBytes = transferTransaction.toBytes();

TransferTransaction transferTransactionDeserialized = (TransferTransaction) Transaction.fromBytes(transactionBytes);

var transactionResponse = transferTransactionDeserialized
.addHbarTransfer(recipientId, amount)
.setTransactionMemo("transfer test")
.execute(client);

System.out.println("transaction ID: " + transactionResponse);

TransactionRecord record = transactionResponse.getRecord(client);

System.out.println("transferred " + amount + "...");

Hbar senderBalanceAfter = new AccountBalanceQuery()
.setAccountId(OPERATOR_ID)
.execute(client)
.hbars;

Hbar receiptBalanceAfter = new AccountBalanceQuery()
.setAccountId(recipientId)
.execute(client)
.hbars;

System.out.println("" + OPERATOR_ID + " balance = " + senderBalanceAfter);
System.out.println("" + recipientId + " balance = " + receiptBalanceAfter);
System.out.println("Transfer memo: " + record.transactionMemo);
}
}
Loading

0 comments on commit 6f1239c

Please sign in to comment.