|
| 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 | +}; |
0 commit comments