Skip to content

Commit

Permalink
fix(STK-255): My Stacks breaks for some one time orders (#189)
Browse files Browse the repository at this point in the history
* fix: add safeguards to check if the active order has orderSlots
* fix: make 1 slot orders cancellable under correct scenarios
  • Loading branch information
ElRodrigote authored Jul 25, 2024
1 parent 1c3d72a commit a977cec
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 6 deletions.
6 changes: 4 additions & 2 deletions packages/app/components/StacksTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,12 +195,14 @@ const OrdersProgressText = ({ stackOrder }: StackOrderProps) => {
<BodyText className="text-em-high">
{totalStackOrdersDone(stackOrder).toString()}
</BodyText>
<BodyText className="text-em-low">{`/ ${stackOrder.orderSlots.length} orders`}</BodyText>
<BodyText className="text-em-low">{`/ ${
stackOrder.orderSlots.length || stackOrder.cowOrders.length
} orders`}</BodyText>
</>
);
}

const firtTimeSlot = Number(stackOrder.orderSlots[0]);
const firtTimeSlot = Number(stackOrder.orderSlots[0] ?? stackOrder.startTime);
const date = new Date(firtTimeSlot * 1000); // Convert seconds to milliseconds
const distanceToNow = formatDistanceToNow(date, { addSuffix: true });

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ const StackDetail = ({

export const StackFrequencyAndDates = ({ stackOrder }: StackOrderProps) => {
const orderSlots = stackOrder.orderSlots;
const firstSlot = orderSlots[0];
const lastSlot = orderSlots[orderSlots.length - 1];
const hasSlots = Boolean(orderSlots.length);
const firstSlot = hasSlots ? orderSlots[0] : stackOrder.startTime;
const lastSlot = hasSlots
? orderSlots[orderSlots.length - 1]
: stackOrder.endTime;
const nextSlot = orderSlots[totalOrderSlotsDone(stackOrder)];

return (
Expand Down
17 changes: 15 additions & 2 deletions packages/app/models/order/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,27 @@ import { currentTimestampInSeconds } from "@/utils/datetime";
import { Order } from "@stackly/sdk";

export const totalOrderSlotsDone = (order: Order) => {
/**
* An order that doesn't have slots happens when we have a 1 slot order
* that has start and end date in a timeframe equal or less than 60'.
* As it's not possible to use the slot timestamp, we use the start time
* for this 1 slot order.
*/
if (!order.orderSlots.length)
return order.startTime < currentTimestampInSeconds ? 1 : 0;

return order.orderSlots.reduce((count, orderTimestamp) => {
if (Number(orderTimestamp) < currentTimestampInSeconds) return ++count;
return count;
}, 0);
};

export const allOrderSlotsDone = (order: Order) =>
totalOrderSlotsDone(order) === order.orderSlots.length;
export const allOrderSlotsDone = (order: Order) => {
const orderSlotsLength = order.orderSlots.length;
if (!orderSlotsLength) return totalOrderSlotsDone(order) === 1;

return totalOrderSlotsDone(order) === orderSlotsLength;
};

export const totalFundsAmount = (order: Order) => {
const total =
Expand Down

0 comments on commit a977cec

Please sign in to comment.