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

feat: update agent addresses #173

Merged
merged 28 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
8addd59
feat: update agent addresses
Alejandro-Morales Sep 27, 2023
3426585
Merge branch 'main' into feat/update-agent-addresses
Alejandro-Morales Sep 28, 2023
9b5a06d
fix: tests
Alejandro-Morales Sep 28, 2023
f5269d3
fix: run black
Alejandro-Morales Sep 28, 2023
7e62da3
fix: pylint
Alejandro-Morales Sep 29, 2023
3494270
fix: pylint
Alejandro-Morales Sep 29, 2023
67d4634
fix: variable name
Alejandro-Morales Sep 29, 2023
61220ce
feat: added agent ledger and balance properties
Alejandro-Morales Sep 29, 2023
f85a553
feat: added network_address to agent
Alejandro-Morales Oct 2, 2023
28bf7c9
fix: minor tests fix
Alejandro-Morales Oct 2, 2023
474e9b8
feat: changed name to agent.identifier
Alejandro-Morales Oct 2, 2023
a2c9ce3
fix: black
Alejandro-Morales Oct 2, 2023
32d5e61
feat: extract address from destination
Alejandro-Morales Oct 2, 2023
b7c9155
fix: extract_agent_address
Alejandro-Morales Oct 3, 2023
fe97400
fix: minor fix
Alejandro-Morales Oct 3, 2023
24d1acf
fix: pylint
Alejandro-Morales Oct 3, 2023
0261661
fis: revert fund_agent to original
Alejandro-Morales Oct 3, 2023
6ca7bbf
simplify exmaple 13
Alejandro-Morales Oct 3, 2023
f37dcea
fix: unused import
Alejandro-Morales Oct 4, 2023
c3160f2
feat: update name service resolver
Alejandro-Morales Oct 4, 2023
96df1d9
fix: minor things
Alejandro-Morales Oct 4, 2023
83418ca
fix: tests
Alejandro-Morales Oct 4, 2023
faa7577
feat: update resolver logic
Alejandro-Morales Oct 4, 2023
1a2bf8c
run black
Alejandro-Morales Oct 4, 2023
830e85c
run pylint
Alejandro-Morales Oct 4, 2023
23fe6f8
chore: update identifier parsing function and add name test
jrriehl Oct 5, 2023
8a8a4a2
Merge branch 'main' into feat/update-agent-addresses
jrriehl Oct 6, 2023
d303b64
chore: update comments
jrriehl Oct 6, 2023
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: 1 addition & 1 deletion python/examples/06-send-tokens/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ async def request_funds(ctx: Context):
@alice.on_message(model=TransactionInfo)
async def confirm_transaction(ctx: Context, sender: str, msg: TransactionInfo):
ctx.logger.info(f"Received transaction info from {sender}: {msg}")
tx_resp = await wait_for_tx_to_complete(msg.tx_hash)
tx_resp = await wait_for_tx_to_complete(msg.tx_hash, ctx.ledger)

