|
| 1 | +import { |
| 2 | + TopicCreateTransaction, |
| 3 | + Timestamp, |
| 4 | + CustomFixedFee, |
| 5 | + Hbar, |
| 6 | + TopicUpdateTransaction, |
| 7 | + TopicDeleteTransaction, |
| 8 | + TopicMessageSubmitTransaction, |
| 9 | + CustomFeeLimit, |
| 10 | + AccountId, |
| 11 | +} from "@hashgraph/sdk"; |
| 12 | +import Long from "long"; |
| 13 | + |
| 14 | +import { applyCommonTransactionParams } from "../params/common-tx-params"; |
| 15 | +import { |
| 16 | + TopicCreateParams, |
| 17 | + TopicUpdateParams, |
| 18 | + TopicDeleteParams, |
| 19 | + TopicSubmitMessageParams, |
| 20 | +} from "../params/topic"; |
| 21 | + |
| 22 | +import { sdk } from "../sdk_data"; |
| 23 | +import { TopicResponse } from "../response/topic"; |
| 24 | + |
| 25 | +import { DEFAULT_GRPC_DEADLINE } from "../utils/constants/config"; |
| 26 | +import { getKeyFromString } from "../utils/key"; |
| 27 | + |
| 28 | +export const createTopic = async ({ |
| 29 | + memo, |
| 30 | + adminKey, |
| 31 | + submitKey, |
| 32 | + autoRenewPeriod, |
| 33 | + autoRenewAccountId, |
| 34 | + feeScheduleKey, |
| 35 | + feeExemptKeys, |
| 36 | + customFees, |
| 37 | + commonTransactionParams, |
| 38 | +}: TopicCreateParams): Promise<TopicResponse> => { |
| 39 | + const transaction = new TopicCreateTransaction().setGrpcDeadline( |
| 40 | + DEFAULT_GRPC_DEADLINE, |
| 41 | + ); |
| 42 | + |
| 43 | + if (memo != null) { |
| 44 | + transaction.setTopicMemo(memo); |
| 45 | + } |
| 46 | + |
| 47 | + if (adminKey != null) { |
| 48 | + transaction.setAdminKey(getKeyFromString(adminKey)); |
| 49 | + } |
| 50 | + |
| 51 | + if (submitKey != null) { |
| 52 | + transaction.setSubmitKey(getKeyFromString(submitKey)); |
| 53 | + } |
| 54 | + |
| 55 | + if (autoRenewPeriod != null) { |
| 56 | + transaction.setAutoRenewPeriod(Long.fromString(autoRenewPeriod)); |
| 57 | + } |
| 58 | + |
| 59 | + if (autoRenewAccountId != null) { |
| 60 | + transaction.setAutoRenewAccountId(autoRenewAccountId); |
| 61 | + } |
| 62 | + |
| 63 | + if (feeScheduleKey != null) { |
| 64 | + transaction.setFeeScheduleKey(getKeyFromString(feeScheduleKey)); |
| 65 | + } |
| 66 | + |
| 67 | + if (feeExemptKeys != null && feeExemptKeys.length > 0) { |
| 68 | + transaction.setFeeExemptKeys( |
| 69 | + feeExemptKeys.map((key: string) => getKeyFromString(key)), |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + if (customFees != null && customFees.length > 0) { |
| 74 | + const sdkCustomFees = customFees.map((fee) => { |
| 75 | + if (fee.fixedFee.denominatingTokenId) { |
| 76 | + return new CustomFixedFee() |
| 77 | + .setAmount(Long.fromString(fee.fixedFee.amount)) |
| 78 | + .setDenominatingTokenId(fee.fixedFee.denominatingTokenId) |
| 79 | + .setFeeCollectorAccountId(fee.feeCollectorAccountId) |
| 80 | + .setAllCollectorsAreExempt(fee.feeCollectorsExempt); |
| 81 | + } else { |
| 82 | + return new CustomFixedFee() |
| 83 | + .setHbarAmount( |
| 84 | + Hbar.fromTinybars(Long.fromString(fee.fixedFee.amount)), |
| 85 | + ) |
| 86 | + .setFeeCollectorAccountId(fee.feeCollectorAccountId) |
| 87 | + .setAllCollectorsAreExempt(fee.feeCollectorsExempt); |
| 88 | + } |
| 89 | + }); |
| 90 | + transaction.setCustomFees(sdkCustomFees); |
| 91 | + } |
| 92 | + |
| 93 | + if (commonTransactionParams != null) { |
| 94 | + applyCommonTransactionParams( |
| 95 | + commonTransactionParams, |
| 96 | + transaction, |
| 97 | + sdk.getClient(), |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + const response = await transaction.execute(sdk.getClient()); |
| 102 | + const receipt = await response.getReceipt(sdk.getClient()); |
| 103 | + |
| 104 | + return { |
| 105 | + topicId: receipt.topicId?.toString(), |
| 106 | + status: receipt.status.toString(), |
| 107 | + }; |
| 108 | +}; |
| 109 | + |
| 110 | +export const updateTopic = async ({ |
| 111 | + topicId, |
| 112 | + memo, |
| 113 | + adminKey, |
| 114 | + submitKey, |
| 115 | + autoRenewPeriod, |
| 116 | + autoRenewAccountId, |
| 117 | + expirationTime, |
| 118 | + feeScheduleKey, |
| 119 | + feeExemptKeys, |
| 120 | + customFees, |
| 121 | + commonTransactionParams, |
| 122 | +}: TopicUpdateParams): Promise<TopicResponse> => { |
| 123 | + const transaction = new TopicUpdateTransaction().setGrpcDeadline( |
| 124 | + DEFAULT_GRPC_DEADLINE, |
| 125 | + ); |
| 126 | + |
| 127 | + if (topicId != null) { |
| 128 | + transaction.setTopicId(topicId); |
| 129 | + } |
| 130 | + |
| 131 | + if (memo != null) { |
| 132 | + transaction.setTopicMemo(memo); |
| 133 | + } |
| 134 | + |
| 135 | + if (adminKey != null) { |
| 136 | + console.log("adminKey in updateTopic", adminKey); |
| 137 | + transaction.setAdminKey(getKeyFromString(adminKey)); |
| 138 | + } |
| 139 | + |
| 140 | + if (submitKey != null) { |
| 141 | + transaction.setSubmitKey(getKeyFromString(submitKey)); |
| 142 | + } |
| 143 | + |
| 144 | + if (autoRenewPeriod != null) { |
| 145 | + transaction.setAutoRenewPeriod(Long.fromString(autoRenewPeriod)); |
| 146 | + } |
| 147 | + |
| 148 | + if (autoRenewAccountId != null) { |
| 149 | + console.log("autoRenewAccountId in updateTopic", autoRenewAccountId); |
| 150 | + transaction.setAutoRenewAccountId(autoRenewAccountId); |
| 151 | + } |
| 152 | + |
| 153 | + if (expirationTime != null) { |
| 154 | + transaction.setExpirationTime( |
| 155 | + new Timestamp(Long.fromString(expirationTime), 0), |
| 156 | + ); |
| 157 | + } |
| 158 | + |
| 159 | + if (feeScheduleKey != null) { |
| 160 | + const feeScheduleKeyObj = getKeyFromString(feeScheduleKey); |
| 161 | + transaction.setFeeScheduleKey(feeScheduleKeyObj); |
| 162 | + } |
| 163 | + |
| 164 | + if (feeExemptKeys != null) { |
| 165 | + if (feeExemptKeys.length === 0) { |
| 166 | + transaction.clearFeeExemptKeys(); |
| 167 | + } else { |
| 168 | + transaction.setFeeExemptKeys( |
| 169 | + feeExemptKeys.map((key: string) => getKeyFromString(key)), |
| 170 | + ); |
| 171 | + } |
| 172 | + } |
| 173 | + |
| 174 | + if (customFees != null) { |
| 175 | + if (customFees.length === 0) { |
| 176 | + transaction.clearCustomFees(); |
| 177 | + } else { |
| 178 | + const sdkCustomFees = customFees.map((fee) => { |
| 179 | + if (fee.fixedFee.denominatingTokenId) { |
| 180 | + return new CustomFixedFee() |
| 181 | + .setAmount(Long.fromString(fee.fixedFee.amount)) |
| 182 | + .setDenominatingTokenId( |
| 183 | + fee.fixedFee.denominatingTokenId, |
| 184 | + ) |
| 185 | + .setFeeCollectorAccountId(fee.feeCollectorAccountId) |
| 186 | + .setAllCollectorsAreExempt(fee.feeCollectorsExempt); |
| 187 | + } else { |
| 188 | + return new CustomFixedFee() |
| 189 | + .setHbarAmount( |
| 190 | + Hbar.fromTinybars( |
| 191 | + Long.fromString(fee.fixedFee.amount), |
| 192 | + ), |
| 193 | + ) |
| 194 | + .setFeeCollectorAccountId(fee.feeCollectorAccountId) |
| 195 | + .setAllCollectorsAreExempt(fee.feeCollectorsExempt); |
| 196 | + } |
| 197 | + }); |
| 198 | + transaction.setCustomFees(sdkCustomFees); |
| 199 | + } |
| 200 | + } |
| 201 | + |
| 202 | + if (commonTransactionParams != null) { |
| 203 | + applyCommonTransactionParams( |
| 204 | + commonTransactionParams, |
| 205 | + transaction, |
| 206 | + sdk.getClient(), |
| 207 | + ); |
| 208 | + } |
| 209 | + |
| 210 | + const response = await transaction.execute(sdk.getClient()); |
| 211 | + const receipt = await response.getReceipt(sdk.getClient()); |
| 212 | + |
| 213 | + return { |
| 214 | + status: receipt.status.toString(), |
| 215 | + }; |
| 216 | +}; |
| 217 | + |
| 218 | +export const deleteTopic = async ({ |
| 219 | + topicId, |
| 220 | + commonTransactionParams, |
| 221 | +}: TopicDeleteParams): Promise<TopicResponse> => { |
| 222 | + const transaction = new TopicDeleteTransaction().setGrpcDeadline( |
| 223 | + DEFAULT_GRPC_DEADLINE, |
| 224 | + ); |
| 225 | + |
| 226 | + if (topicId != null) { |
| 227 | + console.log("topicId in deleteTopic", topicId); |
| 228 | + transaction.setTopicId(topicId); |
| 229 | + } |
| 230 | + |
| 231 | + if (commonTransactionParams != null) { |
| 232 | + applyCommonTransactionParams( |
| 233 | + commonTransactionParams, |
| 234 | + transaction, |
| 235 | + sdk.getClient(), |
| 236 | + ); |
| 237 | + } |
| 238 | + |
| 239 | + const response = await transaction.execute(sdk.getClient()); |
| 240 | + const receipt = await response.getReceipt(sdk.getClient()); |
| 241 | + |
| 242 | + return { |
| 243 | + status: receipt.status.toString(), |
| 244 | + }; |
| 245 | +}; |
| 246 | + |
| 247 | +export const submitTopicMessage = async ({ |
| 248 | + topicId, |
| 249 | + message, |
| 250 | + maxChunks, |
| 251 | + chunkSize, |
| 252 | + customFeeLimits, |
| 253 | + commonTransactionParams, |
| 254 | +}: TopicSubmitMessageParams): Promise<TopicResponse> => { |
| 255 | + const transaction = new TopicMessageSubmitTransaction().setGrpcDeadline( |
| 256 | + DEFAULT_GRPC_DEADLINE, |
| 257 | + ); |
| 258 | + |
| 259 | + if (topicId != null) { |
| 260 | + transaction.setTopicId(topicId); |
| 261 | + } |
| 262 | + |
| 263 | + if (message != null) { |
| 264 | + transaction.setMessage(message); |
| 265 | + } else { |
| 266 | + throw new Error("Message is required"); |
| 267 | + } |
| 268 | + |
| 269 | + if (maxChunks != null) { |
| 270 | + transaction.setMaxChunks(maxChunks); |
| 271 | + } |
| 272 | + |
| 273 | + if (chunkSize != null) { |
| 274 | + transaction.setChunkSize(chunkSize); |
| 275 | + } |
| 276 | + |
| 277 | + if (customFeeLimits != null) { |
| 278 | + const sdkCustomFeeLimits = customFeeLimits.map((feeLimit) => { |
| 279 | + const customFixedFees = feeLimit.fixedFees.map((fee) => { |
| 280 | + if (fee.denominatingTokenId) { |
| 281 | + return new CustomFixedFee() |
| 282 | + .setAmount(Long.fromString(fee.amount)) |
| 283 | + .setDenominatingTokenId(fee.denominatingTokenId); |
| 284 | + } else { |
| 285 | + return new CustomFixedFee().setHbarAmount( |
| 286 | + Hbar.fromTinybars(Long.fromString(fee.amount)), |
| 287 | + ); |
| 288 | + } |
| 289 | + }); |
| 290 | + return new CustomFeeLimit() |
| 291 | + .setAccountId(AccountId.fromString(feeLimit.payerId)) |
| 292 | + .setFees(customFixedFees); |
| 293 | + }); |
| 294 | + transaction.setCustomFeeLimits(sdkCustomFeeLimits); |
| 295 | + } |
| 296 | + |
| 297 | + if (commonTransactionParams != null) { |
| 298 | + applyCommonTransactionParams( |
| 299 | + commonTransactionParams, |
| 300 | + transaction, |
| 301 | + sdk.getClient(), |
| 302 | + ); |
| 303 | + } |
| 304 | + const response = await transaction.execute(sdk.getClient()); |
| 305 | + const receipt = await response.getReceipt(sdk.getClient()); |
| 306 | + |
| 307 | + return { |
| 308 | + status: receipt.status.toString(), |
| 309 | + }; |
| 310 | +}; |
0 commit comments