Skip to content

Commit

Permalink
feat(HIP-991): add topic fees to transactions
Browse files Browse the repository at this point in the history
Signed-off-by: venilinvasilev <[email protected]>
  • Loading branch information
venilinvasilev committed Feb 11, 2025
1 parent b965ea8 commit 6b02598
Show file tree
Hide file tree
Showing 7 changed files with 502 additions and 1 deletion.
24 changes: 23 additions & 1 deletion src/token/CustomFixedFee.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import Hbar from "../Hbar.js";
* @namespace proto
* @typedef {import("@hashgraph/proto").proto.ICustomFee} HashgraphProto.proto.ICustomFee
* @typedef {import("@hashgraph/proto").proto.IFixedFee} HashgraphProto.proto.IFixedFee
* @typedef {import("@hashgraph/proto").proto.IFixedCustomFee} HashgraphProto.proto.IFixedCustomFee
*/

export default class CustomFixedFee extends CustomFee {
Expand Down Expand Up @@ -127,7 +128,7 @@ export default class CustomFixedFee extends CustomFee {
* @internal
* @override
* @param {HashgraphProto.proto.ICustomFee} info
* @returns {CustomFee}
* @returns {CustomFixedFee}
*/
// eslint-disable-next-line @typescript-eslint/no-unused-vars
static _fromProtobuf(info) {
Expand Down Expand Up @@ -173,4 +174,25 @@ export default class CustomFixedFee extends CustomFee {
},
};
}

/**
* @internal
* @abstract
* @returns {HashgraphProto.proto.IFixedCustomFee}
*/
_toTopicFeeProtobuf() {
return {
feeCollectorAccountId:
this.feeCollectorAccountId != null
? this.feeCollectorAccountId._toProtobuf()
: null,
fixedFee: {
denominatingTokenId:
this._denominatingTokenId != null
? this._denominatingTokenId._toProtobuf()
: null,
amount: this._amount,
},
};
}
}
125 changes: 125 additions & 0 deletions src/topic/TopicCreateTransaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
*/

