@hashgraph/sdk
Version:
302 lines (278 loc) • 11.6 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _AccountId = _interopRequireDefault(require("../account/AccountId.cjs"));
var _ContractId = _interopRequireDefault(require("../contract/ContractId.cjs"));
var _FileId = _interopRequireDefault(require("../file/FileId.cjs"));
var _TopicId = _interopRequireDefault(require("../topic/TopicId.cjs"));
var _TokenId = _interopRequireDefault(require("../token/TokenId.cjs"));
var _ScheduleId = _interopRequireDefault(require("../schedule/ScheduleId.cjs"));
var _ExchangeRate = _interopRequireDefault(require("../ExchangeRate.cjs"));
var _Status = _interopRequireDefault(require("../Status.cjs"));
var _long = _interopRequireDefault(require("long"));
var HieroProto = _interopRequireWildcard(require("@hashgraph/proto"));
var _TransactionId = _interopRequireDefault(require("../transaction/TransactionId.cjs"));
var hex = _interopRequireWildcard(require("../encoding/hex.cjs"));
function _interopRequireWildcard(e, t) { if ("function" == typeof WeakMap) var r = new WeakMap(), n = new WeakMap(); return (_interopRequireWildcard = function (e, t) { if (!t && e && e.__esModule) return e; var o, i, f = { __proto__: null, default: e }; if (null === e || "object" != typeof e && "function" != typeof e) return f; if (o = t ? n : r) { if (o.has(e)) return o.get(e); o.set(e, f); } for (const t in e) "default" !== t && {}.hasOwnProperty.call(e, t) && ((i = (o = Object.defineProperty) && Object.getOwnPropertyDescriptor(e, t)) && (i.get || i.set) ? o(f, t, i) : f[t] = e[t]); return f; })(e, t); }
function _interopRequireDefault(e) { return e && e.__esModule ? e : { default: e }; }
// SPDX-License-Identifier: Apache-2.0
/**
* @typedef {import("../ExchangeRate.js").ExchangeRateJSON} ExchangeRateJSON
*/
/**
* @typedef {object} TransactionReceiptJSON
* @property {string} status
* @property {?string} accountId
* @property {?string} filedId
* @property {?string} contractId
* @property {?string} topicId
* @property {?string} tokenId
* @property {?string} scheduleId
* @property {?ExchangeRateJSON} exchangeRate
* @property {?ExchangeRateJSON} nextExchangeRate
* @property {?string} topicSequenceNumber
* @property {?string} topicRunningHash
* @property {?string} totalSupply
* @property {?string} scheduledTransactionId
* @property {string[]} serials
* @property {TransactionReceiptJSON[]} duplicates
* @property {TransactionReceiptJSON[]} children
* @property {?string} nodeId
*/
/**
* The consensus result for a transaction, which might not be currently known,
* or may succeed or fail.
*/
class TransactionReceipt {
/**
* @private
* @param {object} props
* @param {Status} props.status
* @param {?AccountId} props.accountId
* @param {?FileId} props.fileId
* @param {?ContractId} props.contractId
* @param {?TopicId} props.topicId
* @param {?TokenId} props.tokenId
* @param {?ScheduleId} props.scheduleId
* @param {?ExchangeRate} props.exchangeRate
* @param {?ExchangeRate} props.nextExchangeRate
* @param {?Long} props.topicSequenceNumber
* @param {?Uint8Array} props.topicRunningHash
* @param {?Long} props.totalSupply
* @param {?TransactionId} props.scheduledTransactionId
* @param {Long[]} props.serials
* @param {TransactionReceipt[]} props.duplicates
* @param {TransactionReceipt[]} props.children
* @param {?Long} props.nodeId
*/
constructor(props) {
/**
* Whether the transaction succeeded or failed (or is unknown).
*
* @readonly
*/
this.status = props.status;
/**
* The account ID, if a new account was created.
*
* @readonly
*/
this.accountId = props.accountId;
/**
* The file ID, if a new file was created.
*
* @readonly
*/
this.fileId = props.fileId;
/**
* The contract ID, if a new contract was created.
*
* @readonly
*/
this.contractId = props.contractId;
/**
* The topic ID, if a new topic was created.
*
* @readonly
*/
this.topicId = props.topicId;
/**
* The token ID, if a new token was created.
*
* @readonly
*/
this.tokenId = props.tokenId;
/**
* The schedule ID, if a new schedule was created.
*
* @readonly
*/
this.scheduleId = props.scheduleId;
/**
* The exchange rate of Hbars to cents (USD).
*
* @readonly
*/
this.exchangeRate = props.exchangeRate;
/**
* The next exchange rate of Hbars to cents (USD).
*
* @readonly
*/
this.nextExchangeRate = props.nextExchangeRate;
/**
* Updated sequence number for a consensus service topic.
*
* @readonly
*/
this.topicSequenceNumber = props.topicSequenceNumber;
/**
* Updated running hash for a consensus service topic.
*
* @readonly
*/
this.topicRunningHash = props.topicRunningHash;
/**
* Updated total supply for a token
*
* @readonly
*/
this.totalSupply = props.totalSupply;
this.scheduledTransactionId = props.scheduledTransactionId;
this.serials = props.serials ?? [];
/**
* @readonly
*/
this.duplicates = props.duplicates ?? [];
/**
* @readonly
*/
this.children = props.children ?? [];
/**
* @readonly
* @description 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.
*/
this.nodeId = props.nodeId;
Object.freeze(this);
}
/**
* @internal
* @returns {HieroProto.proto.ITransactionGetReceiptResponse}
*/
_toProtobuf() {
const duplicates = this.duplicates.map(receipt => (/** @type {HieroProto.proto.ITransactionReceipt} */
receipt._toProtobuf().receipt));
const children = this.children.map(receipt => (/** @type {HieroProto.proto.ITransactionReceipt} */
receipt._toProtobuf().receipt));
return {
duplicateTransactionReceipts: duplicates,
childTransactionReceipts: children,
receipt: {
status: this.status.valueOf(),
accountID: this.accountId != null ? this.accountId._toProtobuf() : null,
fileID: this.fileId != null ? this.fileId._toProtobuf() : null,
contractID: this.contractId != null ? this.contractId._toProtobuf() : null,
topicID: this.topicId != null ? this.topicId._toProtobuf() : null,
tokenID: this.tokenId != null ? this.tokenId._toProtobuf() : null,
scheduleID: this.scheduleId != null ? this.scheduleId._toProtobuf() : null,
topicRunningHash: this.topicRunningHash == null ? null : this.topicRunningHash,
topicSequenceNumber: this.topicSequenceNumber,
exchangeRate: {
nextRate: this.nextExchangeRate != null ? this.nextExchangeRate._toProtobuf() : null,
currentRate: this.exchangeRate != null ? this.exchangeRate._toProtobuf() : null
},
scheduledTransactionID: this.scheduledTransactionId != null ? this.scheduledTransactionId._toProtobuf() : null,
serialNumbers: this.serials,
newTotalSupply: this.totalSupply,
nodeId: this.nodeId
}
};
}
/**
* @internal
* @param {HieroProto.proto.ITransactionGetReceiptResponse} response
* @returns {TransactionReceipt}
*/
static _fromProtobuf(response) {
const receipt = /** @type {HieroProto.proto.ITransactionReceipt} */
response.receipt;
const children = response.childTransactionReceipts != null ? response.childTransactionReceipts.map(child => TransactionReceipt._fromProtobuf({
receipt: child
})) : [];
const duplicates = response.duplicateTransactionReceipts != null ? response.duplicateTransactionReceipts.map(duplicate => TransactionReceipt._fromProtobuf({
receipt: duplicate
})) : [];
return new TransactionReceipt({
status: _Status.default._fromCode(receipt.status != null ? receipt.status : 0),
accountId: receipt.accountID != null ? _AccountId.default._fromProtobuf(receipt.accountID) : null,
fileId: receipt.fileID != null ? _FileId.default._fromProtobuf(receipt.fileID) : null,
contractId: receipt.contractID != null ? _ContractId.default._fromProtobuf(receipt.contractID) : null,
topicId: receipt.topicID != null ? _TopicId.default._fromProtobuf(receipt.topicID) : null,
tokenId: receipt.tokenID != null ? _TokenId.default._fromProtobuf(receipt.tokenID) : null,
scheduleId: receipt.scheduleID != null ? _ScheduleId.default._fromProtobuf(receipt.scheduleID) : null,
exchangeRate: receipt.exchangeRate != null ? _ExchangeRate.default._fromProtobuf(/** @type {HieroProto.proto.IExchangeRate} */
receipt.exchangeRate.currentRate) : null,
nextExchangeRate: receipt.exchangeRate != null ? _ExchangeRate.default._fromProtobuf(/** @type {HieroProto.proto.IExchangeRate} */
receipt.exchangeRate.nextRate) : null,
topicSequenceNumber: receipt.topicSequenceNumber == null ? null : _long.default.fromString(receipt.topicSequenceNumber.toString()),
topicRunningHash: receipt.topicRunningHash != null ? new Uint8Array(receipt.topicRunningHash) : null,
totalSupply: receipt.newTotalSupply != null ? _long.default.fromString(receipt.newTotalSupply.toString()) : null,
scheduledTransactionId: receipt.scheduledTransactionID != null ? _TransactionId.default._fromProtobuf(receipt.scheduledTransactionID) : null,
serials: receipt.serialNumbers != null ? receipt.serialNumbers.map(serial => _long.default.fromValue(serial)) : [],
children,
duplicates,
nodeId: receipt.nodeId != null ? receipt.nodeId : null
});
}
/**
* @param {Uint8Array} bytes
* @returns {TransactionReceipt}
*/
static fromBytes(bytes) {
return TransactionReceipt._fromProtobuf(HieroProto.proto.TransactionGetReceiptResponse.decode(bytes));
}
/**
* @returns {Uint8Array}
*/
toBytes() {
return HieroProto.proto.TransactionGetReceiptResponse.encode(this._toProtobuf()).finish();
}
/**
* @returns {TransactionReceiptJSON}
*/
toJSON() {
return {
status: this.status.toString(),
accountId: this.accountId?.toString() || null,
filedId: this.fileId?.toString() || null,
contractId: this.contractId?.toString() || null,
topicId: this.topicId?.toString() || null,
tokenId: this.tokenId?.toString() || null,
scheduleId: this.scheduleId?.toString() || null,
exchangeRate: this.exchangeRate?.toJSON() || null,
nextExchangeRate: this.nextExchangeRate?.toJSON() || null,
topicSequenceNumber: this.topicSequenceNumber?.toString() || null,
topicRunningHash: this.topicRunningHash != null ? hex.encode(this.topicRunningHash) : null,
totalSupply: this.totalSupply?.toString() || null,
scheduledTransactionId: this.scheduledTransactionId?.toString() || null,
serials: this.serials.map(serial => serial.toString()),
duplicates: this.duplicates.map(receipt => receipt.toJSON()),
children: this.children.map(receipt => receipt.toJSON()),
nodeId: this.nodeId?.toString() || null
};
}
/**
* @returns {string}
*/
toString() {
return JSON.stringify(this.toJSON());
}
}
exports.default = TransactionReceipt;