Skip to content

Commit

Permalink
Merge pull request #125 from ShantelPeters/airdrop-endpoint
Browse files Browse the repository at this point in the history
add endpoints for airdrop
  • Loading branch information
djeck1432 authored Oct 28, 2024
2 parents 013d3ba + 1898dba commit 1013bd3
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 0 deletions.
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)

0 comments on commit 1013bd3

Please sign in to comment.