import AccountId from "../account/AccountId.js";
import CustomFixedFee from "../token/CustomFixedFee.js";
import Transaction, {
DEFAULT_AUTO_RENEW_PERIOD,
TRANSACTION_REGISTRY,
Expand Down Expand Up @@ -50,8 +51,11 @@ export default class TopicCreateTransaction extends Transaction {
* @param {object} props
* @param {Key} [props.adminKey]
* @param {Key} [props.submitKey]
* @param {Key} [props.feeScheduleKey]
* @param {Duration | Long | number} [props.autoRenewPeriod]
* @param {AccountId | string} [props.autoRenewAccountId]
* @param {CustomFixedFee[]} [props.customFees]
* @param {Key[]} [props.feeExemptKeys]
* @param {string} [props.topicMemo]
*/
constructor(props = {}) {
Expand All @@ -69,6 +73,18 @@ export default class TopicCreateTransaction extends Transaction {
*/
this._submitKey = null;

/**
* @private
* @type {?Key}
*/
this._feeScheduleKey = null;

/**
* @private
* @type {?Key[]}
*/
this._feeExemptKeys = null;

/**
* @private
* @type {?AccountId}
Expand All @@ -81,6 +97,12 @@ export default class TopicCreateTransaction extends Transaction {
*/
this._autoRenewPeriod = new Duration(DEFAULT_AUTO_RENEW_PERIOD);

/**
* @private
* @type {?CustomFixedFee[]}
*/
this._customFees = null;

/**
* @private
* @type {?string}
Expand All @@ -95,6 +117,14 @@ export default class TopicCreateTransaction extends Transaction {
this.setSubmitKey(props.submitKey);
}

if (props.feeScheduleKey != null) {
this.setFeeScheduleKey(props.feeScheduleKey);
}

if (props.feeExemptKeys != null) {
this.setFeeExemptKeys(props.feeExemptKeys);
}

if (props.autoRenewAccountId != null) {
this.setAutoRenewAccountId(props.autoRenewAccountId);
}
Expand All @@ -103,6 +133,10 @@ export default class TopicCreateTransaction extends Transaction {
this.setAutoRenewPeriod(props.autoRenewPeriod);
}

if (props.customFees != null) {
this.setCustomFees(props.customFees);
}

if (props.topicMemo != null) {
this.setTopicMemo(props.topicMemo);
}
Expand Down Expand Up @@ -140,6 +174,16 @@ export default class TopicCreateTransaction extends Transaction {
create.submitKey != null
? Key._fromProtobufKey(create.submitKey)
: undefined,
feeScheduleKey:
create.feeScheduleKey != null
? Key._fromProtobufKey(create.feeScheduleKey)
: undefined,
feeExemptKeys:
create.feeExemptKeyList != null
? create.feeExemptKeyList.map((key) =>
Key._fromProtobufKey(key),
)
: undefined,
autoRenewAccountId:
create.autoRenewAccount != null
? AccountId._fromProtobuf(create.autoRenewAccount)
Expand All @@ -150,6 +194,12 @@ export default class TopicCreateTransaction extends Transaction {
? create.autoRenewPeriod.seconds
: undefined
: undefined,
customFees:
create.customFees != null
? create.customFees.map((customFee) =>
CustomFixedFee._fromProtobuf(customFee),
)
: undefined,
topicMemo: create.memo != null ? create.memo : undefined,
}),
transactions,
Expand Down Expand Up @@ -238,6 +288,46 @@ export default class TopicCreateTransaction extends Transaction {
return this;
}

/**
* Returns the key which allows updates to the new topic’s fees.
* @returns {?Key}
*/
getFeeScheduleKey() {
return this._feeScheduleKey;
}

/**
* Sets the key which allows updates to the new topic’s fees.
* @param {Key} feeScheduleKey
* @returns {this}
*/
setFeeScheduleKey(feeScheduleKey) {
this._requireNotFrozen();
this._feeScheduleKey = feeScheduleKey;

return this;
}

/**
* Returns the keys that will be exempt from paying fees.
* @returns {?Key[]}
*/
getFeeExemptKeys() {
return this._feeExemptKeys;
}

/**
* Sets the keys that will be exempt from paying fees.
* @param {Key[]} feeExemptKeys
* @returns {this}
*/
setFeeExemptKeys(feeExemptKeys) {
this._requireNotFrozen();
this._feeExemptKeys = feeExemptKeys;

return this;
}

/**
* @deprecated - Use `getAutoRenewAccountId()` instead
* @returns {?AccountId}
Expand Down Expand Up @@ -298,6 +388,27 @@ export default class TopicCreateTransaction extends Transaction {
return this;
}

/**
* Returns the fixed fees to assess when a message is submitted to the new topic.
* @returns {?CustomFixedFee[]}
*/
getCustomFees() {
return this._customFees;
}

/**
* Sets the fixed fees to assess when a message is submitted to the new topic.
*
* @param {CustomFixedFee[]} customFees
* @returns {this}
*/
setCustomFees(customFees) {
this._requireNotFrozen();
this._customFees = customFees;

return this;
}

/**
* @param {Client} client
*/
Expand Down Expand Up @@ -340,11 +451,25 @@ export default class TopicCreateTransaction extends Transaction {
this._submitKey != null
? this._submitKey._toProtobufKey()
: null,
feeScheduleKey:
this._feeScheduleKey != null
? this._feeScheduleKey._toProtobufKey()
: null,
feeExemptKeyList:
this._feeExemptKeys != null
? this._feeExemptKeys.map((key) => key._toProtobufKey())
: null,
autoRenewAccount:
this._autoRenewAccountId != null
? this._autoRenewAccountId._toProtobuf()
: null,
autoRenewPeriod: this._autoRenewPeriod._toProtobuf(),
customFees:
this._customFees != null
? this._customFees.map((customFee) =>
customFee._toTopicFeeProtobuf(),
)
: null,
memo: this._topicMemo,
};
}
Expand Down
52 changes: 52 additions & 0 deletions src/topic/TopicInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import Duration from "../Duration.js";
import * as HashgraphProto from "@hashgraph/proto";
import Key from "../Key.js";
import LedgerId from "../LedgerId.js";
import CustomFixedFee from "../token/CustomFixedFee.js";

/**
* Current state of a topic.
Expand All @@ -41,8 +42,11 @@ export default class TopicInfo {
* @param {?Timestamp} props.expirationTime
* @param {?Key} props.adminKey
* @param {?Key} props.submitKey
* @param {?Key} props.feeScheduleKey
* @param {?Key[]} props.feeExemptKeys
* @param {?Duration} props.autoRenewPeriod
* @param {?AccountId} props.autoRenewAccountId
* @param {?CustomFixedFee[]} props.customFees
* @param {LedgerId|null} props.ledgerId
*/
constructor(props) {
Expand Down Expand Up @@ -95,6 +99,18 @@ export default class TopicInfo {
*/
this.submitKey = props.submitKey;

/**
* Access control for updating topic fees. Null If there is no key.
*
* @readonly
*/
this.feeScheduleKey = props.feeScheduleKey;

/**
* The keys that will are exempt from paying fees.
* @readonly
*/
this.feeExemptKeys = props.feeExemptKeys;
/**
* @readonly
*/
Expand All @@ -105,6 +121,12 @@ export default class TopicInfo {
*/
this.autoRenewAccountId = props.autoRenewAccountId;

/**
* The fixed fees assessed when a message is submitted to the topic.
* @readonly
*/
this.customFees = props.customFees;

this.ledgerId = props.ledgerId;

Object.freeze(this);
Expand Down Expand Up @@ -147,6 +169,16 @@ export default class TopicInfo {
info.submitKey != null
? Key._fromProtobufKey(info.submitKey)
: null,
feeScheduleKey:
info.feeScheduleKey != null
? Key._fromProtobufKey(info.feeScheduleKey)
: null,
feeExemptKeys:
info.feeExemptKeyList != null
? info.feeExemptKeyList.map((key) =>
Key._fromProtobufKey(key),
)
: null,
autoRenewPeriod:
info.autoRenewPeriod != null
? new Duration(
Expand All @@ -157,6 +189,12 @@ export default class TopicInfo {
info.autoRenewAccount != null
? AccountId._fromProtobuf(info.autoRenewAccount)
: null,
customFees:
info.customFees != null
? info.customFees.map((customFee) =>
CustomFixedFee._fromProtobuf(customFee),
)
: null,
ledgerId:
info.ledgerId != null
? LedgerId.fromBytes(info.ledgerId)
Expand Down Expand Up @@ -187,6 +225,14 @@ export default class TopicInfo {
this.submitKey != null
? this.submitKey._toProtobufKey()
: null,
feeScheduleKey:
this.feeScheduleKey != null
? this.feeScheduleKey._toProtobufKey()
: null,
feeExemptKeyList:
this.feeExemptKeys != null
? this.feeExemptKeys.map((key) => key._toProtobufKey())
: null,
autoRenewPeriod:
this.autoRenewPeriod != null
? this.autoRenewPeriod._toProtobuf()
Expand All @@ -195,6 +241,12 @@ export default class TopicInfo {
this.autoRenewAccountId != null
? this.autoRenewAccountId._toProtobuf()
: null,
customFees:
this.customFees != null
? this.customFees.map((customFee) =>
customFee._toProtobuf(),
)
: null,
},
};
}
Expand Down
Loading

0 comments on commit 6b02598

Please sign in to comment.