Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
48 changes: 47 additions & 1 deletion packages/app/components/stackbox/FrequencyOptionsCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@

import { ChangeEvent, useEffect, useState } from "react";

import { add } from "date-fns";
import { add, daysToWeeks } from "date-fns";
import { twMerge } from "tailwind-merge";

import { useStrategyContext } from "@/contexts";
import { FREQUENCY_OPTIONS } from "@/models";
import { BodyText, RadioButton, TextInput } from "@/ui";
import { cx } from "class-variance-authority";
Expand Down Expand Up @@ -54,6 +55,26 @@ const getCroppedFrequency = (frequency: FREQUENCY_OPTIONS) => {
return isMonthFrequency ? frequency.substring(0, 2) : frequency.charAt(0);
};

const parseDaysToFrequencyAmount = (
days: number,
frequency: FREQUENCY_OPTIONS
) => {
switch (frequency) {
case FREQUENCY_OPTIONS.hour:
return days * 24;
case FREQUENCY_OPTIONS.day:
return days;
case FREQUENCY_OPTIONS.week:
return daysToWeeks(days);
case FREQUENCY_OPTIONS.month:
return days / 30;
default: {
console.error("Invalid frequency option", frequency);
return maxCustomFrequencies[frequency];
}
}
};

export const FrequencyOptionsCard = ({
frequency,
setEndDate,
Expand All @@ -63,6 +84,8 @@ export const FrequencyOptionsCard = ({
);
const [customFrequency, setCustomFrequency] = useState("");

const { deselectStrategy, selectedStrategy } = useStrategyContext();

const handleCustomFrequencyChange = (
event: ChangeEvent<HTMLInputElement>
) => {
Expand All @@ -82,6 +105,8 @@ export const FrequencyOptionsCard = ({
setDefaultFrequency("");
setCustomFrequency(newValue);
}

if (selectedStrategy) deselectStrategy();
};

useEffect(() => {
Expand All @@ -97,6 +122,26 @@ export const FrequencyOptionsCard = ({
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [defaultFrequency, customFrequency]);

useEffect(() => {
if (selectedStrategy) {
const frequencyAmount = parseDaysToFrequencyAmount(
selectedStrategy.daysAmount,
frequency
).toString();

const isDefaultStrategy =
defaultFrequencyOptions[frequency].includes(frequencyAmount);

if (isDefaultStrategy) {
setDefaultFrequency(frequencyAmount);
setCustomFrequency("");
} else {
setCustomFrequency(frequencyAmount);
setDefaultFrequency("");
}
}
}, [frequency, selectedStrategy]);

return (
<div
className={twMerge([
Expand All @@ -120,6 +165,7 @@ export const FrequencyOptionsCard = ({
onChange={(event) => {
setDefaultFrequency(event.target.value as FREQUENCY_OPTIONS);
if (!!customFrequency) setCustomFrequency("");
if (selectedStrategy) deselectStrategy();
}}
value={freqOption}
>
Expand Down
6 changes: 3 additions & 3 deletions packages/app/components/strategies/constants.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export const STRATEGY_CATEGORIES: { [chainId: number]: ChainStrategies } = {
{
id: 3,
buyToken: gnosisTokens.WETH,
daysAmount: 10,
daysAmount: 4,
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know that 10 days of hourly trades is a lot. Any specific reason for choosing 4?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It fits the current constraint of the maximum hours per hourly trade accepted in our custom timeframe input. Right now, we have a maximum of 96 hours, so 4 days * 24 hours = 96.

We can change any of these variables, but I made this change to fit the constraints explained above.

frequency: FREQUENCY_OPTIONS.hour,
sellAmountPerTimeframe: 5,
sellToken: gnosisTokens.USDC,
Expand Down Expand Up @@ -136,7 +136,7 @@ export const STRATEGY_CATEGORIES: { [chainId: number]: ChainStrategies } = {
{
id: 3,
buyToken: arbitrumTokens.WETH,
daysAmount: 10,
daysAmount: 4,
frequency: FREQUENCY_OPTIONS.hour,
sellAmountPerTimeframe: 5,
sellToken: arbitrumTokens.USDC,
Expand Down Expand Up @@ -179,7 +179,7 @@ export const STRATEGY_CATEGORIES: { [chainId: number]: ChainStrategies } = {
{
id: 3,
buyToken: baseTokens.CBBTC,
daysAmount: 10,
daysAmount: 4,
frequency: FREQUENCY_OPTIONS.hour,
sellAmountPerTimeframe: 5,
sellToken: baseTokens.USDC,
Expand Down
Loading