Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Simulator] Fix market price used for marginal price #1606

Merged
merged 8 commits into from
Dec 17, 2024
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
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,13 @@ export const CreateOverlappingStrategy: FC<Props> = (props) => {
const [touched, setTouched] = useState(false);
const [anchor, setAnchor] = useState<'buy' | 'sell' | undefined>();

useEffect(() => {
// Hack: on focus from input while scrolling as it prevents reactive state to behave correctly
if (document.activeElement instanceof HTMLInputElement) {
document.activeElement.blur();
}
}, [state.start]);

const { buyMarginal, sellMarginal } = useMemo(() => {
const min = state.buy.min;
const max = state.sell.max;
Expand Down
11 changes: 8 additions & 3 deletions src/pages/simulator/overlapping/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import { useSimulatorOverlappingInput } from 'hooks/useSimulatorOverlappingInput
import { useGetTokenPriceHistory } from 'libs/queries/extApi/tokenPrice';
import { simulatorInputOverlappingRoute } from 'libs/routing/routes/sim';
import { defaultEnd, defaultStart } from 'components/strategies/common/utils';
import { FormEvent, useEffect } from 'react';
import { FormEvent, useEffect, useMemo } from 'react';
import { formatNumber, roundSearchParam } from 'utils/helpers';

export const SimulatorInputOverlappingPage = () => {
Expand All @@ -24,6 +24,11 @@ export const SimulatorInputOverlappingPage = () => {
end: defaultEnd().toString(),
});

const marketPrice = useMemo(() => {
if (!state.start) return data?.[0].open;
return data?.find((d) => d.date.toString() === state.start)?.open;
}, [data, state.start]);

useEffect(() => {
if (searchState.sellMax || searchState.buyMin) return;
dispatch('baseToken', searchState.baseToken);
Expand Down Expand Up @@ -65,7 +70,7 @@ export const SimulatorInputOverlappingPage = () => {
const { buyPriceMarginal, sellPriceMarginal } = calculateOverlappingPrices(
formatNumber(state.buy.min),
formatNumber(state.sell.max),
data[0].open.toString(),
marketPrice!.toString(),
state.spread
);

Expand Down Expand Up @@ -102,7 +107,7 @@ export const SimulatorInputOverlappingPage = () => {
<CreateOverlappingStrategy
state={state}
dispatch={dispatch}
marketPrice={data?.[0].open ?? 0}
marketPrice={marketPrice ?? 0}
spread={state.spread}
setSpread={(v) => dispatch('spread', v)}
/>
Expand Down