Skip to content
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

add endpoints for airdrop #125

Merged
merged 6 commits into from
Oct 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions web_app/api/serializers/airdrop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
"""
This module defines the serializers for the airdrop data.
"""

from pydantic import BaseModel
from typing import List

class AirdropItem(BaseModel):
"""
A model to represent individual airdrop data with only the necessary fields.
"""
amount: float # Amount of the airdrop tokens
proof: str # Proof for claiming the airdrop
is_claimed: bool # Whether the airdrop has been claimed
recipient: str #Recipient address of the airdrop

class AirdropResponseModel(BaseModel):
"""
A model to encapsulate a list of AirdropItem instances, providing a structured response.
"""
airdrops: List[AirdropItem] # A list of filtered and validated airdrop items
55 changes: 55 additions & 0 deletions web_app/contract_tools/airdrop.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
"""
This module defines the contract tools for the airdrop data.
"""

from typing import List
from api.serializers.airdrop import AirdropItem, AirdropResponseModel
from contract_tools.api_request import APIRequest

class ZkLendAirdrop:
"""
A class to fetch and validate airdrop data
for a specified contract.
"""

def __init__(self, api: APIRequest):
"""
Initializes the ZkLendAirdrop class with an APIRequest instance.
Args:
api (APIRequest): An instance of APIRequest for making API calls.
"""
self.api = api

async def get_contract_airdrop(self, contract_id: str) -> AirdropResponseModel:
"""
Fetches all available airdrops
for a specific contract asynchronously.
Args:
contract_id (str): The ID of the contract
for which to fetch airdrop data.
Returns:
AirdropResponseModel: A validated list of airdrop items
for the specified contract.
"""
endpoint = f"/contracts/{contract_id}/airdrops"
response = await self.api.fetch(endpoint)
return self._validate_response(response)

def _validate_response(self, data: List[dict]) -> AirdropResponseModel:
"""
Validates and formats the response data, keeping only necessary fields.
Args:
data (List[dict]): Raw response data from the API.
Returns:
AirdropResponseModel: Structured and validated airdrop data.
"""
validated_items = []
for item in data:
validated_item = AirdropItem(
amount=item["amount"],
proof=item["proof"],
is_claimed=item["is_claimed"],
recipient=item["recipient"]
)
validated_items.append(validated_item)
return AirdropResponseModel(airdrops=validated_items)
Loading