-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
161 additions
and
7 deletions.
There are no files selected for viewing
111 changes: 111 additions & 0 deletions
111
packages/nextjs/components/scaffold-eth/Input/ERC20Input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
import { useMemo, useState } from "react"; | ||
import { CommonInputProps, InputBase, SIGNED_NUMBER_REGEX } from "~~/components/scaffold-eth"; | ||
import { useGlobalState } from "~~/services/store/store"; | ||
|
||
const MAX_DECIMALS_USD = 2; | ||
|
||
function etherValueToDisplayValue(usdMode: boolean, etherValue: string, nativeCurrencyPrice: number) { | ||
if (usdMode && nativeCurrencyPrice) { | ||
const parsedEthValue = parseFloat(etherValue); | ||
if (Number.isNaN(parsedEthValue)) { | ||
return etherValue; | ||
} else { | ||
// We need to round the value rather than use toFixed, | ||
// since otherwise a user would not be able to modify the decimal value | ||
return ( | ||
Math.round(parsedEthValue * nativeCurrencyPrice * 10 ** MAX_DECIMALS_USD) / | ||
10 ** MAX_DECIMALS_USD | ||
).toString(); | ||
} | ||
} else { | ||
return etherValue; | ||
} | ||
} | ||
|
||
function displayValueToEtherValue(usdMode: boolean, displayValue: string, nativeCurrencyPrice: number) { | ||
if (usdMode && nativeCurrencyPrice) { | ||
const parsedDisplayValue = parseFloat(displayValue); | ||
if (Number.isNaN(parsedDisplayValue)) { | ||
// Invalid number. | ||
return displayValue; | ||
} else { | ||
// Compute the ETH value if a valid number. | ||
return (parsedDisplayValue / nativeCurrencyPrice).toString(); | ||
} | ||
} else { | ||
return displayValue; | ||
} | ||
} | ||
|
||
/** | ||
* Input for ETH amount with USD conversion. | ||
* | ||
* onChange will always be called with the value in ETH | ||
*/ | ||
export const ERC20Input = ({ value, name, placeholder, onChange, disabled }: CommonInputProps) => { | ||
const [transitoryDisplayValue, setTransitoryDisplayValue] = useState<string>(); | ||
const nativeCurrencyPrice = useGlobalState(state => state.nativeCurrencyPrice); | ||
const [usdMode] = useState(false); | ||
|
||
// The displayValue is derived from the ether value that is controlled outside of the component | ||
// In usdMode, it is converted to its usd value, in regular mode it is unaltered | ||
const displayValue = useMemo(() => { | ||
const newDisplayValue = etherValueToDisplayValue(usdMode, value, nativeCurrencyPrice); | ||
if (transitoryDisplayValue && parseFloat(newDisplayValue) === parseFloat(transitoryDisplayValue)) { | ||
return transitoryDisplayValue; | ||
} | ||
// Clear any transitory display values that might be set | ||
setTransitoryDisplayValue(undefined); | ||
return newDisplayValue; | ||
}, [nativeCurrencyPrice, transitoryDisplayValue, usdMode, value]); | ||
|
||
const handleChangeNumber = (newValue: string) => { | ||
if (newValue && !SIGNED_NUMBER_REGEX.test(newValue)) { | ||
return; | ||
} | ||
|
||
// Following condition is a fix to prevent usdMode from experiencing different display values | ||
// than what the user entered. This can happen due to floating point rounding errors that are introduced in the back and forth conversion | ||
// if (usdMode) { | ||
// const decimals = newValue.split(".")[1]; | ||
// if (decimals && decimals.length > MAX_DECIMALS_USD) { | ||
// return; | ||
// } | ||
// } | ||
|
||
// Since the display value is a derived state (calculated from the ether value), usdMode would not allow introducing a decimal point. | ||
// This condition handles a transitory state for a display value with a trailing decimal sign | ||
if (newValue.endsWith(".") || newValue.endsWith(".0")) { | ||
setTransitoryDisplayValue(newValue); | ||
} else { | ||
setTransitoryDisplayValue(undefined); | ||
} | ||
|
||
const newEthValue = displayValueToEtherValue(usdMode, newValue, nativeCurrencyPrice); | ||
onChange(newEthValue); | ||
}; | ||
|
||
// const toggleMode = () => { | ||
// setUSDMode(!usdMode); | ||
// }; | ||
|
||
return ( | ||
<InputBase | ||
name={name} | ||
value={displayValue} | ||
placeholder={placeholder} | ||
onChange={handleChangeNumber} | ||
disabled={disabled} | ||
// prefix={<span className="pl-4 -mr-2 text-accent self-center">{usdMode ? "$" : "Ξ"}</span>} | ||
// suffix={ | ||
// <button | ||
// className={`btn btn-primary h-[2.2rem] min-h-[2.2rem] ${nativeCurrencyPrice > 0 ? "" : "hidden"}`} | ||
// onClick={toggleMode} | ||
// disabled={!usdMode && !nativeCurrencyPrice} | ||
// > | ||
// <ArrowsRightLeftIcon className="h-3 w-3 cursor-pointer" aria-hidden="true" /> | ||
// </button> | ||
// } | ||
/> | ||
); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters