-
Notifications
You must be signed in to change notification settings - Fork 145
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #125 from ShantelPeters/airdrop-endpoint
add endpoints for airdrop
- Loading branch information
Showing
2 changed files
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
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 |
This file contains 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
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) |