coin_received = tx_resp.events["coin_received"]
if (
Expand Down
4 changes: 3 additions & 1 deletion python/examples/08-local-network-interaction/agent2.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ class Message(Model):
message: str


RECIPIENT_ADDRESS = "agent1q2kxet3vh0scsf0sm7y2erzz33cve6tv5uk63x64upw5g68kr0chkv7hw50"
RECIPIENT_ADDRESS = (
"test-agent://agent1q2kxet3vh0scsf0sm7y2erzz33cve6tv5uk63x64upw5g68kr0chkv7hw50"
)

alice = Agent(
name="alice",
Expand Down
4 changes: 3 additions & 1 deletion python/examples/09-booking-protocol-demo/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
from uagents.setup import fund_agent_if_low


RESTAURANT_ADDRESS = "agent1qfpqn9jhvp9cg33f27q6jvmuv52dgyg9rfuu37rmxrletlqe7lewwjed5gy"
RESTAURANT_ADDRESS = (
"test-agent://agent1qfpqn9jhvp9cg33f27q6jvmuv52dgyg9rfuu37rmxrletlqe7lewwjed5gy"
)

user = Agent(
name="user",
Expand Down
4 changes: 3 additions & 1 deletion python/examples/10-cleaning-demo/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@
from uagents.setup import fund_agent_if_low


CLEANER_ADDRESS = "agent1qdfdx6952trs028fxyug7elgcktam9f896ays6u9art4uaf75hwy2j9m87w"
CLEANER_ADDRESS = (
"test-agent://agent1qdfdx6952trs028fxyug7elgcktam9f896ays6u9art4uaf75hwy2j9m87w"
)

user = Agent(
name="user",
Expand Down
9 changes: 5 additions & 4 deletions python/examples/13-agent-name-service/agent1.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from cosmpy.aerial.wallet import LocalWallet

from uagents.network import get_ledger, get_name_service_contract
from uagents.network import get_name_service_contract
from uagents.setup import fund_agent_if_low
from uagents import Agent, Context, Model

Expand All @@ -19,19 +19,20 @@ class Message(Model):
endpoint=["http://localhost:8001/submit"],
)

ledger = get_ledger()

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


for wallet in [my_wallet, bob.wallet]:
fund_agent_if_low(wallet.address())


@bob.on_event("startup")
async def register_agent_name(ctx: Context):
await name_service_contract.register(
ledger, my_wallet, ctx.address, ctx.name, DOMAIN
bob.ledger, my_wallet, ctx.address, ctx.name, DOMAIN
)


Expand Down
1 change: 0 additions & 1 deletion python/examples/13-agent-name-service/agent2.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ class Message(Model):
endpoint=["http://localhost:8000/submit"],
)


fund_agent_if_low(alice.wallet.address())


Expand Down
53 changes: 45 additions & 8 deletions python/src/uagents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

from cosmpy.aerial.wallet import LocalWallet, PrivateKey
from cosmpy.crypto.address import Address
from cosmpy.aerial.client import LedgerClient

from uagents.asgi import ASGIServer
from uagents.context import (
Expand All @@ -34,6 +35,8 @@
REGISTRATION_UPDATE_INTERVAL_SECONDS,
LEDGER_PREFIX,
REGISTRATION_RETRY_INTERVAL_SECONDS,
TESTNET_PREFIX,
MAINNET_PREFIX,
parse_endpoint_config,
parse_agentverse_config,
get_logger,
Expand Down Expand Up @@ -125,10 +128,12 @@ 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.

Properties:
name (str): The name of the agent.
address (str): The address of the agent used for communication.
identifier (str): The Agent Identifier, including network prefix and address.
wallet (LocalWallet): The agent's wallet for transacting on the ledger.
storage (KeyValueStore): The key-value store for storage operations.
mailbox (Dict[str, str]): The mailbox configuration for the agent (deprecated and replaced
Expand All @@ -151,6 +156,7 @@ def __init__(
resolve: Optional[Resolver] = None,
max_resolver_endpoints: Optional[int] = None,
version: Optional[str] = None,
test: Optional[bool] = True,
):
"""
Initialize an Agent instance.
Expand Down Expand Up @@ -210,8 +216,8 @@ def __init__(
else:
self._mailbox_client = None

self._ledger = get_ledger()
self._almanac_contract = get_almanac_contract()
self._ledger = get_ledger(test)
self._almanac_contract = get_almanac_contract(test)
self._storage = KeyValueStore(self.address[0:16])
self._interval_handlers: List[Tuple[IntervalCallback, float]] = []
self._interval_messages: Set[str] = set()
Expand All @@ -224,6 +230,7 @@ def __init__(
self._message_queue = asyncio.Queue()
self._on_startup = []
self._on_shutdown = []
self._test = test
self._version = version or "0.1.0"

# initialize the internal agent protocol
Expand All @@ -234,6 +241,7 @@ def __init__(

self._ctx = Context(
self._identity.address,
self.identifier,
self._name,
self._storage,
self._resolver,
Expand Down Expand Up @@ -308,6 +316,17 @@ def address(self) -> str:
"""
return self._identity.address

@property
def identifier(self) -> str:
"""
Get the Agent Identifier, including network prefix and address.

Returns:
str: The agent's identifier.
"""
prefix = TESTNET_PREFIX if self._test else MAINNET_PREFIX
return prefix + self._identity.address

@property
def wallet(self) -> LocalWallet:
"""
Expand All @@ -318,6 +337,16 @@ def wallet(self) -> LocalWallet:
"""
return self._wallet

@property
def ledger(self) -> LedgerClient:
"""
Get the ledger of the agent.

Returns:
LedgerClient: The agent's ledger
"""
return self._ledger

@property
def storage(self) -> KeyValueStore:
"""
Expand Down Expand Up @@ -358,6 +387,17 @@ def mailbox_client(self) -> MailboxClient:
"""
return self._mailbox_client

@property
def balance(self) -> int:
"""
Get the balance of the agent.

Returns:
int: Bank balance.
"""

return self.ledger.query_bank_balance(Address(self.wallet.address()))

@mailbox.setter
def mailbox(self, config: Union[str, Dict[str, str]]):
"""
Expand Down Expand Up @@ -472,11 +512,7 @@ async def register(self):
or list(self.protocols.keys())
!= self._almanac_contract.get_protocols(self.address)
):
agent_balance = self._ledger.query_bank_balance(
Address(self.wallet.address())
)

if agent_balance < REGISTRATION_FEE:
if self.balance < REGISTRATION_FEE:
self._logger.warning(
f"I do not have enough funds to register on Almanac contract\
\nFund using wallet address: {self.wallet.address()}"
Expand All @@ -485,7 +521,7 @@ async def register(self):
self._logger.info("Registering on almanac contract...")
signature = self.sign_registration()
await self._almanac_contract.register(
self._ledger,
self.ledger,
self.wallet,
self.address,
list(self.protocols.keys()),
Expand Down Expand Up @@ -806,6 +842,7 @@ async def _process_message_queue(self):

context = Context(
self._identity.address,
self.identifier,
self._name,
self._storage,
self._resolver,
Expand Down
24 changes: 15 additions & 9 deletions python/src/uagents/config.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
import logging
import sys
from enum import Enum


from typing import Any, Dict, List, Optional, Union

from uvicorn.logging import DefaultFormatter

logging.basicConfig(level=logging.INFO)


class AgentNetwork(Enum):
FETCHAI_TESTNET = 1
FETCHAI_MAINNET = 2


AGENT_PREFIX = "agent"
LEDGER_PREFIX = "fetch"
USER_PREFIX = "user"
CONTRACT_ALMANAC = "fetch1tjagw8g8nn4cwuw00cf0m5tl4l6wfw9c0ue507fhx9e3yrsck8zs0l3q4w"
CONTRACT_NAME_SERVICE = (
TESTNET_PREFIX = "test-agent"
MAINNET_PREFIX = "agent"

MAINNET_CONTRACT_ALMANAC = (
"fetch1mezzhfj7qgveewzwzdk6lz5sae4dunpmmsjr9u7z0tpmdsae8zmquq3y0y"
)
TESTNET_CONTRACT_ALMANAC = (
"fetch1tjagw8g8nn4cwuw00cf0m5tl4l6wfw9c0ue507fhx9e3yrsck8zs0l3q4w"
)
MAINNET_CONTRACT_NAME_SERVICE = (
"fetch1479lwv5vy8skute5cycuz727e55spkhxut0valrcm38x9caa2x8q99ef0q"
)
TESTNET_CONTRACT_NAME_SERVICE = (
"fetch1mxz8kn3l5ksaftx8a9pj9a6prpzk2uhxnqdkwuqvuh37tw80xu6qges77l"
)
REGISTRATION_FEE = 500000000000000000
REGISTRATION_DENOM = "atestfet"
REGISTRATION_UPDATE_INTERVAL_SECONDS = 3600
REGISTRATION_RETRY_INTERVAL_SECONDS = 60
AVERAGE_BLOCK_INTERVAL = 5.7
AGENT_NETWORK = AgentNetwork.FETCHAI_TESTNET

AGENTVERSE_URL = "https://agentverse.ai"
ALMANAC_API_URL = AGENTVERSE_URL + "/v1/almanac/"
Expand Down
Loading
Loading