Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[wip] feat: Add reputation score #64

Open
wants to merge 5 commits into
base: v0.13.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# CDP Python SDK Changelog

## Unreleased
- Add support for fetching address reputation
- Add `reputation` method to `Address` to fetch the reputation of the address.

### [0.12.1] - 2024-12-10

Expand Down
18 changes: 18 additions & 0 deletions cdp/address.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from collections.abc import Iterator
from decimal import Decimal

from cdp.address_reputation import AddressReputation
from cdp.asset import Asset
from cdp.balance import Balance
from cdp.balance_map import BalanceMap
Expand All @@ -23,6 +24,7 @@ def __init__(self, network_id: str, address_id: str) -> None:
"""
self._network_id = network_id
self._id = address_id
self._reputation: AddressReputation | None = None

@property
def address_id(self) -> str:
Expand Down Expand Up @@ -133,6 +135,22 @@ def transactions(self) -> Iterator[Transaction]:
"""
return Transaction.list(network_id=self.network_id, address_id=self.address_id)

def reputation(self) -> AddressReputation:
"""Get the reputation of the address.

Returns:
AddressReputation: The reputation of the address.

"""
if self._reputation is not None:
return self._reputation

response = Cdp.api_clients.reputation.get_address_reputation(
network_id=self.network_id, address_id=self.address_id
)
self._reputation = AddressReputation(response)
return self._reputation

def __str__(self) -> str:
"""Return a string representation of the Address."""
return f"Address: (address_id: {self.address_id}, network_id: {self.network_id})"
Expand Down
38 changes: 38 additions & 0 deletions cdp/address_reputation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from cdp.client import AddressReputationMetadata
from cdp.client.models.address_reputation import AddressReputation as AddressReputationModel


class AddressReputation:
"""A representation of the reputation of a blockchain address."""

def __init__(self, model: AddressReputationModel) -> None:
"""Initialize the AddressReputation class."""
if not model:
raise ValueError("model is required")

self._score = model.score
self._metadata = model.metadata

@property
def metadata(self) -> AddressReputationMetadata:
"""Return the metadata of the address."""
return self._metadata

@property
def score(self) -> int:
"""Return the score of the address."""
return self._score

@property
def risky(self) -> bool:
"""Return whether the address is risky."""
return self.score < 0

def __str__(self) -> str:
"""Return a string representation of the AddressReputation."""
metadata = ", ".join(f"{key}={getattr(self.metadata, key)}" for key in vars(self.metadata))
return f"Address Reputation: (score={self.score}, metadata=({metadata}))"

def __repr__(self) -> str:
"""Return a string representation of the AddressReputation."""
return str(self)
17 changes: 17 additions & 0 deletions cdp/api_clients.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from cdp.cdp_api_client import CdpApiClient
from cdp.client import ReputationApi
from cdp.client.api.addresses_api import AddressesApi
from cdp.client.api.assets_api import AssetsApi
from cdp.client.api.balance_history_api import BalanceHistoryApi
Expand Down Expand Up @@ -55,6 +56,7 @@ def __init__(self, cdp_client: CdpApiClient) -> None:
self._balance_history: BalanceHistoryApi | None = None
self._transaction_history: TransactionHistoryApi | None = None
self._fund: FundApi | None = None
self._reputation: ReputationApi | None = None

@property
def wallets(self) -> WalletsApi:
Expand Down Expand Up @@ -250,3 +252,18 @@ def fund(self) -> FundApi:
if self._fund is None:
self._fund = FundApi(api_client=self._cdp_client)
return self._fund

@property
def reputation(self) -> ReputationApi:
"""Get the ReputationApi client instance.

Returns:
ReputationApi: The ReputationApi client instance.

Note:
This property lazily initializes the ReputationApi client on first access.

"""
if self._reputation is None:
self._reputation = ReputationApi(api_client=self._cdp_client)
return self._reputation
3 changes: 1 addition & 2 deletions cdp/client/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,12 @@
from cdp.client.exceptions import ApiException

