Skip to content

Commit

Permalink
cdp sdk initial implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
John-peterson-coinbase committed Sep 18, 2024
1 parent c741685 commit ddf9f90
Show file tree
Hide file tree
Showing 135 changed files with 30,404 additions and 0 deletions.
23 changes: 23 additions & 0 deletions cdp/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
from cdp.address import Address
from cdp.asset import Asset
from cdp.balance import Balance
from cdp.cdp import Cdp
from cdp.faucet_transaction import FaucetTransaction
from cdp.sponsored_send import SponsoredSend
from cdp.trade import Trade
from cdp.transaction import Transaction
from cdp.transfer import Transfer
from cdp.wallet import Wallet

__all__ = [
"Cdp",
"Wallet",
"Asset",
"Transfer",
"Address",
"Transaction",
"Balance",
"FaucetTransaction",
"Trade",
"SponsoredSend",
]
94 changes: 94 additions & 0 deletions cdp/address.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from decimal import Decimal
from cdp.cdp import Cdp
from cdp.faucet_transaction import FaucetTransaction
from cdp.asset import Asset
from cdp.balance import Balance


class Address:
"""
A class representing an address.
"""

def __init__(self, network_id: str, address_id: str) -> None:
"""
Initialize the Address class.
Args:
network_id (str): The network ID.
address_id (str): The address ID.
"""
self._network_id = network_id
self._id = address_id

@property
def address_id(self) -> str:
"""
Get the address ID.
Returns:
str: The address ID.
"""
return self._id

@property
def network_id(self) -> str:
"""
Get the network ID.
Returns:
str: The network ID.
"""
return self._network_id

@property
def can_sign(self) -> bool:
"""
Get whether the address can sign.
Returns:
bool: Whether the address can sign.
"""
return False

def faucet(self, asset_id=None):
"""
Request faucet funds.
Args:
asset_id (str): The asset ID.
Returns:
FaucetTransaction: The faucet transaction object.
"""
model = Cdp.api_clients.external_addresses.request_external_faucet_funds(
network_id=self.network_id, address_id=self.address_id, asset_id=asset_id
)

return FaucetTransaction(model)

def balance(self, asset_id):
"""
Get the balance of the address.
Args:
asset_id (str): The asset ID.
Returns:
Decimal: The balance of the address.
"""
model = Cdp.api_clients.external_addresses.get_external_address_balance(
network_id=self.network_id,
address_id=self.address_id,
asset_id=Asset.primary_denomination(asset_id),
)

return Decimal(0) if model is None else Balance.from_model(model, asset_id).amount

def __str__(self) -> str:
"""Return a string representation of the Address."""
return f"Address: (address_id: {self.address_id}, network_id: {self.network_id})"

def __repr__(self) -> str:
"""Return a string representation of the Address."""
return str(self)
148 changes: 148 additions & 0 deletions cdp/api_clients.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
from cdp.client.api.addresses_api import AddressesApi
from cdp.client.api.assets_api import AssetsApi
from cdp.client.api.external_addresses_api import ExternalAddressesApi
from cdp.client.api.networks_api import NetworksApi
from cdp.client.api.trades_api import TradesApi
from cdp.client.api.transfers_api import TransfersApi
from cdp.client.api.wallets_api import WalletsApi
from cdp.cdp_api_client import CdpApiClient


class ApiClients:
"""A container class for all API clients used in the Coinbase SDK.
This class provides lazy-loaded access to various API clients, ensuring
that each client is only instantiated when it's first accessed.
Attributes:
_cdp_client (CdpApiClient): The CDP API client used to initialize individual API clients.
_wallets (Optional[WalletsApi]): The WalletsApi client instance.
_addresses (Optional[AddressesApi]): The AddressesApi client instance.
_external_addresses (Optional[ExternalAddressesApi]): The ExternalAddressesApi client instance.
_transfers (Optional[TransfersApi]): The TransfersApi client instance.
_networks (Optional[NetworksApi]): The NetworksApi client instance.
_assets (Optional[AssetsApi]): The AssetsApi client instance.
_trades (Optional[TradesApi]): The TradesApi client instance.
"""

def __init__(self, cdp_client: CdpApiClient) -> None:
"""Initialize the ApiClients instance.
Args:
cdp_client (CdpApiClient): The CDP API client to use for initializing individual API clients.
"""
self._cdp_client: CdpApiClient = cdp_client
self._wallets: WalletsApi | None = None
self._addresses: AddressesApi | None = None
self._external_addresses: ExternalAddressesApi | None = None
self._transfers: TransfersApi | None = None
self._networks: NetworksApi | None = None
self._assets: AssetsApi | None = None
self._trades: TradesApi | None = None

@property
def wallets(self) -> WalletsApi:
"""Get the WalletsApi client instance.
Returns:
WalletsApi: The WalletsApi client instance.
Note:
This property lazily initializes the WalletsApi client on first access.
"""
if self._wallets is None:
self._wallets = WalletsApi(api_client=self._cdp_client)
return self._wallets

@property
def addresses(self) -> AddressesApi:
"""Get the AddressesApi client instance.
Returns:
AddressesApi: The AddressesApi client instance.
Note:
This property lazily initializes the AddressesApi client on first access.
"""
if self._addresses is None:
self._addresses = AddressesApi(api_client=self._cdp_client)
return self._addresses

@property
def external_addresses(self) -> ExternalAddressesApi:
"""Get the ExternalAddressesApi client instance.
Returns:
ExternalAddressesApi: The ExternalAddressesApi client instance.
Note:
This property lazily initializes the ExternalAddressesApi client on first access.
"""
if self._external_addresses is None:
self._external_addresses = ExternalAddressesApi(api_client=self._cdp_client)
return self._external_addresses

@property
def transfers(self) -> TransfersApi:
"""Get the TransfersApi client instance.
Returns:
TransfersApi: The TransfersApi client instance.
Note:
This property lazily initializes the TransfersApi client on first access.
"""
if self._transfers is None:
self._transfers = TransfersApi(api_client=self._cdp_client)
return self._transfers

@property
def networks(self) -> NetworksApi:
"""Get the NetworksApi client instance.
Returns:
NetworksApi: The NetworksApi client instance.
Note:
This property lazily initializes the NetworksApi client on first access.
"""
if self._networks is None:
self._networks = NetworksApi(api_client=self._cdp_client)
return self._networks

@property
def assets(self) -> AssetsApi:
"""Get the AssetsApi client instance.
Returns:
AssetsApi: The AssetsApi client instance.
Note:
This property lazily initializes the AssetsApi client on first access.
"""
if self._assets is None:
self._assets = AssetsApi(api_client=self._cdp_client)
return self._assets

@property
def trades(self) -> TradesApi:
"""Get the TradesApi client instance.
Returns:
TradesApi: The TradesApi client instance.
Note:
This property lazily initializes the TradesApi client on first access.
"""
if self._trades is None:
self._trades = TradesApi(api_client=self._cdp_client)
return self._trades
Loading

0 comments on commit ddf9f90

Please sign in to comment.