Skip to content

feat: update agent addresses #173

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

Merged
merged 28 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from 7 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
8 changes: 5 additions & 3 deletions python/examples/06-send-tokens/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
from uagents.network import wait_for_tx_to_complete
from uagents.setup import fund_agent_if_low

# pylint: disable=protected-access


class PaymentRequest(Model):
wallet_address: str
Expand All @@ -19,8 +21,8 @@ class TransactionInfo(Model):
alice = Agent(name="alice", seed="alice secret phrase")
bob = Agent(name="bob", seed="bob secret phrase")

fund_agent_if_low(alice.wallet.address())
fund_agent_if_low(bob.wallet.address())
fund_agent_if_low(alice)
fund_agent_if_low(bob)


@alice.on_interval(period=10.0)
Expand All @@ -36,7 +38,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
2 changes: 1 addition & 1 deletion python/examples/08-local-network-interaction/agent1.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Message(Model):
endpoint=["http://127.0.0.1:8001/submit"],
)

fund_agent_if_low(bob.wallet.address())
fund_agent_if_low(bob)


@bob.on_message(model=Message)
Expand Down
6 changes: 4 additions & 2 deletions 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 All @@ -15,7 +17,7 @@ class Message(Model):
endpoint=["http://127.0.0.1:8000/submit"],
)

fund_agent_if_low(alice.wallet.address())
fund_agent_if_low(alice)


@alice.on_interval(period=2.0)
Expand Down
2 changes: 1 addition & 1 deletion python/examples/09-booking-protocol-demo/restaurant.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
},
)

fund_agent_if_low(restaurant.wallet.address())
fund_agent_if_low(restaurant)

# build the restaurant agent from stock protocols and publish their details
restaurant.include(query_proto, publish_manifest=True)
Expand Down
2 changes: 1 addition & 1 deletion python/examples/09-booking-protocol-demo/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
)

fund_agent_if_low(user.wallet.address())
fund_agent_if_low(user)

table_query = QueryTableRequest(
guests=3,
Expand Down
2 changes: 1 addition & 1 deletion python/examples/10-cleaning-demo/cleaner.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
)

fund_agent_if_low(cleaner.wallet.address())
fund_agent_if_low(cleaner)


# build the cleaning service agent from the cleaning protocol
Expand Down
2 changes: 1 addition & 1 deletion python/examples/10-cleaning-demo/user.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
},
)

fund_agent_if_low(user.wallet.address())
fund_agent_if_low(user)

request = ServiceRequest(
user=user.name,
Expand Down
2 changes: 1 addition & 1 deletion python/examples/11-mailbox-agents/alice.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class Message(Model):
mailbox=f"{API_KEY}@wss://agentverse.ai",
)

fund_agent_if_low(agent.wallet.address())
fund_agent_if_low(agent)


@agent.on_message(model=Message, replies={Message})
Expand Down
2 changes: 1 addition & 1 deletion python/examples/11-mailbox-agents/bob.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class Message(Model):
mailbox=f"{API_KEY}@wss://agentverse.ai",
)

fund_agent_if_low(agent.wallet.address())
fund_agent_if_low(agent)


@agent.on_interval(period=2.0)
Expand Down
2 changes: 1 addition & 1 deletion python/examples/12-remote-agents/agent1.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ class Message(Model):
endpoint=[f"{http_tunnel.public_url}/submit"],
)

fund_agent_if_low(alice.wallet.address())
fund_agent_if_low(alice)


@alice.on_message(model=Message)
Expand Down
2 changes: 1 addition & 1 deletion python/examples/12-remote-agents/agent2.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class Message(Model):
endpoint=["http://127.0.0.1:8001/submit"],
)

fund_agent_if_low(bob.wallet.address())
fund_agent_if_low(bob)

ALICE_ADDRESS = "agent1qv2l7qzcd2g2rcv2p93tqflrcaq5dk7c2xc7fcnfq3s37zgkhxjmq5mfyvz"

Expand Down
20 changes: 15 additions & 5 deletions python/examples/13-agent-name-service/agent1.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
from cosmpy.aerial.wallet import LocalWallet

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

from uagents.config import REGISTRATION_FEE


# NOTE: Run agent1.py before running agent2.py

# pylint: disable=protected-access


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

ledger = get_ledger()
fund_agent_if_low(bob)

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

for wallet in [my_wallet, bob.wallet]:
fund_agent_if_low(wallet.address())
faucet = get_faucet()
AGENT_BALANCE = bob._ledger.query_bank_balance(my_wallet)

if AGENT_BALANCE < REGISTRATION_FEE:
print("Adding funds to wallet...")
faucet.get_wealth(my_wallet)
print("Adding funds to wallet...complete")


@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[-65:], ctx.name, DOMAIN
)


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


fund_agent_if_low(alice.wallet.address())
fund_agent_if_low(alice)


@alice.on_interval(period=5)
Expand Down
6 changes: 3 additions & 3 deletions python/examples/14-broadcast/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
bob = Agent(name="bob", seed="bob recovery phrase")
charles = Agent(name="charles", seed="charles recovery phrase")

