diff --git a/client/src/app/routes/stardust/TransactionPage.tsx b/client/src/app/routes/stardust/TransactionPage.tsx index 3f6008b12..c438f9ef3 100644 --- a/client/src/app/routes/stardust/TransactionPage.tsx +++ b/client/src/app/routes/stardust/TransactionPage.tsx @@ -72,58 +72,8 @@ const TransactionPage: React.FC> = ({ }); }, [inputs, inputsExtraInfo]); - useEffect(() => { - if (block?.payload?.type === PayloadType.Transaction) { - const transactionPayload = block.payload as ITransactionPayload; - const transactionEssence = transactionPayload.essence as RegularTransactionEssence; - setIncludedBlockId(Utils.blockId(block)); - setTangleNetworkId(transactionEssence.networkId); - setInputsCommitment(transactionEssence.inputsCommitment); - } - }, [block]); - const updateInputExtraInfo = async (input: IInput) => { - if (isExpirationExists(input)) { - if (isOutputSpent(input)) { - const transactionSpentId = input?.output?.metadata.transactionIdSpent as string; - const transactionTimestamp = await getTransactionTimestamp(transactionSpentId, apiClient, network); - - const expirationUnlockCondition = getUnlockCondition(input, UnlockConditionType.Expiration) as ExpirationUnlockCondition; - const isSpentAfterUnlock = - expirationUnlockCondition && - transactionTimestamp && - DateHelper.isExpired(expirationUnlockCondition.unixTime * 1000, transactionTimestamp); - - let indexes: number[] = []; - if (isSpentAfterUnlock) { - indexes = getUnlockConditionIndexes(input, UnlockConditionType.Expiration); - } else { - indexes = getUnlockConditionIndexes(input, UnlockConditionType.Address); - } - setInputsExtraInfo((prev) => ({ - ...prev, - [input.outputId]: { - unlockConditionOpenedIndexes: indexes, - }, - })); - } else { - const expirationUnlockCondition = getUnlockCondition(input, UnlockConditionType.Expiration) as ExpirationUnlockCondition; - const isExpired = expirationUnlockCondition && DateHelper.isExpired(expirationUnlockCondition.unixTime * 1000); - let indexes: number[] = []; - if (isExpired) { - indexes = getUnlockConditionIndexes(input, UnlockConditionType.Expiration); - } else { - indexes = getUnlockConditionIndexes(input, UnlockConditionType.Address); - } - - setInputsExtraInfo((prev) => ({ - ...prev, - [input.outputId]: { - unlockConditionOpenedIndexes: indexes, - }, - })); - } - } else { + if (!isExpirationExists(input)) { const indexes = getUnlockConditionIndexes(input, UnlockConditionType.Address); setInputsExtraInfo((prev) => ({ ...prev, @@ -131,9 +81,57 @@ const TransactionPage: React.FC> = ({ unlockConditionOpenedIndexes: indexes, }, })); + return; + } + + const expirationUnlockCondition = getUnlockCondition(input, UnlockConditionType.Expiration) as ExpirationUnlockCondition; + const expirationConditionTimestamp = expirationUnlockCondition.unixTime * 1000; + + if (!isOutputSpent(input)) { + const isExpired = DateHelper.isExpired(expirationConditionTimestamp); + const indexes: number[] = isExpired ? + getUnlockConditionIndexes(input, UnlockConditionType.Expiration) : + getUnlockConditionIndexes(input, UnlockConditionType.Address); + + setInputsExtraInfo((prev) => ({ + ...prev, + [input.outputId]: { + unlockConditionOpenedIndexes: indexes, + }, + })); + return; + } + + const transactionSpentId = input?.output?.metadata.transactionIdSpent as string; + const transactionTimestamp = await getTransactionTimestamp(transactionSpentId, apiClient, network); + + if (!transactionTimestamp) { + console.error(`Failed to get transaction timestamp for ${transactionSpentId}`); } + const isSpentAfterUnlock = DateHelper.isExpired(expirationConditionTimestamp, transactionTimestamp); + + const indexes = isSpentAfterUnlock ? + getUnlockConditionIndexes(input, UnlockConditionType.Expiration) : + getUnlockConditionIndexes(input, UnlockConditionType.Address); + + setInputsExtraInfo((prev) => ({ + ...prev, + [input.outputId]: { + unlockConditionOpenedIndexes: indexes, + }, + })); }; + useEffect(() => { + if (block?.payload?.type === PayloadType.Transaction) { + const transactionPayload = block.payload as ITransactionPayload; + const transactionEssence = transactionPayload.essence as RegularTransactionEssence; + setIncludedBlockId(Utils.blockId(block)); + setTangleNetworkId(transactionEssence.networkId); + setInputsCommitment(transactionEssence.inputsCommitment); + } + }, [block]); + useEffect(() => { (async () => { if (!inputs) return; @@ -367,7 +365,7 @@ function isExpirationExists(input: IInput) { async function getTransactionTimestamp(transactionId: string, apiClient: StardustApiClient, network: string) { const blockResp = await apiClient.transactionIncludedBlockDetails({ network, transactionId }); - if (blockResp?.block?.payload?.type !== PayloadType.Transaction) return null; + if (blockResp?.block?.payload?.type !== PayloadType.Transaction) return; const includedBlockId = Utils.blockId(blockResp.block); @@ -378,10 +376,10 @@ async function getTransactionTimestamp(transactionId: string, apiClient: Stardus const blockMetadata = blockMetadataResp?.metadata; const referencedByMilestoneIndex = blockMetadata?.referencedByMilestoneIndex; - if (referencedByMilestoneIndex === undefined) return null; + if (referencedByMilestoneIndex === undefined) return; const milestoneResp = await apiClient.milestoneDetails({ network, milestoneIndex: referencedByMilestoneIndex }); - if (!milestoneResp?.milestone?.timestamp) return null; + if (!milestoneResp?.milestone?.timestamp) return; return DateHelper.milliseconds(milestoneResp.milestone.timestamp); } diff --git a/client/src/helpers/dateHelper.ts b/client/src/helpers/dateHelper.ts index f1cd843fc..a07db5957 100644 --- a/client/src/helpers/dateHelper.ts +++ b/client/src/helpers/dateHelper.ts @@ -41,12 +41,12 @@ export class DateHelper { /** * Check if the date is expired. - * @param valueInMs The value to format in milliseconds. - * @param compareTime + * @param value The value to check in milliseconds. + * @param compareWith * @returns Boolean if the date is expired. */ - public static isExpired(valueInMs: number, compareTime?: number): boolean { - return moment(valueInMs).isBefore(compareTime ? moment(compareTime) : moment()); + public static isExpired(value: number, compareWith: number = Date.now()): boolean { + return moment(moment(compareWith)).isAfter(value); } /**