-
Notifications
You must be signed in to change notification settings - Fork 9
add basic ENS name resolution for address lookup #771
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome! We love to see contributors. Great initiative
src/utils/miscellaneous.ts
Outdated
@@ -265,4 +265,4 @@ export const isAnOrderId = (text: string): boolean => text.match(/^0x[a-fA-F0-9] | |||
* | |||
* @param text Possible address string to check | |||
*/ | |||
export const isAnAddressAccount = (text: string): boolean => text.match(/^0x[a-fA-F0-9]{40}$/)?.input !== undefined | |||
export const isAnAddressAccount = (text: string): boolean => text.match(/^0x[a-fA-F0-9]{40}$|[a-zA-Z0-9]+\.[a-zA-Z]+$/)?.input !== undefined |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should we have a separate pattern just for the ENS?
src/hooks/useSearchSubmit.ts
Outdated
web3.eth.ens | ||
.getAddress(query) | ||
.then((res) => res && res.length > 0 && history.push(`/${pathPrefix}/${res}`)) | ||
.catch(() => history.push(`/address/null`)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In my latest commit, I route to /address/${query}
so let me know if it should be something yet different. If it's a valid ENS domain, it will just show no records (since there will be valid ETH address behind it) or else the generic "Page not found" where an invalid address is provided as the query parameter.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
First of all, thank you for the time to submit this PR, really appreciate it.
I've left a couple of comments in the code.
Regarding the behaviour, I can't get the ENS resolution to work locally.
Any (valid or invalid) ens fails resolution. Did it work out for you fine?
I wonder if I have any local setting making it fail...
src/utils/miscellaneous.ts
Outdated
@@ -265,4 +265,4 @@ export const isAnOrderId = (text: string): boolean => text.match(/^0x[a-fA-F0-9] | |||
* | |||
* @param text Possible address string to check | |||
*/ | |||
export const isAnAddressAccount = (text: string): boolean => text.match(/^0x[a-fA-F0-9]{40}$/)?.input !== undefined | |||
export const isAnAddressAccount = (text: string): boolean => text.match(/^0x[a-fA-F0-9]{40}$|[a-zA-Z0-9]+\.[a-zA-Z]+$/)?.input !== undefined |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ESlist fix
export const isAnAddressAccount = (text: string): boolean => text.match(/^0x[a-fA-F0-9]{40}$|[a-zA-Z0-9]+\.[a-zA-Z]+$/)?.input !== undefined | |
export const isAnAddressAccount = (text: string): boolean => | |
text.match(/^0x[a-fA-F0-9]{40}$|[a-zA-Z0-9]+\.[a-zA-Z]+$/)?.input !== undefined | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I think would be best to have a separated query only for ENS addresses.
The way you suggest will force all addresses to be queried against the network, but we do not need that since we already know it's an address simply by the regex.
In the future sure, we could have different checks for smart contracts wallets, tokens, contracts, etc, but that's a task for another time :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, my bad. I added a check in the callback hook that runs the regex again for just the second half (i.e. [a-zA-Z0-9]+\.[a-zA-Z]+$/
) to see if it looks like a domain name since an ETH address will never have a period in it so this bypasses the network check when it's just a normal 0x... address.
Also, I was able to get ens resolution working. I just looked on etherscan for a recent transaction with the settlement contract that comes from a ens domain and used that to test |
I'm sorry, you are right. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Indeed, it's working 👍
Some more suggestions below
src/hooks/useSearchSubmit.ts
Outdated
@@ -22,8 +23,22 @@ export function useSearchSubmit(): (query: string) => void { | |||
// Orders, transactions, tokens, batches | |||
const path = pathAccordingTo(query) | |||
const pathPrefix = prefixNetwork ? `${prefixNetwork}/${path}` : `${path}` | |||
|
|||
query && query.length > 0 && history.push(`/${pathPrefix}/${query}`) | |||
if (pathPrefix === 'address' && query.match(/[a-zA-Z0-9]+\.[a-zA-Z]+$/)?.input !== undefined) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you refactor the ENS regex and reuse here and in the isAnAddressAccount
helper?
I'm thinking something like:
isEns(query:string) => boolean
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also, I was testing locally and you'd want to compare 'address' against path
, not pathPrefix
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requested changes are made and added a test for the new helper.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let me know if you see anything else. Otherwise, it seems to be working well locally and hopefully I've dealt with any test coverage issues.
src/hooks/useSearchSubmit.ts
Outdated
history.push(`/address/${res}`) | ||
} else { | ||
history.push(`/address/${query}`) | ||
} | ||
}) | ||
.catch(() => history.push(`/address/${query}`)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Keep the ${pathPrefix}
to keep the network in the query.
Unless you want to always ever redirect to mainnet address?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nope, just an error on my part in understanding how it works. I added the pathPrefix
back.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome!
Hey @acolytec3 great contribution!!! please join our Discord (chat.cowswap.exchange) so that MasterCoW can DM you ;) |
Already there :-)
|
awesome, found you!! |
Summary
Adds basic ENS name resolution to the address lookup route in the Protocol Explorer.
To Test
/address/null
to display the default error message for an address that cannot be foundBackground
ENS resolver function:
web3.eth.ens.getAddress
to do the ENS name resolution since that's what is readily available in your web3 provider though that method appears to have been marked deprecated inweb3
.ethers
or something (which I also see in your package.json)Address validation regex updates
.com
,.org
,.xyz
). It looks like ENS only currently allows two other TLDs at (.xyz
,kred
) but according to this article any DNS TLD could conceptually be used so no reason to not let anyone enter any arbitrary valid DNS address to support potential future extensions of ENS.