diff --git a/dlt-connector/src/config/const.ts b/dlt-connector/src/config/const.ts index e0dfc82a3..fa66eb6e7 100644 --- a/dlt-connector/src/config/const.ts +++ b/dlt-connector/src/config/const.ts @@ -1,3 +1,5 @@ export const LOG4JS_BASE_CATEGORY = 'dlt' // 7 days export const MIN_TOPIC_EXPIRE_MILLISECONDS_FOR_UPDATE = 1000 * 60 * 60 * 24 * 7 +// 10 minutes +export const MIN_TOPIC_EXPIRE_MILLISECONDS_FOR_SEND_MESSAGE = 1000 * 60 * 10 \ No newline at end of file diff --git a/dlt-connector/src/interactions/sendToHiero/SendToHiero.context.ts b/dlt-connector/src/interactions/sendToHiero/SendToHiero.context.ts index 1dd74976c..f510be3bd 100644 --- a/dlt-connector/src/interactions/sendToHiero/SendToHiero.context.ts +++ b/dlt-connector/src/interactions/sendToHiero/SendToHiero.context.ts @@ -28,7 +28,7 @@ import { DeferredTransferTransactionRole } from './DeferredTransferTransaction.r import { RedeemDeferredTransferTransactionRole } from './RedeemDeferredTransferTransaction.role' import { RegisterAddressTransactionRole } from './RegisterAddressTransaction.role' import { TransferTransactionRole } from './TransferTransaction.role' - +import { isTopicStillOpen } from '../../utils/hiero' const logger = getLogger(`${LOG4JS_BASE_CATEGORY}.interactions.sendToHiero.SendToHieroContext`) /** @@ -46,6 +46,10 @@ export async function SendToHieroContext( const outboundTransaction = builder.buildOutbound() validate(outboundTransaction) + if (!isTopicStillOpen(role.getRecipientCommunityTopicId())) { + throw new Error('recipient topic is not open long enough for sending messages') + } + // send outbound transaction to hiero at first, because we need the transaction id for inbound transaction const outboundHieroTransactionIdString = await sendViaHiero( outboundTransaction, diff --git a/dlt-connector/src/utils/hiero.ts b/dlt-connector/src/utils/hiero.ts new file mode 100644 index 000000000..9ea62042f --- /dev/null +++ b/dlt-connector/src/utils/hiero.ts @@ -0,0 +1,16 @@ +import { HieroId } from '../schemas/typeGuard.schema' +import { HieroClient } from '../client/hiero/HieroClient' +import { MIN_TOPIC_EXPIRE_MILLISECONDS_FOR_SEND_MESSAGE } from '../config/const' + +/** + * Checks whether the given topic in the Hedera network will remain open + * for sending messages for at least `MIN_TOPIC_EXPIRE_MILLISECONDS_FOR_SEND_MESSAGE` milliseconds. + * + * @param {HieroId} hieroTopicId - The topic ID to check. + * @returns {Promise} `true` if the topic is still open long enough, otherwise `false`. + */ +export async function isTopicStillOpen(hieroTopicId: HieroId): Promise { + const hieroClient = HieroClient.getInstance() + const topicInfo = await hieroClient.getTopicInfo(hieroTopicId) + return topicInfo.expirationTime.getTime() > new Date().getTime() + MIN_TOPIC_EXPIRE_MILLISECONDS_FOR_SEND_MESSAGE +} \ No newline at end of file