Skip to content

Commit

Permalink
Merge pull request #11 from crypto-com/BLCKCHN-209-defi-module
Browse files Browse the repository at this point in the history
[BLCKCHN-209] Add Defi Module to py client
  • Loading branch information
yeehan-crypto-com authored Dec 17, 2024
2 parents 8917937 + 7fa4f8e commit a00989b
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 2 deletions.
4 changes: 4 additions & 0 deletions crypto_com_developer_platform_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from .block import Block
from .client import Client
from .contract import Contract
from .defi import Defi
from .exchange import Exchange
from .interfaces.chain_interfaces import CronosEvm, CronosZkEvm
from .interfaces.defi_interfaces import DefiProtocol
from .token import Token
from .transaction import Transaction
from .wallet import Wallet
Expand All @@ -17,4 +19,6 @@
"CronosEvm",
"CronosZkEvm",
"Exchange",
"Defi",
"DefiProtocol",
]
6 changes: 4 additions & 2 deletions crypto_com_developer_platform_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,19 @@ def init(cls, api_key: str, chain_id: str, provider: str = "") -> None:

from .block import Block
from .contract import Contract
from .defi import Defi
from .exchange import Exchange
from .token import Token
from .transaction import Transaction
from .wallet import Wallet
from .exchange import Exchange


Contract.init(cls())
Wallet.init(cls())
Block.init(cls())
Transaction.init(cls())
Token.init(cls())
Exchange.init(cls())
Defi.init(cls())

@classmethod
def get_api_key(cls) -> str:
Expand Down
61 changes: 61 additions & 0 deletions crypto_com_developer_platform_client/defi.py
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 crypto_com_developer_platform_client/integrations/defi_api.py
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}")
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"

0 comments on commit a00989b

Please sign in to comment.