@hiero-ledger/sdk
Version:
307 lines (279 loc) • 9.11 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _Timestamp = _interopRequireDefault(require("../Timestamp.cjs"));
var _AccountId = _interopRequireDefault(require("../account/AccountId.cjs"));
var _Transaction = _interopRequireWildcard(require("../transaction/Transaction.cjs"));
var _Key = _interopRequireDefault(require("../Key.cjs"));
var _Hbar = _interopRequireDefault(require("../Hbar.cjs"));
var HieroProto = _interopRequireWildcard(require("@hashgraph/proto"));
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("bignumber.js").default} BigNumber
* @typedef {import("../channel/Channel.js").default} Channel
* @typedef {import("../client/Client.js").default<*, *>} Client
* @typedef {import("../transaction/TransactionId.js").default} TransactionId
* @typedef {import("../PublicKey.js").default} PublicKey
* @typedef {import("../PrivateKey.js").default} PrivateKey
*/
/**
* Create a new Hedera™ crypto-currency account.
*/
class ScheduleCreateTransaction extends _Transaction.default {
/**
* @param {object} [props]
* @param {Key} [props.adminKey]
* @param {AccountId} [props.payerAccountID]
* @param {string} [props.scheduleMemo]
* @param {Timestamp} [props.expirationTime]
* @param {boolean} [props.waitForExpiry]
*/
constructor(props = {}) {
super();
/**
* @private
* @type {?Key}
*/
this._adminKey = null;
/**
* @private
* @type {?Transaction}
*/
this._scheduledTransaction = null;
/**
* @private
* @type {?AccountId}
*/
this._payerAccountId = null;
/**
* @private
* @type {?string}
*/
this._scheduleMemo = null;
/**
* @private
* @type {Set<string>}
*/
this._scheduledSignerPublicKeys = new Set();
/**
* @private
* @type {?Timestamp}
*/
this._expirationTime = null;
/**
* @private
* @type {?boolean}
*/
this._waitForExpiry = null;
if (props.adminKey != null) {
this.setAdminKey(props.adminKey);
}
if (props.payerAccountID != null) {
this.setPayerAccountId(props.payerAccountID);
}
if (props.scheduleMemo != null) {
this.setScheduleMemo(props.scheduleMemo);
}
this._defaultMaxTransactionFee = new _Hbar.default(5);
}
/**
* @internal
* @param {HieroProto.proto.ITransaction[]} transactions
* @param {HieroProto.proto.ISignedTransaction[]} signedTransactions
* @param {TransactionId[]} transactionIds
* @param {AccountId[]} nodeIds
* @param {HieroProto.proto.ITransactionBody[]} bodies
* @returns {ScheduleCreateTransaction}
*/
static _fromProtobuf(transactions, signedTransactions, transactionIds, nodeIds, bodies) {
const body = bodies[0];
const create = /** @type {HieroProto.proto.IScheduleCreateTransactionBody} */
body.scheduleCreate;
const scheduledTransaction = new ScheduleCreateTransaction({
adminKey: create.adminKey != null ? _Key.default._fromProtobufKey(create.adminKey) : undefined,
payerAccountID: create.payerAccountID != null ? _AccountId.default._fromProtobuf(/** @type {HieroProto.proto.IAccountID} */
create.payerAccountID) : undefined,
scheduleMemo: create.memo != null ? create.memo : undefined,
waitForExpiry: create.waitForExpiry != null ? create.waitForExpiry : undefined,
expirationTime: create.expirationTime != null ? _Timestamp.default._fromProtobuf(create.expirationTime) : undefined
});
if (body.scheduleCreate != null) {
const scheduleCreateBody = body.scheduleCreate.scheduledTransactionBody;
const scheduleCreateBodyBytes = HieroProto.proto.TransactionBody.encode(
// @ts-ignore
scheduleCreateBody).finish();
const signedScheduledCreateTransaction = HieroProto.proto.SignedTransaction.encode({
bodyBytes: scheduleCreateBodyBytes
}).finish();
const scheduleCreatetransaction = {
signedTransactionBytes: signedScheduledCreateTransaction
};
const txlist = HieroProto.proto.TransactionList.encode({
transactionList: [scheduleCreatetransaction]
}).finish();
const finalScheduledDecodedTx = _Transaction.default.fromBytes(txlist);
scheduledTransaction._setScheduledTransaction(finalScheduledDecodedTx);
}
return _Transaction.default._fromProtobufTransactions(scheduledTransaction, transactions, signedTransactions, transactionIds, nodeIds, bodies);
}
/**
* @internal
* @param {Transaction} tx
* @returns {this}
*/
_setScheduledTransaction(tx) {
this._scheduledTransaction = tx;
return this;
}
/**
* @returns {?Key}
*/
get adminKey() {
return this._adminKey;
}
/**
* Set the key for this account.
*
* This is the key that must sign each transfer out of the account.
*
* If `receiverSignatureRequired` is true, then the key must also sign
* any transfer into the account.
*
* @param {Key} key
* @returns {this}
*/
setAdminKey(key) {
this._requireNotFrozen();
this._adminKey = key;
return this;
}
/**
* @returns {?AccountId}
*/
get payerAccountId() {
return this._payerAccountId;
}
/**
* @param {AccountId} account
* @returns {this}
*/
setPayerAccountId(account) {
this._requireNotFrozen();
this._payerAccountId = account;
return this;
}
/**
* @param {string} memo
* @returns {this}
*/
setScheduleMemo(memo) {
this._requireNotFrozen();
this._scheduleMemo = memo;
return this;
}
/**
* @returns {?string}
*/
get getScheduleMemo() {
this._requireNotFrozen();
return this._scheduleMemo;
}
/**
* @param {Transaction} transaction
* @returns {this}
*/
setScheduledTransaction(transaction) {
this._requireNotFrozen();
transaction._requireNotFrozen();
this._scheduledTransaction = transaction.schedule()._scheduledTransaction;
return this;
}
/**
* @param {Client} client
*/
_validateChecksums(client) {
if (this._payerAccountId != null) {
this._payerAccountId.validateChecksum(client);
}
}
/**
* @override
* @internal
* @param {Channel} channel
* @param {HieroProto.proto.ITransaction} request
* @returns {Promise<HieroProto.proto.ITransactionResponse>}
*/
_execute(channel, request) {
return channel.schedule.createSchedule(request);
}
/**
* @override
* @protected
* @returns {NonNullable<HieroProto.proto.TransactionBody["data"]>}
*/
_getTransactionDataCase() {
return "scheduleCreate";
}
/**
* @override
* @protected
* @returns {HieroProto.proto.IScheduleCreateTransactionBody}
*/
_makeTransactionData() {
return {
adminKey: this._adminKey != null ? this._adminKey._toProtobufKey() : null,
payerAccountID: this._payerAccountId != null ? this._payerAccountId._toProtobuf() : null,
scheduledTransactionBody: this._scheduledTransaction != null ? this._scheduledTransaction._getScheduledTransactionBody() : null,
memo: this._scheduleMemo,
waitForExpiry: this._waitForExpiry,
expirationTime: this._expirationTime != null ? this._expirationTime._toProtobuf() : null
};
}
/**
* @returns {string}
*/
_getLogId() {
const timestamp = /** @type {import("../Timestamp.js").default} */
this._transactionIds.current.validStart;
return `ScheduleCreateTransaction:${timestamp.toString()}`;
}
/**
* @param {?Timestamp} expirationTime
* @returns {this}
*/
setExpirationTime(expirationTime) {
this._expirationTime = expirationTime;
return this;
}
/**
* @returns {?Timestamp}
*/
get expirationTime() {
this._requireNotFrozen();
return this._expirationTime;
}
/**
* @param {boolean} waitForExpiry
* @returns {this}
*/
setWaitForExpiry(waitForExpiry) {
this._waitForExpiry = waitForExpiry;
return this;
}
/**
* @returns {?boolean}
*/
get waitForExpiry() {
this._requireNotFrozen();
return this._waitForExpiry;
}
}
exports.default = ScheduleCreateTransaction;
_Transaction.TRANSACTION_REGISTRY.set("scheduleCreate",
// eslint-disable-next-line @typescript-eslint/unbound-method
ScheduleCreateTransaction._fromProtobuf);
_Transaction.SCHEDULE_CREATE_TRANSACTION.push(() => new ScheduleCreateTransaction());