diff --git a/cdp-agentkit-core/python/cdp_agentkit_core/actions/__init__.py b/cdp-agentkit-core/python/cdp_agentkit_core/actions/__init__.py index 51a9ad40..42e26ebb 100644 --- a/cdp-agentkit-core/python/cdp_agentkit_core/actions/__init__.py +++ b/cdp-agentkit-core/python/cdp_agentkit_core/actions/__init__.py @@ -1,4 +1,5 @@ -from cdp_agentkit_core.actions.cdp_action import CdpAction +from cdp_agentkit_core.actions.cdp_action import CdpAction # noqa: I001 + from cdp_agentkit_core.actions.address_reputation import AddressReputationAction from cdp_agentkit_core.actions.deploy_nft import DeployNftAction from cdp_agentkit_core.actions.deploy_token import DeployTokenAction diff --git a/cdp-agentkit-core/python/cdp_agentkit_core/actions/address_reputation.py b/cdp-agentkit-core/python/cdp_agentkit_core/actions/address_reputation.py index 6ba79d51..a3eb4d0d 100644 --- a/cdp-agentkit-core/python/cdp_agentkit_core/actions/address_reputation.py +++ b/cdp-agentkit-core/python/cdp_agentkit_core/actions/address_reputation.py @@ -1,8 +1,8 @@ +import re from collections.abc import Callable from cdp import Address from pydantic import BaseModel, Field, field_validator -import re from cdp_agentkit_core.actions import CdpAction @@ -31,7 +31,20 @@ class AddressReputationInput(BaseModel): ) @field_validator("address") + @classmethod def validate_address(cls, v: str) -> str: + """Validate that the provided address is a valid Ethereum address. + + Args: + v (str): The address string to validate + + Returns: + str: The validated address string + + Raises: + ValueError: If the address format is invalid + + """ if not re.match(r"^0x[a-fA-F0-9]{40}$", v): raise ValueError("Invalid Ethereum address format") return v @@ -46,6 +59,7 @@ def check_address_reputation(address: str, network: str) -> str: Returns: str: A string containing the reputation json data or error message + """ try: address = Address(network, address) diff --git a/cdp-agentkit-core/python/tests/actions/test_address_reputation.py b/cdp-agentkit-core/python/tests/actions/test_address_reputation.py index 24e3af23..d72350a4 100644 --- a/cdp-agentkit-core/python/tests/actions/test_address_reputation.py +++ b/cdp-agentkit-core/python/tests/actions/test_address_reputation.py @@ -1,8 +1,12 @@ from unittest.mock import patch import pytest +from cdp.address_reputation import ( + AddressReputation, + AddressReputationMetadata, + AddressReputationModel, +) -from cdp.address_reputation import AddressReputation, AddressReputationMetadata, AddressReputationModel from cdp_agentkit_core.actions.address_reputation import ( AddressReputationAction, AddressReputationInput, @@ -47,9 +51,8 @@ def test_address_reputation_input_model_invalid_address(): ) -def test_address_reputation_success(address_factory): +def test_address_reputation_success(): """Test successful address reputation check.""" - # Create the model and reputation instances mock_model = AddressReputationModel( score=85, metadata=AddressReputationMetadata( @@ -67,27 +70,27 @@ def test_address_reputation_success(address_factory): ) mock_reputation = AddressReputation(model=mock_model) - with patch('cdp_agentkit_core.actions.address_reputation.Address') as MockAddress: - mock_address_instance = MockAddress.return_value + with patch('cdp_agentkit_core.actions.address_reputation.Address') as mock_address: + mock_address_instance = mock_address.return_value mock_address_instance.reputation.return_value = mock_reputation - + action_response = check_address_reputation(MOCK_ADDRESS, MOCK_NETWORK) expected_response = str(mock_reputation) - MockAddress.assert_called_once_with(MOCK_NETWORK, MOCK_ADDRESS) + mock_address.assert_called_once_with(MOCK_NETWORK, MOCK_ADDRESS) mock_address_instance.reputation.assert_called_once() assert action_response == expected_response -def test_address_reputation_failure(address_factory): +def test_address_reputation_failure(): """Test address reputation check failure.""" - with patch('cdp_agentkit_core.actions.address_reputation.Address') as MockAddress: - mock_address_instance = MockAddress.return_value + with patch('cdp_agentkit_core.actions.address_reputation.Address') as mock_address: + mock_address_instance = mock_address.return_value mock_address_instance.reputation.side_effect = Exception("API error") - + action_response = check_address_reputation(MOCK_ADDRESS, MOCK_NETWORK) expected_response = "Error checking address reputation: API error" - MockAddress.assert_called_once_with(MOCK_NETWORK, MOCK_ADDRESS) + mock_address.assert_called_once_with(MOCK_NETWORK, MOCK_ADDRESS) mock_address_instance.reputation.assert_called_once() assert action_response == expected_response diff --git a/cdp-agentkit-core/typescript/src/tests/address_reputation_test.ts b/cdp-agentkit-core/typescript/src/tests/address_reputation_test.ts index 81779198..cdae0620 100644 --- a/cdp-agentkit-core/typescript/src/tests/address_reputation_test.ts +++ b/cdp-agentkit-core/typescript/src/tests/address_reputation_test.ts @@ -1,11 +1,11 @@ -import { Wallet, Address } from "@coinbase/coinbase-sdk"; -import { AddressReputationAction, AddressReputationInput } from "../actions/cdp/address_reputation"; +import { Address } from "@coinbase/coinbase-sdk"; +import { AddressReputationAction } from "../actions/cdp/address_reputation"; const MOCK_ADDRESS = "0x1234567890123456789012345678901234567890"; const MOCK_NETWORK = "base-sepolia"; jest.mock("@coinbase/coinbase-sdk", () => ({ - Address: jest.fn() + Address: jest.fn(), })); describe("Address Reputation Input", () => { @@ -53,6 +53,7 @@ describe("Address Reputation Action", () => { }); it("should successfully check address reputation", async () => { + // TODO: ask John if there is a better way... const mockReputation = { score: 85, metadata: { @@ -65,12 +66,12 @@ describe("Address Reputation Action", () => { bridge_transactions_performed: 5, lend_borrow_stake_transactions: 10, ens_contract_interactions: 2, - smart_contract_deployments: 1 + smart_contract_deployments: 1, }, - toString: () => "Address Reputation: (score=85, metadata=(...))" - }; + // eslint-disable-next-line @typescript-eslint/no-explicit-any + } as unknown as jest.Mocked; - mockAddress.reputation.mockResolvedValue(mockReputation as any); + mockAddress.reputation.mockResolvedValue(mockReputation); const args = { network: MOCK_NETWORK, @@ -80,6 +81,7 @@ describe("Address Reputation Action", () => { const action = new AddressReputationAction(); const response = await action.func(args); + console.log(mockReputation.toString()); expect(response).toBe(mockReputation.toString()); });