Skip to content

Commit

Permalink
chore: refactoring to simplify if/else conditions.
Browse files Browse the repository at this point in the history
Signed-off-by: Eugene Panteleymonchuk <[email protected]>
  • Loading branch information
panteleymonchuk committed Jan 29, 2024
1 parent ecf421d commit 4dc9464
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 58 deletions.
106 changes: 52 additions & 54 deletions client/src/app/routes/stardust/TransactionPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -72,68 +72,66 @@ const TransactionPage: React.FC<RouteComponentProps<TransactionPageProps>> = ({
});
}, [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,
[input.outputId]: {
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;
Expand Down Expand Up @@ -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);

Expand All @@ -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);
}
Expand Down
8 changes: 4 additions & 4 deletions client/src/helpers/dateHelper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

/**
Expand Down

0 comments on commit 4dc9464

Please sign in to comment.