Skip to content

Commit

Permalink
fix: Merkl apr link and reduce merkl api calls (#10498)
Browse files Browse the repository at this point in the history
<!--
Before opening a pull request, please read the [contributing
guidelines](https://github.com/pancakeswap/pancake-frontend/blob/develop/CONTRIBUTING.md)
first
-->

<!-- start pr-codex -->

---

## PR-Codex overview
This PR updates the `FarmV3ApyButton` component styling and adds a new
`chainIdToChainName` mapping. It also refactors the `useMerklInfo` hook
and updates Merkl pool links.

### Detailed summary
- Added `chainIdToChainName` mapping for different chains
- Updated Merkl pool links to include chain name
- Refactored `useMerklInfo` hook to improve data fetching and processing

> The following files were skipped due to too many changes:
`apps/web/src/config/constants/merklPools.json`

> ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your
question}`

<!-- end pr-codex -->
  • Loading branch information
memoyil authored Aug 30, 2024
1 parent 91aa230 commit 815d896
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 74 deletions.
60 changes: 30 additions & 30 deletions apps/web/src/config/constants/merklPools.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,107 +2,107 @@
{
"chainId": 42161,
"address": "0x060d8a5a7C03882e33AcA8FC304BabE869e21Ee9",
"link": "https://merkl.angle.money/?status=live&search=0x060d8a5a7C03882e33AcA8FC304BabE869e21Ee9&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0x060d8a5a7C03882e33AcA8FC304BabE869e21Ee9"
},
{
"chainId": 42161,
"address": "0x93CCe474015007B38dA0eceA96671EE4dc3d40Ad",
"link": "https://merkl.angle.money/?status=live&search=0x93CCe474015007B38dA0eceA96671EE4dc3d40Ad&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0x93CCe474015007B38dA0eceA96671EE4dc3d40Ad"
},
{
"chainId": 42161,
"address": "0x11d53EC50bc8F54B9357fbFe2A7dE034FC00f8b3",
"link": "https://merkl.angle.money/?status=live&search=0x11d53EC50bc8F54B9357fbFe2A7dE034FC00f8b3&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0x11d53EC50bc8F54B9357fbFe2A7dE034FC00f8b3"
},
{
"chainId": 42161,
"address": "0xb0C6Be7b6aaB831092A3c7ef3Fe2Ab4F48B80b6A",
"link": "https://merkl.angle.money/?status=live&search=0xb0C6Be7b6aaB831092A3c7ef3Fe2Ab4F48B80b6A&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0xb0C6Be7b6aaB831092A3c7ef3Fe2Ab4F48B80b6A"
},
{
"chainId": 42161,
"address": "0xE8BB56ee968333bf18f9CBbE8eEef350540F9607",
"link": "https://merkl.angle.money/?status=live&search=0xE8BB56ee968333bf18f9CBbE8eEef350540F9607&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0xE8BB56ee968333bf18f9CBbE8eEef350540F9607"
},
{
"chainId": 42161,
"address": "0x7e928afb59f5dE9D2f4d162f754C6eB40c88aA8E",
"link": "https://merkl.angle.money/?status=live&search=0x7e928afb59f5dE9D2f4d162f754C6eB40c88aA8E&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0x7e928afb59f5dE9D2f4d162f754C6eB40c88aA8E"
},
{
"chainId": 42161,
"address": "0x4bfc22A4dA7f31F8a912a79A7e44a822398b4390",
"link": "https://merkl.angle.money/?status=live&search=0x4bfc22A4dA7f31F8a912a79A7e44a822398b4390&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0x4bfc22A4dA7f31F8a912a79A7e44a822398b4390"
},
{
"chainId": 42161,
"address": "0x5A17cbf5F866BDe11C28861a2742764Fac0Eba4B",
"link": "https://merkl.angle.money/?status=live&search=0x5A17cbf5F866BDe11C28861a2742764Fac0Eba4B&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0x5A17cbf5F866BDe11C28861a2742764Fac0Eba4B"
},
{
"chainId": 42161,
"address": "0x7fCDC35463E3770c2fB992716Cd070B63540b947",
"link": "https://merkl.angle.money/?status=live&search=0x7fCDC35463E3770c2fB992716Cd070B63540b947&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0x7fCDC35463E3770c2fB992716Cd070B63540b947"
},
{
"chainId": 42161,
"address": "0xd9e2a1a61B6E61b275cEc326465d417e52C1b95c",
"link": "https://merkl.angle.money/?status=live&search=0xd9e2a1a61B6E61b275cEc326465d417e52C1b95c&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0xd9e2a1a61B6E61b275cEc326465d417e52C1b95c"
},
{
"chainId": 42161,
"address": "0x4fC7aC44DAe553464d92c049175133239A8705C1",
"link": "https://merkl.angle.money/?status=live&search=0x4fC7aC44DAe553464d92c049175133239A8705C1&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0x4fC7aC44DAe553464d92c049175133239A8705C1"
},
{
"chainId": 42161,
"address": "0x389938CF14Be379217570D8e4619E51fBDafaa21",
"link": "https://merkl.angle.money/?status=live&search=0x389938CF14Be379217570D8e4619E51fBDafaa21&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0x389938CF14Be379217570D8e4619E51fBDafaa21"
},
{
"chainId": 42161,
"address": "0x0BaCc7a9717e70EA0DA5Ac075889Bd87d4C81197",
"link": "https://merkl.angle.money/?status=live&search=0x0BaCc7a9717e70EA0DA5Ac075889Bd87d4C81197&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0x0BaCc7a9717e70EA0DA5Ac075889Bd87d4C81197"
},
{
"chainId": 42161,
"address": "0x1D2031Fa0dA6fb4bDA57E377C7273c22eab930D6",
"link": "https://merkl.angle.money/?status=live&search=0x1D2031Fa0dA6fb4bDA57E377C7273c22eab930D6&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0x1D2031Fa0dA6fb4bDA57E377C7273c22eab930D6"
},
{
"chainId": 42161,
"address": "0x64dae6685725Dbd0a0e63fE522c9134d0EaA7258",
"link": "https://merkl.angle.money/?status=live&search=0x64dae6685725Dbd0a0e63fE522c9134d0EaA7258&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0x64dae6685725Dbd0a0e63fE522c9134d0EaA7258"
},
{
"chainId": 42161,
"address": "0x3ABBbBb4C254b9327F1a0580BdbCcb51B0b5Fb08",
"link": "https://merkl.angle.money/?status=live&search=0x3ABBbBb4C254b9327F1a0580BdbCcb51B0b5Fb08&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0x3ABBbBb4C254b9327F1a0580BdbCcb51B0b5Fb08"
},
{
"chainId": 42161,
"address": "0xfeb104864f3aD0C75E6B1bB4A0EDB3f89e562e6e",
"link": "https://merkl.angle.money/?status=live&search=0xfeb104864f3aD0C75E6B1bB4A0EDB3f89e562e6e&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0xfeb104864f3aD0C75E6B1bB4A0EDB3f89e562e6e"
},
{
"chainId": 42161,
"address": "0xC75908421566eA77A73B14D9cD0479C568f2B7A7",
"link": "https://merkl.angle.money/?status=live&search=0xC75908421566eA77A73B14D9cD0479C568f2B7A7&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0xC75908421566eA77A73B14D9cD0479C568f2B7A7"
},
{
"chainId": 42161,
"address": "0xB27fdE8d172bb146186Bac819Ceb12fE4FA9262A",
"link": "https://merkl.angle.money/?status=live&search=0xB27fdE8d172bb146186Bac819Ceb12fE4FA9262A&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0xB27fdE8d172bb146186Bac819Ceb12fE4FA9262A"
},
{
"chainId": 42161,
"address": "0xF5Fac36c2429e1Cf84D4aBACdB18477Ef32589c9",
"link": "https://merkl.angle.money/?status=live&search=0xF5Fac36c2429e1Cf84D4aBACdB18477Ef32589c9&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0xF5Fac36c2429e1Cf84D4aBACdB18477Ef32589c9"
},
{
"chainId": 42161,
"address": "0xe37304F7489ed253b2A46A1d9DabDcA3d311D22E",
"link": "https://merkl.angle.money/?status=live&search=0xe37304F7489ed253b2A46A1d9DabDcA3d311D22E&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0xe37304F7489ed253b2A46A1d9DabDcA3d311D22E"
},
{
"chainId": 42161,
Expand All @@ -112,46 +112,46 @@
{
"chainId": 42161,
"address": "0xe2d9b612446180334D56e10e82Dc78E3dEe814B0",
"link": "https://merkl.angle.money/?status=live&search=0xe2d9b612446180334D56e10e82Dc78E3dEe814B0&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0xe2d9b612446180334D56e10e82Dc78E3dEe814B0"
},
{
"chainId": 42161,
"address": "0x3C2bf5C0Be7be4919fae1330b748E89f165259D7",
"link": "https://merkl.angle.money/?status=live&search=0x3C2bf5C0Be7be4919fae1330b748E89f165259D7&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0x3C2bf5C0Be7be4919fae1330b748E89f165259D7"
},
{
"chainId": 42161,
"address": "0xb9F9C2c31C3d00DD0EB987c818b4078F786d6F6E",
"link": "https://merkl.angle.money/?status=live&search=0xb9F9C2c31C3d00DD0EB987c818b4078F786d6F6E&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0xb9F9C2c31C3d00DD0EB987c818b4078F786d6F6E"
},
{
"chainId": 42161,
"address": "0xf0346b437Fe43e64Fa799C2BD3cf5Db1F7e9327C",
"link": "https://merkl.angle.money/?status=live&search=0xf0346b437Fe43e64Fa799C2BD3cf5Db1F7e9327C&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0xf0346b437Fe43e64Fa799C2BD3cf5Db1F7e9327C"
},
{
"chainId": 42161,
"address": "0xfA4971DC5aD81b4FccAFFAD0a584d13192B7D2BA",
"link": "https://merkl.angle.money/?status=live&search=0xfA4971DC5aD81b4FccAFFAD0a584d13192B7D2BA&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0xfA4971DC5aD81b4FccAFFAD0a584d13192B7D2BA"
},
{
"chainId": 42161,
"address": "0x1CAF100CD74792D4be6C64621C2E21c7830868c4",
"link": "https://merkl.angle.money/?status=live&search=0x1CAF100CD74792D4be6C64621C2E21c7830868c4&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0x1CAF100CD74792D4be6C64621C2E21c7830868c4"
},
{
"chainId": 42161,
"address": "0xcFBcc53E96908Cc3719f2807457Db0b89870c4de",
"link": "https://merkl.angle.money/?status=live&search=0xcFBcc53E96908Cc3719f2807457Db0b89870c4de&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0xcFBcc53E96908Cc3719f2807457Db0b89870c4de"
},
{
"chainId": 42161,
"address": "0x04a35D7920f2f2fF9fAA447fF8cFaD47fc7cED2b",
"link": "https://merkl.angle.money/?status=live&search=0x04a35D7920f2f2fF9fAA447fF8cFaD47fc7cED2b&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0x04a35D7920f2f2fF9fAA447fF8cFaD47fc7cED2b"
},
{
"chainId": 42161,
"address": "0xfb5076B8649022E9057FB6Eb7cbaC686CAcC2448",
"link": "https://merkl.angle.money/?status=live&search=0xfb5076B8649022E9057FB6Eb7cbaC686CAcC2448&chain=42161%2C"
"link": "https://merkl.angle.money/arbitrum/pool/2/0xfb5076B8649022E9057FB6Eb7cbaC686CAcC2448"
}
]
101 changes: 58 additions & 43 deletions apps/web/src/hooks/useMerkl.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { useToast } from '@pancakeswap/uikit'
import { useQuery } from '@tanstack/react-query'
import { ToastDescriptionWithTx } from 'components/Toast'
import { distributorABI } from 'config/abi/AngleProtocolDistributor'
import { FAST_INTERVAL } from 'config/constants'
import { DISTRIBUTOR_ADDRESSES } from 'config/merkl'
import useAccountActiveChain from 'hooks/useAccountActiveChain'
import { useCallWithGasPrice } from 'hooks/useCallWithGasPrice'
Expand Down Expand Up @@ -33,9 +34,10 @@ export function useMerklInfo(poolAddress: string | null): {
merklApr?: number
} {
const { account, chainId } = useAccountActiveChain()
const lists = useAllLists()

const { data: merklData } = useQuery({
queryKey: ['merklAprData', chainId, poolAddress],
const { data: merklApr } = useQuery({
queryKey: ['merklAprData', chainId],
queryFn: async () => {
const resp = await fetch(`https://api.angle.money/v2/merkl?chainIds[]=${chainId}&AMMs[]=pancakeswapv3`)
if (resp.ok) {
Expand All @@ -44,41 +46,70 @@ export function useMerklInfo(poolAddress: string | null): {
}
throw resp
},
enabled: Boolean(chainId) && Boolean(poolAddress),
enabled: Boolean(chainId && poolAddress),
staleTime: FAST_INTERVAL,
retryDelay: (attemptIndex) => Math.min(2000 * 2 ** attemptIndex, 30000),
select: (data) => {
return data?.[chainId ?? 0]?.pools?.[poolAddress ?? '']?.aprs?.['Average APR (rewards / pool TVL)'] as
| number
| undefined
},
})

