The status variable in the OrderBook::getOrderDetailsString() function is being calculated twice, first time with a ternary operator, second time with if else statements. It has no significant impact, however it boosts gas costs for the function
Description
First time, the status variable is calculated using ternary operator, second time with if else statements.
@> string memory status = order.isActive
? (block.timestamp < order.deadlineTimestamp ? "Active" : "Expired (Active but past deadline)")
: "Inactive (Filled/Cancelled)";
if (order.isActive && block.timestamp >= order.deadlineTimestamp) {
@> status = "Expired (Awaiting Cancellation)";
} else if (!order.isActive) {
@> status = "Inactive (Filled/Cancelled)";
} else {
@> status = "Active";
}
Risk
Impact:
There is no significant impact, however the gas costs rise
Proof of Concept
string memory status = order.isActive
? (block.timestamp < order.deadlineTimestamp ? "Active" : "Expired (Active but past deadline)")
: "Inactive (Filled/Cancelled)";
if (order.isActive && block.timestamp >= order.deadlineTimestamp) {
status = "Expired (Awaiting Cancellation)";
} else if (!order.isActive) {
status = "Inactive (Filled/Cancelled)";
} else {
status = "Active";
}
Recommended Mitigation
Consider calculating the status variable with one method, via ternary operator or via if else statements, not both of them
string memory status = order.isActive
? (block.timestamp < order.deadlineTimestamp ? "Active" : "Expired (Active but past deadline)")
: "Inactive (Filled/Cancelled)";
- if (order.isActive && block.timestamp >= order.deadlineTimestamp) {
- status = "Expired (Awaiting Cancellation)";
- } else if (!order.isActive) {
- status = "Inactive (Filled/Cancelled)";
- } else {
- status = "Active";
- }