From a1da1d532b8e54d82e38ed2d3a1593bc851e4413 Mon Sep 17 00:00:00 2001 From: Willian Mitsuda Date: Sat, 25 Sep 2021 18:40:00 -0300 Subject: [PATCH] Add uint256 decoder with display mode toggler --- src/transaction/decoder/DecodedParamRow.tsx | 5 +- src/transaction/decoder/Uint256Decoder.tsx | 58 +++++++++++++++++++++ 2 files changed, 62 insertions(+), 1 deletion(-) create mode 100644 src/transaction/decoder/Uint256Decoder.tsx diff --git a/src/transaction/decoder/DecodedParamRow.tsx b/src/transaction/decoder/DecodedParamRow.tsx index 493f6b3..f0f0013 100644 --- a/src/transaction/decoder/DecodedParamRow.tsx +++ b/src/transaction/decoder/DecodedParamRow.tsx @@ -1,5 +1,6 @@ import React, { ReactNode } from "react"; import { ParamType } from "@ethersproject/abi"; +import Uint256Decoder from "./Uint256Decoder"; import AddressDecoder from "./AddressDecoder"; import BooleanDecoder from "./BooleanDecoder"; import BytesDecoder from "./BytesDecoder"; @@ -42,7 +43,9 @@ const DecodedParamRow: React.FC = ({ {paramType.type} - {paramType.baseType === "address" ? ( + {paramType.baseType === "uint256" ? ( + + ) : paramType.baseType === "address" ? ( ) : paramType.baseType === "bool" ? ( diff --git a/src/transaction/decoder/Uint256Decoder.tsx b/src/transaction/decoder/Uint256Decoder.tsx new file mode 100644 index 0000000..65b50c1 --- /dev/null +++ b/src/transaction/decoder/Uint256Decoder.tsx @@ -0,0 +1,58 @@ +import React, { useState } from "react"; +import { hexlify } from "@ethersproject/bytes"; +import { commify, formatEther } from "@ethersproject/units"; +import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"; +import { faSync } from "@fortawesome/free-solid-svg-icons/faSync"; + +type Uint256DecoderProps = { + r: any; +}; + +enum DisplayMode { + RAW, + HEX, + EIGHTEEN_DECIMALS, +} + +const Uint256Decoder: React.FC = ({ r }) => { + const [displayMode, setDisplayMode] = useState( + DisplayMode.EIGHTEEN_DECIMALS + ); + + const toggleModes = () => { + setDisplayMode( + displayMode === DisplayMode.EIGHTEEN_DECIMALS ? 0 : displayMode + 1 + ); + }; + + return ( +
+ + + {displayMode === DisplayMode.RAW ? ( + <>{commify(r.toString())} + ) : displayMode === DisplayMode.HEX ? ( + <>{hexlify(r)} + ) : ( + <>{commify(formatEther(r))} + )} + +
+ ); +}; + +export default React.memo(Uint256Decoder);