fetch-logo
ConceptsConceptsGuidesGuidesExamplesExamplesReferencesReferencesAPIsAPIs
GitHub (opens in a new tab)
  • Examples
      • Create your first agent
      • Interval task with Agents
      • Multiple agents communication
      • Local Network Interaction
      • Agents communication
      • Agents storage
      • Table booking service with Agents
      • Cleaning service with Agents
      • Send tokens with Agents
      • Verify messages with Agents
      • Communicating with other agents wallet
      • Agents communication using Agentverse Mailbox service
      • Agents Broadcast
      • Agents Name Service
      • Query an agent using a proxy API
      • Register a dice roll agent as a Function
      • Register a coin toss agent as a Function
      • Register a local agent as a Function
      • Using News API to build network of Primary and Secondary functions
      • Locally Hosted Agent with LangChain Integration
      • Hugging face API agent as a Function
      • On Query decorator example
      • Agents and Functions Creation using APIs
      • Adding Secret to agent using Agentverse API
      • React app with agents 'on_query' decorator
      • Open Dialogue Chit-Chat
      • Predefined Dialogue Chit-Chat
      • Chat API example
      • DeltaV Dialogue Chit-Chat
  • Examples
      • Create your first agent
      • Interval task with Agents
      • Multiple agents communication
      • Local Network Interaction
      • Agents communication
      • Agents storage
      • Table booking service with Agents
      • Cleaning service with Agents
      • Send tokens with Agents
        • Introduction
      • Verify messages with Agents
      • Communicating with other agents wallet
      • Agents communication using Agentverse Mailbox service
      • Agents Broadcast
      • Agents Name Service
      • Query an agent using a proxy API
      • Register a dice roll agent as a Function
      • Register a coin toss agent as a Function
      • Register a local agent as a Function
      • Using News API to build network of Primary and Secondary functions
      • Locally Hosted Agent with LangChain Integration
      • Hugging face API agent as a Function
      • On Query decorator example
      • Agents and Functions Creation using APIs
      • Adding Secret to agent using Agentverse API
      • React app with agents 'on_query' decorator
      • Open Dialogue Chit-Chat
      • Predefined Dialogue Chit-Chat
      • Chat API example
      • DeltaV Dialogue Chit-Chat
Examples
Intermediate topics
Send tokens with Agents

Send tokens with Agents

Introduction

This file can be run on any platform supporting Python, with the necessary install permissions. This example shows how to use Agents to make them send tokens with one another.

Supporting documentation

  • Creating an agent ↗️
  • Creating an interval task ↗️
  • Communicating with other agents ↗️
  • Register in Almanac ↗️
  • Almanac Contract ↗️
  • Protocols ↗️
  • How to use agents to send tokens ↗️

The agent

sending_tokens.py
from uagents import Agent, Bureau, Context, Model
from uagents.network import wait_for_tx_to_complete
from uagents.setup import fund_agent_if_low
 
class PaymentRequest(Model):
    wallet_address: str
    amount: int
    denom: str
 
class TransactionInfo(Model):
    tx_hash: str
 
AMOUNT = 100
DENOM = "atestfet"
 
alice = Agent(name="alice", seed="alice secret phrase")
bob = Agent(name="bob", seed="bob secret phrase")
 
fund_agent_if_low(alice.wallet.address())
fund_agent_if_low(bob.wallet.address(), min_balance=AMOUNT)
 
@alice.on_interval(period=10.0)
async def request_funds(ctx: Context):
    await ctx.send(
        bob.address,
        PaymentRequest(
            wallet_address=str(alice.wallet.address()), amount=AMOUNT, denom=DENOM
        ),
    )
 
@alice.on_message(model=TransactionInfo)
async def confirm_transaction(ctx: Context, sender: str, msg: TransactionInfo):
    ctx.logger.info(f"Received transaction info from {sender}: {msg}")
 
    tx_resp = await wait_for_tx_to_complete(msg.tx_hash, ctx.ledger)
    coin_received = tx_resp.events["coin_received"]
 
    if (
        coin_received["receiver"] == str(alice.wallet.address())
        and coin_received["amount"] == f"{AMOUNT}{DENOM}"
    ):
        ctx.logger.info(f"Transaction was successful: {coin_received}")
 
@bob.on_message(model=PaymentRequest, replies=TransactionInfo)
async def send_payment(ctx: Context, sender: str, msg: PaymentRequest):
    ctx.logger.info(f"Received payment request from {sender}: {msg}")
    transaction = ctx.ledger.send_tokens(
        msg.wallet_address, msg.amount, msg.denom, bob.wallet
    )
 
    await ctx.send(alice.address, TransactionInfo(tx_hash=transaction.tx_hash))
 
bureau = Bureau()
bureau.add(alice)
bureau.add(bob)
 
if __name__ == "__main__":
    bureau.run()

Expected output

[  bob]: Received payment request from agent1qdp9j2ev86k3h5acaayjm8tpx36zv4mjxn05pa2kwesspstzj697xy5vk2a: wallet_address='fetch1967p3vkp0yngdfturv4ypq2p4g760ml705wcxy' amount=100 denom='atestfet'
[alice]: Received transaction info from agent1q2kxet3vh0scsf0sm7y2erzz33cve6tv5uk63x64upw5g68kr0chkv7hw50: tx_hash='DB662CCF415C7B0C9A02928967BE1817506D02A041AA05CA48DCE5CF87D5A638'
[alice]: Transaction was successful: {'receiver': 'fetch1967p3vkp0yngdfturv4ypq2p4g760ml705wcxy', 'amount': '100atestfet'}

Was this page helpful?

Cleaning service with AgentsVerify messages with Agents
footer-logo

Main website

Integrations

Events

We’re hiring!

Twitter (opens in a new tab)Telegram (opens in a new tab)Discord (opens in a new tab)GitHub (opens in a new tab)Youtube (opens in a new tab)LinkedIn (opens in a new tab)Reddit (opens in a new tab)
Sign up for developer updates