Using uAgents storage function

Using uAgents storage function


In this guide, we want to illustrate how storage functions are called and how to use them. We want to create a uAgent which gets a value from the storage (starting from 0) every second. Then prints it, and puts the new value back into the storage but increased by 1 unit.


  1. First of all, let's create a Python script, and name it: touch

  2. Then, we need to open the script in the text editor of choice and import the necessary classes, Agent and Context, from the uagents library.

  3. Let's then create a uAgent named alice which logs a message every second using the .on_interval() decorator, indicating the current count. The on_interval() function takes a Context object as a parameter: the Context object contains a storage attribute, which is used to store and retrieve data between method calls.
    from uagents import Agent, Context
    alice = Agent(name="alice", seed="alice recovery phrase")
    async def on_interval(ctx: Context):
        current_count ="count") or 0
    "My count is: {current_count}")
    "count", current_count + 1)
    if __name__ == "__main__":

    Here, the on_interva() function retrieves the current count from the storage attribute using the method. It prints the current_count value, and then increments it by 1, and stores the updated count back to the storage attribute using the method. The current count is then logged using the method.

  4. Save the script.

Run the script

On your terminal, make sure you activated the virtual environment.

Run the script: python

The output should look as follows:

[alice]: My count is: 1
[alice]: My count is: 2
[alice]: My count is: 3