const merklApr = merklData?.[chainId ?? 0]?.pools?.[poolAddress ?? '']?.aprs?.['Average APR (rewards / pool TVL)'] as
| number
| undefined

const { data, isPending, refetch } = useQuery({
queryKey: [`fetchMerkl-${chainId}-${poolAddress}-${account || 'no-account'}`],
queryKey: [`fetchMerkl-${chainId}-${account || 'no-account'}`],
queryFn: async () => {
const responsev2 = await fetch(
`${MERKL_API_V2}?chainIds[]=${chainId}${account ? `&user=${account}` : ''}&AMMs[]=pancakeswapv3`,
)

if (!responsev2.ok) {
throw responsev2
}

const merklDataV2 = await responsev2.json()

if (!chainId || !merklDataV2[chainId]) return null

const { pools, transactionData } = merklDataV2[chainId]

const hasLive = first(
Object.keys(pools)
.filter((poolId) => poolId === poolAddress && pools[poolId].meanAPR !== 0)
.map((poolId) => pools[poolId]),
)
return { pools, transactionData }
},
enabled: Boolean(chainId && poolAddress),
staleTime: FAST_INTERVAL,
retryDelay: (attemptIndex) => Math.min(2000 * 2 ** attemptIndex, 30000),
})

const merklPoolData = first(
Object.keys(pools)
.filter((poolId) => poolId === poolAddress)
.map((poolId) => pools[poolId]),
)
return useMemo(() => {
if (!data)
return {
rewardsPerToken: [],
rewardTokenAddresses: [],
transactionData: null,
refreshData: refetch,
hasMerkl: false,
isPending,
}

const { pools, transactionData } = data

const hasLive = first(
Object.keys(pools)
.filter((poolId) => poolId === poolAddress && pools[poolId].meanAPR !== 0)
.map((poolId) => pools[poolId]),
)

const rewardsPerTokenObject = merklPoolData?.rewardsPerToken
const merklPoolData = first(
Object.keys(pools)
.filter((poolId) => poolId === poolAddress)
.map((poolId) => pools[poolId]),
)

const rewardsPerToken = rewardsPerTokenObject
const rewardsPerTokenObject = merklPoolData?.rewardsPerToken

const rewardResult = {
hasMerkl: Boolean(hasLive),
rewardsPerToken: rewardsPerTokenObject
? Object.keys(rewardsPerTokenObject)
.map((tokenAddress) => {
const tokenInfo = rewardsPerTokenObject[tokenAddress]
Expand All @@ -88,29 +119,13 @@ export function useMerklInfo(poolAddress: string | null): {
return CurrencyAmount.fromRawAmount(token, tokenInfo.unclaimedUnformatted)
})
.filter(Boolean)
: []

return {
hasMerkl: Boolean(hasLive),
rewardsPerToken,
rewardTokenAddresses: uniq(merklPoolData?.distributionData?.map((d) => d.token)),
transactionData,
isPending,
}
},
})

const lists = useAllLists()

return useMemo(() => {
if (!data)
return {
rewardsPerToken: [],
transactionData: null,
refreshData: refetch,
}
: [],
rewardTokenAddresses: uniq(merklPoolData?.distributionData?.map((d) => d.token)),
transactionData,
isPending,
}

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

const rewardCurrencies = (rewardTokenAddresses as string[])
.reduce<TokenInfo[]>((result, address) => {
Expand All @@ -136,7 +151,7 @@ export function useMerklInfo(poolAddress: string | null): {
refreshData: refetch,
merklApr,
}
}, [chainId, data, lists, refetch, merklApr])
}, [chainId, data, lists, refetch, merklApr, isPending, poolAddress])
}

