@hashgraph/sdk
Version:
365 lines (335 loc) • 15.7 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
var _Transaction = _interopRequireDefault(require("../transaction/Transaction.cjs"));
var _AccountId = _interopRequireDefault(require("./AccountId.cjs"));
var _ContractId = _interopRequireDefault(require("../contract/ContractId.cjs"));
var _TokenId = _interopRequireDefault(require("../token/TokenId.cjs"));
var _NftId = _interopRequireDefault(require("../token/NftId.cjs"));
var _long = _interopRequireDefault(require("long"));
var _Hbar = _interopRequireDefault(require("../Hbar.cjs"));
var _HbarAllowance = _interopRequireDefault(require("./HbarAllowance.cjs"));
var _TokenAllowance = _interopRequireDefault(require("./TokenAllowance.cjs"));
var _TokenNftAllowance = _interopRequireDefault(require("./TokenNftAllowance.cjs"));
var util = _interopRequireWildcard(require("../util.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
/**
* @namespace proto
* @typedef {import("@hiero-ledger/proto").proto.ITransaction} HieroProto.proto.ITransaction
* @typedef {import("@hiero-ledger/proto").proto.ISignedTransaction} HieroProto.proto.ISignedTransaction
* @typedef {import("@hiero-ledger/proto").proto.TransactionBody} HieroProto.proto.TransactionBody
* @typedef {import("@hiero-ledger/proto").proto.ITransactionBody} HieroProto.proto.ITransactionBody
* @typedef {import("@hiero-ledger/proto").proto.ITransactionResponse} HieroProto.proto.ITransactionResponse
* @typedef {import("@hiero-ledger/proto").proto.IAccountID} HieroProto.proto.IAccountID
* @typedef {import("@hiero-ledger/proto").proto.IContractID} HieroProto.proto.IContractID
*/
/**
* @typedef {import("../channel/Channel.js").default} Channel
* @typedef {import("../client/Client.js").default<*, *>} Client
* @typedef {import("../transaction/TransactionId.js").default} TransactionId
* @typedef {import("bignumber.js").default} BigNumber
* @typedef {import("../long.js").LongObject} LongObject
*/
/**
* @deprecated - No longer supported via Hedera Protobufs
* Change properties for the given account.
*/
class AccountAllowanceAdjustTransaction extends _Transaction.default {
/**
* @param {object} [props]
* @param {HbarAllowance[]} [props.hbarAllowances]
* @param {TokenAllowance[]} [props.tokenAllowances]
* @param {TokenNftAllowance[]} [props.nftAllowances]
*/
constructor(props = {}) {
super();
/**
* @private
* @type {HbarAllowance[]}
*/
this._hbarAllowances = props.hbarAllowances != null ? props.hbarAllowances : [];
/**
* @private
* @type {TokenAllowance[]}
*/
this._tokenAllowances = props.tokenAllowances != null ? props.tokenAllowances : [];
/**
* @private
* @type {TokenNftAllowance[]}
*/
this._nftAllowances = props.nftAllowances != null ? props.nftAllowances : [];
}
/**
* @returns {HbarAllowance[]}
*/
get hbarAllowances() {
return this._hbarAllowances;
}
/**
* @deprecated
* @param {AccountId | string} spenderAccountId
* @param {number | string | Long | LongObject | BigNumber | Hbar} amount
* @returns {AccountAllowanceAdjustTransaction}
*/
addHbarAllowance(spenderAccountId, amount) {
const value = amount instanceof _Hbar.default ? amount : new _Hbar.default(amount);
return this._adjustHbarAllowance(null, spenderAccountId, util.requireNotNegative(value));
}
/**
* @param {AccountId | string | null} ownerAccountId
* @param {AccountId | ContractId | string} spenderAccountId
* @param {Hbar} amount
* @returns {AccountAllowanceAdjustTransaction}
*/
_adjustHbarAllowance(ownerAccountId, spenderAccountId, amount) {
this._requireNotFrozen();
this._hbarAllowances.push(new _HbarAllowance.default({
spenderAccountId: typeof spenderAccountId === "string" ? _AccountId.default.fromString(spenderAccountId) : spenderAccountId instanceof _ContractId.default ? _AccountId.default.fromEvmAddress(spenderAccountId.shard, spenderAccountId.realm, spenderAccountId.toEvmAddress()) : spenderAccountId,
ownerAccountId: typeof ownerAccountId === "string" ? _AccountId.default.fromString(ownerAccountId) : ownerAccountId instanceof _ContractId.default ? _AccountId.default.fromEvmAddress(ownerAccountId.shard, ownerAccountId.realm, ownerAccountId.toEvmAddress()) : ownerAccountId,
amount: amount
}));
return this;
}
/**
* @deprecated
* @param {AccountId | string} ownerAccountId
* @param {AccountId | string} spenderAccountId
* @param {number | string | Long | LongObject | BigNumber | Hbar} amount
* @returns {AccountAllowanceAdjustTransaction}
*/
grantHbarAllowance(ownerAccountId, spenderAccountId, amount) {
const value = amount instanceof _Hbar.default ? amount : new _Hbar.default(amount);
return this._adjustHbarAllowance(ownerAccountId, spenderAccountId, util.requireNotNegative(value));
}
/**
* @deprecated
* @param {AccountId | string} ownerAccountId
* @param {AccountId | string} spenderAccountId
* @param {number | string | Long | LongObject | BigNumber | Hbar} amount
* @returns {AccountAllowanceAdjustTransaction}
*/
revokeHbarAllowance(ownerAccountId, spenderAccountId, amount) {
const value = amount instanceof _Hbar.default ? amount : new _Hbar.default(amount);
return this._adjustHbarAllowance(ownerAccountId, spenderAccountId, util.requireNotNegative(value).negated());
}
/**
* @returns {TokenAllowance[]}
*/
get tokenAllowances() {
return this._tokenAllowances;
}
/**
* @deprecated
* @param {TokenId | string} tokenId
* @param {AccountId | string} spenderAccountId
* @param {Long | number} amount
* @returns {AccountAllowanceAdjustTransaction}
*/
addTokenAllowance(tokenId, spenderAccountId, amount) {
return this._adjustTokenAllowance(tokenId, null, spenderAccountId, util.requireNotNegative(_long.default.fromValue(amount)));
}
/**
* @param {TokenId | string} tokenId
* @param {AccountId | string | null} ownerAccountId
* @param {AccountId | ContractId | string} spenderAccountId
* @param {Long | number} amount
* @returns {AccountAllowanceAdjustTransaction}
*/
_adjustTokenAllowance(tokenId, ownerAccountId, spenderAccountId, amount) {
this._requireNotFrozen();
this._tokenAllowances.push(new _TokenAllowance.default({
tokenId: typeof tokenId === "string" ? _TokenId.default.fromString(tokenId) : tokenId,
spenderAccountId: typeof spenderAccountId === "string" ? _AccountId.default.fromString(spenderAccountId) : spenderAccountId instanceof _ContractId.default ? _AccountId.default.fromEvmAddress(spenderAccountId.shard, spenderAccountId.realm, spenderAccountId.toEvmAddress()) : spenderAccountId,
ownerAccountId: typeof ownerAccountId === "string" ? _AccountId.default.fromString(ownerAccountId) : ownerAccountId instanceof _ContractId.default ? _AccountId.default.fromEvmAddress(ownerAccountId.shard, ownerAccountId.realm, ownerAccountId.toEvmAddress()) : ownerAccountId,
amount: typeof amount === "number" ? _long.default.fromNumber(amount) : amount
}));
return this;
}
/**
* @deprecated
* @param {TokenId | string} tokenId
* @param {AccountId | string} ownerAccountId
* @param {AccountId | string} spenderAccountId
* @param {Long | number} amount
* @returns {AccountAllowanceAdjustTransaction}
*/
grantTokenAllowance(tokenId, ownerAccountId, spenderAccountId, amount) {
return this._adjustTokenAllowance(tokenId, ownerAccountId, spenderAccountId, util.requireNotNegative(_long.default.fromValue(amount)));
}
/**
* @deprecated
* @param {TokenId | string} tokenId
* @param {AccountId | string} ownerAccountId
* @param {AccountId | string} spenderAccountId
* @param {Long | number} amount
* @returns {AccountAllowanceAdjustTransaction}
*/
revokeTokenAllowance(tokenId, ownerAccountId, spenderAccountId, amount) {
return this._adjustTokenAllowance(tokenId, ownerAccountId, spenderAccountId, util.requireNotNegative(_long.default.fromValue(amount)));
}
/**
* @deprecated
* @param {NftId | string} nftId
* @param {AccountId | string} spenderAccountId
* @returns {AccountAllowanceAdjustTransaction}
*/
addTokenNftAllowance(nftId, spenderAccountId) {
const id = typeof nftId === "string" ? _NftId.default.fromString(nftId) : nftId;
return this._adjustTokenNftAllowance(id, null, spenderAccountId);
}
/**
* @param {NftId} nftId
* @param {AccountId | string | null} ownerAccountId
* @param {AccountId | ContractId | string} spenderAccountId
* @returns {AccountAllowanceAdjustTransaction}
*/
_adjustTokenNftAllowance(nftId, ownerAccountId, spenderAccountId) {
this._requireNotFrozen();
const spender = typeof spenderAccountId === "string" ? _AccountId.default.fromString(spenderAccountId) : spenderAccountId instanceof _ContractId.default ? _AccountId.default.fromEvmAddress(spenderAccountId.shard, spenderAccountId.realm, spenderAccountId.toEvmAddress()) : spenderAccountId;
const owner = typeof ownerAccountId === "string" ? _AccountId.default.fromString(ownerAccountId) : ownerAccountId instanceof _ContractId.default ? _AccountId.default.fromEvmAddress(ownerAccountId.shard, ownerAccountId.realm, ownerAccountId.toEvmAddress()) : ownerAccountId;
let found = false;
for (const allowance of this._nftAllowances) {
if (allowance.tokenId.compare(nftId.tokenId) === 0 && allowance.spenderAccountId != null && allowance.spenderAccountId.compare(spender) === 0) {
if (allowance.serialNumbers != null) {
allowance.serialNumbers.push(nftId.serial);
}
found = true;
break;
}
}
if (!found) {
this._nftAllowances.push(new _TokenNftAllowance.default({
tokenId: nftId.tokenId,
spenderAccountId: spender,
serialNumbers: [nftId.serial],
ownerAccountId: owner,
allSerials: false,
delegatingSpender: null
}));
}
return this;
}
/**
* @deprecated
* @param {NftId | string} nftId
* @param {AccountId | string} ownerAccountId
* @param {AccountId | string} spenderAccountId
* @returns {AccountAllowanceAdjustTransaction}
*/
grantTokenNftAllowance(nftId, ownerAccountId, spenderAccountId) {
const id = typeof nftId === "string" ? _NftId.default.fromString(nftId) : nftId;
util.requireNotNegative(id.serial);
return this._adjustTokenNftAllowance(id, ownerAccountId, spenderAccountId);
}
/**
* @deprecated
* @param {NftId | string} nftId
* @param {AccountId | string} ownerAccountId
* @param {AccountId | string} spenderAccountId
* @returns {AccountAllowanceAdjustTransaction}
*/
revokeTokenNftAllowance(nftId, ownerAccountId, spenderAccountId) {
const id = typeof nftId === "string" ? _NftId.default.fromString(nftId) : nftId;
util.requireNotNegative(id.serial);
return this._adjustTokenNftAllowance(new _NftId.default(id.tokenId, id.serial.negate()), ownerAccountId, spenderAccountId);
}
/**
* @deprecated - use `grantTokenNftAllowanceAllSerials()` instead
* @param {TokenId | string} tokenId
* @param {AccountId | string} spenderAccountId
* @returns {AccountAllowanceAdjustTransaction}
*/
addAllTokenNftAllowance(tokenId, spenderAccountId) {
return this._adjustTokenNftAllowanceAllSerials(tokenId, null, spenderAccountId, true);
}
/**
* @deprecated
* @param {TokenId | string} tokenId
* @param {AccountId | string} ownerAccountId
* @param {AccountId | string} spenderAccountId
* @returns {AccountAllowanceAdjustTransaction}
*/
grantTokenNftAllowanceAllSerials(tokenId, ownerAccountId, spenderAccountId) {
return this._adjustTokenNftAllowanceAllSerials(tokenId, ownerAccountId, spenderAccountId, true);
}
/**
* @deprecated
* @param {TokenId | string} tokenId
* @param {AccountId | string} ownerAccountId
* @param {AccountId | string} spenderAccountId
* @returns {AccountAllowanceAdjustTransaction}
*/
revokeTokenNftAllowanceAllSerials(tokenId, ownerAccountId, spenderAccountId) {
return this._adjustTokenNftAllowanceAllSerials(tokenId, ownerAccountId, spenderAccountId, false);
}
/**
* @param {TokenId | string} tokenId
* @param {AccountId | string | null} ownerAccountId
* @param {AccountId | ContractId | string} spenderAccountId
* @param {boolean} allSerials
* @returns {AccountAllowanceAdjustTransaction}
*/
_adjustTokenNftAllowanceAllSerials(tokenId, ownerAccountId, spenderAccountId, allSerials) {
this._requireNotFrozen();
this._nftAllowances.push(new _TokenNftAllowance.default({
tokenId: typeof tokenId === "string" ? _TokenId.default.fromString(tokenId) : tokenId,
ownerAccountId: ownerAccountId != null ? typeof ownerAccountId === "string" ? _AccountId.default.fromString(ownerAccountId) : ownerAccountId instanceof _ContractId.default ? _AccountId.default.fromEvmAddress(ownerAccountId.shard, ownerAccountId.realm, ownerAccountId.toEvmAddress()) : ownerAccountId : null,
spenderAccountId: typeof spenderAccountId === "string" ? _AccountId.default.fromString(spenderAccountId) : spenderAccountId instanceof _ContractId.default ? _AccountId.default.fromEvmAddress(spenderAccountId.shard, spenderAccountId.realm, spenderAccountId.toEvmAddress()) : spenderAccountId,
serialNumbers: null,
allSerials,
delegatingSpender: null
}));
return this;
}
/**
* @param {Client} client
*/
_validateChecksums(client) {
this._hbarAllowances.map(allowance => allowance._validateChecksums(client));
this._tokenAllowances.map(allowance => allowance._validateChecksums(client));
this._nftAllowances.map(allowance => allowance._validateChecksums(client));
}
/**
* @override
* @internal
* @param {Channel} channel
* @param {HieroProto.proto.ITransaction} request
* @returns {Promise<HieroProto.proto.ITransactionResponse>}
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
_execute(channel, request) {
return Promise.reject(new Error("This feature has been deprecated for this class."));
}
// eslint-disable-next-line jsdoc/require-returns-check
/**
* @deprecated
* @override
* @protected
* @returns {NonNullable<HieroProto.proto.TransactionBody["data"]>}
*/
_getTransactionDataCase() {
throw new Error("This feature has been deprecated for this class.");
}
// eslint-disable-next-line jsdoc/require-returns-check
/**
* @override
* @protected
* @returns {object}
*/
_makeTransactionData() {
throw new Error("This feature has been deprecated.");
}
/**
* @returns {string}
*/
_getLogId() {
const timestamp = /** @type {import("../Timestamp.js").default} */
this._transactionIds.current.validStart;
return `AccountAllowanceAdjustTransaction:${timestamp.toString()}`;
}
}
exports.default = AccountAllowanceAdjustTransaction;