-
Notifications
You must be signed in to change notification settings - Fork 0
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 #11 from crypto-com/BLCKCHN-209-defi-module
[BLCKCHN-209] Add Defi Module to py client
- Loading branch information
Showing
5 changed files
with
165 additions
and
2 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
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
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,61 @@ | ||
from typing import Optional | ||
|
||
from .client import Client | ||
from .integrations.api_interfaces import ApiResponse | ||
from .integrations.defi_api import ( | ||
get_all_farms, | ||
get_farm_by_symbol, | ||
get_whitelisted_tokens, | ||
) | ||
from .interfaces.defi_interfaces import DefiProtocol | ||
|
||
|
||
class Defi: | ||
""" | ||
Defi class for managing DeFi-related operations like getting whitelisted tokens and farm information. | ||
""" | ||
|
||
_client: Client | ||
|
||
@classmethod | ||
def init(cls, client: Client) -> None: | ||
""" | ||
Initialize the Defi class with a Client instance. | ||
:param client: An instance of the Client class. | ||
""" | ||
cls._client = client | ||
|
||
@classmethod | ||
def get_whitelisted_tokens(cls, protocol: DefiProtocol) -> ApiResponse: | ||
""" | ||
Get whitelisted tokens for a specific DeFi project. | ||
:param protocol: The DeFi protocol (e.g., DefiProtocol.H2FINANCE, DefiProtocol.VVSFINANCE) | ||
:return: List of whitelisted tokens for the project | ||
""" | ||
api_key = cls._client.get_api_key() | ||
return get_whitelisted_tokens(protocol.value, api_key) | ||
|
||
@classmethod | ||
def get_all_farms(cls, protocol: DefiProtocol) -> ApiResponse: | ||
""" | ||
Get all farms for a specific DeFi project. | ||
:param protocol: The DeFi protocol (e.g., DefiProtocol.H2FINANCE, DefiProtocol.VVSFINANCE) | ||
:return: List of all farms for the project | ||
""" | ||
api_key = cls._client.get_api_key() | ||
return get_all_farms(protocol.value, api_key) | ||
|
||
@classmethod | ||
def get_farm_by_symbol(cls, protocol: DefiProtocol, symbol: str) -> ApiResponse: | ||
""" | ||
Get specific farm information by symbol for a DeFi project. | ||
:param protocol: The DeFi protocol (e.g., DefiProtocol.H2FINANCE, DefiProtocol.VVSFINANCE) | ||
:param symbol: The farm symbol (e.g., 'zkCRO-MOON', 'CRO-GOLD') | ||
:return: Information about the specific farm | ||
""" | ||
api_key = cls._client.get_api_key() | ||
return get_farm_by_symbol(protocol.value, symbol, api_key) |
87 changes: 87 additions & 0 deletions
87
crypto_com_developer_platform_client/integrations/defi_api.py
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,87 @@ | ||
from typing import Any, Dict | ||
|
||
import requests | ||
|
||
from .api_interfaces import ApiResponse | ||
|
||
|
||
def get_whitelisted_tokens(project: str, api_key: str) -> ApiResponse: | ||
""" | ||
Get whitelisted tokens for a specific DeFi project. | ||
:param project: The DeFi project name | ||
:param api_key: The API key for authentication | ||
:return: List of whitelisted tokens | ||
:raises Exception: If the request fails or server responds with an error | ||
""" | ||
url = f"""https://developer-platform-api.crypto.com/v1/cdc-developer-platform/defi/whitelisted-tokens/{ | ||
project}?apiKey={api_key}""" | ||
|
||
try: | ||
response = requests.get( | ||
url, headers={'Content-Type': 'application/json'}) | ||
response.raise_for_status() | ||
return response.json() | ||
except requests.RequestException as e: | ||
error_message = str(e) | ||
if hasattr(e, 'response') and e.response is not None: | ||
if e.response.status_code not in (200, 201): | ||
error_body = e.response.json() | ||
error_message = error_body.get('error') or f"HTTP error! status: { | ||
e.response.status_code}" | ||
raise Exception(f"Failed to get whitelisted tokens: {error_message}") | ||
|
||
|
||
def get_all_farms(project: str, api_key: str) -> ApiResponse: | ||
""" | ||
Get all farms for a specific DeFi project. | ||
:param project: The DeFi project name | ||
:param api_key: The API key for authentication | ||
:return: List of all farms | ||
:raises Exception: If the request fails or server responds with an error | ||
""" | ||
url = f"""https://developer-platform-api.crypto.com/v1/cdc-developer-platform/defi/farms/{ | ||
project}?apiKey={api_key}""" | ||
|
||
try: | ||
response = requests.get( | ||
url, headers={'Content-Type': 'application/json'}) | ||
response.raise_for_status() | ||
return response.json() | ||
except requests.RequestException as e: | ||
error_message = str(e) | ||
if hasattr(e, 'response') and e.response is not None: | ||
if e.response.status_code not in (200, 201): | ||
error_body = e.response.json() | ||
error_message = error_body.get('error') or f"HTTP error! status: { | ||
e.response.status_code}" | ||
raise Exception(f"Failed to get farms: {error_message}") | ||
|
||
|
||
def get_farm_by_symbol(project: str, symbol: str, api_key: str) -> ApiResponse: | ||
""" | ||
Get specific farm information by symbol for a DeFi project. | ||
:param project: The DeFi project name | ||
:param symbol: The farm symbol | ||
:param api_key: The API key for authentication | ||
:return: Information about the specific farm | ||
:raises Exception: If the request fails or server responds with an error | ||
""" | ||
url = f"""https://developer-platform-api.crypto.com/v1/cdc-developer-platform/defi/farms/{ | ||
project}/{symbol}?apiKey={api_key}""" | ||
|
||
try: | ||
response = requests.get( | ||
url, headers={'Content-Type': 'application/json'}) | ||
response.raise_for_status() | ||
return response.json() | ||
except requests.RequestException as e: | ||
error_message = str(e) | ||
if hasattr(e, 'response') and e.response is not None: | ||
if e.response.status_code not in (200, 201): | ||
error_body = e.response.json() | ||
error_message = error_body.get('error') or f"HTTP error! status: { | ||
e.response.status_code}" | ||
raise Exception(f"Failed to get farm by symbol: {error_message}") |
9 changes: 9 additions & 0 deletions
9
crypto_com_developer_platform_client/interfaces/defi_interfaces.py
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,9 @@ | ||
from enum import Enum | ||
|
||
|
||
class DefiProtocol(Enum): | ||
""" | ||
Enum representing supported DeFi protocols. | ||
""" | ||
H2 = "h2finance" | ||
VVS = "vvsfinance" |