Skip to content

Commit

Permalink
fix: truncate decimal places to 8 max on swap from.
Browse files Browse the repository at this point in the history
  • Loading branch information
kallen-ledger committed Mar 8, 2024
1 parent c7f072f commit 9ca5979
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions libs/ledger-live-common/src/exchange/swap/hooks/useFromState.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,26 @@ export const useFromState = ({
);

const setFromAmount: SwapTransactionType["setFromAmount"] = useCallback(
amount => debouncedSetFromAmount(amount),
[debouncedSetFromAmount],
amount => {
const decimalPlacesForCoinOrToken = fromState.currency?.units[0].magnitude;

if (decimalPlacesForCoinOrToken) {
// remove the last {x} amount of decimal places so it goes to a max of 8.
// i.e if the value if 0.123456789 it will instead become 0.12345678
const tokenValue = new BigNumber(amount).dividedBy(
new BigNumber(10).pow(decimalPlacesForCoinOrToken),
);
const truncatedTokenValue = tokenValue.decimalPlaces(8, BigNumber.ROUND_DOWN);
const truncatedWeiValue = truncatedTokenValue.multipliedBy(
new BigNumber(10).pow(decimalPlacesForCoinOrToken),
);

return debouncedSetFromAmount(truncatedWeiValue);
}
// if for whatever reason we cannot get the decimal places then just return the amount
return debouncedSetFromAmount(amount);
},
[debouncedSetFromAmount, fromState.currency?.units],
);

return {
Expand Down

0 comments on commit 9ca5979

Please sign in to comment.