# import models into sdk package
from cdp.client.models.abi import ABI
from cdp.client.models.address import Address
from cdp.client.models.address_balance_list import AddressBalanceList
from cdp.client.models.address_historical_balance_list import AddressHistoricalBalanceList
from cdp.client.models.address_list import AddressList
from cdp.client.models.address_reputation import AddressReputation
from cdp.client.models.address_reputation_metadata import AddressReputationMetadata
from cdp.client.models.address_risk import AddressRisk
from cdp.client.models.address_transaction_list import AddressTransactionList
from cdp.client.models.asset import Asset
from cdp.client.models.balance import Balance
Expand Down Expand Up @@ -116,6 +114,7 @@
from cdp.client.models.payload_signature import PayloadSignature
from cdp.client.models.payload_signature_list import PayloadSignatureList
from cdp.client.models.read_contract_request import ReadContractRequest
from cdp.client.models.register_smart_contract_request import RegisterSmartContractRequest
from cdp.client.models.seed_creation_event import SeedCreationEvent
from cdp.client.models.seed_creation_event_result import SeedCreationEventResult
from cdp.client.models.server_signer import ServerSigner
Expand Down
15 changes: 15 additions & 0 deletions cdp/client/api/addresses_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,7 @@ def _create_address_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey'
]

return self.api_client.param_serialize(
Expand Down Expand Up @@ -618,6 +619,7 @@ def _create_payload_signature_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey'
]

return self.api_client.param_serialize(
Expand Down Expand Up @@ -893,6 +895,8 @@ def _get_address_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down Expand Up @@ -1183,6 +1187,8 @@ def _get_address_balance_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down Expand Up @@ -1473,6 +1479,8 @@ def _get_payload_signature_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down Expand Up @@ -1765,6 +1773,8 @@ def _list_address_balances_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down Expand Up @@ -2059,6 +2069,8 @@ def _list_addresses_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down Expand Up @@ -2368,6 +2380,8 @@ def _list_payload_signatures_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down Expand Up @@ -2663,6 +2677,7 @@ def _request_faucet_funds_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey'
]

return self.api_client.param_serialize(
Expand Down
2 changes: 2 additions & 0 deletions cdp/client/api/assets_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,6 +293,8 @@ def _get_asset_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down
2 changes: 2 additions & 0 deletions cdp/client/api/balance_history_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -343,6 +343,8 @@ def _list_address_historical_balance_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down
2 changes: 2 additions & 0 deletions cdp/client/api/contract_events_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -396,6 +396,8 @@ def _list_contract_events_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down
6 changes: 6 additions & 0 deletions cdp/client/api/contract_invocations_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -340,6 +340,7 @@ def _broadcast_contract_invocation_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey'
]

return self.api_client.param_serialize(
Expand Down Expand Up @@ -643,6 +644,7 @@ def _create_contract_invocation_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey'
]

return self.api_client.param_serialize(
Expand Down Expand Up @@ -933,6 +935,8 @@ def _get_contract_invocation_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down Expand Up @@ -1242,6 +1246,8 @@ def _list_contract_invocations_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down
8 changes: 8 additions & 0 deletions cdp/client/api/external_addresses_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,8 @@ def _get_external_address_balance_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down Expand Up @@ -601,6 +603,8 @@ def _get_faucet_transaction_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down Expand Up @@ -893,6 +897,8 @@ def _list_external_address_balances_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down Expand Up @@ -1202,6 +1208,8 @@ def _request_external_faucet_funds_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down
6 changes: 6 additions & 0 deletions cdp/client/api/fund_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -326,6 +326,7 @@ def _create_fund_operation_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey'
]

return self.api_client.param_serialize(
Expand Down Expand Up @@ -629,6 +630,7 @@ def _create_fund_quote_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey'
]

return self.api_client.param_serialize(
Expand Down Expand Up @@ -919,6 +921,8 @@ def _get_fund_operation_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down Expand Up @@ -1228,6 +1232,8 @@ def _list_fund_operations_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down
4 changes: 4 additions & 0 deletions cdp/client/api/mpc_wallet_stake_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,7 @@ def _broadcast_staking_operation_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey'
]

return self.api_client.param_serialize(
Expand Down Expand Up @@ -641,6 +642,7 @@ def _create_staking_operation_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey'
]

return self.api_client.param_serialize(
Expand Down Expand Up @@ -931,6 +933,8 @@ def _get_staking_operation_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down
2 changes: 2 additions & 0 deletions cdp/client/api/networks_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,8 @@ def _get_network_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down
2 changes: 2 additions & 0 deletions cdp/client/api/onchain_identity_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,8 @@ def _resolve_identity_by_address_serialize(

# authentication setting
_auth_settings: List[str] = [
'apiKey',
'session'
]

return self.api_client.param_serialize(
Expand Down
Loading
Loading