Skip to content

Commit a4ab6f8

Browse files
feat(TCK): Implement CreateTopic transaction endpoint
Signed-off-by: Mario Dimitrov <mario.dimitrov@limechain.tech>
1 parent cb77d92 commit a4ab6f8

File tree

3 files changed

+126
-0
lines changed

3 files changed

+126
-0
lines changed

tck/methods/topic.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
import {
2+
TopicCreateTransaction,
3+
Timestamp,
4+
CustomFixedFee,
5+
Hbar,
6+
} from "@hashgraph/sdk";
7+
import Long from "long";
8+
9+
import { applyCommonTransactionParams } from "../params/common-tx-params";
10+
import { TopicCreateParams } from "../params/topic";
11+
12+
import { sdk } from "../sdk_data";
13+
import { TopicResponse } from "../response/topic";
14+
15+
import { DEFAULT_GRPC_DEADLINE } from "../utils/constants/config";
16+
import { getKeyFromString } from "../utils/key";
17+
18+
export const createTopic = async ({
19+
memo,
20+
adminKey,
21+
submitKey,
22+
autoRenewPeriod,
23+
autoRenewAccount,
24+
feeScheduleKey,
25+
feeExemptKeys,
26+
customFees,
27+
commonTransactionParams,
28+
}: TopicCreateParams): Promise<TopicResponse> => {
29+
const transaction = new TopicCreateTransaction().setGrpcDeadline(
30+
DEFAULT_GRPC_DEADLINE,
31+
);
32+
// TODO: remove this
33+
transaction.setMaxTransactionFee(new Hbar(50));
34+
35+
if (memo != null) {
36+
transaction.setTopicMemo(memo);
37+
}
38+
39+
if (adminKey != null) {
40+
transaction.setAdminKey(getKeyFromString(adminKey));
41+
}
42+
43+
if (submitKey != null) {
44+
transaction.setSubmitKey(getKeyFromString(submitKey));
45+
}
46+
47+
if (autoRenewPeriod != null) {
48+
transaction.setAutoRenewPeriod(Long.fromString(autoRenewPeriod));
49+
}
50+
51+
if (autoRenewAccount != null) {
52+
transaction.setAutoRenewAccountId(autoRenewAccount);
53+
}
54+
55+
if (feeScheduleKey != null) {
56+
transaction.setFeeScheduleKey(getKeyFromString(feeScheduleKey));
57+
}
58+
59+
if (feeExemptKeys != null && feeExemptKeys.length > 0) {
60+
transaction.setFeeExemptKeys(
61+
feeExemptKeys.map((key: string) => getKeyFromString(key)),
62+
);
63+
}
64+
65+
if (customFees != null && customFees.length > 0) {
66+
const sdkCustomFees = customFees.map((fee) => {
67+
if (fee.fixedFee.denominatingTokenId) {
68+
return new CustomFixedFee()
69+
.setAmount(Long.fromString(fee.fixedFee.amount))
70+
.setDenominatingTokenId(fee.fixedFee.denominatingTokenId)
71+
.setFeeCollectorAccountId(fee.feeCollectorAccountId)
72+
.setAllCollectorsAreExempt(fee.feeCollectorsExempt);
73+
} else {
74+
return new CustomFixedFee()
75+
.setHbarAmount(
76+
Hbar.fromTinybars(Long.fromString(fee.fixedFee.amount)),
77+
)
78+
.setFeeCollectorAccountId(fee.feeCollectorAccountId)
79+
.setAllCollectorsAreExempt(fee.feeCollectorsExempt);
80+
}
81+
});
82+
transaction.setCustomFees(sdkCustomFees);
83+
}
84+
85+
if (commonTransactionParams != null) {
86+
applyCommonTransactionParams(
87+
commonTransactionParams,
88+
transaction,
89+
sdk.getClient(),
90+
);
91+
}
92+
93+
const response = await transaction.execute(sdk.getClient());
94+
const receipt = await response.getReceipt(sdk.getClient());
95+
96+
return {
97+
topicId: receipt.topicId?.toString(),
98+
status: receipt.status.toString(),
99+
};
100+
};

tck/params/topic.ts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
export interface TopicCreateParams {
2+
readonly memo?: string;
3+
readonly adminKey?: string;
4+
readonly submitKey?: string;
5+
readonly autoRenewPeriod: string;
6+
readonly autoRenewAccount?: string;
7+
readonly feeScheduleKey?: string;
8+
readonly feeExemptKeys?: string[];
9+
readonly customFees?: CustomFee[];
10+
readonly commonTransactionParams?: Record<string, any>;
11+
}
12+
13+
export interface CustomFee {
14+
readonly feeCollectorAccountId: string;
15+
readonly feeCollectorsExempt: boolean;
16+
readonly fixedFee: FixedFee;
17+
}
18+
19+
export interface FixedFee {
20+
readonly amount: string;
21+
readonly denominatingTokenId?: string;
22+
}

tck/response/topic.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export interface TopicResponse {
2+
readonly topicId?: string;
3+
readonly status: string;
4+
}

0 commit comments

Comments
 (0)