Small adjustments to toggling and formatting

This commit is contained in:
Willian Mitsuda 2021-09-25 18:57:07 -03:00
parent a1da1d532b
commit 22b9c5b687
1 changed files with 21 additions and 10 deletions

View File

@ -1,5 +1,6 @@
import React, { useState } from "react"; import React, { useState } from "react";
import { hexlify } from "@ethersproject/bytes"; import { BigNumber } from "@ethersproject/bignumber";
import { hexlify, hexZeroPad } from "@ethersproject/bytes";
import { commify, formatEther } from "@ethersproject/units"; import { commify, formatEther } from "@ethersproject/units";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import { faSync } from "@fortawesome/free-solid-svg-icons/faSync"; import { faSync } from "@fortawesome/free-solid-svg-icons/faSync";
@ -10,19 +11,29 @@ type Uint256DecoderProps = {
enum DisplayMode { enum DisplayMode {
RAW, RAW,
HEX,
EIGHTEEN_DECIMALS, EIGHTEEN_DECIMALS,
HEX,
_LAST,
} }
const VERY_BIG_NUMBER = BigNumber.from(10).pow(BigNumber.from(36));
const initDisplayMode = (r: any): DisplayMode => {
const n = BigNumber.from(r);
if (n.gte(VERY_BIG_NUMBER)) {
return DisplayMode.HEX;
}
return DisplayMode.RAW;
};
const Uint256Decoder: React.FC<Uint256DecoderProps> = ({ r }) => { const Uint256Decoder: React.FC<Uint256DecoderProps> = ({ r }) => {
const [displayMode, setDisplayMode] = useState<DisplayMode>( const [displayMode, setDisplayMode] = useState<DisplayMode>(
DisplayMode.EIGHTEEN_DECIMALS initDisplayMode(r)
); );
const toggleModes = () => { const toggleModes = () => {
setDisplayMode( const next = displayMode + 1;
displayMode === DisplayMode.EIGHTEEN_DECIMALS ? 0 : displayMode + 1 setDisplayMode(next === DisplayMode._LAST ? 0 : next);
);
}; };
return ( return (
@ -36,17 +47,17 @@ const Uint256Decoder: React.FC<Uint256DecoderProps> = ({ r }) => {
</div> </div>
<span> <span>
{displayMode === DisplayMode.RAW {displayMode === DisplayMode.RAW
? "Raw number" ? "Raw"
: displayMode === DisplayMode.HEX : displayMode === DisplayMode.HEX
? "Hex number" ? "Hex"
: "18 decimals"} : "18 dec"}
</span> </span>
</button> </button>
<span> <span>
{displayMode === DisplayMode.RAW ? ( {displayMode === DisplayMode.RAW ? (
<>{commify(r.toString())}</> <>{commify(r.toString())}</>
) : displayMode === DisplayMode.HEX ? ( ) : displayMode === DisplayMode.HEX ? (
<>{hexlify(r)}</> <>{hexZeroPad(hexlify(r), 32)}</>
) : ( ) : (
<>{commify(formatEther(r))}</> <>{commify(formatEther(r))}</>
)} )}