Skip to content

Commit

Permalink
refactor: Lazy calculation
Browse files Browse the repository at this point in the history
  • Loading branch information
memoyil committed Dec 31, 2024
1 parent eef35b8 commit aa41f2a
Showing 1 changed file with 18 additions and 16 deletions.
34 changes: 18 additions & 16 deletions apps/web/src/hooks/useMerkl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -86,11 +86,11 @@ export function useMerklInfo(poolAddress?: string): {
})

const { data: userData } = useQuery({
queryKey: [`fetchMerkl-${chainId}-${account}`],
queryKey: [`fetchMerkl-${account}`],
queryFn: async () => {
if (!chainId) return undefined

const responsev4 = await fetch(`${MERKL_API_V4}/users/${account}/rewards?chainId=${chainId}`)
const responsev4 = await fetch(`${MERKL_API_V4}/users/${account}/rewards`)

if (!responsev4.ok) {
throw responsev4
Expand Down Expand Up @@ -196,31 +196,33 @@ export function useMerklInfo(poolAddress?: string): {

const { rewardsPerToken = [], rewardTokenAddresses = [], ...rest } = rewardResult

const rewardCurrencies = (rewardTokenAddresses as string[])
.reduce<TokenInfo[]>((result, address) => {
Object.values(lists).find((list) => {
const token: TokenInfo | undefined = list?.current?.tokens.find((t) => isAddressEqual(t.address, address))
const rewardCurrencies = rewardsPerToken.length
? rewardsPerToken
: (rewardTokenAddresses as string[])
.reduce<TokenInfo[]>((result, address) => {
Object.values(lists).find((list) => {
const token: TokenInfo | undefined = list?.current?.tokens.find((t) => isAddressEqual(t.address, address))

if (token) return result.push(token)
if (token) return result.push(token)

return false
})
return false
})

return result
}, [])
.map((info) => {
const t = new Token(chainId as number, info.address, info.decimals, info.symbol)
return result
}, [])
.map((info) => {
const t = new Token(chainId as number, info.address, info.decimals, info.symbol)

return CurrencyAmount.fromRawAmount(t, '0')
})
return CurrencyAmount.fromRawAmount(t, '0')
})

const merklApr = data?.pools?.find((pool) => isAddressEqual(pool.identifier, poolAddress))?.apr as
| number
| undefined

return {
...rest,
rewardsPerToken: rewardsPerToken.length ? rewardsPerToken : rewardCurrencies,
rewardsPerToken: rewardCurrencies,
refreshData: refetch,
merklApr,
}
Expand Down

0 comments on commit aa41f2a

Please sign in to comment.