Agents address
Introduction
Agents within the Fetch Ecosystem are characterized by different addresses. These can allow the agent to perform different actions, including sending messages or interacting with the Almanac contract .
It is possible to distinguish between two different types of addresses:
-
uAgent address
: this address identifies the agent within the Fetch Network. It's similar to a username within a chat platform, allowing other agents to discover and communicate with that specific agent by querying that agent's information from the Almanac contract. -
Fetch network address
: this address is linked to the agent's wallet on the Fetch.ai network. It is essential to perform multiple functionalities like holding cryptocurrency, interacting with the Fetch Ledgerand performing secure transactions. This address is needed to register an agent to the Almanac
contract. Note, you must ensure the agents has enough funds available to perform operations in the Fetch Network, however this is all done automatically and no funds are currently required.
If you want to retrieve the address of an agent, you can either use the print()
function and specify which of the above addresses you wish to print out, or by calling the .address()
or .wallet.address()
methods using the agent
object to retrieve specific information.
Let's now check how these ways of retrieving addresses look like!
Prerequisites
Make sure you have read the following resources before going on with this guide:
Imports needed
Print uAgent address
You can print the uAgent address
related to your agent in the following way:
-
First of all, create a Python script and name it:
touch uagent-address.py
-
We then need to import the
Agent
class from theuagents
library to create an agent,alice
. Then, using theprint()
function, we will print the relateduAgent address
. Importantly, remember that theseed
parameter is used, when creating an agent, to set fixed addresses, otherwise a random address will be generated every time you run the agent:uagent-address.pyfrom uagents import Agent agent = Agent(name="alice", seed="alice recovery phrase") print("uAgent address: ", agent.address) if __name__ == "__main__": agent.run()
-
Save the script.
The output would be as follows:
uAgent address: agent1qww3ju3h6kfcuqf54gkghvt2pqe8qp97a7nzm2vp8plflc0epzcjsv79t
INFO: [alice]: Starting server on http://0.0.0.0:8000 (Press CTRL+C to quit)
Print Fetch network address
You can print the Fetch network address
related to your agent in the following way:
-
Let's create a Python script, and name it:
touch fetch-address.py
-
As before, we first need to import the
Agent
class from theuagents
library to create an Agent,alice
. Then, using theprint()
function, we will print the relatedFetch Network address
:fetch-address.pyfrom uagents import Agent agent = Agent(name="alice", seed="alice recovery phrase") print("Fetch network address: ", agent.wallet.address()) if __name__ == "__main__": agent.run()
-
Save the script.
The output would be as follows:
Fetch network address: fetch1454hu0n9eszzg8p7mvan3ep7484jxl5mkf9phg
INFO: [alice]: Starting server on http://0.0.0.0:8000 (Press CTRL+C to quit)
Print agent name and address using name
and address
methods
In this guide, we aim at showing how to create an agent being able to say hello and printing its name
and address
using the uagents
library tools. Indeed, it is possible to retrieve the name
and address
of any agent directly from the agent
object representing the agent you create and initialise. More specifically, we refer to the following methods:
-
.name()
: this returns the provided name of the agent, if specified, otherwise, if the agent's name is not explicitly set, then it will use the first ten characters of the agent's address as its name. -
.address()
: this returns the unique address of the agent in the formagent1...
. This address is essential for other agents to interact with your agent.
Let's get started and use the agent
object to make our agent print its name and address!
Walk-through
-
First of all, you need to create a Python script and name it:
touch my_agent.py
-
We then need to import the necessary classes
Agent
andContext
from theuagents
library, and then create an instance of theAgent
class,alice
. Below you can see theagent
object being initialised:from uagents import Agent, Context agent = Agent(name="alice", seed="alice recovery phrase")
-
We would then need to assign the agent the behavior to be executed. In this case,
agent
could send a message when it is being run saying hello and printing itsname
andaddress
using theagent
object:@agent.on_event("startup") async def introduce_agent(ctx: Context): ctx.logger.info(f"Hello, I'm agent {agent.name} and my address is {agent.address}.") if __name__ == "__main__": agent.run()
This
introduce_agent()
function takes a single argumentctx
of typeContext
. The message is printed out using thectx.logger.info()
method, and includes the agent's name obtained from attributename
and retrieved usingagent.name()
method. The same for the agent's address, which is obtained from attributeaddress
and retrieved usingagent.address()
method. -
Save the script.
The overall script should look as follows:
from uagents import Agent, Context
agent = Agent(name="alice", seed="alice recovery phrase")
@agent.on_event("startup")
async def introduce_agent(ctx: Context):
ctx.logger.info(f"Hello, I'm agent {agent.name} and my address is {agent.address}.")
if __name__ == "__main__":
agent.run()
Run the script
If you are using a Poetry virtual environment, make sure that you have activated it.
On your terminal, run the script: python my_agent.py
The output should be as follows:
INFO: Hello, I'm agent alice and my address is agent1qww3ju3h6kfcuqf54gkghvt2pqe8qp97a7nzm2vp8plfxflc0epzcjsv79t.
INFO: [alice]: Starting server on http://0.0.0.0:8000 (Press CTRL+C to quit)