Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor useVoucherAddress hook #958

Merged
merged 1 commit into from
Feb 28, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions src/hooks/aelin/vouched-pools/useAelinVouchedPools.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@ import useHardCodedVouchedPools from '@/src/hooks/aelin/useAelinHardCodedVouched

export default function useAelinVouchedPools() {
const voucherAddress = useVoucherAddress()

const { data: user, error, isValidating } = useAelinUser(voucherAddress || ZERO_ADDRESS)

const { data: aelinVouchAddress } = useEnsResolver(
env.NEXT_PUBLIC_AELIN_VOUCHER_ENS_ADDRESS as string,
)
Expand Down
45 changes: 26 additions & 19 deletions src/hooks/aelin/vouched-pools/useVoucherAddress.tsx
Original file line number Diff line number Diff line change
@@ -1,37 +1,44 @@
import { useRouter } from 'next/router'
import { useEffect, useState } from 'react'

import { isValidAddress } from 'ethereumjs-util'
import { isAddress } from '@ethersproject/address'

import { isValidENSName, useEnsResolver } from '../../useEnsResolvers'
import env from '@/config/env'
import { ZERO_ADDRESS } from '@/src/constants/misc'

export default function useVoucherAddress() {
const [voucherAddress, setVoucherAddress] = useState<string | null>(null)

const {
query: { voucher },
} = useRouter()

const [voucherAddress, setVoucherAddress] = useState<string | null>(null)
const { data: aelinVouchAddress } = useEnsResolver(
env.NEXT_PUBLIC_AELIN_VOUCHER_ENS_ADDRESS as string,
)
const { data: ensAddress } = useEnsResolver(voucher as string)
const { data: ensDotEthAddress } = useEnsResolver(voucher + '.eth')
const formattedVoucher =
voucher && typeof voucher === 'string'
? isAddress(voucher)
? voucher
: voucher.endsWith('.eth')
? voucher
: `${voucher}.eth`
: env.NEXT_PUBLIC_AELIN_VOUCHER_ENS_ADDRESS

const { data: ensAddress } = useEnsResolver(formattedVoucher as string)

useEffect(() => {
if (!voucher || typeof voucher !== 'string') {
setVoucherAddress(aelinVouchAddress ?? null)
} else if (isValidAddress(voucher)) {
setVoucherAddress(voucher)
} else if (isValidENSName(voucher) && ensAddress) {
setVoucherAddress(ensAddress)
} else if (isValidENSName(voucher + '.eth') && ensDotEthAddress) {
setVoucherAddress(ensDotEthAddress)
} else {
setVoucherAddress(ZERO_ADDRESS)
function getVoucherAddress(): string {
if (isAddress(formattedVoucher as string)) {
return formattedVoucher as string
}

if (isValidENSName(formattedVoucher as string) && ensAddress) {
return ensAddress
}

return env.NEXT_PUBLIC_AELIN_VOUCHER_ENS_ADDRESS as string
}
}, [voucher, voucherAddress, aelinVouchAddress, ensAddress, ensDotEthAddress])

setVoucherAddress(getVoucherAddress())
}, [voucher, voucherAddress, ensAddress, formattedVoucher])

return voucherAddress
}
19 changes: 16 additions & 3 deletions src/hooks/useEnsResolvers.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { JsonRpcProvider } from '@ethersproject/providers'
import useSWR, { SWRConfiguration } from 'swr'
import useSWR from 'swr'

import { Chains, getNetworkConfig } from '@/src/constants/chains'

Expand All @@ -10,7 +10,7 @@ const { rpcUrl: optimismRpcUrl } = getNetworkConfig(Chains.optimism)
export const optimismRpcProvider = new JsonRpcProvider(optimismRpcUrl)

// Get ens name by address
export const useEnsLookUpAddress = (address: string, swrOptions?: SWRConfiguration) => {
export const useEnsLookUpAddress = (address: string) => {
const { data, isValidating } = useSWR(
mainnetRpcProvider && address ? ['ensLookUpAddress', address] : null,

Expand All @@ -25,7 +25,13 @@ export const useEnsLookUpAddress = (address: string, swrOptions?: SWRConfigurati
return address
}
},
swrOptions,
{
refreshWhenHidden: false,
revalidateOnFocus: false,
revalidateOnMount: false,
revalidateOnReconnect: false,
refreshWhenOffline: false,
},
)

return {
Expand Down Expand Up @@ -68,6 +74,13 @@ export const useEnsResolver = (name: string | undefined) => {
return null
}
},
{
refreshWhenHidden: false,
revalidateOnFocus: false,
revalidateOnMount: false,
revalidateOnReconnect: false,
refreshWhenOffline: false,
},
)

return {
Expand Down