com.hedera.hashgraph.sdk.TransactionReceipt Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of sdk-full Show documentation
Show all versions of sdk-full Show documentation
Hedera™ Hashgraph SDK for Java
/*-
*
* Hedera Java SDK
*
* Copyright (C) 2020 - 2024 Hedera Hashgraph, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package com.hedera.hashgraph.sdk;
import com.google.common.base.MoreObjects;
import com.google.protobuf.ByteString;
import com.google.protobuf.InvalidProtocolBufferException;
import com.hedera.hashgraph.sdk.proto.ExchangeRateSet;
import com.hedera.hashgraph.sdk.proto.TimestampSeconds;
import org.bouncycastle.util.encoders.Hex;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.List;
/**
* The consensus result for a transaction, which might not be currently
* known, or may succeed or fail.
*/
public final class TransactionReceipt {
/**
* The transaction's ID
*/
@Nullable
public final TransactionId transactionId;
/**
* Whether the transaction succeeded or failed (or is unknown).
*/
public final Status status;
/**
* The exchange rate of Hbars to cents (USD).
*/
public final ExchangeRate exchangeRate;
/**
* The account ID, if a new account was created.
*/
@Nullable
public final AccountId accountId;
/**
* The file ID, if a new file was created.
*/
@Nullable
public final FileId fileId;
/**
* The contract ID, if a new contract was created.
*/
@Nullable
public final ContractId contractId;
/**
* The topic ID, if a new topic was created.
*/
@Nullable
public final TopicId topicId;
/**
* The token ID, if a new token was created.
*/
@Nullable
public final TokenId tokenId;
/**
* Updated sequence number for a consensus service topic.
* Set for {@link TopicMessageSubmitTransaction}.
*/
@Nullable
public final Long topicSequenceNumber;
/**
* Updated running hash for a consensus service topic.
* Set for {@link TopicMessageSubmitTransaction}.
*/
@Nullable
public final ByteString topicRunningHash;
/**
* In the receipt of TokenMint, TokenWipe, TokenBurn, For fungible tokens - the current total
* supply of this token. For non fungible tokens - the total number of NFTs issued for a given
* tokenID
*/
public final Long totalSupply;
/**
* In the receipt of a ScheduleCreate, the id of the newly created Scheduled Entity
*/
@Nullable
public final ScheduleId scheduleId;
/**
* In the receipt of a ScheduleCreate or ScheduleSign that resolves to SUCCESS, the
* TransactionID that should be used to query for the receipt or record of the relevant
* scheduled transaction
*/
@Nullable
public final TransactionId scheduledTransactionId;
/**
* In the receipt of a TokenMint for tokens of type NON_FUNGIBLE_UNIQUE, the serial numbers of
* the newly created NFTs
*/
public final List serials;
/**
* In the receipt of a NodeCreate, NodeUpdate, NodeDelete, the id of the newly created node.
* An affected node identifier.
* This value SHALL be set following a `createNode` transaction.
* This value SHALL be set following a `updateNode` transaction.
* This value SHALL be set following a `deleteNode` transaction.
* This value SHALL NOT be set following any other transaction.
*/
public final long nodeId;
/**
* The receipts of processing all transactions with the given id, in consensus time order.
*/
public final List duplicates;
/**
* The receipts (if any) of all child transactions spawned by the transaction with the
* given top-level id, in consensus order. Always empty if the top-level status is UNKNOWN.
*/
public final List children;
TransactionReceipt(
@Nullable TransactionId transactionId,
Status status,
ExchangeRate exchangeRate,
@Nullable AccountId accountId,
@Nullable FileId fileId,
@Nullable ContractId contractId,
@Nullable TopicId topicId,
@Nullable TokenId tokenId,
@Nullable Long topicSequenceNumber,
@Nullable ByteString topicRunningHash,
Long totalSupply,
@Nullable ScheduleId scheduleId,
@Nullable TransactionId scheduledTransactionId,
List serials,
long nodeId,
List duplicates,
List children
) {
this.transactionId = transactionId;
this.status = status;
this.exchangeRate = exchangeRate;
this.accountId = accountId;
this.fileId = fileId;
this.contractId = contractId;
this.topicId = topicId;
this.tokenId = tokenId;
this.topicSequenceNumber = topicSequenceNumber;
this.topicRunningHash = topicRunningHash;
this.totalSupply = totalSupply;
this.scheduleId = scheduleId;
this.scheduledTransactionId = scheduledTransactionId;
this.serials = serials;
this.nodeId = nodeId;
this.duplicates = duplicates;
this.children = children;
}
/**
* Create transaction receipt from protobuf.
*
* @param transactionReceipt the protobuf
* @param duplicates list of duplicates
* @param children list of children
* @return the new transaction receipt
*/
static TransactionReceipt fromProtobuf(
com.hedera.hashgraph.sdk.proto.TransactionReceipt transactionReceipt,
List duplicates,
List children,
@Nullable TransactionId transactionId
) {
var status = Status.valueOf(transactionReceipt.getStatus());
var rate = transactionReceipt.getExchangeRate();
var exchangeRate = ExchangeRate.fromProtobuf(rate.getCurrentRate());
var accountId =
transactionReceipt.hasAccountID()
? AccountId.fromProtobuf(transactionReceipt.getAccountID())
: null;
var fileId =
transactionReceipt.hasFileID()
? FileId.fromProtobuf(transactionReceipt.getFileID())
: null;
var contractId =
transactionReceipt.hasContractID()
? ContractId.fromProtobuf(transactionReceipt.getContractID())
: null;
var topicId =
transactionReceipt.hasTopicID()
? TopicId.fromProtobuf(transactionReceipt.getTopicID())
: null;
var tokenId =
transactionReceipt.hasTokenID()
? TokenId.fromProtobuf(transactionReceipt.getTokenID())
: null;
var topicSequenceNumber =
transactionReceipt.getTopicSequenceNumber() == 0
? null
: transactionReceipt.getTopicSequenceNumber();
var topicRunningHash =
transactionReceipt.getTopicRunningHash().isEmpty()
? null
: transactionReceipt.getTopicRunningHash();
var totalSupply = transactionReceipt.getNewTotalSupply();
var scheduleId =
transactionReceipt.hasScheduleID()
? ScheduleId.fromProtobuf(transactionReceipt.getScheduleID())
: null;
var scheduledTransactionId =
transactionReceipt.hasScheduledTransactionID()
? TransactionId.fromProtobuf(transactionReceipt.getScheduledTransactionID())
: null;
var serials = transactionReceipt.getSerialNumbersList();
var nodeId = transactionReceipt.getNodeId();
return new TransactionReceipt(
transactionId,
status,
exchangeRate,
accountId,
fileId,
contractId,
topicId,
tokenId,
topicSequenceNumber,
topicRunningHash,
totalSupply,
scheduleId,
scheduledTransactionId,
serials,
nodeId,
duplicates,
children
);
}
/**
* Create a transaction receipt from a protobuf.
*
* @param transactionReceipt the protobuf
* @return the new transaction receipt
*/
public static TransactionReceipt fromProtobuf(com.hedera.hashgraph.sdk.proto.TransactionReceipt transactionReceipt) {
return fromProtobuf(transactionReceipt, new ArrayList<>(), new ArrayList<>(), null);
}
static TransactionReceipt fromProtobuf(
com.hedera.hashgraph.sdk.proto.TransactionReceipt transactionReceipt,
@Nullable TransactionId transactionId
) {
return fromProtobuf(transactionReceipt, new ArrayList<>(), new ArrayList<>(), transactionId);
}
/**
* Create a transaction receipt from a byte array.
*
* @param bytes the byte array
* @return the new transaction receipt
* @throws InvalidProtocolBufferException when there is an issue with the protobuf
*/
public static TransactionReceipt fromBytes(byte[] bytes) throws InvalidProtocolBufferException {
return fromProtobuf(com.hedera.hashgraph.sdk.proto.TransactionReceipt.parseFrom(bytes).toBuilder().build());
}
/**
* Validate the transaction status in the receipt.
*
* @param shouldValidate Whether to perform transaction status validation
* @return {@code this}
* @throws ReceiptStatusException when shouldValidate is true and the transaction status is not SUCCESS
*/
public TransactionReceipt validateStatus(boolean shouldValidate) throws ReceiptStatusException {
if (shouldValidate && status != Status.SUCCESS && status != Status.FEE_SCHEDULE_FILE_PART_UPLOADED) {
throw new ReceiptStatusException(transactionId, this);
}
return this;
}
/**
* Create the protobuf.
*
* @return the protobuf representation
*/
com.hedera.hashgraph.sdk.proto.TransactionReceipt toProtobuf() {
var transactionReceiptBuilder = com.hedera.hashgraph.sdk.proto.TransactionReceipt.newBuilder()
.setStatus(status.code)
.setExchangeRate(ExchangeRateSet.newBuilder()
.setCurrentRate(com.hedera.hashgraph.sdk.proto.ExchangeRate.newBuilder()
.setHbarEquiv(exchangeRate.hbars)
.setCentEquiv(exchangeRate.cents)
.setExpirationTime(TimestampSeconds.newBuilder()
.setSeconds(exchangeRate.expirationTime.getEpochSecond())
)
)
)
.setNewTotalSupply(totalSupply);
if (accountId != null) {
transactionReceiptBuilder.setAccountID(accountId.toProtobuf());
}
if (fileId != null) {
transactionReceiptBuilder.setFileID(fileId.toProtobuf());
}
if (contractId != null) {
transactionReceiptBuilder.setContractID(contractId.toProtobuf());
}
if (topicId != null) {
transactionReceiptBuilder.setTopicID(topicId.toProtobuf());
}
if (tokenId != null) {
transactionReceiptBuilder.setTokenID(tokenId.toProtobuf());
}
if (topicSequenceNumber != null) {
transactionReceiptBuilder.setTopicSequenceNumber(topicSequenceNumber);
}
if (topicRunningHash != null) {
transactionReceiptBuilder.setTopicRunningHash(topicRunningHash);
}
if (scheduleId != null) {
transactionReceiptBuilder.setScheduleID(scheduleId.toProtobuf());
}
if (scheduledTransactionId != null) {
transactionReceiptBuilder.setScheduledTransactionID(scheduledTransactionId.toProtobuf());
}
for (var serial : serials) {
transactionReceiptBuilder.addSerialNumbers(serial);
}
transactionReceiptBuilder.setNodeId(nodeId);
return transactionReceiptBuilder.build();
}
@Override
public String toString() {
return MoreObjects.toStringHelper(this)
.add("transactionId", transactionId)
.add("status", status)
.add("exchangeRate", exchangeRate)
.add("accountId", accountId)
.add("fileId", fileId)
.add("contractId", contractId)
.add("topicId", topicId)
.add("tokenId", tokenId)
.add("topicSequenceNumber", topicSequenceNumber)
.add("topicRunningHash", topicRunningHash != null ? Hex.encode(topicRunningHash.toByteArray()) : null)
.add("totalSupply", totalSupply)
.add("scheduleId", scheduleId)
.add("scheduledTransactionId", scheduledTransactionId)
.add("serials", serials)
.add("nodeId", nodeId)
.add("duplicates", duplicates)
.add("children", children)
.toString();
}
/**
* Create the byte array.
*
* @return the byte array representation
*/
public byte[] toBytes() {
return toProtobuf().toByteArray();
}
}