Skip to Content

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 Ledger and 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!

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:

    windows
    echo. > 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 agent = Agent(name="alice", seed="alice recovery phrase", port=8000, endpoint=["http://127.0.0.1:8000/submit"]) print("uAgent address: ", agent.address) if __name__ == "__main__": agent.run()
  3. Save the script.

The output would be as follows:

uAgent address: agent1qww3ju3h6kfcuqf54gkghvt2pqe8qp97a7nzm2vp8plfxflc0epzcjsv79t INFO: [alice]: Registration on Almanac API successful INFO: [alice]: Registering on almanac contract... INFO: [alice]: Registering on almanac contract...complete INFO: [alice]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A8000&address=agent1qww3ju3h6kfcuqf54gkghvt2pqe8qp97a7nzm2vp8plfxflc0epzcjsv79t 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:

  1. Let’s create a Python script, and name it:

    windows
    echo. > fetch_address.py
  2. As before, we first 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 Fetch Network address:

    fetch_address.py
    from uagents import Agent agent = Agent(name="alice", seed="alice recovery phrase", port=8000, endpoint=["http://127.0.0.1:8000/submit"]) print("Fetch network address: ", agent.wallet.address()) if __name__ == "__main__": agent.run()
  3. Save the script.

The output would be as follows:

Fetch network address: fetch1454hu0n9eszzg8p7mvan3ep7484jxl5mkf9phg INFO: [alice]: Registration on Almanac API successful INFO: [alice]: Registering on almanac contract... INFO: [alice]: Registering on almanac contract...complete INFO: [alice]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A8000&address=agent1qww3ju3h6kfcuqf54gkghvt2pqe8qp97a7nzm2vp8plfxflc0epzcjsv79t 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 initialize. 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 form agent1.... 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

  1. First of all, you need to create a Python script and name it:

    windows
    echo. > 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. Below you can see the agent object being initialized:

    my_agent.py
    from uagents import Agent, Context agent = Agent(name="alice", seed="alice recovery phrase", port=8000, endpoint=["http://127.0.0.1:8000/submit"])
  3. 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 its name and address using the agent object:

    my_agent.py
    @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 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 and retrieved using agent.name() method. The same for the agent’s address, which is obtained from attribute address and retrieved using agent.address() method.

  4. Save the script.

The overall script should look as follows:

my_agent.py
from uagents import Agent, Context agent = Agent(name="alice", seed="alice recovery phrase", port=8000, endpoint=["http://127.0.0.1:8000/submit"]) @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

On your terminal, run the script: python my_agent.py

The output should be as follows:

INFO: [alice]: Registration on Almanac API successful INFO: [alice]: Registering on almanac contract... INFO: [alice]: Registering on almanac contract...complete INFO: [alice]: Agent inspector available at https://agentverse.ai/inspect/?uri=http%3A//127.0.0.1%3A8000&address=agent1qww3ju3h6kfcuqf54gkghvt2pqe8qp97a7nzm2vp8plfxflc0epzcjsv79t INFO: [alice]: 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)
Last updated on