Skip to content

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

mastilver
Copy link
Contributor

No description provided.

Comment on lines +516 to +595

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;
}
};
Copy link
Contributor Author

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.

Comment on lines +517 to +529
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',
}
Copy link
Contributor Author

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?

@mastilver mastilver force-pushed the additional-merklr-rewards branch from d8df865 to d289c0a Compare April 22, 2025 15:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant