Skip to content

Commit

Permalink
fix formatting
Browse files Browse the repository at this point in the history
  • Loading branch information
phdargen authored and 0xRAG committed Jan 16, 2025
1 parent 7fe0182 commit 0c40277
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 44 deletions.
5 changes: 5 additions & 0 deletions cdp-agentkit-core/python/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@

## Unreleased

### Added

- Added `get_balance_nft` action.
- Added `transfer_nft` action.

## [0.0.8] - 2025-01-13

### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
from cdp_agentkit_core.actions.wow.sell_token import WowSellTokenAction
from cdp_agentkit_core.actions.wrap_eth import WrapEthAction


# WARNING: All new CdpAction subclasses must be imported above, otherwise they will not be discovered
# by get_all_cdp_actions(). The import ensures the class is registered as a subclass of CdpAction.
def get_all_cdp_actions() -> list[type[CdpAction]]:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

from cdp_agentkit_core.actions import CdpAction


GET_BALANCE_NFT_PROMPT = """
This tool will get the NFTs (ERC721 tokens) owned by the wallet for a specific NFT contract.
Expand All @@ -15,17 +14,17 @@
- address: (Optional) The address to check NFT balance for. If not provided, uses the wallet's default address
"""


class GetBalanceNftInput(BaseModel):
"""Input argument schema for get NFT balance action."""
contract_address: str = Field(
...,
description="The NFT contract address to check balance for"
)

contract_address: str = Field(..., description="The NFT contract address to check balance for")
address: str | None = Field(
None,
description="The address to check NFT balance for. If not provided, uses the wallet's default address"
description="The address to check NFT balance for. If not provided, uses the wallet's default address",
)


def get_balance_nft(
wallet: Wallet,
contract_address: str,
Expand All @@ -40,17 +39,13 @@ def get_balance_nft(
Returns:
str: A message containing the NFT balance details.
"""
try:
check_address = address if address is not None else wallet.default_address.address_id

owned_tokens = SmartContract.read(
wallet.network_id,
contract_address,
"tokensOfOwner",
args={
"owner": check_address
}
wallet.network_id, contract_address, "tokensOfOwner", args={"owner": check_address}
)

if not owned_tokens:
Expand All @@ -62,6 +57,7 @@ def get_balance_nft(
except Exception as e:
return f"Error getting NFT balance for address {check_address} in contract {contract_address}: {e!s}"


class GetBalanceNftAction(CdpAction):
"""Get NFT balance action."""

Expand Down
33 changes: 14 additions & 19 deletions cdp-agentkit-core/python/cdp_agentkit_core/actions/transfer_nft.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,28 @@
- The wallet must either own the NFT or have approval to transfer it
"""


class TransferNftInput(BaseModel):
"""Input argument schema for NFT transfer action."""
contract_address: str = Field(
...,
description="The NFT contract address to interact with"
)
token_id: str = Field(
...,
description="The ID of the NFT to transfer"
)

contract_address: str = Field(..., description="The NFT contract address to interact with")
token_id: str = Field(..., description="The ID of the NFT to transfer")
destination: str = Field(
...,
description="The destination to transfer the NFT, e.g. `0x58dBecc0894Ab4C24F98a0e684c989eD07e4e027`, `example.eth`, `example.base.eth`"
description="The destination to transfer the NFT, e.g. `0x58dBecc0894Ab4C24F98a0e684c989eD07e4e027`, `example.eth`, `example.base.eth`",
)
from_address: str = Field(
default=None,
description="The address to transfer from. If not provided, defaults to the wallet's default address"
description="The address to transfer from. If not provided, defaults to the wallet's default address",
)


def transfer_nft(
wallet: Wallet,
contract_address: str,
token_id: str,
wallet: Wallet,
contract_address: str,
token_id: str,
destination: str,
from_address: str | None = None
from_address: str | None = None,
) -> str:
"""Transfer an NFT (ERC721 token) to a destination address.
Expand All @@ -56,23 +53,21 @@ def transfer_nft(
Returns:
str: A message containing the transfer details.
"""
try:
from_addr = from_address if from_address is not None else wallet.default_address.address_id
transfer_result = wallet.invoke_contract(
contract_address=contract_address,
method="transferFrom",
args={
"from": from_addr,
"to": destination,
"tokenId": token_id
}
args={"from": from_addr, "to": destination, "tokenId": token_id},
).wait()
except Exception as e:
return f"Error transferring the NFT (contract: {contract_address}, ID: {token_id}) from {from_addr} to {destination}): {e!s}"

return f"Transferred NFT (ID: {token_id}) from contract {contract_address} to {destination}.\nTransaction hash: {transfer_result.transaction_hash}\nTransaction link: {transfer_result.transaction_link}"


class TransferNftAction(CdpAction):
"""Transfer NFT action."""

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,9 @@ def test_get_balance_nft_no_tokens(wallet_factory):
MOCK_CONTRACT_ADDRESS,
)

expected_response = f"Address {MOCK_ADDRESS} owns no NFTs in contract {MOCK_CONTRACT_ADDRESS}"
expected_response = (
f"Address {MOCK_ADDRESS} owns no NFTs in contract {MOCK_CONTRACT_ADDRESS}"
)
assert action_response == expected_response


Expand Down
16 changes: 5 additions & 11 deletions cdp-agentkit-core/python/tests/actions/test_transfer_nft.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ def test_transfer_nft_success(wallet_factory, contract_invocation_factory):
"""Test successful NFT transfer with valid parameters."""
mock_wallet = wallet_factory()
mock_contract_invocation = contract_invocation_factory()

# Set the mock wallet address
mock_wallet.address = "0xdefaultAddress"

# Set the mock transaction properties
mock_contract_invocation.transaction_hash = "0xvalidTransactionHash"
mock_contract_invocation.transaction_link = "https://basescan.org/tx/0xvalidTransactionHash"
Expand All @@ -52,10 +52,7 @@ def test_transfer_nft_success(wallet_factory, contract_invocation_factory):
) as mock_contract_invocation_wait,
):
action_response = transfer_nft(
mock_wallet,
MOCK_CONTRACT_ADDRESS,
MOCK_TOKEN_ID,
MOCK_DESTINATION
mock_wallet, MOCK_CONTRACT_ADDRESS, MOCK_TOKEN_ID, MOCK_DESTINATION
)

expected_response = f"Transferred NFT (ID: {MOCK_TOKEN_ID}) from contract {MOCK_CONTRACT_ADDRESS} to {MOCK_DESTINATION}.\nTransaction hash: {mock_contract_invocation.transaction_hash}\nTransaction link: {mock_contract_invocation.transaction_link}"
Expand All @@ -81,10 +78,7 @@ def test_transfer_nft_api_error(wallet_factory):
mock_wallet, "invoke_contract", side_effect=Exception("API error")
) as mock_invoke_contract:
action_response = transfer_nft(
mock_wallet,
MOCK_CONTRACT_ADDRESS,
MOCK_TOKEN_ID,
MOCK_DESTINATION
mock_wallet, MOCK_CONTRACT_ADDRESS, MOCK_TOKEN_ID, MOCK_DESTINATION
)

expected_response = f"Error transferring the NFT (contract: {MOCK_CONTRACT_ADDRESS}, ID: {MOCK_TOKEN_ID}) from {mock_wallet.address} to {MOCK_DESTINATION}): API error"
Expand All @@ -98,4 +92,4 @@ def test_transfer_nft_api_error(wallet_factory):
"to": MOCK_DESTINATION,
"tokenId": MOCK_TOKEN_ID,
},
)
)

0 comments on commit 0c40277

Please sign in to comment.