Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/app/backing/components/AllocationInput/AllocationInput.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export const AllocationInput = ({
if (!editing) return

try {
updateBacking(normaliseBackingValue(onchainBacking, value))
updateBacking(preventLeftoverDecimals(onchainBacking, value))
} catch (error) {
setParsingError(error as Error)
}
Expand Down Expand Up @@ -185,9 +185,21 @@ export const AllocationInput = ({
)
}

// 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.
function normaliseBackingValue(onchainBacking: bigint, value: string) {
return Number(formatEther(onchainBacking)).toFixed(0) === value ? onchainBacking : parseEther(value)
// A little bit of a hacky way to check that the input is equal to the on-chain backing **in the integer part**
// If it does, it should be set to the onchain value.
// The purpose of this is to prevent leftover fractions in the available balance, which cannot be used up.
//
// IMPORTANT: Should the sysmtem ever support fractional STRIF, this function should be removed,
// as it will not be needed since the UI will have to adapt to that change, too.
function preventLeftoverDecimals(onchainBacking: bigint, value: string): bigint {
const onchainBackingStr = formatEther(onchainBacking)
const [onchainBackingIntegerValue] = onchainBackingStr.split('.')

if (onchainBackingIntegerValue === value) {
return onchainBacking
}

return parseEther(value)
}

function isValidBalanceFraction(
Expand Down