Skip to content

Commit 56ca5f5

Browse files
committed
chore: make normaliseBackingValue purpose clearer
1 parent f2be690 commit 56ca5f5

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/app/backing/components/AllocationInput/AllocationInput.tsx

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export const AllocationInput = ({
118118
if (!editing) return
119119

120120
try {
121-
updateBacking(normaliseBackingValue(onchainBacking, value))
121+
updateBacking(preventLeftoverDecimals(onchainBacking, value))
122122
} catch (error) {
123123
setParsingError(error as Error)
124124
}
@@ -185,9 +185,18 @@ export const AllocationInput = ({
185185
)
186186
}
187187

188-
// A little bit of a hacky way to check that the input is equal to the on-chain backing in the integer part and if so it should be set to that to prevent accidental fractions.
189-
function normaliseBackingValue(onchainBacking: bigint, value: string) {
190-
return Number(formatEther(onchainBacking)).toFixed(0) === value ? onchainBacking : parseEther(value)
188+
// A little bit of a hacky way to check that the input is equal to the on-chain backing **in the integer part**
189+
// If it does, it should be set to the onchain value.
190+
// The purpose of this is to prevent leftover fractions in the available balance, which cannot be used up.
191+
function preventLeftoverDecimals(onchainBacking: bigint, value: string): bigint {
192+
const onchainBackingStr = formatEther(onchainBacking)
193+
const [onchainBackingIntegerValue] = onchainBackingStr.split('.')
194+
195+
if (onchainBackingIntegerValue === value) {
196+
return onchainBacking
197+
}
198+
199+
return parseEther(value)
191200
}
192201

193202
function isValidBalanceFraction(

0 commit comments

Comments
 (0)