Skip to content

Commit

Permalink
fix: swap amount in fiat is calculated wrong (#6406)
Browse files Browse the repository at this point in the history
### Description
This PR fixes SwapScreenV2 incorrect fiat calculations when entering
amount in fiat.

P.S. This part will be covered with tests in the follow-up PR for the
sake of not withholding the ability to patch the latest release as it
appears to be more time-consuming.

### Test plan

https://github.com/user-attachments/assets/47e0c263-00c4-4ad3-b529-48839e28f70c

### Related issues
Fixes RET-1296

### Backwards compatibility
Yes

### Network scalability

If a new NetworkId and/or Network are added in the future, the changes
in this PR will:

- [x] Continue to work without code changes, OR trigger a compilation
error (guaranteeing we find it when a new network is added)
  • Loading branch information
sviderock authored and jeanregisser committed Jan 10, 2025
1 parent a4694ae commit efd70c1
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 10 deletions.
8 changes: 6 additions & 2 deletions src/components/TokenEnterAmount.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ describe('TokenEnterAmount', () => {
} as TokenBalance,
})

await act(async () => result.current.handleToggleAmountType())
await act(async () => {
result.current.handleToggleAmountType()
})
await act(async () => result.current.handleAmountInputChange('1234.67'))
await act(async () => result.current.handleAmountInputChange('1234.678'))

Expand Down Expand Up @@ -255,7 +257,9 @@ describe('TokenEnterAmount', () => {
} as TokenBalance,
})

await act(() => result.current.handleToggleAmountType())
await act(() => {
result.current.handleToggleAmountType()
})
expect(result.current.amountType).toBe('local')
// the processedAmounts should be unchanged when toggling amount type with no amount entered
expect(result.current.processedAmounts).toStrictEqual({
Expand Down
21 changes: 17 additions & 4 deletions src/components/TokenEnterAmount.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -211,16 +211,20 @@ export function useEnterAmount(props: {
}
}, [amount, amountType, localCurrencySymbol, usdToLocalRate, props.token])

function handleToggleAmountType() {
if (!props.token) return
/**
* @returns New amount type
*/
function handleToggleAmountType(forcedAmountType?: AmountEnteredIn) {
if (!props.token) return amountType

const newAmountType = amountType === 'local' ? 'token' : 'local'
const newAmountType = forcedAmountType ?? (amountType === 'local' ? 'token' : 'local')
setAmountType(newAmountType)
setAmount(
newAmountType === 'local'
? processedAmounts.local.bignum?.toFixed(2) || ''
: processedAmounts.token.bignum?.decimalPlaces(props.token.decimals).toString() || ''
)
return newAmountType
}

function handleAmountInputChange(val: string) {
Expand Down Expand Up @@ -259,7 +263,16 @@ export function useEnterAmount(props: {
}

const rawValue = unformatNumberForProcessing(value)
const roundedAmount = new BigNumber(rawValue).decimalPlaces(props.token?.decimals).toString()
const roundedAmount =
amountType === 'token'
? new BigNumber(rawValue).decimalPlaces(props.token.decimals).toString()
: roundFiatValue(
convertTokenToLocalAmount({
tokenAmount: new BigNumber(rawValue),
tokenInfo: props.token,
usdToLocalRate,
})
)
setAmount(roundedAmount)
}

Expand Down
15 changes: 11 additions & 4 deletions src/swap/SwapScreenV2.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,10 @@ export default function SwapScreenV2({ route }: Props) {

const {
amount: amountFrom,
amountType,
amountType: amountTypeFrom,
processedAmounts: processedAmountsFrom,
handleAmountInputChange,
handleToggleAmountType,
handleToggleAmountType: handleToggleAmountTypeFrom,
handleSelectPercentageAmount,
} = useEnterAmount({
inputRef: inputFromRef,
Expand All @@ -177,8 +177,10 @@ export default function SwapScreenV2({ route }: Props) {

const {
amount: amountTo,
amountType: amountTypeTo,
processedAmounts: processedAmountsTo,
replaceAmount: replaceAmountTo,
handleToggleAmountType: handleToggleAmountTypeTo,
} = useEnterAmount({ token: toToken, inputRef: inputToRef })

const filterChipsFrom = useFilterChips(Field.FROM)
Expand Down Expand Up @@ -650,6 +652,11 @@ export default function SwapScreenV2({ route }: Props) {
})
}

function handleToggleAmountType() {
const newAmountType = handleToggleAmountTypeFrom()
handleToggleAmountTypeTo(newAmountType)
}

function handleSelectAmountPercentage(percentage: number) {
handleSelectPercentageAmount(percentage)
setSelectedPercentage(percentage)
Expand Down Expand Up @@ -698,7 +705,7 @@ export default function SwapScreenV2({ route }: Props) {
tokenAmount={processedAmountsFrom.token.displayAmount}
localAmount={processedAmountsFrom.local.displayAmount}
onInputChange={handleAmountInputChange}
amountType={amountType}
amountType={amountTypeFrom}
toggleAmountType={handleToggleAmountType}
onOpenTokenPicker={() => handleOpenTokenPicker(Field.FROM)}
testID="SwapAmountInput"
Expand All @@ -723,7 +730,7 @@ export default function SwapScreenV2({ route }: Props) {
inputRef={inputToRef}
tokenAmount={processedAmountsTo.token.displayAmount}
localAmount={processedAmountsTo.local.displayAmount}
amountType={amountType}
amountType={amountTypeTo}
onOpenTokenPicker={() => handleOpenTokenPicker(Field.TO)}
loading={shouldShowSkeletons}
testID="SwapAmountInput"
Expand Down

0 comments on commit efd70c1

Please sign in to comment.