AI Agents
Agents address ๐Ÿค–๐Ÿ“ซ
Bookmark

Agents address ๐Ÿค–๐Ÿ“ซ

Introduction

Each agent 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:

If you want 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 agent in the following way:

  1. First of all, create a Python script and name it by running: touch uagent-address.py

  2. We then need to import the Agent class from the uagents library to create an agent, alice. Then, using the print function, we will print the related uAgent 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.py
    from uagents import Agent
     
    alice = Agent(name="alice", seed="alice recovery phrase")
     
    print("uAgent address: ", alice.address)
  3. Save the script.

The output would be as follows:

uAgent address: agent1qww3ju3h6kfcuqf54gkghvt2pqe8qp97a7nzm2vp8plfxflc0epzcjsv79t

Print Fetch network address

You can print the Fetch network address related to your agent in the following way:

  1. Let's create a Python script, and name it by running: touch fetch-address.py

  2. As before, we first need to import the Agent class from the uagents library to create a uAgent, alice. Then, using the print() function, we will print the related Fetch Network address:

    fetch-address.py
    from uagents import Agent
     
    alice = Agent(name="alice", seed="alice recovery phrase")
     
    print("Fetch network address: ", alice.wallet.address())
  3. Save the script.

The output would be as follows:

Fetch network address: fetch1454hu0n9eszzg8p7mvan3ep7484jxl5mkf9phg

Print agent name and address using Context class

In this guide, we aim at showing how to create an agent 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 form agent1.... 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

  1. First of all, you need to create a Python script and name it by running: touch my_agent.py

  2. We then need to import the necessary classes Agent and Context from the uagents library, and then create an instance of the Agent class, alice:

    from uagents import Agent, Context
     
    alice = Agent(name="alice", seed="alice recovery phrase")
  3. We would then need to assign the agent 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 argument ctx of type Context. The message is printed out using the ctx.logger.info() method, and includes the agent's name obtained from attribute name of the Context class and retrieved using ctx.name() method. The same for the agent's address, which is obtained from attribute address of the Context class and retrieved using ctx.address() method.

  4. Save the script.

The overall script should look as follows:

my_agent.py
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.

Was this page helpful?

Bookmark