From 5f629fc78668dd080ccc37aed6bbbd9fdb8805a1 Mon Sep 17 00:00:00 2001 From: Pierre Seznec Date: Wed, 19 Jun 2024 17:21:54 +0200 Subject: [PATCH] change input amount when selected token changes --- src/store/tokenStore.ts | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/src/store/tokenStore.ts b/src/store/tokenStore.ts index 426c0df7..9fba21e9 100644 --- a/src/store/tokenStore.ts +++ b/src/store/tokenStore.ts @@ -135,6 +135,21 @@ export const useTokenStore = create((set, get) => ({ if (!selectedToken) { set({ selectedToken }); } + const currentSelectedToken = get().selectedToken; + const inputAmount = useOperationStore.getState().inputAmount; + if ( + inputAmount && + currentSelectedToken?.decimals !== selectedToken?.decimals + ) { + const decsDiff = selectedToken!.decimals - currentSelectedToken!.decimals; + let newAmount: bigint; + if (decsDiff > 0) { + newAmount = inputAmount * 10n ** BigInt(decsDiff); + } else { + newAmount = inputAmount / 10n ** BigInt(-decsDiff); + } + useOperationStore.setState({ inputAmount: newAmount }); + } // if selected token is not in the list, set first selectable token // selectable is the token supported on the current evm chain selected const selectable = get().getTokens();