-
Notifications
You must be signed in to change notification settings - Fork 883
add Merkl rewards to existing pools #1827
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
base: master
Are you sure you want to change the base?
add Merkl rewards to existing pools #1827
Conversation
|
||
const merklNetworks = { | ||
1: 'ethereum', | ||
137: 'polygon', | ||
10: 'optimism', | ||
42161: 'arbitrum', | ||
1101: 'polygon_zkevm', | ||
8453: 'base', | ||
60808: 'bob', | ||
146: 'sonic', | ||
43114: 'avax', | ||
80094: 'berachain', | ||
56: 'bsc', | ||
} | ||
|
||
exports.addMerklRewardApy = async ( | ||
pools, | ||
protocolId, | ||
poolAddressGetter, | ||
) => { | ||
try { | ||
let merklPools = []; | ||
let pageI = 0; | ||
|
||
while(true) { | ||
let data; | ||
try { | ||
data = await exports.getData(`https://api.merkl.xyz/v4/opportunities?mainProtocolId=${protocolId}&status=LIVE&items=100&page=${pageI}`); | ||
} catch (err) { | ||
console.log(`failed to fetch Merkl data for ${protocolId}: ${err}`); | ||
break; | ||
} | ||
|
||
if (data.length === 0) { | ||
break; | ||
} | ||
|
||
merklPools.push(...data); | ||
pageI++; | ||
} | ||
|
||
|
||
const merklPoolsMap = Object.fromEntries(Object.keys(merklNetworks).map(id => [merklNetworks[id], {}])); | ||
merklPools.forEach(pool => { | ||
if (!merklNetworks[pool.chainId]) { | ||
return; | ||
} | ||
|
||
merklPoolsMap[merklNetworks[pool.chainId]][pool.identifier.toLowerCase()] = { | ||
apyReward: pool.apr, | ||
rewardTokens: [...new Set(pool.rewardsRecord?.breakdowns.map(x => x.token.address) || [])] | ||
} | ||
}); | ||
|
||
return pools.map(pool => { | ||
const poolAddress = poolAddressGetter ? poolAddressGetter(pool) : pool.pool; | ||
const merklRewards = merklPoolsMap[pool.chain.toLowerCase()][poolAddress.toLowerCase()]; | ||
|
||
if (!merklRewards) { | ||
return pool; | ||
} | ||
|
||
// if the data is already present, don't overwrite it | ||
if (pool.apyReward || (pool.rewardTokens && pool.rewardTokens.length !== 0)) { | ||
console.log('pool already has apyReward or rewardTokens', pool.pool); | ||
return pool; | ||
} | ||
|
||
return { | ||
...pool, | ||
...merklRewards, | ||
} | ||
}); | ||
} catch (err) { | ||
console.log(`Failed to add Merkl reward apy to ${protocolId}: ${err}`); | ||
|
||
// If we fail to fetch Merkl data, just return the original pools | ||
return pools; | ||
} | ||
}; |
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.
I don't know if this is the right place for this?
I was thinking about moving it into Merkl adapter.
const merklNetworks = { | ||
1: 'ethereum', | ||
137: 'polygon', | ||
10: 'optimism', | ||
42161: 'arbitrum', | ||
1101: 'polygon_zkevm', | ||
8453: 'base', | ||
60808: 'bob', | ||
146: 'sonic', | ||
43114: 'avax', | ||
80094: 'berachain', | ||
56: 'bsc', | ||
} |
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.
Do you think I should import that from the merkl adapter?
maybe expose it as a separate file?
d8df865
to
d289c0a
Compare
No description provided.