fund_agent_if_low(alice.wallet.address())
fund_agent_if_low(bob.wallet.address())
fund_agent_if_low(charles.wallet.address())
fund_agent_if_low(alice)
fund_agent_if_low(bob)
fund_agent_if_low(charles)


class BroadcastExampleRequest(Model):
Expand Down
32 changes: 18 additions & 14 deletions python/src/uagents/agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,7 @@ class Agent(Sink):
def __init__(
self,
name: Optional[str] = None,
test: Optional[bool] = True,
port: Optional[int] = None,
seed: Optional[str] = None,
endpoint: Optional[Union[str, List[str], Dict[str, dict]]] = None,
Expand All @@ -167,6 +168,7 @@ def __init__(
version (Optional[str]): The version of the agent.
"""
self._name = name
self._test = test
self._port = port if port is not None else 8000
self._background_tasks: Set[asyncio.Task] = set()
self._resolver = (
Expand Down Expand Up @@ -210,9 +212,9 @@ def __init__(
else:
self._mailbox_client = None

self._ledger = get_ledger()
self._almanac_contract = get_almanac_contract()
self._storage = KeyValueStore(self.address[0:16])
self._ledger = get_ledger(self._test)
self._almanac_contract = get_almanac_contract(self._test)
self._storage = KeyValueStore(self._identity.address[0:16])
self._interval_handlers: List[Tuple[IntervalCallback, float]] = []
self._interval_messages: Set[str] = set()
self._signed_message_handlers: Dict[str, MessageCallback] = {}
Expand All @@ -233,7 +235,7 @@ def __init__(
self.protocols: Dict[str, Protocol] = {}

self._ctx = Context(
self._identity.address,
self.address,
self._name,
self._storage,
self._resolver,
Expand All @@ -248,7 +250,7 @@ def __init__(
)

# register with the dispatcher
self._dispatcher.register(self.address, self)
self._dispatcher.register(self._identity.address, self)

if not self._use_mailbox:
self._server = ASGIServer(
Expand Down Expand Up @@ -281,7 +283,7 @@ def _initialize_wallet_and_identity(self, seed, name):
prefix=LEDGER_PREFIX,
)
if name is None:
self._name = self.address[0:16]
self._name = self._identity.address[0:16]

@property
def name(self) -> str:
Expand All @@ -301,7 +303,8 @@ def address(self) -> str:
Returns:
str: The agent's address.
"""
return self._identity.address
prefix = "test-agent://" if self._test else "agent://"
return prefix + self._identity.address

@property
def wallet(self) -> LocalWallet:
Expand Down Expand Up @@ -413,7 +416,7 @@ def sign_registration(self) -> str:
assert self._almanac_contract.address is not None
return self._identity.sign_registration(
str(self._almanac_contract.address),
self._almanac_contract.get_sequence(self.address),
self._almanac_contract.get_sequence(self._identity.address),
)

def update_endpoints(self, endpoints: List[Dict[str, Any]]):
Expand Down Expand Up @@ -460,12 +463,13 @@ async def register(self):
# register if not yet registered or registration is about to expire
# or anything has changed from the last registration
if (
not self._almanac_contract.is_registered(self.address)
or self._almanac_contract.get_expiry(self.address)
not self._almanac_contract.is_registered(self._identity.address)
or self._almanac_contract.get_expiry(self._identity.address)
< REGISTRATION_UPDATE_INTERVAL_SECONDS
or self._endpoints != self._almanac_contract.get_endpoints(self.address)
or self._endpoints
!= self._almanac_contract.get_endpoints(self._identity.address)
or list(self.protocols.keys())
!= self._almanac_contract.get_protocols(self.address)
!= self._almanac_contract.get_protocols(self._identity.address)
):
agent_balance = self._ledger.query_bank_balance(
Address(self.wallet.address())
Expand All @@ -482,7 +486,7 @@ async def register(self):
await self._almanac_contract.register(
self._ledger,
self.wallet,
self.address,
self._identity.address,
list(self.protocols.keys()),
self._endpoints,
signature,
Expand Down Expand Up @@ -800,7 +804,7 @@ async def _process_message_queue(self):
continue

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

# 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"
TESTNET_PREFIX = "test-agent://"
MAINNET_PREFIX = "agent://"

CONTRACT_ALMANAC = "fetch1tjagw8g8nn4cwuw00cf0m5tl4l6wfw9c0ue507fhx9e3yrsck8zs0l3q4w"
TESTNET_CONTRACT_ALMANAC = (
"fetch1tjagw8g8nn4cwuw00cf0m5tl4l6wfw9c0ue507fhx9e3yrsck8zs0l3q4w"
)
CONTRACT_NAME_SERVICE = (
"fetch1mxz8kn3l5ksaftx8a9pj9a6prpzk2uhxnqdkwuqvuh37tw80xu6qges77l"
)
Expand All @@ -25,7 +27,6 @@ class AgentNetwork(Enum):
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