UNPKG

@fairmint/canton-node-sdk

Version:
60 lines 2.61 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.getCurrentMiningRoundContext = getCurrentMiningRoundContext; exports.getCurrentMiningRoundDomainId = getCurrentMiningRoundDomainId; /** * Finds the latest mining round that is currently open (\`opensAt\` is in the past) * and returns useful context information. * * @throws Error if no mining round satisfies the criteria */ async function getCurrentMiningRoundContext(validatorClient) { const miningRoundsResponse = await validatorClient.getOpenAndIssuingMiningRounds(); // Filter for rounds that have opened already (opensAt <= now) const now = new Date(); const validOpenRounds = miningRoundsResponse.open_mining_rounds.filter(round => { try { const opensAtRaw = round?.contract?.payload?.opensAt; if (!opensAtRaw) return false; const opensAt = new Date(opensAtRaw); return opensAt <= now; } catch { return false; } }); if (validOpenRounds.length === 0) { throw new Error('No open mining rounds found with opensAt <= now'); } // Use the *last* round that has opened (the most recent open one) const lastOpenRound = validOpenRounds[validOpenRounds.length - 1]; const openMiningRoundContract = { contractId: lastOpenRound.contract.contract_id, templateId: lastOpenRound.contract.template_id, createdEventBlob: lastOpenRound.contract.created_event_blob, synchronizerId: lastOpenRound.domain_id, // Using domainId as synchronizerId (legacy behaviour) }; const issuingMiningRounds = (miningRoundsResponse.issuing_mining_rounds || []).map((round) => ({ round: round.round_number, contractId: (round.contract_id ?? round.contract?.contract_id), })); return { openMiningRound: openMiningRoundContract.contractId, openMiningRoundContract, issuingMiningRounds, }; } /** * Gets the domain ID from the current mining round context. * This is useful for operations that need to automatically determine the domain ID. * * @param validatorClient - Validator API client for getting mining round information * @returns Promise resolving to the domain ID string * @throws Error if no mining round satisfies the criteria */ async function getCurrentMiningRoundDomainId(validatorClient) { const miningRoundContext = await getCurrentMiningRoundContext(validatorClient); return miningRoundContext.openMiningRoundContract.synchronizerId; } //# sourceMappingURL=mining-rounds.js.map