Getting uAgent addresses π€π«
Introduction
Each uAgent within the Fetch ecosystem is 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 is the main uAgent identifier. Other uAgents can use this address to query the agent's information in the Almanac contract. -
Fetch network address
: this is the address providing the agent with the capabilities for interacting with the Fetch ledger, including registration in the Almanac contract.
To retrieve the address of your agent, you can either use the print()
function and specify which of the above addresses you wish to print out, or by calling the Context
class and related methods to retrieve specific information.
Let's now check how these ways of retrieving addresses look like!
Print uAgent address
You can print the uAgent address
related to your uAgent 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 the
uagents
library to create a uAgent,alice
. Then, using theprint
function, we will print the relateduAgent address
. Importantly, remember that the seed 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 alice = Agent(name="alice", seed="alice recovery phrase") print("uAgent address: ", alice.address)
The output would be as follows:
uAgent address: agent1qww3ju3h6kfcuqf54gkghvt2pqe8qp97a7nzm2vp8plfxflc0epzcjsv79t
Print Fetch network address
You can print the Fetch network address
related to your uAgent 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 a uAgent,alice
. Then, using theprint()
function, we will print the relatedFetch Network address
:fetch-address.pyfrom uagents import Agent alice = Agent(name="alice", seed="alice recovery phrase") print("Fetch network address: ", alice.wallet.address())
The output would be as follows:
Fetch network address: fetch1454hu0n9eszzg8p7mvan3ep7484jxl5mkf9phg
Print uAgent name and address using Context class
In this guide, we aim at showing how to create a uAgent being able to say hello and printing its name and address retrieving such information from the Context
class imported from the uagents
library.
The Context
class is a crucial component which represents the execution context of an agent. It encapsulates different attributes and methods which allow an agent to interact with its environment, send and receive messages, and manage its state and identity. Within this class, we can distinguish multiple attributes and methods, including:
-
name
: which 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
: which 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 Context
class 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
:from uagents import Agent, Context alice = Agent(name="alice", seed="alice recovery phrase")
-
We would then need to assign the uAgent the behavior to be executed. In this case, alice could send a message when it is being run saying hello and printing its name and address:
@alice.on_event("startup") async def introduce_agent(ctx: Context): ctx.logger.info(f"Hello, I'm agent {ctx.name} and my address is {ctx.address}." if __name__ == "__main__": alice.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
of theContext
class and retrieved usingctx.name()
method. The same for the agent's address, which is obtained from attributeaddress
of theContext
class and retrieved usingctx.address()
method. -
Save the script.
The overall script should look as follows:
from uagents import Agent, Context
alice = Agent(name="alice", seed="alice recovery phrase")
@alice.on_event("startup")
async def introduce_agent(ctx: Context):
ctx.logger.info(f"Hello, I'm agent {ctx.name} and my address is {ctx.address}.")
if __name__ == "__main__":
alice.run()
Run the script
On your terminal, make sure to have activated the virtual environment.
Run the script: my_agent.py
The output should be as follows:
Hello, I'm agent alice and my address is agent1qww3ju3h6kfcuqf54gkghvt2pqe8qp97a7nzm2vp8plfxflc0epzcjsv79t.