export default function useMerkl(poolAddress: string | null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,10 @@ const StyledLi = styled.li`
height: 6px;
background: ${({ theme }) => theme.colors.text};
}
@-moz-document url-prefix() {
padding-left: 16px; /* Apply padding-left: 16px in Firefox only */
}
`

type FarmV3ApyButtonProps = {
Expand Down
12 changes: 11 additions & 1 deletion scripts/updateMerkl/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ type MerklConfigResponse = {
[chainId: number]: MerklConfig
}

export const chainIdToChainName = {
1: 'ethereum',
56: 'bnb smart chain',
324: 'zkync',
1101: 'polygon zkevm',
8453: 'base',
42161: 'arbitrum',
59144: 'linea',
} as const

const fetchAllMerklConfig = async (): Promise<MerklConfigResponse> => {
const response = await fetch('https://api.angle.money/v2/merkl')

Expand All @@ -53,7 +63,7 @@ const parseMerklConfig = (merklConfig: MerklConfig[]): MerklConfigPool[] => {
return pools.map((pool) => ({
chainId: pool.chainId,
address: pool.pool,
link: `https://merkl.angle.money/?status=live&search=${pool.pool}&chain=${pool.chainId}%2C`,
link: encodeURI(`https://merkl.angle.money/${chainIdToChainName[pool.chainId]}/pool/2/${pool.pool}`),
}))
}

Expand Down

0 comments on commit 815d896

Please sign in to comment.