Skip to content

Commit

Permalink
fix slippage
Browse files Browse the repository at this point in the history
  • Loading branch information
Ridel1e committed Jan 4, 2024
1 parent 810f158 commit ceb9e81
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/common/constants/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const MinerFeeDecimals = 2;

export const defaultSlippage = 3;
export const MIN_SLIPPAGE = 0.01;

export const MAX_SLIPPAGE = 100;
export const SlippageDecimals = 2;

Expand Down
16 changes: 13 additions & 3 deletions src/components/OperationSettings/OperationSettings.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,11 @@ import { FC, useState } from 'react';
import { filter, skip } from 'rxjs';

import { MIN_NITRO } from '../../common/constants/erg';
import { defaultSlippage, MIN_SLIPPAGE } from '../../common/constants/settings';
import {
defaultSlippage,
MAX_SLIPPAGE,
MIN_SLIPPAGE,
} from '../../common/constants/settings';
import { useSubscription } from '../../common/hooks/useObservable';
import { AssetInfo } from '../../common/models/AssetInfo';
import { Currency } from '../../common/models/Currency';
Expand All @@ -40,6 +44,9 @@ const slippageTxFailCheck: CheckFn<number> = (value) =>
const minSlippageCheck: CheckFn<number> = (value) =>
isNaN(value) || value < MIN_SLIPPAGE ? 'minSlippage' : undefined;

const maxSlippageCheck: CheckFn<number> = (value) =>
isNaN(value) || value > MAX_SLIPPAGE ? 'maxSlippage' : undefined;

const nitroCheck: CheckFn<number> = (value) =>
isNaN(value) || value < MIN_NITRO ? 'minNitro' : undefined;

Expand Down Expand Up @@ -83,6 +90,7 @@ export const OperationSettings: FC<OperationSettingsProps> = ({
},
slippage: {
minSlippage: t`Minimal Slippage is` + ` ${MIN_SLIPPAGE}`,
maxSlippage: t`Maximal Slippage is` + ` ${MAX_SLIPPAGE}`,
},
};

Expand All @@ -91,7 +99,7 @@ export const OperationSettings: FC<OperationSettingsProps> = ({
const form = useForm<SettingsModel>({
slippage: useForm.ctrl(
slippage,
[minSlippageCheck],
[minSlippageCheck, maxSlippageCheck],
[slippageCheck, slippageTxFailCheck],
),
nitro: useForm.ctrl(nitro, [nitroCheck]),
Expand All @@ -114,7 +122,9 @@ export const OperationSettings: FC<OperationSettingsProps> = ({
useSubscription(
form.controls.slippage.valueChanges$.pipe(
skip(1),
filter((value) => !!value && value >= MIN_SLIPPAGE),
filter(
(value) => !!value && value >= MIN_SLIPPAGE && value <= MAX_SLIPPAGE,
),
),
(slippage) => setSlippage(slippage),
[slippage, nitro],
Expand Down
13 changes: 12 additions & 1 deletion src/network/cardano/settings/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ import { RustModule } from '@spectrumlabs/cardano-dex-sdk/build/main/utils/rustL
import { filter, map, Observable, startWith, zip } from 'rxjs';

import { MIN_NITRO } from '../../../common/constants/erg';
import { defaultSlippage } from '../../../common/constants/settings';
import {
defaultSlippage,
MAX_SLIPPAGE,
} from '../../../common/constants/settings';
import { useObservable } from '../../../common/hooks/useObservable';
import { Address } from '../../../common/types';
import { localStorageManager } from '../../../common/utils/localStorageManager';
Expand Down Expand Up @@ -52,6 +55,14 @@ const getNewSelectedAddress = (
};

export const initializeSettings = (): void => {
const currentSettings: CardanoSettings =
localStorageManager.get(SETTINGS_KEY) || defaultCardanoSettings;

setSettings({
...currentSettings,
slippage: Math.min(currentSettings.slippage, MAX_SLIPPAGE),
});

zip([
getUsedAddresses().pipe(filter(Boolean)),
getUnusedAddresses().pipe(filter(Boolean)),
Expand Down

0 comments on commit ceb9e81

Please sign in to comment.