Created
August 26, 2025 08:33
-
-
Save BaptistG/82321a9b83fff099a07173c8da2e5ebd to your computer and use it in GitHub Desktop.
Export Merkl campaign to CSV
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import requests | |
| import csv | |
| campaign_id = "0x..." # CampaignId (can be found in the advanced tab under an opportunity in the Merkl app) | |
| chainId= 1 # ChainId (eg. 1 for Ethereum) | |
| blacklist_addresses = [] # Addresses to exclude from the file | |
| decimals_reward_token = 18 # Decimals of the reward token, it's 18 most of the time | |
| def get_campaigns_rewards(chainId, campaignId, decimals_reward_token): | |
| result = {} | |
| print(f"Processing {campaignId}") | |
| url = f"https://api.merkl.xyz/v4/rewards/?items=1000&chainId={chainId}&campaignId={campaignId}&page=" | |
| page = 0 | |
| continue_fetching = True | |
| total_recipients = 0 | |
| while continue_fetching: | |
| response = requests.get(url + str(page)) | |
| if response.status_code != 200: | |
| print(f"Error fetching rewards for campaign {campaignId}: {response.status_code}") | |
| continue_fetching = False | |
| break | |
| data = response.json() | |
| if (page == 0 and len(data) == 0): | |
| print(f"No rewards found for campaign {campaignId}, skipping") | |
| if len(data) == 0: | |
| continue_fetching = False | |
| else: | |
| for item in data: | |
| if item['recipient'] in blacklist_addresses: | |
| continue | |
| if item['recipient'] not in result.keys(): | |
| result[item['recipient']] = 0 | |
| total_recipients +=1 | |
| result[item['recipient']] += (int(item['amount'])+int(int(item['pending']))) / (10**decimals_reward_token) | |
| if len(data) < 1000: | |
| continue_fetching = False | |
| page += 1 | |
| print(f"Total recipient for {campaignId}: {total_recipients}") | |
| with open(f'campaign_{campaignId}_rewards.csv', 'w', newline='') as csvfile: | |
| fieldnames = ['address', 'rewards'] | |
| writer = csv.DictWriter(csvfile, fieldnames=fieldnames) | |
| writer.writeheader() | |
| for address, reward_amount in result.items(): | |
| writer.writerow({'address': address, 'rewards': reward_amount}) | |
| return | |
| if __name__ == "__main__": | |
| get_campaigns_rewards(chainId, campaign_id, decimals_reward_token) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment