From e7920faf2ba6f42713f19baa526b11691305d775 Mon Sep 17 00:00:00 2001 From: Gavrila Andrei Date: Tue, 14 Jan 2025 12:07:47 +0200 Subject: [PATCH 1/4] Fixed swap calculation when first amount was entered before second token (#42) * Fix value of secondToken when firstAmount and secondToken is filled * Update to 2.1.4-alpha.0 * Update changelog * Update export --- CHANGELOG.md | 2 ++ src/hooks/index.ts | 1 + src/hooks/usePrevious.ts | 11 +++++++++++ src/hooks/useSwapFormHandlers.ts | 6 ++++++ 4 files changed, 20 insertions(+) create mode 100644 src/hooks/usePrevious.ts diff --git a/CHANGELOG.md b/CHANGELOG.md index 4ae2d11..c252493 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +- [Fixed swap calculation when first amount was entered before second token](https://github.com/multiversx/mx-sdk-dapp-swap/pull/42) + ## [[2.1.3](https://github.com/multiversx/mx-sdk-dapp-swap/pull/41)] - 2024-12-04 - [Progressive Fetching for Tokens](https://github.com/multiversx/mx-sdk-dapp-swap/pull/40) diff --git a/src/hooks/index.ts b/src/hooks/index.ts index 72429f9..7f4dd5d 100644 --- a/src/hooks/index.ts +++ b/src/hooks/index.ts @@ -12,3 +12,4 @@ export * from './useSwapFormHandlers'; export * from './useInputAmountUsdValue'; export * from './useFetchMaintenanceFlag'; export * from './useIntersectionObserver'; +export * from './usePrevious'; diff --git a/src/hooks/usePrevious.ts b/src/hooks/usePrevious.ts new file mode 100644 index 0000000..5f53c6f --- /dev/null +++ b/src/hooks/usePrevious.ts @@ -0,0 +1,11 @@ +import { useRef, useEffect } from 'react'; + +export const usePrevious = (value: T): T | undefined => { + const ref = useRef(undefined); + + useEffect(() => { + ref.current = value; + }, [value]); + + return ref.current; +}; diff --git a/src/hooks/useSwapFormHandlers.ts b/src/hooks/useSwapFormHandlers.ts index d425c85..9991e87 100644 --- a/src/hooks/useSwapFormHandlers.ts +++ b/src/hooks/useSwapFormHandlers.ts @@ -10,6 +10,7 @@ import { getCorrectAmountsOnTokenChange } from 'utils'; import { useInputAmountUsdValue } from './useInputAmountUsdValue'; +import { usePrevious } from './usePrevious'; import { GetSwapRouteType } from './useSwapRoute'; type SwapFormStateType = { @@ -39,6 +40,7 @@ export const useSwapFormHandlers = ({ const [activeRoute, setActiveRoute] = useState(); const isSwitching = useRef(); const lastInputTouched = useRef(); + const prevSecondToken = usePrevious(formState.secondToken); const inputAmountsUsdValue = useInputAmountUsdValue({ swapRoute: activeRoute, @@ -254,6 +256,10 @@ export const useSwapFormHandlers = ({ secondToken: option }; }); + + if (!prevSecondToken && formState.firstAmount) { + handleOnChangeFirstAmount(formState.firstAmount); + } }; const handleOnFirstMaxBtnChange = () => { From 4ca570eb25a994fabc16a93e827ac3a0c93d1c88 Mon Sep 17 00:00:00 2001 From: Gavrila Andrei Date: Tue, 14 Jan 2025 12:08:11 +0200 Subject: [PATCH 2/4] 2.1.4 --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index bc08ab0..4cf2184 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@multiversx/sdk-dapp-swap", - "version": "2.1.3", + "version": "2.1.4", "description": "A library to hold the main logic for swapping between tokens on the MultiversX blockchain", "author": "MultiversX", "license": "GPL-3.0-or-later", From 35371b1eebce213377354a9bba16d3ec511dd7d4 Mon Sep 17 00:00:00 2001 From: Gavrila Andrei Date: Tue, 14 Jan 2025 12:09:16 +0200 Subject: [PATCH 3/4] Update changelog --- CHANGELOG.md | 2 ++ 1 file changed, 2 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index c252493..0598459 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +## [[2.1.4](https://github.com/multiversx/mx-sdk-dapp-swap/pull/43)] - 2024-12-04 + - [Fixed swap calculation when first amount was entered before second token](https://github.com/multiversx/mx-sdk-dapp-swap/pull/42) ## [[2.1.3](https://github.com/multiversx/mx-sdk-dapp-swap/pull/41)] - 2024-12-04 From 7fe6dc6545e4260888db7cc7af62e72d935f1517 Mon Sep 17 00:00:00 2001 From: Gavrila Andrei Date: Tue, 14 Jan 2025 12:09:37 +0200 Subject: [PATCH 4/4] Update date --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0598459..10d15d0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] -## [[2.1.4](https://github.com/multiversx/mx-sdk-dapp-swap/pull/43)] - 2024-12-04 +## [[2.1.4](https://github.com/multiversx/mx-sdk-dapp-swap/pull/43)] - 2025-01-14 - [Fixed swap calculation when first amount was entered before second token](https://github.com/multiversx/mx-sdk-dapp-swap/pull/42)