Skip to content

Commit

Permalink
Fix UNS name resolution throwing error (#554)
Browse files Browse the repository at this point in the history
Resolves #507
  • Loading branch information
jagodarybacka authored Nov 3, 2023
2 parents 19e2b8f + e190b1a commit b113f77
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 36 deletions.
12 changes: 6 additions & 6 deletions src/redux-state/thunks/wallet.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,23 +24,23 @@ export const fetchWalletName = createDappAsyncThunk(
claim: { useConnectedWallet },
} = getState()

const { name, avatar } = await resolveAddressToWalletData(address)
const data = await resolveAddressToWalletData(address)

if (name) {
if (data) {
dispatch(
updateConnectedWallet({
address,
name,
avatar,
...(data.name ? { name: data.name } : {}),
...(data.avatar ? { name: data.avatar } : {}),
})
)

if (useConnectedWallet) {
dispatch(setClaimingUser({ name, address }))
dispatch(setClaimingUser({ name: data.name, address }))
}
}

return name
return data?.name
}
)

Expand Down
25 changes: 14 additions & 11 deletions src/shared/utils/names.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const NAMES_CACHE_STRORAGE_KEY = "taho.cachedNames"
const MAX_CACHE_AGE = 1000 * 60 * 60 * 24 * 7 // 1 week

const resolveAddressPromiseCache: {
[address: string]: Promise<WalletData>
[address: string]: Promise<WalletData | null>
} = {}

const getCachedNames = () => {
Expand Down Expand Up @@ -52,9 +52,11 @@ const resolveENSPromise = (address: string) =>
})

const resolveUNSPromise = (address: string) =>
resolveAddressToUNS(address).then((name): string => {
resolveAddressToUNS(address).then((name): WalletData | null => {
if (!name) return null

addCachedName({ type: "uns", address, name })
return name
return { name }
})

const resolveUnknownNamePromise = () =>
Expand All @@ -70,17 +72,20 @@ const resolveAddressToWalletDataWithoutCache = async (address: string) => {
resolveENSPromise(normalizedAddress),
resolveUNSPromise(normalizedAddress),
resolveUnknownNamePromise(),
]) as Promise<WalletData>
])
}

const { name, avatar } = await resolveAddressPromiseCache[normalizedAddress]
const cachedResult =
(await resolveAddressPromiseCache[normalizedAddress]) ?? {}

const { name, avatar } = cachedResult

return { name, avatar }
return name ? { name, avatar } : null
}

export const resolveAddressToWalletData = async (
address: string
): Promise<WalletData> => {
): Promise<WalletData | null> => {
const cachedNames = getCachedNames()

const normalizedAddress = normalizeAddress(address)
Expand All @@ -90,11 +95,9 @@ export const resolveAddressToWalletData = async (
return cachedItem.ens ?? cachedItem.uns
}

const { name, avatar } = await resolveAddressToWalletDataWithoutCache(
normalizedAddress
)
const data = await resolveAddressToWalletDataWithoutCache(normalizedAddress)

return { name, avatar }
return data ? { name: data.name, avatar: data.avatar } : null
}

export const resolveNameToAddress = async (addressOrName: string) => {
Expand Down
32 changes: 18 additions & 14 deletions src/shared/utils/uns.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,23 +50,27 @@ export const resolveUNS = async (name: string) => {
}

export const resolveAddressToUNS = async (address: string) => {
const response: UNSReverseResponse = await fetchJson({
url: `https://resolve.unstoppabledomains.com/domains/?owners=${address}&sortBy=id&sortDirection=ASC`,
headers: {
Authorization: `Bearer ${process.env.UNS_API_KEY}`,
},
})
try {
const response: UNSReverseResponse = await fetchJson({
url: `https://resolve.unstoppabledomains.com/domains/?owners=${address}&sortBy=id&sortDirection=ASC`,
headers: {
Authorization: `Bearer ${process.env.UNS_API_KEY}`,
},
})

const { data }: UNSReverseResponse = response
const { data }: UNSReverseResponse = response

const name = data[0]?.id ?? null
const name = data[0]?.id ?? null

if (!name) {
throw new Error("Invalid UNS domain name")
}
if (!name) {
throw new Error("Invalid UNS domain name")
}

// FIXME: UNS tend to resolve faster than ENS, so to prefer ENS names let's wait a bit
await wait(10000)
// FIXME: UNS tend to resolve faster than ENS, so to prefer ENS names let's wait a bit
await wait(10000)

return name
return name
} catch {
return null
}
}
10 changes: 5 additions & 5 deletions src/ui/Island/RealmDetails/LeaderboardList/LeaderboardItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,12 @@ export default function LeaderboardItem({

useEffect(() => {
const getName = async () => {
const { name, avatar: userAvatar } = await resolveAddressToWalletData(
address
)
const data = await resolveAddressToWalletData(address)

if (name) setUsername(name)
if (userAvatar) setWalletAvatar(userAvatar)
if (!data) return

if (data.name) setUsername(data.name)
if (data.avatar) setWalletAvatar(data.avatar)
}
getName()
}, [address])
Expand Down

0 comments on commit b113f77

Please sign in to comment.