From 62df32b82f0462f07ab799eef9566fb2fb62a194 Mon Sep 17 00:00:00 2001 From: Willian Mitsuda Date: Fri, 2 Jul 2021 16:05:18 -0300 Subject: [PATCH 1/4] Initial ENS support for resolving names -> address on search --- src/AddressTransactions.tsx | 66 ++++++++++++++++++++++++++++--------- src/App.tsx | 2 +- src/Search.tsx | 3 +- 3 files changed, 53 insertions(+), 18 deletions(-) diff --git a/src/AddressTransactions.tsx b/src/AddressTransactions.tsx index a598cc8..762356f 100644 --- a/src/AddressTransactions.tsx +++ b/src/AddressTransactions.tsx @@ -1,5 +1,6 @@ import React, { useState, useEffect, useMemo } from "react"; import { useParams, useLocation, useHistory } from "react-router-dom"; +import { ethers } from "ethers"; import queryString from "query-string"; import Blockies from "react-blockies"; import StandardFrame from "./StandardFrame"; @@ -12,10 +13,10 @@ import PendingResults from "./search/PendingResults"; import TransactionItem from "./search/TransactionItem"; import { SearchController } from "./search/search"; import { useFeeToggler } from "./search/useFeeToggler"; -import { ethers } from "ethers"; +import { provider } from "./ethersconfig"; type BlockParams = { - address: string; + addressOrName: string; direction?: string; }; @@ -33,13 +34,33 @@ const AddressTransactions: React.FC = () => { hash = qs.h as string; } - // Normalize to checksummed address - const checksummedAddress = useMemo( - () => ethers.utils.getAddress(params.address), - [params.address] - ); - if (params.address !== checksummedAddress) { - console.log("NORMALIZE"); + const [checksummedAddress, setChecksummedAddress] = useState(); + const [isENS, setENS] = useState(); + + // If it looks like it is an ENS name, try to resolve it + useEffect(() => { + if (ethers.utils.isAddress(params.addressOrName)) { + // Normalize to checksummed address + setChecksummedAddress(ethers.utils.getAddress(params.addressOrName)); + return; + } + + const resolveName = async () => { + const resolvedAddress = await provider.resolveName(params.addressOrName); + if (resolvedAddress !== null) { + setENS(true); + setChecksummedAddress(resolvedAddress); + } + }; + resolveName(); + }, [params.addressOrName]); + + // Request came with a non-checksummed address; fix the URL + if ( + !isENS && + checksummedAddress && + params.addressOrName !== checksummedAddress + ) { history.replace( `/address/${checksummedAddress}${ params.direction ? "/" + params.direction : "" @@ -49,6 +70,10 @@ const AddressTransactions: React.FC = () => { const [controller, setController] = useState(); useEffect(() => { + if (!checksummedAddress) { + return; + } + const readFirstPage = async () => { const _controller = await SearchController.firstPage(checksummedAddress); setController(_controller); @@ -100,24 +125,33 @@ const AddressTransactions: React.FC = () => { const page = useMemo(() => controller?.getPage(), [controller]); - document.title = `Address ${params.address} | Otterscan`; + document.title = `Address ${params.addressOrName} | Otterscan`; const [feeDisplay, feeDisplayToggler] = useFeeToggler(); + if (!checksummedAddress) { + return <>; + } + return (
Address - {params.address} + {checksummedAddress} - + + {isENS && ( + + ENS: {params.addressOrName} + + )}
@@ -130,7 +164,7 @@ const AddressTransactions: React.FC = () => { )} { ))} @@ -159,7 +193,7 @@ const AddressTransactions: React.FC = () => { )} ( - + diff --git a/src/Search.tsx b/src/Search.tsx index 1fe081b..7b5adc3 100644 --- a/src/Search.tsx +++ b/src/Search.tsx @@ -27,7 +27,8 @@ const Search: React.FC = () => { return <>; } - history.replace("/"); + // Assume it is an ENS name + history.replace(`/address/${q}`); return <>; }; From 0c7cc1fc646c0ceec1c13ab0138f71d1d18da5dc Mon Sep 17 00:00:00 2001 From: Willian Mitsuda Date: Fri, 2 Jul 2021 16:20:50 -0300 Subject: [PATCH 2/4] Fix error handling --- src/AddressTransactions.tsx | 148 +++++++++++++++++++----------------- 1 file changed, 79 insertions(+), 69 deletions(-) diff --git a/src/AddressTransactions.tsx b/src/AddressTransactions.tsx index 762356f..b7985fa 100644 --- a/src/AddressTransactions.tsx +++ b/src/AddressTransactions.tsx @@ -36,6 +36,7 @@ const AddressTransactions: React.FC = () => { const [checksummedAddress, setChecksummedAddress] = useState(); const [isENS, setENS] = useState(); + const [error, setError] = useState(); // If it looks like it is an ENS name, try to resolve it useEffect(() => { @@ -50,6 +51,9 @@ const AddressTransactions: React.FC = () => { if (resolvedAddress !== null) { setENS(true); setChecksummedAddress(resolvedAddress); + setError(false); + } else { + setError(true); } }; resolveName(); @@ -129,82 +133,88 @@ const AddressTransactions: React.FC = () => { const [feeDisplay, feeDisplayToggler] = useFeeToggler(); - if (!checksummedAddress) { - return <>; - } - return ( - -
- - Address - - {checksummedAddress} - - - {isENS && ( - - ENS: {params.addressOrName} - - )} -
-
- -
-
- {page === undefined ? ( - <>Waiting for search results... - ) : ( - <>{page.length} transactions on this page - )} -
- -
- - {controller ? ( + {error ? ( + + "{params.addressOrName}" is not an ETH address or ENS name. + + ) : ( + checksummedAddress && ( <> - {controller.getPage().map((tx) => ( - - ))} -
-
- {page !== undefined && ( - <>{page.length} transactions on this page + +
+ + Address + + {checksummedAddress} + + + {isENS && ( + + ENS: {params.addressOrName} + )}
- + +
+
+ {page === undefined ? ( + <>Waiting for search results... + ) : ( + <>{page.length} transactions on this page + )} +
+ +
+ -
+ {controller ? ( + <> + {controller.getPage().map((tx) => ( + + ))} +
+
+ {page !== undefined && ( + <>{page.length} transactions on this page + )} +
+ +
+ + ) : ( + + )} + - ) : ( - - )} - + ) + )} ); }; From 1965b8d3a6c7edc8ac39668c59da4ab2921ad6de Mon Sep 17 00:00:00 2001 From: Willian Mitsuda Date: Fri, 2 Jul 2021 16:22:43 -0300 Subject: [PATCH 3/4] Turn on autofocus in homepage --- src/Home.tsx | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Home.tsx b/src/Home.tsx index a67796a..ca99185 100644 --- a/src/Home.tsx +++ b/src/Home.tsx @@ -66,6 +66,7 @@ const Home: React.FC = () => { size={50} placeholder="Search by address / txn hash / block number" onChange={handleChange} + autoFocus >