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

chore(core): update network and prefix properties #614

Merged
merged 2 commits into from
Jan 24, 2025
Merged
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
7 changes: 4 additions & 3 deletions python/docs/api/uagents/agent.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,8 @@ An agent that interacts within a communication environment.
- `protocols` _Dict[str, Protocol]_ - Dictionary mapping all supported protocol digests to their
corresponding protocols.
- `_ctx` _Context_ - The context for agent interactions.
- `_test` _bool_ - True if the agent will register and transact on the testnet.
- `_network` _str_ - The network to use for the agent ('mainnet' or 'testnet').
- `_prefix` _str_ - The address prefix for the agent (determined by the network).
- `_enable_agent_inspector` _bool_ - Enable the agent inspector REST endpoints.
- `_metadata` _Dict[str, Any]_ - Metadata associated with the agent.
- `_readme` _Optional[str]_ - The agent's README file.
Expand Down Expand Up @@ -194,7 +195,7 @@ def __init__(name: Optional[str] = None,
wallet_key_derivation_index: Optional[int] = 0,
max_resolver_endpoints: Optional[int] = None,
version: Optional[str] = None,
test: bool = True,
network: AgentNetwork = "testnet",
loop: Optional[asyncio.AbstractEventLoop] = None,
log_level: Union[int, str] = logging.INFO,
enable_agent_inspector: bool = True,
Expand Down Expand Up @@ -222,7 +223,7 @@ Initialize an Agent instance.
- `wallet_key_derivation_index` _Optional[int]_ - The index used for deriving the wallet key.
- `max_resolver_endpoints` _Optional[int]_ - The maximum number of endpoints to resolve.
- `version` _Optional[str]_ - The version of the agent.
- `test` _Optional[bool]_ - True if the agent will register and transact on the testnet.
- `network` _Literal["mainnet", "testnet"]_ - The network to use for the agent.
- `loop` _Optional[asyncio.AbstractEventLoop]_ - The asyncio event loop to use.
- `log_level` _Union[int, str]_ - The logging level for the agent.
- `enable_agent_inspector` _bool_ - Enable the agent inspector for debugging.
Expand Down
18 changes: 10 additions & 8 deletions python/docs/api/uagents/network.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ Raised when an agent has insufficient funds for a transaction.
#### get`_`ledger

```python
def get_ledger(test: bool = True) -> LedgerClient
def get_ledger(network: AgentNetwork = "testnet") -> LedgerClient
```

Get the Ledger client.

**Arguments**:

- `test` _bool_ - Whether to use the testnet or mainnet. Defaults to True.
- `network` _AgentNetwork, optional_ - The network to use. Defaults to "testnet".


**Returns**:
Expand Down Expand Up @@ -356,14 +356,15 @@ Get the agent's sequence number for Almanac registration.
#### get`_`almanac`_`contract

```python
def get_almanac_contract(test: bool = True) -> Optional[AlmanacContract]
def get_almanac_contract(
network: AgentNetwork = "testnet") -> Optional[AlmanacContract]
```

Get the AlmanacContract instance.

**Arguments**:

- `test` _bool_ - Whether to use the testnet or mainnet. Defaults to True.
- `network` _AgentNetwork_ - The network to use. Defaults to "testnet".


**Returns**:
Expand Down Expand Up @@ -406,7 +407,7 @@ Execute a query with additional checks and error handling.

**Raises**:

- `RuntimeError` - If the contract address is not set or the query fails.
- `ValueError` - If the response from contract is not a dict.

<a id="src.uagents.network.NameServiceContract.is_name_available"></a>

Expand Down Expand Up @@ -495,8 +496,8 @@ Retrieve the previous records for a given name within a specified domain.

```python
def get_registration_tx(name: str, wallet_address: Address,
agent_records: Union[List[Dict[str, Any]],
str], domain: str, test: bool)
agent_records: Union[List[Dict[str, Any]], str],
domain: str, network: AgentNetwork)
```

Get the registration transaction for registering a name within a domain.
Expand Down Expand Up @@ -563,7 +564,8 @@ Unregister a name within a domain using the NameService contract.
#### get`_`name`_`service`_`contract

```python
def get_name_service_contract(test: bool = True) -> NameServiceContract
def get_name_service_contract(
network: AgentNetwork = "testnet") -> NameServiceContract
```

Get the NameServiceContract instance.
Expand Down
8 changes: 5 additions & 3 deletions python/docs/api/uagents/resolver.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ Parse an agent identifier string into prefix, name, and address.
#### query`_`record

```python
def query_record(agent_address: str, service: str, test: bool) -> dict
def query_record(agent_address: str, service: str,
network: AgentNetwork) -> dict
```

Query a record from the Almanac contract.
Expand All @@ -102,6 +103,7 @@ Query a record from the Almanac contract.

- `agent_address` _str_ - The address of the agent.
- `service` _str_ - The type of service to query.
- `network` _AgentNetwork_ - The network to query (mainnet or testnet).


**Returns**:
Expand All @@ -113,15 +115,15 @@ Query a record from the Almanac contract.
#### get`_`agent`_`address

```python
def get_agent_address(name: str, test: bool) -> Optional[str]
def get_agent_address(name: str, network: AgentNetwork) -> Optional[str]
```

Get the agent address associated with the provided name from the name service contract.

**Arguments**:

- `name` _str_ - The name to query.
- `test` _bool_ - Whether to use the testnet or mainnet contract.
- `network` _AgentNetwork_ - The network to query (mainnet or testnet).


**Returns**:
Expand Down
14 changes: 14 additions & 0 deletions python/docs/api/uagents/utils.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,17 @@ Log a message with the given logger and level.
- `level` _int_ - The logging level.
- `message` _str_ - The message to log.

<a id="src.uagents.utils.set_global_log_level"></a>

#### set`_`global`_`log`_`level

```python
def set_global_log_level(level: Union[int, str])
```

Set the log level for all modules globally. Can still be overruled manually.

**Arguments**:

- `level` _Union[int, str]_ - The logging level as defined in _logging_.

2 changes: 1 addition & 1 deletion python/examples/13-agent-name-service/agent1.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class Message(Model):


my_wallet = LocalWallet.from_unsafe_seed("registration test wallet")
name_service_contract = get_name_service_contract(test=True)
name_service_contract = get_name_service_contract()
faucet = get_faucet()
DOMAIN = "example.agent"

Expand Down
31 changes: 18 additions & 13 deletions python/src/uagents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,11 @@
from uagents.resolver import GlobalResolver, Resolver
from uagents.storage import KeyValueStore, get_or_create_private_keys
from uagents.types import (
AddressPrefix,
AgentEndpoint,
AgentInfo,
AgentMetadata,
AgentNetwork,
EventCallback,
IntervalCallback,
JsonStr,
Expand Down Expand Up @@ -249,7 +251,8 @@ class Agent(Sink):
protocols (Dict[str, Protocol]): Dictionary mapping all supported protocol digests to their
corresponding protocols.
_ctx (Context): The context for agent interactions.
_test (bool): True if the agent will register and transact on the testnet.
_network (str): The network to use for the agent ('mainnet' or 'testnet').
_prefix (str): The address prefix for the agent (determined by the network).
_enable_agent_inspector (bool): Enable the agent inspector REST endpoints.
_metadata (Dict[str, Any]): Metadata associated with the agent.
_readme (Optional[str]): The agent's README file.
Expand Down Expand Up @@ -284,7 +287,7 @@ def __init__(
wallet_key_derivation_index: Optional[int] = 0,
max_resolver_endpoints: Optional[int] = None,
version: Optional[str] = None,
test: bool = True,
network: AgentNetwork = "testnet",
loop: Optional[asyncio.AbstractEventLoop] = None,
log_level: Union[int, str] = logging.INFO,
enable_agent_inspector: bool = True,
Expand All @@ -311,7 +314,7 @@ def __init__(
wallet_key_derivation_index (Optional[int]): The index used for deriving the wallet key.
max_resolver_endpoints (Optional[int]): The maximum number of endpoints to resolve.
version (Optional[str]): The version of the agent.
test (Optional[bool]): True if the agent will register and transact on the testnet.
network (Literal["mainnet", "testnet"]): The network to use for the agent.
loop (Optional[asyncio.AbstractEventLoop]): The asyncio event loop to use.
log_level (Union[int, str]): The logging level for the agent.
enable_agent_inspector (bool): Enable the agent inspector for debugging.
Expand Down Expand Up @@ -354,8 +357,8 @@ def __init__(
almanac_api_url=self._almanac_api_url,
)

self._ledger = get_ledger(test)
self._almanac_contract = get_almanac_contract(test)
self._ledger = get_ledger(network)
self._almanac_contract = get_almanac_contract(network)
self._storage = KeyValueStore(self.address[0:16])
self._interval_handlers: List[Tuple[IntervalCallback, float]] = []
self._interval_messages: Set[str] = set()
Expand All @@ -371,7 +374,10 @@ def __init__(
self._message_queue = asyncio.Queue()
self._on_startup = []
self._on_shutdown = []
self._test = test
self._network = network
self._prefix: AddressPrefix = (
MAINNET_PREFIX if network == "mainnet" else TESTNET_PREFIX
)
self._version = version or "0.1.0"
self._registration_policy = registration_policy or None

Expand All @@ -380,7 +386,7 @@ def __init__(
self._ledger,
self._wallet,
self._almanac_contract,
self._test,
self._network == "testnet",
almanac_api=self._almanac_api_url,
)
self._metadata = self._initialize_metadata(metadata)
Expand Down Expand Up @@ -424,7 +430,7 @@ async def _handle_error_message(ctx: Context, sender: str, msg: ErrorMessage):
async def _handle_get_info(_ctx: Context):
return AgentInfo(
address=self.address,
prefix=TESTNET_PREFIX if self._test else MAINNET_PREFIX,
prefix=self._prefix,
endpoints=self._endpoints,
protocols=list(self.protocols.keys()),
)
Expand All @@ -450,7 +456,7 @@ async def _handle_connect(_ctx: Context, request: AgentverseConnectRequest):
return await register_in_agentverse(
request,
self._identity,
TESTNET_PREFIX if self._test else MAINNET_PREFIX,
self._prefix,
self._agentverse,
agent_details,
)
Expand Down Expand Up @@ -601,8 +607,7 @@ def identifier(self) -> str:
Returns:
str: The agent's identifier.
"""
prefix = TESTNET_PREFIX if self._test else MAINNET_PREFIX
return prefix + "://" + self._identity.address
return self._prefix + "://" + self._identity.address

@property
def wallet(self) -> LocalWallet:
Expand Down Expand Up @@ -675,7 +680,7 @@ def info(self) -> AgentInfo:
"""
return AgentInfo(
address=self.address,
prefix=TESTNET_PREFIX if self._test else MAINNET_PREFIX,
prefix=self._prefix,
endpoints=self._endpoints,
protocols=list(self.protocols.keys()),
metadata=self.metadata,
Expand Down Expand Up @@ -1508,7 +1513,7 @@ def _update_agent(self, agent: Agent):
agent._ledger,
agent._wallet,
agent._almanac_contract,
agent._test,
agent._network == "testnet",
logger=agent._logger,
)

Expand Down
Loading
Loading