Skip to content

Commit

Permalink
add exchange module
Browse files Browse the repository at this point in the history
  • Loading branch information
yeehan-crypto-com committed Dec 5, 2024
1 parent 369e762 commit bdf638c
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 2 deletions.
2 changes: 2 additions & 0 deletions crypto_com_developer_platform_client/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .block import Block
from .client import Client
from .contract import Contract
from .exchange import Exchange
from .interfaces.chain_interfaces import CronosEvm, CronosZkEvm
from .token import Token
from .transaction import Transaction
Expand All @@ -15,4 +16,5 @@
"Token",
"CronosEvm",
"CronosZkEvm",
"Exchange",
]
4 changes: 3 additions & 1 deletion crypto_com_developer_platform_client/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,14 @@ def init(cls, api_key: str, chain_id: str, provider: str = "") -> None:
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())

@classmethod
def get_api_key(cls) -> str:
Expand Down
43 changes: 43 additions & 0 deletions crypto_com_developer_platform_client/exchange.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from .client import Client
from .integrations.api_interfaces import ApiResponse
from .integrations.exchange_api import get_all_tickers, get_ticker_by_instrument


class Exchange:
"""
Exchange class for managing Crypto.com Exchange-related operations like retrieving ticker information (Chain agnostic).
"""

_client: Client

@classmethod
def init(cls, client: Client) -> None:
"""
Initialize the Exchange class with a Client instance.
:param client: An instance of the Client class.
"""
cls._client = client

@classmethod
def get_all_tickers(cls) -> ApiResponse:
"""
Get all tickers from the Crypto.com Exchange (Chain agnostic).
:return: A list of all available tickers and their information.
"""
return get_all_tickers()

@classmethod
def get_ticker_by_instrument(cls, instrument_name: str) -> ApiResponse:
"""
Get ticker information for a specific instrument from the Crypto.com Exchange (Chain agnostic).
:param instrument_name: The name of the instrument to get ticker information for.
:return: Ticker information for the specified instrument.
:raises ValueError: If instrument_name is None or empty.
"""
if not instrument_name:
raise ValueError("Instrument name is required")

return get_ticker_by_instrument(instrument_name)
50 changes: 50 additions & 0 deletions crypto_com_developer_platform_client/integrations/exchange_api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import requests

from .api_interfaces import ApiResponse


def get_all_tickers() -> ApiResponse:
"""
Get all tickers from the Crypto.com Exchange (Chain agnostic).
:return: A list of all available tickers and their information.
:raises Exception: If the ticker retrieval fails or the server responds with an error.
"""
url = f"""https://developer-platform-api.crypto.com/v1/cdc-developer-platform/exchange/tickers"""

response = requests.get(url, headers={'Content-Type': 'application/json'})

if response.status_code not in (200, 201):
error_body = response.json()
server_error_message = (
error_body.get('error') or
f"HTTP error! status: {response.status_code}"
)
raise Exception(f"Failed to fetch all tickers: {server_error_message}")

return response.json()


def get_ticker_by_instrument(instrument_name: str) -> ApiResponse:
"""
Get ticker information for a specific instrument from the Crypto.com Exchange (Chain agnostic).
:param instrument_name: The name of the instrument to get ticker information for.
:return: Ticker information for the specified instrument.
:raises Exception: If the ticker retrieval fails, does not exist or the server responds with an error.
"""
url = f"""https://developer-platform-api.crypto.com/v1/cdc-developer-platform/exchange/tickers/{
instrument_name}"""

response = requests.get(url, headers={'Content-Type': 'application/json'})

if response.status_code not in (200, 201):
error_body = response.json()
server_error_message = (
error_body.get('error') or
f"HTTP error! status: {response.status_code}"
)
raise Exception(f"""Failed to fetch ticker for instrument {
instrument_name}: {server_error_message}""")

return response.json()
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "crypto-com-developer-platform-client"
version = "1.0.5"
version = "1.0.6"
description = "A python client to interact with @cryptocom/developer-platform-service"
authors = ["Ric Arcifa <[email protected]>"]
license = "MIT"
Expand Down

0 comments on commit bdf638c

Please sign in to comment.